about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/api/get
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2026-02-15 22:24:32 +0100
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2026-02-15 22:25:06 +0100
commite5f90f4474cb96a78080395980283e4b2ce40214 (patch)
treecaac3300795eae8e4cb1ee3c1c4bf85cd5950402 /crates/rocie-server/src/api/get
parentchore(treewide): Update (diff)
downloadserver-e5f90f4474cb96a78080395980283e4b2ce40214.zip
feat(treewide): Add recipes and user handling
Diffstat (limited to 'crates/rocie-server/src/api/get')
-rw-r--r--crates/rocie-server/src/api/get/auth/mod.rs28
-rw-r--r--crates/rocie-server/src/api/get/auth/product.rs62
-rw-r--r--crates/rocie-server/src/api/get/auth/recipe.rs219
-rw-r--r--crates/rocie-server/src/api/get/auth/recipe_parent.rs108
-rw-r--r--crates/rocie-server/src/api/get/no_auth/mod.rs7
-rw-r--r--crates/rocie-server/src/api/get/no_auth/state.rs41
6 files changed, 435 insertions, 30 deletions
diff --git a/crates/rocie-server/src/api/get/auth/mod.rs b/crates/rocie-server/src/api/get/auth/mod.rs
index c51f6a7..0821222 100644
--- a/crates/rocie-server/src/api/get/auth/mod.rs
+++ b/crates/rocie-server/src/api/get/auth/mod.rs
@@ -1,9 +1,12 @@
 use actix_web::web;
+use log::info;
+use percent_encoding::percent_decode_str;
 
 pub(crate) mod inventory;
 pub(crate) mod product;
 pub(crate) mod product_parent;
 pub(crate) mod recipe;
+pub(crate) mod recipe_parent;
 pub(crate) mod unit;
 pub(crate) mod unit_property;
 pub(crate) mod user;
