diff options
Diffstat (limited to 'crates/rocie-server/src/api/set/unit.rs')
| -rw-r--r-- | crates/rocie-server/src/api/set/unit.rs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/crates/rocie-server/src/api/set/unit.rs b/crates/rocie-server/src/api/set/unit.rs new file mode 100644 index 0000000..4281e05 --- /dev/null +++ b/crates/rocie-server/src/api/set/unit.rs @@ -0,0 +1,49 @@ +use actix_web::{HttpResponse, Responder, Result, post, web}; +use serde::Deserialize; +use utoipa::ToSchema; + +use crate::{app::App, storage::sql::{insert::Operations, unit::{Unit, UnitId}}}; + +#[derive(Deserialize, ToSchema)] +struct UnitStub { + full_name_plural: String, + full_name_singular: String, + short_name: String, + description: Option<String>, +} + +/// Register an Unit +#[utoipa::path( + responses( + ( + status = 200, + description = "Product successfully registered in database", + body = UnitId, + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String, + ) + ), + request_body = UnitStub, +)] +#[post("/unit/new")] +pub(crate) async fn register_unit( + app: web::Data<App>, + unit: web::Json<UnitStub>, +) -> Result<impl Responder> { + let mut ops = Operations::new("register unit"); + + let unit = Unit::register( + unit.full_name_singular.clone(), + unit.full_name_plural.clone(), + unit.short_name.clone(), + unit.description.clone(), + &mut ops, + ); + + ops.apply(&app).await?; + + Ok(HttpResponse::Ok().json(unit.id)) +} |
