From c91dce4f77ae12453203f0a28b91efb6533cc095 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Tue, 9 Dec 2025 13:07:14 +0100 Subject: feat(rocie-server): Implement basic user handling and authentication --- crates/rocie-server/src/api/get/auth/inventory.rs | 53 +++ crates/rocie-server/src/api/get/auth/mod.rs | 32 ++ crates/rocie-server/src/api/get/auth/product.rs | 362 +++++++++++++++++++++ .../src/api/get/auth/product_parent.rs | 111 +++++++ crates/rocie-server/src/api/get/auth/recipe.rs | 76 +++++ crates/rocie-server/src/api/get/auth/unit.rs | 120 +++++++ .../rocie-server/src/api/get/auth/unit_property.rs | 79 +++++ crates/rocie-server/src/api/get/auth/user.rs | 85 +++++ crates/rocie-server/src/api/get/inventory.rs | 44 --- crates/rocie-server/src/api/get/mod.rs | 31 +- crates/rocie-server/src/api/get/no_auth/mod.rs | 3 + crates/rocie-server/src/api/get/product.rs | 237 -------------- crates/rocie-server/src/api/get/product_parent.rs | 64 ---- crates/rocie-server/src/api/get/recipe.rs | 66 ---- crates/rocie-server/src/api/get/unit.rs | 105 ------ crates/rocie-server/src/api/get/unit_property.rs | 68 ---- 16 files changed, 923 insertions(+), 613 deletions(-) create mode 100644 crates/rocie-server/src/api/get/auth/inventory.rs create mode 100644 crates/rocie-server/src/api/get/auth/mod.rs create mode 100644 crates/rocie-server/src/api/get/auth/product.rs create mode 100644 crates/rocie-server/src/api/get/auth/product_parent.rs create mode 100644 crates/rocie-server/src/api/get/auth/recipe.rs create mode 100644 crates/rocie-server/src/api/get/auth/unit.rs create mode 100644 crates/rocie-server/src/api/get/auth/unit_property.rs create mode 100644 crates/rocie-server/src/api/get/auth/user.rs delete mode 100644 crates/rocie-server/src/api/get/inventory.rs create mode 100644 crates/rocie-server/src/api/get/no_auth/mod.rs delete mode 100644 crates/rocie-server/src/api/get/product.rs delete mode 100644 crates/rocie-server/src/api/get/product_parent.rs delete mode 100644 crates/rocie-server/src/api/get/recipe.rs delete mode 100644 crates/rocie-server/src/api/get/unit.rs delete mode 100644 crates/rocie-server/src/api/get/unit_property.rs (limited to 'crates/rocie-server/src/api/get') diff --git a/crates/rocie-server/src/api/get/auth/inventory.rs b/crates/rocie-server/src/api/get/auth/inventory.rs new file mode 100644 index 0000000..24a8e3d --- /dev/null +++ b/crates/rocie-server/src/api/get/auth/inventory.rs @@ -0,0 +1,53 @@ +use actix_identity::Identity; +use actix_web::{HttpResponse, Responder, Result, get, web}; + +use crate::{ + app::App, + storage::sql::{ + product::{ProductId, ProductIdStub}, + product_amount::ProductAmount, + }, +}; + +/// Get the amount of an product +#[utoipa::path( + responses( + ( + status = OK, + description = "Product found in database and amount fetched", + body = ProductAmount + ), + ( + status = NOT_FOUND, + description = "Product not found in database" + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), + params( + ( + "id" = ProductId, + description = "Product id" + ), + ) +)] +#[get("/inventory/{id}")] +pub(crate) async fn amount_by_id( + app: web::Data, + id: web::Path, + _user: Identity, +) -> Result { + let id = id.into_inner(); + + match ProductAmount::from_id(&app, id.into()).await? { + Some(product) => Ok(HttpResponse::Ok().json(product)), + None => Ok(HttpResponse::NotFound().finish()), + } +} diff --git a/crates/rocie-server/src/api/get/auth/mod.rs b/crates/rocie-server/src/api/get/auth/mod.rs new file mode 100644 index 0000000..c51f6a7 --- /dev/null +++ b/crates/rocie-server/src/api/get/auth/mod.rs @@ -0,0 +1,32 @@ +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; +pub(crate) mod user; + +pub(crate) fn register_paths(cfg: &mut web::ServiceConfig) { + cfg.service(inventory::amount_by_id) + .service(product::product_by_id) + .service(product::product_by_name) + .service(product::product_suggestion_by_name) + .service(product::products_by_product_parent_id_direct) + .service(product::products_by_product_parent_id_indirect) + .service(product::products_in_storage) + .service(product::products_registered) + .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::unit_by_id) + .service(unit::units) + .service(unit::units_by_property_id) + .service(unit_property::unit_properties) + .service(unit_property::unit_property_by_id) + .service(user::users) + .service(user::user_by_id); +} diff --git a/crates/rocie-server/src/api/get/auth/product.rs b/crates/rocie-server/src/api/get/auth/product.rs new file mode 100644 index 0000000..1a1e31d --- /dev/null +++ b/crates/rocie-server/src/api/get/auth/product.rs @@ -0,0 +1,362 @@ +use actix_identity::Identity; +use actix_web::{HttpRequest, HttpResponse, Responder, Result, get, web}; +use log::info; +use percent_encoding::percent_decode_str; + +use crate::{ + app::App, + storage::sql::{ + product::{Product, ProductId, ProductIdStub}, + product_amount::ProductAmount, + product_parent::{ProductParent, ProductParentId, ProductParentIdStub}, + }, +}; + +/// A String, that is not url-decoded on parse. +struct UrlEncodedString(String); + +impl UrlEncodedString { + /// Percent de-encode a given string + fn percent_decode(&self) -> Result { + percent_decode_str(self.0.replace('+', "%20").as_str()) + .decode_utf8() + .map(|s| s.to_string()) + .inspect(|s| info!("Decoded `{}` as `{s}`", self.0)) + } + + fn from_str(inner: &str) -> Self { + Self(inner.to_owned()) + } +} + +/// Get Product by id +#[utoipa::path( + responses( + ( + status = OK, + description = "Product found from database", + body = Product + ), + ( + status = NOT_FOUND, + description = "Product not found in database" + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), + params( + ( + "id" = ProductId, + description = "Product id" + ), + ) +)] +#[get("/product/by-id/{id}")] +pub(crate) async fn product_by_id( + app: web::Data, + id: web::Path, + _user: Identity, +) -> Result { + let id = id.into_inner(); + + match Product::from_id(&app, id.into()).await? { + Some(product) => Ok(HttpResponse::Ok().json(product)), + + None => Ok(HttpResponse::NotFound().finish()), + } +} + +/// Get Product by name +#[utoipa::path( + responses( + ( + status = OK, + description = "Product found from database", + body = Product + ), + ( + status = NOT_FOUND, + description = "Product not found in database" + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), + params( + ( + "name" = String, + description = "Name of the product" + ), + ) +)] +#[get("/product/by-name/{name}")] +pub(crate) async fn product_by_name( + app: web::Data, + req: HttpRequest, + name: web::Path, + _user: Identity, +) -> Result { + drop(name); + + let name = UrlEncodedString::from_str( + req.path() + .strip_prefix("/product/by-name/") + .expect("Will always exists"), + ); + let name = name.percent_decode()?; + + match Product::from_name(&app, name).await? { + Some(product) => Ok(HttpResponse::Ok().json(product)), + + None => Ok(HttpResponse::NotFound().finish()), + } +} + +/// Get Product suggestion by name +#[utoipa::path( + responses( + ( + status = OK, + description = "Product suggestions found from database", + body = Vec + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), + params( + ( + "name" = String, + description = "Partial name of a product" + ), + ) +)] +#[get("/product/by-part-name/{name}")] +pub(crate) async fn product_suggestion_by_name( + app: web::Data, + req: HttpRequest, + name: web::Path, + _user: Identity, +) -> Result { + drop(name); + + let name = UrlEncodedString::from_str( + req.path() + .strip_prefix("/product/by-part-name/") + .expect("Will always exists"), + ); + let name = &name.percent_decode()?; + + let all = Product::get_all(&app).await?; + + let matching = all + .into_iter() + .filter(|product| product.name.starts_with(name.as_str())) + .collect::>(); + + Ok(HttpResponse::Ok().json(matching)) +} + +/// Return all registered products +#[utoipa::path( + responses( + ( + status = OK, + description = "All products found", + body = Vec + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), +)] +#[get("/products_registered/")] +pub(crate) async fn products_registered( + app: web::Data, + _user: Identity, +) -> Result { + 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 + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), +)] +#[get("/products_in_storage/")] +pub(crate) async fn products_in_storage( + app: web::Data, + _user: Identity, +) -> Result { + 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 + ), + ( + status = NOT_FOUND, + description = "Product parent id not found in database" + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + 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, + id: web::Path, + _user: Identity, +) -> Result { + 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> { + let mut all = Product::get_all(app) + .await? + .into_iter() + .filter(|prod| prod.parent.is_some_and(|val| val == parent.id)) + .collect::>(); + + 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 + ), + ( + status = NOT_FOUND, + description = "Product parent id not found in database" + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + 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, + id: web::Path, + _user: Identity, +) -> Result { + 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::>(); + + Ok(HttpResponse::Ok().json(all)) + } else { + Ok(HttpResponse::NotFound().finish()) + } +} diff --git a/crates/rocie-server/src/api/get/auth/product_parent.rs b/crates/rocie-server/src/api/get/auth/product_parent.rs new file mode 100644 index 0000000..6c3351d --- /dev/null +++ b/crates/rocie-server/src/api/get/auth/product_parent.rs @@ -0,0 +1,111 @@ +use actix_identity::Identity; +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 + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), +)] +#[get("/product_parents/")] +pub(crate) async fn product_parents( + app: web::Data, + _user: Identity, +) -> Result { + let all: Vec = 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 + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), +)] +#[get("/product_parents_toplevel/")] +pub(crate) async fn product_parents_toplevel( + app: web::Data, + _user: Identity, +) -> Result { + let all: Vec = 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 + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + 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, + id: web::Path, + _user: Identity, +) -> Result { + 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/auth/recipe.rs b/crates/rocie-server/src/api/get/auth/recipe.rs new file mode 100644 index 0000000..cb80597 --- /dev/null +++ b/crates/rocie-server/src/api/get/auth/recipe.rs @@ -0,0 +1,76 @@ +use actix_identity::Identity; +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 = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + 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, + id: web::Path, + _user: Identity, +) -> Result { + 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 = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), +)] +#[get("/recipe/all")] +pub(crate) async fn recipes(app: web::Data, _user: Identity) -> Result { + let all = Recipe::get_all(&app).await?; + + Ok(HttpResponse::Ok().json(all)) +} diff --git a/crates/rocie-server/src/api/get/auth/unit.rs b/crates/rocie-server/src/api/get/auth/unit.rs new file mode 100644 index 0000000..980d9c7 --- /dev/null +++ b/crates/rocie-server/src/api/get/auth/unit.rs @@ -0,0 +1,120 @@ +use actix_identity::Identity; +use actix_web::{HttpResponse, Responder, Result, get, web}; + +use crate::{ + app::App, + storage::sql::{ + unit::{Unit, UnitId, UnitIdStub}, + unit_property::{UnitPropertyId, UnitPropertyIdStub}, + }, +}; + +/// Return all registered units +#[utoipa::path( + responses( + ( + status = OK, + description = "All units founds", + body = Vec + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), +)] +#[get("/units/")] +pub(crate) async fn units(app: web::Data, _user: Identity) -> Result { + let all = Unit::get_all(&app).await?; + + 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 + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + 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, + id: web::Path, + _user: Identity, +) -> Result { + let id = id.into_inner(); + let all = Unit::get_all(&app) + .await? + .into_iter() + .filter(|unit| unit.unit_property == id.into()) + .collect::>(); + + Ok(HttpResponse::Ok().json(all)) +} + +/// Get Unit by id +#[utoipa::path( + responses( + ( + status = OK, + description = "Unit found from database", + body = Unit + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + 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" + ), + ) +)] +#[get("/unit/{id}")] +pub(crate) async fn unit_by_id( + app: web::Data, + id: web::Path, + _user: Identity, +) -> Result { + let id = id.into_inner(); + + match Unit::from_id(&app, id.into()).await? { + Some(unit) => Ok(HttpResponse::Ok().json(unit)), + None => Ok(HttpResponse::NotFound().finish()), + } +} diff --git a/crates/rocie-server/src/api/get/auth/unit_property.rs b/crates/rocie-server/src/api/get/auth/unit_property.rs new file mode 100644 index 0000000..f5b070a --- /dev/null +++ b/crates/rocie-server/src/api/get/auth/unit_property.rs @@ -0,0 +1,79 @@ +use actix_identity::Identity; +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 + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), +)] +#[get("/unit-properties/")] +pub(crate) async fn unit_properties( + app: web::Data, + _user: Identity, +) -> Result { + 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 = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + 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, + id: web::Path, + _user: Identity, +) -> Result { + 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/get/auth/user.rs b/crates/rocie-server/src/api/get/auth/user.rs new file mode 100644 index 0000000..e4a5046 --- /dev/null +++ b/crates/rocie-server/src/api/get/auth/user.rs @@ -0,0 +1,85 @@ +use actix_identity::Identity; +use actix_web::{HttpResponse, Responder, Result, get, web}; + +use crate::{ + app::App, + storage::sql::user::{User, UserId, UserIdStub}, +}; + +/// Get all registered users. +#[utoipa::path( + responses( + ( + status = OK, + description = "Users found in database and fetched", + body = Vec, + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), +)] +#[get("/users")] +async fn users(app: web::Data, _user: Identity) -> Result { + let output = User::get_all(&app).await?; + + Ok(HttpResponse::Ok().json(output)) +} + +/// Get an specific user by id. +#[utoipa::path( + responses( + ( + status = OK, + description = "User found in database and fetched", + body = User, + ), + ( + status = NOT_FOUND, + description = "User not found in database" + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = FORBIDDEN, + description = "The current logged in user is not allowed to access this end-point." + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), + params( + ( + "id" = UserId, + description = "User id" + ), + ) +)] +#[get("/user/{id}")] +async fn user_by_id( + id: web::Path, + app: web::Data, + user: Identity, +) -> Result { + let id: UserId = id.into_inner().into(); + + if user.id().expect("to have one") != id.to_string() { + return Ok(HttpResponse::Forbidden() + .body("You must be logged-in as the same user, you request the info for.")); + } + + match User::from_id(&app, id).await? { + Some(user) => Ok(HttpResponse::Ok().json(user)), + None => Ok(HttpResponse::NotFound().finish()), + } +} diff --git a/crates/rocie-server/src/api/get/inventory.rs b/crates/rocie-server/src/api/get/inventory.rs deleted file mode 100644 index d1ca436..0000000 --- a/crates/rocie-server/src/api/get/inventory.rs +++ /dev/null @@ -1,44 +0,0 @@ -use actix_web::{HttpResponse, Responder, Result, get, web}; - -use crate::{ - app::App, - storage::sql::{product::{ProductId, ProductIdStub}, product_amount::ProductAmount}, -}; - -/// Get the amount of an product -#[utoipa::path( - responses( - ( - status = OK, - description = "Product found in database and amount fetched", - body = ProductAmount - ), - ( - status = NOT_FOUND, - description = "Product not found in database" - ), - ( - status = INTERNAL_SERVER_ERROR, - description = "Server encountered error", - body = String - ) - ), - params( - ( - "id" = ProductId, - description = "Product id" - ), - ) -)] -#[get("/inventory/{id}")] -pub(crate) async fn amount_by_id( - app: web::Data, - id: web::Path, -) -> Result { - let id = id.into_inner(); - - match ProductAmount::from_id(&app, id.into()).await? { - Some(product) => Ok(HttpResponse::Ok().json(product)), - None => Ok(HttpResponse::NotFound().finish()), - } -} diff --git a/crates/rocie-server/src/api/get/mod.rs b/crates/rocie-server/src/api/get/mod.rs index 487b55c..c6ee9ab 100644 --- a/crates/rocie-server/src/api/get/mod.rs +++ b/crates/rocie-server/src/api/get/mod.rs @@ -1,29 +1,2 @@ -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; - -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_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) - .service(inventory::amount_by_id); -} +pub(crate) mod auth; +pub(crate) mod no_auth; diff --git a/crates/rocie-server/src/api/get/no_auth/mod.rs b/crates/rocie-server/src/api/get/no_auth/mod.rs new file mode 100644 index 0000000..38a041c --- /dev/null +++ b/crates/rocie-server/src/api/get/no_auth/mod.rs @@ -0,0 +1,3 @@ +use actix_web::web; + +pub(crate) fn register_paths(cfg: &mut web::ServiceConfig) {} diff --git a/crates/rocie-server/src/api/get/product.rs b/crates/rocie-server/src/api/get/product.rs deleted file mode 100644 index 4216f9b..0000000 --- a/crates/rocie-server/src/api/get/product.rs +++ /dev/null @@ -1,237 +0,0 @@ -use actix_web::{HttpRequest, HttpResponse, Responder, Result, get, web}; -use log::info; -use percent_encoding::percent_decode_str; - -use crate::{ - app::App, - storage::sql::{ - product::{Product, ProductId, ProductIdStub}, - product_amount::ProductAmount, - product_parent::{ProductParent, ProductParentId, ProductParentIdStub}, - }, -}; - -/// A String, that is not url-decoded on parse. -struct UrlEncodedString(String); - -impl UrlEncodedString { - /// Percent de-encode a given string - fn percent_decode(&self) -> Result { - percent_decode_str(self.0.replace('+', "%20").as_str()) - .decode_utf8() - .map(|s| s.to_string()) - .inspect(|s| info!("Decoded `{}` as `{s}`", self.0)) - } - - fn from_str(inner: &str) -> Self { - Self(inner.to_owned()) - } -} - -/// Get Product by id -#[utoipa::path( - responses( - (status = OK, description = "Product found from database", body = Product), - (status = NOT_FOUND, description = "Product not found in database"), - (status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String) - ), - params( - ("id" = ProductId, description = "Product id" ), - ) -)] -#[get("/product/by-id/{id}")] -pub(crate) async fn product_by_id( - app: web::Data, - id: web::Path, -) -> Result { - let id = id.into_inner(); - - match Product::from_id(&app, id.into()).await? { - Some(product) => Ok(HttpResponse::Ok().json(product)), - None => Ok(HttpResponse::NotFound().finish()), - } -} - -/// Get Product by name -#[utoipa::path( - responses( - (status = OK, description = "Product found from database", body = Product), - (status = NOT_FOUND, description = "Product not found in database"), - (status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String) - ), - params( - ("name" = String, description = "Name of the product" ), - ) -)] -#[get("/product/by-name/{name}")] -pub(crate) async fn product_by_name( - app: web::Data, - req: HttpRequest, - name: web::Path, -) -> Result { - drop(name); - - let name = UrlEncodedString::from_str( - req.path() - .strip_prefix("/product/by-name/") - .expect("Will always exists"), - ); - let name = name.percent_decode()?; - - match Product::from_name(&app, name).await? { - Some(product) => Ok(HttpResponse::Ok().json(product)), - None => Ok(HttpResponse::NotFound().finish()), - } -} - -/// Get Product suggestion by name -#[utoipa::path( - responses( - (status = OK, description = "Product suggestions found from database", body = Vec), - (status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String) - ), - params( - ("name" = String, description = "Partial name of a product" ), - ) -)] -#[get("/product/by-part-name/{name}")] -pub(crate) async fn product_suggestion_by_name( - app: web::Data, - req: HttpRequest, - name: web::Path, -) -> Result { - drop(name); - - let name = UrlEncodedString::from_str( - req.path() - .strip_prefix("/product/by-part-name/") - .expect("Will always exists"), - ); - let name = &name.percent_decode()?; - - let all = Product::get_all(&app).await?; - - let matching = all - .into_iter() - .filter(|product| product.name.starts_with(name.as_str())) - .collect::>(); - - Ok(HttpResponse::Ok().json(matching)) -} - -/// Return all registered products -#[utoipa::path( - responses( - (status = OK, description = "All products found", body = Vec), - (status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String) - ), -)] -#[get("/products_registered/")] -pub(crate) async fn products_registered(app: web::Data) -> Result { - 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), - (status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String) - ), -)] -#[get("/products_in_storage/")] -pub(crate) async fn products_in_storage(app: web::Data) -> Result { - 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), - (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, - id: web::Path, -) -> Result { - 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> { - let mut all = Product::get_all(app) - .await? - .into_iter() - .filter(|prod| prod.parent.is_some_and(|val| val == parent.id)) - .collect::>(); - - 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), - (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, - id: web::Path, -) -> Result { - 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::>(); - - 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 deleted file mode 100644 index a62397c..0000000 --- a/crates/rocie-server/src/api/get/product_parent.rs +++ /dev/null @@ -1,64 +0,0 @@ -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), - (status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String) - ), -)] -#[get("/product_parents/")] -pub(crate) async fn product_parents(app: web::Data) -> Result { - let all: Vec = 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), - (status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String) - ), -)] -#[get("/product_parents_toplevel/")] -pub(crate) async fn product_parents_toplevel(app: web::Data) -> Result { - let all: Vec = 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), - (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, - id: web::Path, -) -> Result { - 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 deleted file mode 100644 index 70bab39..0000000 --- a/crates/rocie-server/src/api/get/recipe.rs +++ /dev/null @@ -1,66 +0,0 @@ -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, - id: web::Path, -) -> Result { - 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) -> Result { - 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 deleted file mode 100644 index caafaa3..0000000 --- a/crates/rocie-server/src/api/get/unit.rs +++ /dev/null @@ -1,105 +0,0 @@ -use actix_web::{HttpResponse, Responder, Result, get, web}; - -use crate::{ - app::App, - storage::sql::{ - unit::{Unit, UnitId, UnitIdStub}, - unit_property::{UnitPropertyId, UnitPropertyIdStub}, - }, -}; - -/// Return all registered units -#[utoipa::path( - responses( - ( - status = OK, - description = "All units founds", - body = Vec - ), - ( - status = INTERNAL_SERVER_ERROR, - description = "Server encountered error", - body = String - ) - ), -)] -#[get("/units/")] -pub(crate) async fn units(app: web::Data) -> Result { - let all = Unit::get_all(&app).await?; - - 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 - ), - ( - 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, - id: web::Path, -) -> Result { - let id = id.into_inner(); - let all = Unit::get_all(&app) - .await? - .into_iter() - .filter(|unit| unit.unit_property == id.into()) - .collect::>(); - - Ok(HttpResponse::Ok().json(all)) -} - -/// 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 - ) - ), - params( - ( - "id" = UnitId, - description = "Unit id" - ), - ) -)] -#[get("/unit/{id}")] -pub(crate) async fn unit_by_id( - app: web::Data, - id: web::Path, -) -> Result { - let id = id.into_inner(); - - match Unit::from_id(&app, id.into()).await? { - 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 deleted file mode 100644 index 3160480..0000000 --- a/crates/rocie-server/src/api/get/unit_property.rs +++ /dev/null @@ -1,68 +0,0 @@ -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 - ), - ( - status = INTERNAL_SERVER_ERROR, - description = "Server encountered error", - body = String - ) - ), -)] -#[get("/unit-properties/")] -pub(crate) async fn unit_properties(app: web::Data) -> Result { - 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, - id: web::Path, -) -> Result { - 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()), - } -} -- cgit 1.4.1