about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/api
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rocie-server/src/api')
-rw-r--r--crates/rocie-server/src/api/get/mod.rs3
-rw-r--r--crates/rocie-server/src/api/get/unit.rs36
-rw-r--r--crates/rocie-server/src/api/get/unit_property.rs68
-rw-r--r--crates/rocie-server/src/api/set/barcode.rs7
-rw-r--r--crates/rocie-server/src/api/set/mod.rs2
-rw-r--r--crates/rocie-server/src/api/set/product.rs15
-rw-r--r--crates/rocie-server/src/api/set/unit.rs13
-rw-r--r--crates/rocie-server/src/api/set/unit_property.rs47
8 files changed, 179 insertions, 12 deletions
diff --git a/crates/rocie-server/src/api/get/mod.rs b/crates/rocie-server/src/api/get/mod.rs
index 21684af..e21a120 100644
--- a/crates/rocie-server/src/api/get/mod.rs
+++ b/crates/rocie-server/src/api/get/mod.rs
@@ -3,11 +3,14 @@ use actix_web::web;
 pub(crate) mod inventory;
 pub(crate) mod product;
 pub(crate) mod unit;
+pub(crate) mod unit_property;
 
 pub(crate) fn register_paths(cfg: &mut web::ServiceConfig) {
     cfg.service(product::product_by_id)
         .service(product::products)
         .service(unit::units)
         .service(unit::unit_by_id)
+        .service(unit_property::unit_properties)
+        .service(unit_property::unit_property_by_id)
         .service(inventory::amount_by_id);
 }
diff --git a/crates/rocie-server/src/api/get/unit.rs b/crates/rocie-server/src/api/get/unit.rs
index 4854ea3..73aa626 100644
--- a/crates/rocie-server/src/api/get/unit.rs
+++ b/crates/rocie-server/src/api/get/unit.rs
@@ -5,8 +5,16 @@ use crate::{app::App, storage::sql::unit::{Unit, UnitId, UnitIdStub}};
 /// Return all registered units
 #[utoipa::path(
     responses(
-        (status = OK, description = "All units founds", body = Vec<Unit>),
-        (status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String)
+        (
+            status = OK,
+            description = "All units founds",
+            body = Vec<Unit>
+        ),
+        (
+            status = INTERNAL_SERVER_ERROR,
+            description = "Server encountered error",
+            body = String
+        )
     ),
 )]
 #[get("/units/")]
@@ -19,12 +27,26 @@ pub(crate) async fn units(app: web::Data<App>) -> Result<impl Responder> {
 /// Get Unit by id
 #[utoipa::path(
     responses(
-        (status = OK, description = "Unit found from database", body = Unit),
-        (status = NOT_FOUND, description = "Unit not found in database"),
-        (status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String)
+        (
+            status = OK,
+            description = "Unit found from database",
+            body = Unit
+        ),
+        (
+            status = NOT_FOUND,
+            description = "Unit not found in database"
+        ),
+        (
+            status = INTERNAL_SERVER_ERROR,
+            description = "Server encountered error",
+            body = String
+        )
     ),
     params(
-        ("id" = UnitId, description = "Unit id" ),
+        (
+            "id" = UnitId,
+            description = "Unit id"
+        ),
     )
 )]
 #[get("/unit/{id}")]
@@ -35,7 +57,7 @@ pub(crate) async fn unit_by_id(
     let id = id.into_inner();
 
     match Unit::from_id(&app, id.into()).await? {
-        Some(product) => Ok(HttpResponse::Ok().json(product)),
+        Some(unit) => Ok(HttpResponse::Ok().json(unit)),
         None => Ok(HttpResponse::NotFound().finish()),
     }
 }
