about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/api/set
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/set
parentchore(treewide): Update (diff)
downloadserver-e5f90f4474cb96a78080395980283e4b2ce40214.zip
feat(treewide): Add recipes and user handling
Diffstat (limited to 'crates/rocie-server/src/api/set')
-rw-r--r--crates/rocie-server/src/api/set/auth/mod.rs2
-rw-r--r--crates/rocie-server/src/api/set/auth/recipe.rs21
-rw-r--r--crates/rocie-server/src/api/set/auth/recipe_parent.rs67
-rw-r--r--crates/rocie-server/src/api/set/no_auth/user.rs10
4 files changed, 89 insertions, 11 deletions
diff --git a/crates/rocie-server/src/api/set/auth/mod.rs b/crates/rocie-server/src/api/set/auth/mod.rs
index 4e733a9..6379f22 100644
--- a/crates/rocie-server/src/api/set/auth/mod.rs
+++ b/crates/rocie-server/src/api/set/auth/mod.rs
@@ -4,6 +4,7 @@ pub(crate) mod barcode;
 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;
@@ -13,6 +14,7 @@ pub(crate) fn register_paths(cfg: &mut web::ServiceConfig) {
         .service(product::associate_barcode)
         .service(product_parent::register_product_parent)
         .service(recipe::add_recipe)
+        .service(recipe_parent::register_recipe_parent)
         .service(unit::register_unit)
         .service(unit_property::register_unit_property)
         .service(barcode::consume_barcode)
diff --git a/crates/rocie-server/src/api/set/auth/recipe.rs b/crates/rocie-server/src/api/set/auth/recipe.rs
index 43a034e..b9f930d 100644
--- a/crates/rocie-server/src/api/set/auth/recipe.rs
+++ b/crates/rocie-server/src/api/set/auth/recipe.rs
@@ -1,5 +1,3 @@
-use std::path::PathBuf;
-
 use actix_identity::Identity;
 use actix_web::{HttpResponse, Responder, error::Result, post, web};
 use serde::Deserialize;
@@ -10,14 +8,18 @@ use crate::{
     storage::sql::{
         insert::Operations,
         recipe::{Recipe, RecipeId},
+        recipe_parent::RecipeParentId,
     },
 };
 
 #[derive(Deserialize, ToSchema)]
 struct RecipeStub {
-    /// The path the recipe should have
-    #[schema(value_type = String)]
-    path: PathBuf,
+    /// The globally unique name of this recipe
+    name: String,
+
+    /// The optional parent of this recipe.
+    #[schema(nullable = false)]
+    parent: Option<RecipeParentId>,
 
     /// The content of this recipe, in cooklang format
     content: String,
@@ -52,7 +54,14 @@ pub(crate) async fn add_recipe(
     let stub = stub.into_inner();
     let mut ops = Operations::new("add recipe parent");
 
-    let recipe = Recipe::new(stub.path, stub.content, &mut ops);
+    let recipe = Recipe::new(
+        &app,
+        stub.name,
+        stub.parent,
+        stub.content,
+        &mut ops,
+    )
+    .await?;
 
     ops.apply(&app).await?;
 
diff --git a/crates/rocie-server/src/api/set/auth/recipe_parent.rs b/crates/rocie-server/src/api/set/auth/recipe_parent.rs
new file mode 100644
index 0000000..e020dd3
--- /dev/null
+++ b/crates/rocie-server/src/api/set/auth/recipe_parent.rs
@@ -0,0 +1,67 @@
+use actix_identity::Identity;
+use actix_web::{HttpResponse, Responder, Result, post, web};
+use serde::Deserialize;
+use utoipa::ToSchema;
+
+use crate::{
+    app::App,
+    storage::sql::{
+        insert::Operations,
+        recipe_parent::{RecipeParent, RecipeParentId},
+    },
+};
+
+#[derive(Deserialize, ToSchema)]
+struct RecipeParentStub {
+    /// The name of the recipe parent
+    name: String,
+
+    /// A description.
+    #[schema(nullable = false)]
+    description: Option<String>,
+
+    /// A parent of this recipe parent, otherwise the parent will be the root of the parent tree.
+    #[schema(nullable = false)]
+    parent: Option<RecipeParentId>,
+}
+
+/// Register a product parent
+#[utoipa::path(
+    responses(
+        (
+            status = OK,
+            description = "Recipe parent successfully registered in database",
+            body = RecipeParentId,
+        ),
+        (
+            status = UNAUTHORIZED,
+            description = "You did not login before calling this endpoint",
+        ),
+        (
+            status = INTERNAL_SERVER_ERROR,
+            description = "Server encountered error",
+            body = String,
+        )
+    ),
+    request_body = RecipeParentStub,
+)]
+#[post("/recipe_parent/new")]
+pub(crate) async fn register_recipe_parent(
+    app: web::Data<App>,
+    parent_stub: web::Json<RecipeParentStub>,
+    _user: Identity,
+) -> Result<impl Responder> {
+    let parent_stub = parent_stub.into_inner();
+    let mut ops = Operations::new("register recipe parent");
+
+    let parent = RecipeParent::register(
+        parent_stub.name,
+        parent_stub.description,
+        parent_stub.parent,
+        &mut ops,
+    );
+
+    ops.apply(&app).await?;
+
+    Ok(HttpResponse::Ok().json(parent.id))
+}
diff --git a/crates/rocie-server/src/api/set/no_auth/user.rs b/crates/rocie-server/src/api/set/no_auth/user.rs
index 7acb482..7ca865c 100644
--- a/crates/rocie-server/src/api/set/no_auth/user.rs
+++ b/crates/rocie-server/src/api/set/no_auth/user.rs
@@ -14,8 +14,8 @@ use crate::{
 
 #[derive(ToSchema, Deserialize, Serialize)]
 struct LoginInfo {
-    /// The id of the user.
-    id: UserId,
+    /// The user name of the user.
+    user_name: String,
 
     /// The password of the user.
     password: String,
@@ -30,7 +30,7 @@ struct LoginInfo {
         ),
         (
             status = NOT_FOUND,
-            description = "User id not found"
+            description = "User name not found"
         ),
         (
             status = FORBIDDEN,
@@ -52,9 +52,9 @@ async fn login(
 ) -> Result<impl Responder> {
     let info = info.into_inner();
 
-    if let Some(user) = User::from_id(&app, info.id).await? {
+    if let Some(user) = User::from_name(&app, info.user_name).await? {
         if user.password_hash.verify(&info.password) {
-            Identity::login(&request.extensions(), info.id.to_string())?;
+            Identity::login(&request.extensions(), user.id.to_string())?;
             Ok(HttpResponse::Ok().finish())
         } else {
             Ok(HttpResponse::Forbidden().finish())