@@ -17,11 +20,19 @@ pub(crate) fn register_paths(cfg: &mut web::ServiceConfig) {
         .service(product::products_by_product_parent_id_indirect)
         .service(product::products_in_storage)
         .service(product::products_registered)
+        .service(product::products_without_product_parent)
         .service(product_parent::product_parents)
         .service(product_parent::product_parents_toplevel)
         .service(product_parent::product_parents_under)
+        .service(recipe::recipe_by_name)
         .service(recipe::recipe_by_id)
         .service(recipe::recipes)
+        .service(recipe::recipes_by_recipe_parent_id_direct)
+        .service(recipe::recipes_by_recipe_parent_id_indirect)
+        .service(recipe::recipes_without_recipe_parent)
+        .service(recipe_parent::recipe_parents)
+        .service(recipe_parent::recipe_parents_toplevel)
+        .service(recipe_parent::recipe_parents_under)
         .service(unit::unit_by_id)
         .service(unit::units)
         .service(unit::units_by_property_id)
@@ -30,3 +41,20 @@ pub(crate) fn register_paths(cfg: &mut web::ServiceConfig) {
         .service(user::users)
         .service(user::user_by_id);
 }
+
+/// 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<String, std::str::Utf8Error> {
+        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())
+    }
+}
diff --git a/crates/rocie-server/src/api/get/auth/product.rs b/crates/rocie-server/src/api/get/auth/product.rs
index 1a1e31d..7e32a0b 100644
--- a/crates/rocie-server/src/api/get/auth/product.rs
+++ b/crates/rocie-server/src/api/get/auth/product.rs
@@ -1,33 +1,13 @@
 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::{
+    api::get::auth::UrlEncodedString, 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<String, std::str::Utf8Error> {
-        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(
@@ -118,7 +98,7 @@ pub(crate) async fn product_by_name(
     );
     let name = name.percent_decode()?;
 
-    match Product::from_name(&app, name).await? {
+    match Product::from_name(&app, name.as_str()).await? {
         Some(product) => Ok(HttpResponse::Ok().json(product)),
 
         None => Ok(HttpResponse::NotFound().finish()),
@@ -360,3 +340,39 @@ pub(crate) async fn products_by_product_parent_id_direct(
         Ok(HttpResponse::NotFound().finish())
     }
 }
+
+/// Get Products by it's absents of a product parent
+///
+/// This will only return products without a product parent associated with it
+#[utoipa::path(
+    responses(
+        (
+            status = OK,
+            description = "Products found from database",
+            body = Vec<Product>
+        ),
+        (
+            status = UNAUTHORIZED,
+            description = "You did not login before calling this endpoint",
+        ),
+        (
+            status = INTERNAL_SERVER_ERROR,
+            description = "Server encountered error",
+            body = String
+        )
+    ),
+)]
+#[get("/product/without-product-parent")]
+pub(crate) async fn products_without_product_parent(
+    app: web::Data<App>,
+    _user: Identity,
+) -> Result<impl Responder> {
+    let all = {
+        let base = Product::get_all(&app).await?;
+        base.into_iter()
+            .filter(|r| r.parent.is_none())
+            .collect::<Vec<_>>()
+    };
+
+    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
index cb80597..e3032b9 100644
--- a/crates/rocie-server/src/api/get/auth/recipe.rs
+++ b/crates/rocie-server/src/api/get/auth/recipe.rs
@@ -1,11 +1,66 @@
 use actix_identity::Identity;
-use actix_web::{HttpResponse, Responder, error::Result, get, web};
+use actix_web::{HttpRequest, HttpResponse, Responder, error::Result, get, web};
 
 use crate::{
+    api::get::auth::UrlEncodedString,
     app::App,
-    storage::sql::recipe::{Recipe, RecipeId, RecipeIdStub},
+    storage::sql::{
+        recipe::{Recipe, RecipeId, RecipeIdStub},
+        recipe_parent::{RecipeParent, RecipeParentId, RecipeParentIdStub},
+    },
 };
 
+/// Get an recipe by it's name.
+#[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(
+        (
+            "name" = String,
+            description = "Recipe name"
+        ),
+    )
+)]
+#[get("/recipe/by-name/{name}")]
+pub(crate) async fn recipe_by_name(
+    app: web::Data<App>,
+    req: HttpRequest,
+    name: web::Path<String>,
+    _user: Identity,
+) -> Result<impl Responder> {
+    drop(name);
+
+    let name = UrlEncodedString::from_str(
+        req.path()
+            .strip_prefix("/recipe/by-name/")
+            .expect("Will always exists"),
+    );
+    let name = name.percent_decode()?;
+
+    match Recipe::from_name(&app, name).await? {
+        Some(recipe) => Ok(HttpResponse::Ok().json(recipe)),
+        None => Ok(HttpResponse::NotFound().finish()),
+    }
+}
+
 /// Get an recipe by it's id.
 #[utoipa::path(
     responses(
@@ -41,9 +96,7 @@ pub(crate) async fn recipe_by_id(
     id: web::Path<RecipeIdStub>,
     _user: Identity,
 ) -> Result<impl Responder> {
-    let id = id.into_inner();
-
-    match Recipe::from_id(&app, id.into()).await? {
+    match Recipe::from_id(&app, id.into_inner().into()).await? {
         Some(recipe) => Ok(HttpResponse::Ok().json(recipe)),
         None => Ok(HttpResponse::NotFound().finish()),
     }
@@ -55,7 +108,7 @@ pub(crate) async fn recipe_by_id(
         (
             status = OK,
             description = "All recipes found in database and fetched",
-            body = Recipe,
+            body = Vec<Recipe>,
         ),
         (
             status = UNAUTHORIZED,
@@ -74,3 +127,157 @@ pub(crate) async fn recipes(app: web::Data<App>, _user: Identity) -> Result<impl
 
     Ok(HttpResponse::Ok().json(all))
 }
+
+/// Get Recipes by it's recipe parent id
+///
+/// This will also return all recipes below this recipe parent id
+#[utoipa::path(
+    responses(
+        (
+            status = OK,
+            description = "Recipes found from database",
+            body = Vec<Recipe>
+        ),
+        (
+            status = NOT_FOUND,
+            description = "Recipe 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" = RecipeParentId,
+            description = "Recipe parent id"
+        ),
+    )
+)]
+#[get("/recipe/by-recipe-parent-id-indirect/{id}")]
+pub(crate) async fn recipes_by_recipe_parent_id_indirect(
+    app: web::Data<App>,
+    id: web::Path<RecipeParentIdStub>,
+    _user: Identity,
+) -> Result<impl Responder> {
+    let id = id.into_inner();
+
+    if let Some(parent) = RecipeParent::from_id(&app, id.into()).await? {
+        async fn collect_recipes(app: &App, parent: RecipeParent) -> Result<Vec<Recipe>> {
+            let mut all = Recipe::get_all(app)
+                .await?
+                .into_iter()
+                .filter(|prod| prod.parent.is_some_and(|val| val == parent.id))
+                .collect::<Vec<_>>();
+
+            if let Some(child) = RecipeParent::get_all(app)
+                .await?
+                .into_iter()
+                .find(|pp| pp.parent.is_some_and(|id| id == parent.id))
+            {
+                all.extend(Box::pin(collect_recipes(app, child)).await?);
+            }
+
+            Ok(all)
+        }
+
+        let all = collect_recipes(&app, parent).await?;
+
+        Ok(HttpResponse::Ok().json(all))
+    } else {
+        Ok(HttpResponse::NotFound().finish())
+    }
+}
+
+/// Get Recipes by it's recipe parent id
+///
+/// This will only return recipes directly associated with this recipe parent id
+#[utoipa::path(
+    responses(
+        (
+            status = OK,
+            description = "Recipes found from database",
+            body = Vec<Recipe>
+        ),
+        (
+            status = NOT_FOUND,
+            description = "Recipe 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" = RecipeParentId,
+            description = "Recipe parent id"
+        ),
+    )
+)]
+#[get("/recipe/by-recipe-parent-id-direct/{id}")]
+pub(crate) async fn recipes_by_recipe_parent_id_direct(
+    app: web::Data<App>,
+    id: web::Path<RecipeParentIdStub>,
+    _user: Identity,
+) -> Result<impl Responder> {
+    let id = id.into_inner();
+
+    if let Some(parent) = RecipeParent::from_id(&app, id.into()).await? {
+        let all = Recipe::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())
+    }
+}
+
+/// Get Recipes by it's absents of a recipe parent
+///
+/// This will only return recipes without a recipe parent associated with it
+#[utoipa::path(
+    responses(
+        (
+            status = OK,
+            description = "Recipes found from database",
+            body = Vec<Recipe>
+        ),
+        (
+            status = UNAUTHORIZED,
+            description = "You did not login before calling this endpoint",
+        ),
+        (
+            status = INTERNAL_SERVER_ERROR,
+            description = "Server encountered error",
+            body = String
+        )
+    ),
+)]
+#[get("/recipe/without-recipe-parent")]
+pub(crate) async fn recipes_without_recipe_parent(
+    app: web::Data<App>,
+    _user: Identity,
+) -> Result<impl Responder> {
+    let all = {
+        let base = Recipe::get_all(&app).await?;
+        base.into_iter()
+            .filter(|r| r.parent.is_none())
+            .collect::<Vec<_>>()
+    };
+
+    Ok(HttpResponse::Ok().json(all))
+}
diff --git a/crates/rocie-server/src/api/get/auth/recipe_parent.rs b/crates/rocie-server/src/api/get/auth/recipe_parent.rs
new file mode 100644
index 0000000..d54082b
--- /dev/null
+++ b/crates/rocie-server/src/api/get/auth/recipe_parent.rs
@@ -0,0 +1,108 @@
+use actix_identity::Identity;
+use actix_web::{HttpResponse, Responder, error::Result, get, web};
+
+use crate::{
+    app::App,
+    storage::sql::recipe_parent::{RecipeParent, RecipeParentId, RecipeParentIdStub},
+};
+
+/// Return all registered recipe parents
+#[utoipa::path(
+    responses(
+        (
+            status = OK,
+            description = "All parents found",
+            body = Vec<RecipeParent>
+        ),
+        (
+            status = UNAUTHORIZED,
+            description = "You did not login before calling this endpoint",
+        ),
+        (
+            status = INTERNAL_SERVER_ERROR,
+            description = "Server encountered error",
+            body = String
+        )
+    ),
+)]
+#[get("/recipe_parents/")]
+pub(crate) async fn recipe_parents(app: web::Data<App>, _user: Identity) -> Result<impl Responder> {
+    let all: Vec<RecipeParent> = RecipeParent::get_all(&app).await?;
+
+    Ok(HttpResponse::Ok().json(all))
+}
+
+/// Return all registered recipe parents, that have no parents themselves
+#[utoipa::path(
+    responses(
+        (
+            status = OK,
+            description = "All parents found",
+            body = Vec<RecipeParent>
+        ),
+        (
+            status = UNAUTHORIZED,
+            description = "You did not login before calling this endpoint",
+        ),
+        (
+            status = INTERNAL_SERVER_ERROR,
+            description = "Server encountered error",
+            body = String
+        )
+    ),
+)]
+#[get("/recipe_parents_toplevel/")]
+pub(crate) async fn recipe_parents_toplevel(
+    app: web::Data<App>,
+    _user: Identity,
+) -> Result<impl Responder> {
+    let all: Vec<RecipeParent> = RecipeParent::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<RecipeParent>
+        ),
+        (
+            status = UNAUTHORIZED,
+            description = "You did not login before calling this endpoint",
+        ),
+        (
+            status = INTERNAL_SERVER_ERROR,
+            description = "Server encountered error",
+            body = String
+        )
+    ),
+    params(
+        (
+            "id" = RecipeParentId,
+            description = "Recipe parent id"
+        ),
+    ),
+)]
+#[get("/recipe_parents_under/{id}")]
+pub(crate) async fn recipe_parents_under(
+    app: web::Data<App>,
+    id: web::Path<RecipeParentIdStub>,
+    _user: Identity,
+) -> Result<impl Responder> {
+    let id = id.into_inner().into();
+
+    let all: Vec<_> = RecipeParent::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/no_auth/mod.rs b/crates/rocie-server/src/api/get/no_auth/mod.rs
index 38a041c..5274b4c 100644
--- a/crates/rocie-server/src/api/get/no_auth/mod.rs
+++ b/crates/rocie-server/src/api/get/no_auth/mod.rs
@@ -1,3 +1,8 @@
 use actix_web::web;
 
-pub(crate) fn register_paths(cfg: &mut web::ServiceConfig) {}
+pub(crate) mod state;
+
+pub(crate) fn register_paths(cfg: &mut web::ServiceConfig) {
+    cfg.service(state::is_logged_in)
+        .service(state::can_be_provisioned);
+}
diff --git a/crates/rocie-server/src/api/get/no_auth/state.rs b/crates/rocie-server/src/api/get/no_auth/state.rs
new file mode 100644
index 0000000..31cbfa5
--- /dev/null
+++ b/crates/rocie-server/src/api/get/no_auth/state.rs
@@ -0,0 +1,41 @@
+use actix_identity::Identity;
+use actix_web::{HttpResponse, Responder, Result, get, web};
+
+use crate::{app::App, storage::sql::user::User};
+
+/// Check if you are logged in
+#[utoipa::path(
+    responses(
+        (
+            status = OK,
+            description = "User login state checked",
+            body = bool
+        )
+    )
+)]
+#[get("/is-logged-in")]
+pub(crate) async fn is_logged_in(user: Option<Identity>) -> impl Responder {
+    HttpResponse::Ok().json(user.is_some())
+}
+
+/// Check if the server can be provisioned
+#[utoipa::path(
+    responses(
+        (
+            status = OK,
+            description = "Provisioning state checked",
+            body = bool
+        ),
+        (
+            status = INTERNAL_SERVER_ERROR,
+            description = "Server encountered error",
+            body = String
+        )
+    )
+)]
+#[get("/can-be-provisioned")]
+pub(crate) async fn can_be_provisioned(app: web::Data<App>) -> Result<impl Responder> {
+    let users = User::get_all(&app).await?;
+
+    Ok(HttpResponse::Ok().json(users.is_empty()))
+}