diff --git a/crates/rocie-server/src/api/get/unit_property.rs b/crates/rocie-server/src/api/get/unit_property.rs
new file mode 100644
index 0000000..3160480
--- /dev/null
+++ b/crates/rocie-server/src/api/get/unit_property.rs
@@ -0,0 +1,68 @@
+use actix_web::{HttpResponse, Responder, Result, get, web};
+
+use crate::{
+    app::App,
+    storage::sql::{
+        unit_property::{UnitProperty, UnitPropertyId, UnitPropertyIdStub},
+    },
+};
+
+/// Return all registered unit properties
+#[utoipa::path(
+    responses(
+        (
+            status = OK,
+            description = "All unit properties founds",
+            body = Vec<UnitProperty>
+        ),
+        (
+            status = INTERNAL_SERVER_ERROR,
+            description = "Server encountered error",
+            body = String
+        )
+    ),
+)]
+#[get("/unit-properties/")]
+pub(crate) async fn unit_properties(app: web::Data<App>) -> Result<impl Responder> {
+    let all = UnitProperty::get_all(&app).await?;
+
+    Ok(HttpResponse::Ok().json(all))
+}
+
+/// Get Unit property by id
+#[utoipa::path(
+    responses(
+        (
+            status = OK,
+            description = "Unit property found from database",
+            body = UnitProperty
+        ),
+        (
+            status = NOT_FOUND,
+            description = "Unit Property not found in database"
+        ),
+        (
+            status = INTERNAL_SERVER_ERROR,
+            description = "Server encountered error",
+            body = String
+        )
+    ),
+    params(
+        (
+            "id" = UnitPropertyId,
+            description = "Unit Property id"
+        ),
+    )
+)]
+#[get("/unit-property/{id}")]
+pub(crate) async fn unit_property_by_id(
+    app: web::Data<App>,
+    id: web::Path<UnitPropertyIdStub>,
+) -> Result<impl Responder> {
+    let id = id.into_inner();
+
+    match UnitProperty::from_id(&app, id.into()).await? {
+        Some(unit_property) => Ok(HttpResponse::Ok().json(unit_property)),
+        None => Ok(HttpResponse::NotFound().finish()),
+    }
+}
diff --git a/crates/rocie-server/src/api/set/barcode.rs b/crates/rocie-server/src/api/set/barcode.rs
index 60b1650..29eac9e 100644
--- a/crates/rocie-server/src/api/set/barcode.rs
+++ b/crates/rocie-server/src/api/set/barcode.rs
@@ -4,8 +4,9 @@ use log::debug;
 use crate::{
     app::App,
     storage::sql::{
-        barcode::{Barcode, BarcodeId, BarcodeIdStub, UnitAmount},
+        barcode::{Barcode, BarcodeId, BarcodeIdStub},
         insert::Operations,
+        unit::UnitAmount,
     },
 };
 
@@ -86,7 +87,9 @@ pub(crate) async fn consume_barcode(
 
     match barcode {
         Some(barcode) => {
-            barcode.consume(&app, unit_amount.into_inner(), &mut ops).await?;
+            barcode
+                .consume(&app, unit_amount.into_inner(), &mut ops)
+                .await?;
 
             ops.apply(&app).await?;
 
diff --git a/crates/rocie-server/src/api/set/mod.rs b/crates/rocie-server/src/api/set/mod.rs
index a7ddab5..5dd0219 100644
--- a/crates/rocie-server/src/api/set/mod.rs
+++ b/crates/rocie-server/src/api/set/mod.rs
@@ -3,11 +3,13 @@ use actix_web::web;
 pub(crate) mod barcode;
 pub(crate) mod product;
 pub(crate) mod unit;
+pub(crate) mod unit_property;
 
 pub(crate) fn register_paths(cfg: &mut web::ServiceConfig) {
     cfg.service(product::register_product)
         .service(product::associate_barcode)
         .service(unit::register_unit)
+        .service(unit_property::register_unit_property)
         .service(barcode::consume_barcode)
         .service(barcode::buy_barcode);
 }
diff --git a/crates/rocie-server/src/api/set/product.rs b/crates/rocie-server/src/api/set/product.rs
index d347ee7..7372d23 100644
--- a/crates/rocie-server/src/api/set/product.rs
+++ b/crates/rocie-server/src/api/set/product.rs
@@ -9,13 +9,22 @@ use crate::{
         insert::Operations,
         product::{Product, ProductId, ProductIdStub},
         unit::Unit,
+        unit_property::UnitPropertyId,
     },
 };
 
 #[derive(Deserialize, ToSchema)]
 struct ProductStub {
+    /// The name of the product
     name: String,
+
+    /// The Unit Property to use for this product.
+    unit_property: UnitPropertyId,
+
+    /// A description.
     description: Option<String>,
+
+    /// A parent of this product, otherwise the parent will be the root of the parent tree.
     parent: Option<ProductId>,
 }
 
@@ -46,6 +55,7 @@ pub(crate) async fn register_product(
         product_stub.name.clone(),
         product_stub.description.clone(),
         product_stub.parent,
+        product_stub.unit_property,
         &mut ops,
     );
 
@@ -77,7 +87,10 @@ pub(crate) async fn register_product(
         )
     ),
     params (
-        ("id" = ProductId, description = "The id of the product to associated the barcode with"),
+        (
+            "id" = ProductId,
+            description = "The id of the product to associated the barcode with"
+        ),
     ),
     request_body = Barcode,
 )]
