diff options
Diffstat (limited to 'crates/rocie-server/src/api/get')
| -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 |
5 files changed, 299 insertions, 9 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( |
