|
@@ -3,6 +3,7 @@ import { Recipe } from './model';
|
|
|
import { IMongoService } from '../mongo';
|
|
import { IMongoService } from '../mongo';
|
|
|
import { ObjectId } from 'bson';
|
|
import { ObjectId } from 'bson';
|
|
|
import { json } from 'body-parser';
|
|
import { json } from 'body-parser';
|
|
|
|
|
+import { FilterQuery } from 'mongodb';
|
|
|
|
|
|
|
|
function validRecipe(data: any): Promise<Recipe> {
|
|
function validRecipe(data: any): Promise<Recipe> {
|
|
|
return new Promise(function(resolve, reject) {
|
|
return new Promise(function(resolve, reject) {
|
|
@@ -10,6 +11,35 @@ function validRecipe(data: any): Promise<Recipe> {
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+//{ $or: [{"ingredients.ingredients.name": { "$regex": "beef", "$options": "i"}}, {"ingredients.ingredients.name": { "$regex": "cheese", "$options": "i"}}, {"ingredients.ingredients.name": { "$regex": "mix", "$options": "i"}}]}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+const queryKeys: {[s: string]: (q: string) => {}} = {
|
|
|
|
|
+ 'ing': (query: string) => {
|
|
|
|
|
+ return {
|
|
|
|
|
+ '$or': query.split(',').map(x => {
|
|
|
|
|
+ return {
|
|
|
|
|
+ 'ingredients.ingredients.name': {
|
|
|
|
|
+ '$regex': x,
|
|
|
|
|
+ '$options': 'i'
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function queryParser(query: any): FilterQuery<any> {
|
|
|
|
|
+ let q = {};
|
|
|
|
|
+ for (const key of Object.keys(query)) {
|
|
|
|
|
+ q = {
|
|
|
|
|
+ ...q,
|
|
|
|
|
+ ...queryKeys[key](query[key]),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return q;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
const save = (db: IMongoService) => ({ body }: Request, res: Response, next: NextFunction): Promise<any> => {
|
|
const save = (db: IMongoService) => ({ body }: Request, res: Response, next: NextFunction): Promise<any> => {
|
|
|
return validRecipe(body).then(
|
|
return validRecipe(body).then(
|
|
|
recipe => db.insertOne(recipe).then(
|
|
recipe => db.insertOne(recipe).then(
|
|
@@ -29,9 +59,14 @@ const getAll = (db: IMongoService) => ({ query }: Request, res: Response, next:
|
|
|
return db.find<Recipe>(query).then(recipes => res.json(recipes));
|
|
return db.find<Recipe>(query).then(recipes => res.json(recipes));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+const getByIngredients = (db: IMongoService) => ({ query, params }: Request, res: Response, next: NextFunction) => {
|
|
|
|
|
+ return db.find<Recipe>(queryParser(query)).then(recipes => res.json(recipes))
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
export {
|
|
export {
|
|
|
save,
|
|
save,
|
|
|
getById,
|
|
getById,
|
|
|
get,
|
|
get,
|
|
|
getAll,
|
|
getAll,
|
|
|
|
|
+ getByIngredients,
|
|
|
}
|
|
}
|