diff --git a/crates/rocie-server/src/api/set/unit.rs b/crates/rocie-server/src/api/set/unit.rs
index 4281e05..5f39c8f 100644
--- a/crates/rocie-server/src/api/set/unit.rs
+++ b/crates/rocie-server/src/api/set/unit.rs
@@ -2,13 +2,21 @@ 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}}};
+use crate::{
+    app::App,
+    storage::sql::{
+        insert::Operations,
+        unit::{Unit, UnitId},
+        unit_property::UnitPropertyId,
+    },
+};
 
 #[derive(Deserialize, ToSchema)]
 struct UnitStub {
     full_name_plural: String,
     full_name_singular: String,
     short_name: String,
+    unit_property: UnitPropertyId,
     description: Option<String>,
 }
 
@@ -17,7 +25,7 @@ struct UnitStub {
     responses(
         (
             status = 200,
-            description = "Product successfully registered in database",
+            description = "Unit successfully registered in database",
             body = UnitId,
         ),
         (
@@ -40,6 +48,7 @@ pub(crate) async fn register_unit(
         unit.full_name_plural.clone(),
         unit.short_name.clone(),
         unit.description.clone(),
+        unit.unit_property,
         &mut ops,
     );
 
diff --git a/crates/rocie-server/src/api/set/unit_property.rs b/crates/rocie-server/src/api/set/unit_property.rs
new file mode 100644
index 0000000..9915e84
--- /dev/null
+++ b/crates/rocie-server/src/api/set/unit_property.rs
@@ -0,0 +1,47 @@
+use actix_web::{HttpResponse, Responder, Result, post, web};
+use serde::Deserialize;
+use utoipa::ToSchema;
+
+use crate::{
+    app::App,
+    storage::sql::{
+        insert::Operations,
+        unit_property::{UnitProperty, UnitPropertyId},
+    },
+};
+
+#[derive(Deserialize, ToSchema)]
+struct UnitPropertyStub {
+    name: String,
+    description: Option<String>,
+}
+
+/// Register an Unit Property
+#[utoipa::path(
+    responses(
+        (
+            status = 200,
+            description = "Unit property successfully registered in database",
+            body = UnitPropertyId,
+        ),
+        (
+            status = INTERNAL_SERVER_ERROR,
+            description = "Server encountered error",
+            body = String,
+        )
+    ),
+    request_body = UnitPropertyStub,
+)]
+#[post("/unit-property/new")]
+pub(crate) async fn register_unit_property(
+    app: web::Data<App>,
+    unit: web::Json<UnitPropertyStub>,
+) -> Result<impl Responder> {
+    let mut ops = Operations::new("register unit property");
+
+    let unit = UnitProperty::register(unit.name.clone(), unit.description.clone(), &mut ops);
+
+    ops.apply(&app).await?;
+
+    Ok(HttpResponse::Ok().json(unit.id))
+}