diff --git a/src/frontend/item.rs b/src/frontend/item.rs index 6cc2edf..80fc81f 100644 --- a/src/frontend/item.rs +++ b/src/frontend/item.rs @@ -7,6 +7,7 @@ use std::collections::HashMap; use actix_identity::Identity; use actix_web::{error, get, post, web, Responder}; use maud::html; +use serde::Deserialize; use sqlx::PgPool; use uuid::Uuid; @@ -263,10 +264,28 @@ async fn list(pool: web::Data, user: Identity) -> actix_web::Result, + pub parent: Option, + pub class: Uuid, + pub original_packaging: Option, + pub description: String, +} + +#[derive(Debug, Deserialize)] +pub struct NewItemFormPrefilled { + pub name: Option, + pub parent: Option, + pub class: Option, + pub original_packaging: Option, + pub description: Option, +} + #[get("/items/add")] async fn add_form( pool: web::Data, - form: web::Query, + form: web::Query, user: Identity, ) -> actix_web::Result { let datalist_items = datalist::items(&pool) @@ -340,13 +359,23 @@ async fn add_form( #[post("/items/add")] async fn add_post( - data: web::Form, + data: web::Form, pool: web::Data, _user: Identity, ) -> actix_web::Result { - let item = manage::item::add(&pool, data.into_inner()) - .await - .map_err(error::ErrorInternalServerError)?; + let data = data.into_inner(); + let item = manage::item::add( + &pool, + NewItem { + name: data.name, + class: data.class, + parent: data.parent, + original_packaging: data.original_packaging, + description: data.description, + }, + ) + .await + .map_err(error::ErrorInternalServerError)?; Ok(web::Redirect::to("/item/".to_owned() + &item.id.to_string()).see_other()) } diff --git a/src/frontend/item_class.rs b/src/frontend/item_class.rs index 7c5cc5b..f497deb 100644 --- a/src/frontend/item_class.rs +++ b/src/frontend/item_class.rs @@ -5,6 +5,7 @@ use actix_identity::Identity; use actix_web::{error, get, post, web, Responder}; use maud::html; +use serde::Deserialize; use sqlx::PgPool; use uuid::Uuid; @@ -230,10 +231,24 @@ async fn list(pool: web::Data, user: Identity) -> actix_web::Result, + pub description: String, +} + +#[derive(Debug, Deserialize)] +pub struct NewItemClassFormPrefilled { + pub name: Option, + pub parent: Option, + pub description: Option, +} + #[get("/item-classes/add")] async fn add_form( pool: web::Data, - form: web::Query, + form: web::Query, user: Identity, ) -> actix_web::Result { let datalist_item_classes = datalist::item_classes(&pool) @@ -285,13 +300,21 @@ async fn add_form( #[post("/item-classes/add")] async fn add( - data: web::Form, + data: web::Form, pool: web::Data, _user: Identity, ) -> actix_web::Result { - let item = manage::item_class::add(&pool, data.into_inner()) - .await - .map_err(error::ErrorInternalServerError)?; + let data = data.into_inner(); + let item = manage::item_class::add( + &pool, + NewItemClass { + name: data.name, + parent: data.parent, + description: data.description, + }, + ) + .await + .map_err(error::ErrorInternalServerError)?; Ok(web::Redirect::to("/item-class/".to_owned() + &item.id.to_string()).see_other()) } diff --git a/src/models.rs b/src/models.rs index ae4a26c..9a0272c 100644 --- a/src/models.rs +++ b/src/models.rs @@ -36,21 +36,6 @@ pub struct NewItem { pub description: String, } -// Its structure is not how it could ideally be -// (doubly nested Options where the original struct already has an Option>) -// because the intended usage (in a GET request parameter) does not allow such fine-grained types. -// TODO: this can be automated from NewItem with derive macro -#[derive(Debug, Deserialize)] -pub struct NewItemForm { - #[serde(default)] - pub name: Option, - #[serde(default)] - pub parent: Option, - pub class: Option, - pub original_packaging: Option, - pub description: Option, -} - #[derive(Clone, Debug, Serialize)] pub struct ItemClass { pub id: Uuid, @@ -69,12 +54,3 @@ pub struct NewItemClass { pub parent: Option, pub description: String, } - -// see NewItemForm -#[derive(Debug, Deserialize)] -pub struct NewItemClassForm { - pub name: Option, - #[serde(default)] - pub parent: Option, - pub description: Option, -}