shipping
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
//! Admin management of shipping methods (price + enabled toggle).
|
||||
//! Admin management of shipping methods: add, edit (price + enabled), remove.
|
||||
|
||||
use axum_extra::extract::cookie::CookieJar;
|
||||
use loco_rs::prelude::*;
|
||||
use sea_orm::{ActiveModelTrait, EntityTrait, QueryOrder, Set};
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, EntityTrait, ModelTrait, PaginatorTrait, QueryFilter,
|
||||
QueryOrder, Set,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
@@ -12,6 +15,7 @@ use crate::{
|
||||
shared::{
|
||||
guard,
|
||||
money::{format_price, parse_price_to_cents},
|
||||
slug::{slugify, unique_slug},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -21,6 +25,18 @@ struct ShippingForm {
|
||||
enabled: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct NewShippingForm {
|
||||
name: String,
|
||||
price: String,
|
||||
requires_pickup_point: Option<String>,
|
||||
enabled: Option<String>,
|
||||
}
|
||||
|
||||
fn is_checked(value: &Option<String>) -> bool {
|
||||
matches!(value.as_deref(), Some("on" | "true" | "1"))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn index(
|
||||
auth: auth::JWT,
|
||||
@@ -53,6 +69,48 @@ async fn index(
|
||||
)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn create(
|
||||
auth: auth::JWT,
|
||||
State(ctx): State<AppContext>,
|
||||
Form(form): Form<NewShippingForm>,
|
||||
) -> Result<Response> {
|
||||
guard::current_admin(auth, &ctx).await?;
|
||||
let name = form.name.trim().to_string();
|
||||
if name.is_empty() {
|
||||
return Err(Error::BadRequest("name is required".to_string()));
|
||||
}
|
||||
// Stable unique `code` derived from the name; it's what checkout submits and
|
||||
// what an order stores, so it must not collide with an existing method.
|
||||
let code = unique_slug(&slugify(&name), |candidate| {
|
||||
let ctx = ctx.clone();
|
||||
async move {
|
||||
Ok(shipping_methods::Entity::find()
|
||||
.filter(shipping_methods::Column::Code.eq(candidate))
|
||||
.count(&ctx.db)
|
||||
.await?
|
||||
> 0)
|
||||
}
|
||||
})
|
||||
.await?;
|
||||
// Append after existing methods.
|
||||
let position = shipping_methods::Entity::find().count(&ctx.db).await? as i32;
|
||||
|
||||
shipping_methods::ActiveModel {
|
||||
code: Set(code),
|
||||
name: Set(name),
|
||||
price_cents: Set(parse_price_to_cents(&form.price)?),
|
||||
requires_pickup_point: Set(is_checked(&form.requires_pickup_point)),
|
||||
enabled: Set(is_checked(&form.enabled)),
|
||||
position: Set(position),
|
||||
..Default::default()
|
||||
}
|
||||
.insert(&ctx.db)
|
||||
.await?;
|
||||
|
||||
format::redirect("/admin/shipping")
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn update(
|
||||
auth: auth::JWT,
|
||||
@@ -67,13 +125,30 @@ async fn update(
|
||||
.ok_or_else(|| Error::NotFound)?;
|
||||
let mut active = method.into_active_model();
|
||||
active.price_cents = Set(parse_price_to_cents(&form.price)?);
|
||||
active.enabled = Set(matches!(form.enabled.as_deref(), Some("on" | "true" | "1")));
|
||||
active.enabled = Set(is_checked(&form.enabled));
|
||||
active.update(&ctx.db).await?;
|
||||
format::redirect("/admin/shipping")
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn delete(
|
||||
auth: auth::JWT,
|
||||
Path(id): Path<i32>,
|
||||
State(ctx): State<AppContext>,
|
||||
) -> Result<Response> {
|
||||
guard::current_admin(auth, &ctx).await?;
|
||||
let method = shipping_methods::Entity::find_by_id(id)
|
||||
.one(&ctx.db)
|
||||
.await?
|
||||
.ok_or_else(|| Error::NotFound)?;
|
||||
method.delete(&ctx.db).await?;
|
||||
format::redirect("/admin/shipping")
|
||||
}
|
||||
|
||||
pub fn routes() -> Routes {
|
||||
Routes::new()
|
||||
.add("/admin/shipping", get(index))
|
||||
.add("/admin/shipping", post(create))
|
||||
.add("/admin/shipping/{id}", post(update))
|
||||
.add("/admin/shipping/{id}/delete", post(delete))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user