소스 검색

Create mongo service: Contains all mongo logic and methods

Tati 7 년 전
부모
커밋
ab9cd370df
4개의 변경된 파일27개의 추가작업 그리고 18개의 파일을 삭제
  1. 0 0
      ktchn/src/custom.d.ts
  2. 12 6
      ktchn/src/mongo.ts
  3. 8 7
      ktchn/src/recipes/controller.ts
  4. 7 5
      ktchn/src/recipes/routes.ts

+ 0 - 0
ktchn/src/custom.d.ts


+ 12 - 6
ktchn/src/mongo.ts

@@ -1,13 +1,19 @@
-import { Db, connect, MongoClient, InsertOneWriteOpResult, Collection } from 'mongodb';
-import { Request, Response, NextFunction } from 'express';
+import { Db, connect, MongoClient, InsertOneWriteOpResult, Collection, ObjectID } from 'mongodb';
 
-export default (db: Db) => (col: string) => {
+export interface IDBObject<T extends {}> {
+  _id: ObjectID
+}
+
+export interface IMongoService {
+  insertOne<T>(data: T): Promise<IDBObject<T>>,
+}
+
+export const mongoService = (db: Db) => (col: string) => {
   const collection:Collection = db.collection(col);
 
   return {
-    insertOne: (data: any):Promise<InsertOneWriteOpResult> => {
-      return collection.insertOne(data);
-    }
+    insertOne: <T>(data: T): Promise<IDBObject<T>> => collection.insertOne(data).then(insertOneResult => insertOneResult.ops[0])
   }
 };
 
+export default mongoService;

+ 8 - 7
ktchn/src/recipes/controller.ts

@@ -1,21 +1,22 @@
 import { Request, Response, NextFunction } from 'express'
 import { Recipe } from './model';
+import { IMongoService } from '../mongo';
 
 function isValidRecipe(data: any): data is Recipe {
   return data.name !== undefined;
 }
 
-function validateRecipe({ body }:Request, res:Response, next:NextFunction) {
+const saveRecipe = (db: IMongoService) => ({ body }: Request, res: Response, next: NextFunction): void => {
   if (isValidRecipe(body)) {
-    res.locals = {
-      data: body as Recipe
-    };
-    next();
+    db.insertOne(body).then(DBRecipe => {
+      res.json(DBRecipe);
+      next();
+    })
   } else {
-    next(new Error('Missing recipe information: name'));
+    next(new Error('Missing recipe information'));
   }
 }
 
 export {
-  validateRecipe,
+  saveRecipe,
 }

+ 7 - 5
ktchn/src/recipes/routes.ts

@@ -1,25 +1,27 @@
 import { Request, Response, Router, NextFunction } from "express";
 import Scrape from "./scrape";
 import { Recipe } from './model';
-import { validateRecipe } from './controller';
-import { storeRecipe } from './data';
+import { saveRecipe } from './controller';
 import MongoClient from 'mongodb';
+import { mongoService, IMongoService } from '../mongo';
 
 class RecipeRoutes {
   public router: Router;
+  private RecipeDB: IMongoService;
 
   public constructor(db: MongoClient.Db) {
     this.router = Router();
-    this.init(db);
+    this.RecipeDB = mongoService(db)('recipes');
+    this.init();
   }
 
-  private init(db: MongoClient.Db) {
+  private init() {
     this.router.get("/", (req, res, next) => {
       res.json({
         message: 'hey recipes bb'
       });
     })
-    this.router.post("/", validateRecipe, storeRecipe(db), this.logData)
+    this.router.post("/", saveRecipe(this.RecipeDB))
     this.router.post("/scrape", (req, res, next) => {
        return Scrape(req.body.url).then((x)=>{
          res.json(x)