|
|
@@ -0,0 +1,30 @@
|
|
|
+import auth from "$lib/server/auth.js";
|
|
|
+import Recipe from "$lib/server/models/Recipe.js";
|
|
|
+import {error, json} from "@sveltejs/kit";
|
|
|
+
|
|
|
+export async function POST({request, cookies, params}){
|
|
|
+ const user = await auth(cookies, false);
|
|
|
+ const body = await request.json();
|
|
|
+
|
|
|
+ if(!body.name) error(400, "Recipe name required");
|
|
|
+ if(body.ingredients.length <= 0) error(400, "Ingredients required");
|
|
|
+ if(body.steps.length <= 0) error(400, "Preparation steps required");
|
|
|
+
|
|
|
+ for(let i = 0; i < body.ingredients.length; i++){
|
|
|
+ if(body.ingredients[i].quantity <= 0) error(400, "Quantity must be greater than 0");
|
|
|
+ if(!body.ingredients[i].name) error(400, "Ingredient name required");
|
|
|
+ }
|
|
|
+
|
|
|
+ const recipe = await Recipe.findOne({_id: params.recipeId});
|
|
|
+
|
|
|
+ recipe.name = body.name;
|
|
|
+ recipe.ingredients = body.ingredients;
|
|
|
+ recipe.steps = body.steps;
|
|
|
+ recipe.notes = body.notes;
|
|
|
+ recipe.private = body.private;
|
|
|
+ recipe.updatedAt = new Date();
|
|
|
+
|
|
|
+ await recipe.save();
|
|
|
+
|
|
|
+ return json({msg: "success"}, {status: 200});
|
|
|
+}
|