فهرست منبع

Create query string endpoint for Recipes

Tati 7 سال پیش
والد
کامیت
993c72b8ae
3فایلهای تغییر یافته به همراه20 افزوده شده و 4 حذف شده
  1. 4 1
      ktchn/src/mongo.ts
  2. 12 0
      ktchn/src/recipes/controller.ts
  3. 4 3
      ktchn/src/recipes/routes.ts

+ 4 - 1
ktchn/src/mongo.ts

@@ -1,4 +1,4 @@
-import { Db, connect, MongoClient, InsertOneWriteOpResult, Collection, ObjectID, FilterQuery } from 'mongodb';
+import { Db, connect, MongoClient, InsertOneWriteOpResult, Collection, ObjectID, FilterQuery, ObjectId } from 'mongodb';
 
 export interface IDBDocument<T extends {}> {
   _id: ObjectID
@@ -6,6 +6,8 @@ export interface IDBDocument<T extends {}> {
 
 export interface IMongoService {
   insertOne<T>(data: T): Promise<IDBDocument<T>>,
+  findOne<T>(query: FilterQuery<T>): Promise<IDBDocument<T>>,
+  findOneById<T>(idParam: string): Promise<IDBDocument<T>>
 }
 
 export const mongoService = (db: Db) => (col: string) => {
@@ -14,6 +16,7 @@ export const mongoService = (db: Db) => (col: string) => {
   return {
     insertOne: <T>(data: T): Promise<IDBDocument<T>> => collection.insertOne(data).then(insertOneResult => insertOneResult.ops[0]),
     findOne: <T>(query: FilterQuery<T>) => collection.findOne(query),
+    findOneById: (idParam: string) => collection.findOne({ _id: new ObjectId(idParam) }),
   }
 };
 

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

@@ -1,6 +1,8 @@
 import { Request, Response, NextFunction } from 'express'
 import { Recipe } from './model';
 import { IMongoService } from '../mongo';
+import { ObjectId } from 'bson';
+import { json } from 'body-parser';
 
 function isValidRecipe(data: any): data is Recipe {
   return data.name !== undefined;
@@ -19,6 +21,16 @@ const save = (db: IMongoService) => ({ body }: Request, res: Response, next: Nex
   ));
 };
 
+const get = (db: IMongoService) => ({ query }: Request, res: Response, next: NextFunction) => {
+  return db.findOne<Recipe>(query).then(result => res.json(result));
+}
+
+const getById = (db: IMongoService) => ({ params }: Request, res: Response, next: NextFunction) => {
+  return db.findOneById<Recipe>(params.id).then(recipe => res.json(recipe));
+}
+
 export {
   save,
+  getById,
+  get,
 }

+ 4 - 3
ktchn/src/recipes/routes.ts

@@ -1,7 +1,7 @@
 import { Request, Response, Router, NextFunction } from "express";
 import Scrape from "./scrape";
 import { Recipe } from './model';
-import { save } from './controller';
+import { save, get, getById } from './controller';
 import MongoClient from 'mongodb';
 import { mongoService, IMongoService } from '../mongo';
 import piddleware from '../promise-all-middleware';
@@ -17,8 +17,9 @@ class RecipeRoutes {
   }
 
   private init() {
-    // this.router.get("/", (req, res, next) => )
-    this.router.post('/', piddleware([save(this.RecipeDB)]))
+    this.router.get("/", piddleware([get(this.RecipeDB)]))
+    this.router.get("/:id", piddleware([getById(this.RecipeDB)]))
+    this.router.post("/", piddleware([save(this.RecipeDB)]))
     this.router.post("/scrape", (req, res, next) => {
        return Scrape(req.body.url).then((x)=>{
          res.json(x)