Ver código fonte

Add query endpoint for recipes

Tati 7 anos atrás
pai
commit
a40151cab9
3 arquivos alterados com 15 adições e 10 exclusões
  1. 4 2
      ktchn/src/mongo.ts
  2. 5 4
      ktchn/src/recipes/controller.ts
  3. 6 4
      ktchn/src/recipes/routes.ts

+ 4 - 2
ktchn/src/mongo.ts

@@ -1,4 +1,4 @@
-import { Db, connect, MongoClient, InsertOneWriteOpResult, Collection, ObjectID, FilterQuery, ObjectId } from 'mongodb';
+import { Db, Cursor, MongoClient, InsertOneWriteOpResult, Collection, ObjectID, FilterQuery, ObjectId } from 'mongodb';
 
 export interface IDBDocument<T extends {}> {
   _id: ObjectID
@@ -7,7 +7,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>>
+  findOneById<T>(idParam: string): Promise<IDBDocument<T>>,
+  find<T>(query: FilterQuery<T>): Promise<IDBDocument<T>[]>,
 }
 
 export const mongoService = (db: Db) => (col: string) => {
@@ -17,6 +18,7 @@ export const mongoService = (db: Db) => (col: string) => {
     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) }),
+    find: <T>(query: FilterQuery<T>): Promise<IDBDocument<T>[]> => collection.find(query).toArray(),
   }
 };
 

+ 5 - 4
ktchn/src/recipes/controller.ts

@@ -4,10 +4,6 @@ import { IMongoService } from '../mongo';
 import { ObjectId } from 'bson';
 import { json } from 'body-parser';
 
-function isValidRecipe(data: any): data is Recipe {
-  return data.name !== undefined;
-}
-
 function validRecipe(data: any): Promise<Recipe> {
   return new Promise(function(resolve, reject) {
     return data.name !== undefined ? resolve(data) : reject('Missing data');
@@ -29,8 +25,13 @@ const getById = (db: IMongoService) => ({ params }: Request, res: Response, next
   return db.findOneById<Recipe>(params.id).then(recipe => res.json(recipe));
 }
 
+const getAll = (db: IMongoService) => ({ query }: Request, res: Response, next: NextFunction) => {
+  return db.find<Recipe>(query).then(recipes => res.json(recipes));
+}
+
 export {
   save,
   getById,
   get,
+  getAll,
 }

+ 6 - 4
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, get, getById } from './controller';
+import { save, get, getById, getAll } from './controller';
 import MongoClient from 'mongodb';
 import { mongoService, IMongoService } from '../mongo';
 import piddleware from '../promise-all-middleware';
@@ -17,9 +17,11 @@ class RecipeRoutes {
   }
 
   private init() {
-    this.router.get("/", piddleware([get(this.RecipeDB)]))
-    this.router.get("/:id", piddleware([getById(this.RecipeDB)]))
-    this.router.post("/", piddleware([save(this.RecipeDB)]))
+    this.router.get("/all/", piddleware([getAll(this.RecipeDB)]));
+    this.router.get("/query/", piddleware([get(this.RecipeDB)]));
+    this.router.get("/id/: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)