diff options
Diffstat (limited to 'crates/rocie-server/src/api')
| -rw-r--r-- | crates/rocie-server/src/api/get/mod.rs | 13 | ||||
| -rw-r--r-- | crates/rocie-server/src/api/get/product.rs | 119 | ||||
| -rw-r--r-- | crates/rocie-server/src/api/get/product_parent.rs | 64 | ||||
| -rw-r--r-- | crates/rocie-server/src/api/get/recipe.rs | 66 | ||||
| -rw-r--r-- | crates/rocie-server/src/api/get/unit.rs | 46 | ||||
| -rw-r--r-- | crates/rocie-server/src/api/set/mod.rs | 4 | ||||
| -rw-r--r-- | crates/rocie-server/src/api/set/product.rs | 7 | ||||
| -rw-r--r-- | crates/rocie-server/src/api/set/product_parent.rs | 60 | ||||
| -rw-r--r-- | crates/rocie-server/src/api/set/recipe.rs | 54 | ||||
| -rw-r--r-- | crates/rocie-server/src/api/set/unit.rs | 2 | ||||
| -rw-r--r-- | crates/rocie-server/src/api/set/unit_property.rs | 4 |
11 files changed, 428 insertions, 11 deletions
diff --git a/crates/rocie-server/src/api/get/mod.rs b/crates/rocie-server/src/api/get/mod.rs index 1c32105..487b55c 100644 --- a/crates/rocie-server/src/api/get/mod.rs +++ b/crates/rocie-server/src/api/get/mod.rs @@ -2,6 +2,8 @@ use actix_web::web; pub(crate) mod inventory; pub(crate) mod product; +pub(crate) mod product_parent; +pub(crate) mod recipe; pub(crate) mod unit; pub(crate) mod unit_property; @@ -9,8 +11,17 @@ pub(crate) fn register_paths(cfg: &mut web::ServiceConfig) { cfg.service(product::product_by_id) .service(product::product_by_name) .service(product::product_suggestion_by_name) - .service(product::products) + .service(product::products_registered) + .service(product::products_in_storage) + .service(product::products_by_product_parent_id_indirect) + .service(product::products_by_product_parent_id_direct) + .service(product_parent::product_parents) + .service(product_parent::product_parents_toplevel) + .service(product_parent::product_parents_under) + .service(recipe::recipe_by_id) + .service(recipe::recipes) .service(unit::units) + .service(unit::units_by_property_id) .service(unit::unit_by_id) .service(unit_property::unit_properties) .service(unit_property::unit_property_by_id) diff --git a/crates/rocie-server/src/api/get/product.rs b/crates/rocie-server/src/api/get/product.rs index 55e5d91..4216f9b 100644 --- a/crates/rocie-server/src/api/get/product.rs +++ b/crates/rocie-server/src/api/get/product.rs @@ -4,7 +4,11 @@ use percent_encoding::percent_decode_str; use crate::{ app::App, - storage::sql::product::{Product, ProductId, ProductIdStub}, + storage::sql::{ + product::{Product, ProductId, ProductIdStub}, + product_amount::ProductAmount, + product_parent::{ProductParent, ProductParentId, ProductParentIdStub}, + }, }; /// A String, that is not url-decoded on parse. @@ -65,7 +69,7 @@ pub(crate) async fn product_by_name( req: HttpRequest, name: web::Path<String>, ) -> Result<impl Responder> { - let _name = name; + drop(name); let name = UrlEncodedString::from_str( req.path() @@ -96,7 +100,7 @@ pub(crate) async fn product_suggestion_by_name( req: HttpRequest, name: web::Path<String>, ) -> Result<impl Responder> { - let _name = name; + drop(name); let name = UrlEncodedString::from_str( req.path() @@ -118,13 +122,116 @@ pub(crate) async fn product_suggestion_by_name( /// Return all registered products #[utoipa::path( responses( - (status = OK, description = "All products founds", body = Vec<Product>), + (status = OK, description = "All products found", body = Vec<Product>), (status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String) ), )] -#[get("/products/")] -pub(crate) async fn products(app: web::Data<App>) -> Result<impl Responder> { +#[get("/products_registered/")] +pub(crate) async fn products_registered(app: web::Data<App>) -> Result<impl Responder> { let all = Product::get_all(&app).await?; Ok(HttpResponse::Ok().json(all)) } + +/// Return all products, which non-null amount in storage +#[utoipa::path( + responses( + (status = OK, description = "All products found", body = Vec<Product>), + (status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String) + ), +)] +#[get("/products_in_storage/")] +pub(crate) async fn products_in_storage(app: web::Data<App>) -> Result<impl Responder> { + let all = Product::get_all(&app).await?; + + let mut output_products = Vec::with_capacity(all.len()); + for product in all { + let amount = ProductAmount::from_id(&app, product.id).await?; + + if amount.is_some_and(|amount| amount.amount.value > 0) { + output_products.push(product); + } + } + + Ok(HttpResponse::Ok().json(output_products)) +} + +/// Get Products by it's product parent id +/// +/// This will also return all products below this product parent id +#[utoipa::path( + responses( + (status = OK, description = "Products found from database", body = Vec<Product>), + (status = NOT_FOUND, description = "Product parent id not found in database"), + (status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String) + ), + params( + ("id" = ProductParentId, description = "Product parent id" ), + ) +)] +#[get("/product/by-product-parent-id-indirect/{id}")] +pub(crate) async fn products_by_product_parent_id_indirect( + app: web::Data<App>, + id: web::Path<ProductParentIdStub>, +) -> Result<impl Responder> { + let id = id.into_inner(); + + if let Some(parent) = ProductParent::from_id(&app, id.into()).await? { + async fn collect_products(app: &App, parent: ProductParent) -> Result<Vec<Product>> { + let mut all = Product::get_all(app) + .await? + .into_iter() + .filter(|prod| prod.parent.is_some_and(|val| val == parent.id)) + .collect::<Vec<_>>(); + + if let Some(child) = ProductParent::get_all(app) + .await? + .into_iter() + .find(|pp| pp.parent.is_some_and(|id| id == parent.id)) + { + all.extend(Box::pin(collect_products(app, child)).await?); + } + + Ok(all) + } + + let all = collect_products(&app, parent).await?; + + Ok(HttpResponse::Ok().json(all)) + } else { + Ok(HttpResponse::NotFound().finish()) + } +} + +/// Get Products by it's product parent id +/// +/// This will only return products directly associated with this product parent id +#[utoipa::path( + responses( + (status = OK, description = "Products found from database", body = Vec<Product>), + (status = NOT_FOUND, description = "Product parent id not found in database"), + (status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String) + ), + params( + ("id" = ProductParentId, description = "Product parent id" ), + ) +)] +#[get("/product/by-product-parent-id-direct/{id}")] +pub(crate) async fn products_by_product_parent_id_direct( + app: web::Data<App>, + id: web::Path<ProductParentIdStub>, +) -> Result<impl Responder> { + let id = id.into_inner(); + + if let Some(parent) = ProductParent::from_id(&app, id.into()).await? { + let all = Product::get_all(&app) + .await? + .into_iter() + .filter(|prod| prod.parent.is_some_and(|val| val == parent.id)) + .collect::<Vec<_>>(); + + Ok(HttpResponse::Ok().json(all)) + } else { + Ok(HttpResponse::NotFound().finish()) + } +} diff --git a/crates/rocie-server/src/api/get/product_parent.rs b/crates/rocie-server/src/api/get/product_parent.rs new file mode 100644 index 0000000..a62397c --- /dev/null +++ b/crates/rocie-server/src/api/get/product_parent.rs @@ -0,0 +1,64 @@ +use actix_web::{HttpResponse, Responder, error::Result, get, web}; + +use crate::{ + app::App, + storage::sql::product_parent::{ProductParent, ProductParentId, ProductParentIdStub}, +}; + +/// Return all registered product parents +#[utoipa::path( + responses( + (status = OK, description = "All parents found", body = Vec<ProductParent>), + (status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String) + ), +)] +#[get("/product_parents/")] +pub(crate) async fn product_parents(app: web::Data<App>) -> Result<impl Responder> { + let all: Vec<ProductParent> = ProductParent::get_all(&app).await?; + + Ok(HttpResponse::Ok().json(all)) +} + +/// Return all registered product parents, that have no parents themselves +#[utoipa::path( + responses( + (status = OK, description = "All parents found", body = Vec<ProductParent>), + (status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String) + ), +)] +#[get("/product_parents_toplevel/")] +pub(crate) async fn product_parents_toplevel(app: web::Data<App>) -> Result<impl Responder> { + let all: Vec<ProductParent> = ProductParent::get_all(&app) + .await? + .into_iter() + .filter(|parent| parent.parent.is_none()) + .collect(); + + Ok(HttpResponse::Ok().json(all)) +} + +/// Return all parents, that have this parent as parent +#[utoipa::path( + responses( + (status = OK, description = "All parents found", body = Vec<ProductParent>), + (status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String) + ), + params( + ("id" = ProductParentId, description = "Product parent id" ), + ), +)] +#[get("/product_parents_under/{id}")] +pub(crate) async fn product_parents_under( + app: web::Data<App>, + id: web::Path<ProductParentIdStub>, +) -> Result<impl Responder> { + let id = id.into_inner().into(); + + let all: Vec<_> = ProductParent::get_all(&app) + .await? + .into_iter() + .filter(|parent| parent.parent.is_some_and(|found| found == id)) + .collect(); + + Ok(HttpResponse::Ok().json(all)) +} diff --git a/crates/rocie-server/src/api/get/recipe.rs b/crates/rocie-server/src/api/get/recipe.rs new file mode 100644 index 0000000..70bab39 --- /dev/null +++ b/crates/rocie-server/src/api/get/recipe.rs @@ -0,0 +1,66 @@ +use actix_web::{HttpResponse, Responder, error::Result, get, web}; + +use crate::{ + app::App, + storage::sql::recipe::{Recipe, RecipeId, RecipeIdStub}, +}; + +/// Get an recipe by it's id. +#[utoipa::path( + responses( + ( + status = OK, + description = "Recipe found in database and fetched", + body = Recipe, + ), + ( + status = NOT_FOUND, + description = "Recipe not found in database" + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), + params( + ( + "id" = RecipeId, + description = "Recipe id" + ), + ) +)] +#[get("/recipe/by-id/{id}")] +pub(crate) async fn recipe_by_id( + app: web::Data<App>, + id: web::Path<RecipeIdStub>, +) -> Result<impl Responder> { + let id = id.into_inner(); + + match Recipe::from_id(&app, id.into()).await? { + Some(recipe) => Ok(HttpResponse::Ok().json(recipe)), + None => Ok(HttpResponse::NotFound().finish()), + } +} + +/// Get all added recipes +#[utoipa::path( + responses( + ( + status = OK, + description = "All recipes found in database and fetched", + body = Recipe, + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), +)] +#[get("/recipe/all")] +pub(crate) async fn recipes(app: web::Data<App>) -> Result<impl Responder> { + let all = Recipe::get_all(&app).await?; + + Ok(HttpResponse::Ok().json(all)) +} diff --git a/crates/rocie-server/src/api/get/unit.rs b/crates/rocie-server/src/api/get/unit.rs index 73aa626..caafaa3 100644 --- a/crates/rocie-server/src/api/get/unit.rs +++ b/crates/rocie-server/src/api/get/unit.rs @@ -1,6 +1,12 @@ -use actix_web::{get, web, HttpResponse, Responder, Result}; +use actix_web::{HttpResponse, Responder, Result, get, web}; -use crate::{app::App, storage::sql::unit::{Unit, UnitId, UnitIdStub}}; +use crate::{ + app::App, + storage::sql::{ + unit::{Unit, UnitId, UnitIdStub}, + unit_property::{UnitPropertyId, UnitPropertyIdStub}, + }, +}; /// Return all registered units #[utoipa::path( @@ -24,6 +30,42 @@ pub(crate) async fn units(app: web::Data<App>) -> Result<impl Responder> { Ok(HttpResponse::Ok().json(all)) } +/// Return all registered units for a specific unit property +#[utoipa::path( + responses( + ( + status = OK, + description = "All units founds", + body = Vec<Unit> + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), + params( + ( + "id" = UnitPropertyId, + description = "Unit property id" + ), + ) +)] +#[get("/units-by-property/{id}")] +pub(crate) async fn units_by_property_id( + app: web::Data<App>, + id: web::Path<UnitPropertyIdStub>, +) -> Result<impl Responder> { + let id = id.into_inner(); + let all = Unit::get_all(&app) + .await? + .into_iter() + .filter(|unit| unit.unit_property == id.into()) + .collect::<Vec<_>>(); + + Ok(HttpResponse::Ok().json(all)) +} + /// Get Unit by id #[utoipa::path( responses( diff --git a/crates/rocie-server/src/api/set/mod.rs b/crates/rocie-server/src/api/set/mod.rs index 5dd0219..a6037b9 100644 --- a/crates/rocie-server/src/api/set/mod.rs +++ b/crates/rocie-server/src/api/set/mod.rs @@ -2,12 +2,16 @@ use actix_web::web; pub(crate) mod barcode; pub(crate) mod product; +pub(crate) mod product_parent; +pub(crate) mod recipe; 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(product_parent::register_product_parent) + .service(recipe::add_recipe) .service(unit::register_unit) .service(unit_property::register_unit_property) .service(barcode::consume_barcode) diff --git a/crates/rocie-server/src/api/set/product.rs b/crates/rocie-server/src/api/set/product.rs index 7372d23..74a92d2 100644 --- a/crates/rocie-server/src/api/set/product.rs +++ b/crates/rocie-server/src/api/set/product.rs @@ -8,6 +8,7 @@ use crate::{ barcode::Barcode, insert::Operations, product::{Product, ProductId, ProductIdStub}, + product_parent::ProductParentId, unit::Unit, unit_property::UnitPropertyId, }, @@ -22,10 +23,12 @@ struct ProductStub { unit_property: UnitPropertyId, /// A description. + #[schema(nullable = false)] description: Option<String>, /// A parent of this product, otherwise the parent will be the root of the parent tree. - parent: Option<ProductId>, + #[schema(nullable = false)] + parent: Option<ProductParentId>, } /// Register a product @@ -54,7 +57,7 @@ pub(crate) async fn register_product( let product = Product::register( product_stub.name.clone(), product_stub.description.clone(), - product_stub.parent, + product_stub.parent.into(), product_stub.unit_property, &mut ops, ); diff --git a/crates/rocie-server/src/api/set/product_parent.rs b/crates/rocie-server/src/api/set/product_parent.rs new file mode 100644 index 0000000..f917207 --- /dev/null +++ b/crates/rocie-server/src/api/set/product_parent.rs @@ -0,0 +1,60 @@ +use actix_web::{HttpResponse, Responder, Result, post, web}; +use serde::Deserialize; +use utoipa::ToSchema; + +use crate::{ + app::App, + storage::sql::{ + insert::Operations, + product_parent::{ProductParent, ProductParentId}, + }, +}; + +#[derive(Deserialize, ToSchema)] +struct ProductParentStub { + /// The name of the product parent + name: String, + + /// A description. + #[schema(nullable = false)] + description: Option<String>, + + /// A parent of this product parent, otherwise the parent will be the root of the parent tree. + #[schema(nullable = false)] + parent: Option<ProductParentId>, +} + +/// Register a product parent +#[utoipa::path( + responses( + ( + status = 200, + description = "Product parent successfully registered in database", + body = ProductParentId, + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String, + ) + ), + request_body = ProductParentStub, +)] +#[post("/product_parent/new")] +pub(crate) async fn register_product_parent( + app: web::Data<App>, + product_stub: web::Json<ProductParentStub>, +) -> Result<impl Responder> { + let mut ops = Operations::new("register product parent"); + + let product = ProductParent::register( + product_stub.name.clone(), + product_stub.description.clone(), + product_stub.parent.into(), + &mut ops, + ); + + ops.apply(&app).await?; + + Ok(HttpResponse::Ok().json(product.id)) +} diff --git a/crates/rocie-server/src/api/set/recipe.rs b/crates/rocie-server/src/api/set/recipe.rs new file mode 100644 index 0000000..bb5be37 --- /dev/null +++ b/crates/rocie-server/src/api/set/recipe.rs @@ -0,0 +1,54 @@ +use std::path::PathBuf; + +use actix_web::{HttpResponse, Responder, error::Result, post, web}; +use serde::Deserialize; +use utoipa::ToSchema; + +use crate::{ + app::App, + storage::sql::{ + insert::Operations, + recipe::{Recipe, RecipeId}, + }, +}; + +#[derive(Deserialize, ToSchema)] +struct RecipeStub { + /// The path the recipe should have + #[schema(value_type = String)] + path: PathBuf, + + /// The content of this recipe, in cooklang format + content: String, +} + +/// Register a product parent +#[utoipa::path( + responses( + ( + status = 200, + description = "Product parent successfully registered in database", + body = RecipeId, + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String, + ) + ), + request_body = RecipeStub, +)] +#[post("/recipe/new")] +pub(crate) async fn add_recipe( + app: web::Data<App>, + stub: web::Json<RecipeStub>, +) -> Result<impl Responder> { + let stub = stub.into_inner(); + let mut ops = Operations::new("add recipe parent"); + + let recipe = Recipe::new(stub.path, stub.content, &mut ops); + + ops.apply(&app).await?; + + Ok(HttpResponse::Ok().json(recipe.id)) +} diff --git a/crates/rocie-server/src/api/set/unit.rs b/crates/rocie-server/src/api/set/unit.rs index 5f39c8f..1671918 100644 --- a/crates/rocie-server/src/api/set/unit.rs +++ b/crates/rocie-server/src/api/set/unit.rs @@ -17,6 +17,8 @@ struct UnitStub { full_name_singular: String, short_name: String, unit_property: UnitPropertyId, + + #[schema(nullable = false)] description: Option<String>, } diff --git a/crates/rocie-server/src/api/set/unit_property.rs b/crates/rocie-server/src/api/set/unit_property.rs index 9915e84..ca2960f 100644 --- a/crates/rocie-server/src/api/set/unit_property.rs +++ b/crates/rocie-server/src/api/set/unit_property.rs @@ -12,7 +12,11 @@ use crate::{ #[derive(Deserialize, ToSchema)] struct UnitPropertyStub { + /// The name of the unit property. name: String, + + /// An optional description of the unit property. + #[schema(nullable = false)] description: Option<String>, } |
