import { Request, Response, NextFunction } from 'express' import { Recipe } from './model'; import { IMongoService } from '../mongo'; import { ObjectId } from 'bson'; import { json } from 'body-parser'; function validRecipe(data: any): Promise { return new Promise(function(resolve, reject) { return data.name !== undefined ? resolve(data) : reject('Missing data'); }) } const save = (db: IMongoService) => ({ body }: Request, res: Response, next: NextFunction): Promise => { return validRecipe(body).then( recipe => db.insertOne(recipe).then( dbRecipe => res.json(dbRecipe) )); }; const get = (db: IMongoService) => ({ query }: Request, res: Response, next: NextFunction) => { return db.findOne(query).then(result => res.json(result)); } const getById = (db: IMongoService) => ({ params }: Request, res: Response, next: NextFunction) => { return db.findOneById(params.id).then(recipe => res.json(recipe)); } const getAll = (db: IMongoService) => ({ query }: Request, res: Response, next: NextFunction) => { return db.find(query).then(recipes => res.json(recipes)); } export { save, getById, get, getAll, }