about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/api/set/auth/recipe.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rocie-server/src/api/set/auth/recipe.rs')
-rw-r--r--crates/rocie-server/src/api/set/auth/recipe.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/crates/rocie-server/src/api/set/auth/recipe.rs b/crates/rocie-server/src/api/set/auth/recipe.rs
new file mode 100644
index 0000000..43a034e
--- /dev/null
+++ b/crates/rocie-server/src/api/set/auth/recipe.rs
@@ -0,0 +1,60 @@
+use std::path::PathBuf;
+
+use actix_identity::Identity;
+use actix_web::{HttpResponse, Responder, error::Result, post, web};
+use serde::Deserialize;
+use utoipa::ToSchema;
+
+use crate::{
+    app::App,
+    storage::sql::{
+        insert::Operations,
+        recipe::{Recipe, RecipeId},
+    },
+};
+
+#[derive(Deserialize, ToSchema)]
+struct RecipeStub {
+    /// The path the recipe should have
+    #[schema(value_type = String)]
+    path: PathBuf,
+
+    /// The content of this recipe, in cooklang format
+    content: String,
+}
+
+/// Register a product parent
+#[utoipa::path(
+    responses(
+        (
+            status = OK,
+            description = "Product parent successfully registered in database",
+            body = RecipeId,
+        ),
+        (
+            status = UNAUTHORIZED,
+            description = "You did not login before calling this endpoint",
+        ),
+        (
+            status = INTERNAL_SERVER_ERROR,
+            description = "Server encountered error",
+            body = String,
+        )
+    ),
+    request_body = RecipeStub,
+)]
+#[post("/recipe/new")]
+pub(crate) async fn add_recipe(
+    app: web::Data<App>,
+    stub: web::Json<RecipeStub>,
+    _user: Identity,
+) -> Result<impl Responder> {
+    let stub = stub.into_inner();
+    let mut ops = Operations::new("add recipe parent");
+
+    let recipe = Recipe::new(stub.path, stub.content, &mut ops);
+
+    ops.apply(&app).await?;
+
+    Ok(HttpResponse::Ok().json(recipe.id))
+}