about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/api/get/auth/recipe.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rocie-server/src/api/get/auth/recipe.rs')
-rw-r--r--crates/rocie-server/src/api/get/auth/recipe.rs219
1 files changed, 213 insertions, 6 deletions
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))
+}