Bläddra i källkod

Add delete recipe endpoint

Tatiana Inama 6 år sedan
förälder
incheckning
1c09a01a29
2 ändrade filer med 8 tillägg och 1 borttagningar
  1. 6 0
      ktchn/src/recipes/controller.ts
  2. 2 1
      ktchn/src/recipes/routes.ts

+ 6 - 0
ktchn/src/recipes/controller.ts

@@ -78,6 +78,11 @@ const saveImage = async (recipe: Recipe, _id: ObjectID) => {
   }
 }
 
+const deleteRecipe: Controller = (db) => ({ params }, res) => {
+  return db.deleteMany<Recipe>({_id: new ObjectID(params.id)})
+    .then(result => res.json(result.result))
+}
+
 const save: Controller = (db) => ({ body }, res) => {
   const _id = new ObjectID();
   return validRecipe(body)
@@ -169,4 +174,5 @@ export {
   getAll,
   getByIngredients,
   update,
+  deleteRecipe
 }

+ 2 - 1
ktchn/src/recipes/routes.ts

@@ -1,5 +1,5 @@
 import { Request, Response, Router, NextFunction } from "express";
-import { save, get, getById, getAll, getByIngredients, scrapeRecipe, update } from './controller';
+import { save, get, getById, getAll, getByIngredients, scrapeRecipe, update, deleteRecipe } from './controller';
 import MongoClient from 'mongodb';
 import { mongoService, IMongoService } from '../mongo';
 import piddleware, { chainP } from '../promise-all-middleware';
@@ -24,6 +24,7 @@ class RecipeRoutes {
     this.router.post("/", piddleware([save(this.RecipeDB)]));
     this.router.post("/scrape", chainP([scrapeRecipe(this.IngredientDB)]));
     this.router.put("/edit/:id", piddleware([update(this.RecipeDB)]));
+    this.router.delete("/:id", piddleware([deleteRecipe(this.RecipeDB)]));
   }
 
   private logData(req:Request, res:Response, next: NextFunction): void {