Browse Source

Create Ingredients controller. Added some data to the DB

Tatiana Inama 7 years atrás
parent
commit
00d3ec55e2

+ 217 - 0
ktchn/src/ingredients/controller.ts

@@ -0,0 +1,217 @@
+import { Ingredient } from './model';
+import { IMongoService } from '../mongo';
+import { Request, Response, NextFunction } from 'express';
+
+const create = (db: IMongoService) => (req: Request, res: Response, next: NextFunction) => {
+  const ingredients: Ingredient[] = [{
+    name: 'all purpose flour',
+    equivalences: {
+      cup: 1,
+      gr: 125,
+      tbsp: 15.5,
+      tsp: 47,
+    },
+    referenceUnit: 'cup',
+    prefferedUnit: 'gr',
+    translation: {
+      dutch: 'patentbloem'
+    }
+  }, {
+    name: 'bread flour',
+    equivalences: {
+      cup: 1,
+      gr: 127,
+      tbsp: 12.7,
+      tsp: 38.1,
+    },
+    referenceUnit: 'cup',
+    prefferedUnit: 'gr',
+    translation: {
+      dutch: 'tarwebloem'
+    }
+  }, {
+    name: 'pastry flour',
+    equivalences: {
+      cup: 1,
+      gr: 114,
+      tbsp: 16.8,
+      tsp: 50.6,
+    },
+    referenceUnit: 'cup',
+    prefferedUnit: 'gr',
+    translation: {
+      dutch: 'zeeuwsebloem'
+    }
+  }, {
+    name: 'almond flour',
+    equivalences: {
+      cup: 1,
+      gr: 96,
+      tbsp: 16,
+      tsp: 48,
+    },
+    referenceUnit: 'cup',
+    prefferedUnit: 'gr',
+  }, {
+    name: 'cornflour',
+    equivalences: {
+      cup: 1,
+      gr: 150,
+      tbsp: 16,
+      tsp: 48,
+    },
+    referenceUnit: 'cup',
+    prefferedUnit: 'gr',
+  }, {
+    name: 'self raising flour',
+    equivalences: {
+      cup: 1,
+      gr: 125,
+      tbsp: 15.6,
+      tsp: 47,
+    },
+    referenceUnit: 'cup',
+    prefferedUnit: 'gr',
+  }, {
+    name: 'honey',
+    equivalences: {
+      cup: 1,
+      gr: 340,
+      oz: 12,
+      tbsp: 16,
+      tsp: 48,
+    },
+    referenceUnit: 'cup',
+    prefferedUnit: 'gr',
+  }, {
+    name: 'semolina',
+    equivalences: {
+      cup: 1,
+      gr: 167,
+      oz: 5.9,
+      lb: 0.37,
+      tbsp: 16,
+      tsp: 50,
+    },
+    referenceUnit: 'cup',
+    prefferedUnit: 'gr',
+  }, {
+    name: 'long rice',
+    equivalences: {
+      cup: 1,
+      gr: 185,
+      oz: 6.5,
+      lb: 0.44
+    },
+    referenceUnit: 'cup',
+    prefferedUnit: 'gr',
+  }, {
+    name: 'short rice',
+    equivalences: {
+      cup: 1,
+      gr: 200,
+      oz: 7.05,
+      lb: 0.44,
+    },
+    referenceUnit: 'cup',
+    prefferedUnit: 'gr',
+  }, {
+    name: 'basmati rice',
+    equivalences: {
+      cup: 1,
+      gr: 195,
+      oz: 6.88,
+      lb: 0.43,
+    },
+    referenceUnit: 'cup',
+    prefferedUnit: 'gr',
+  }, {
+    name: 'granulated sugar',
+    variants: ['sugar'],
+    equivalences: {
+      cup: 1,
+      gr: 200,
+      oz: 7.1,
+      tbsp: 14,
+      tsp: 42,
+      lb: 0.4
+    },
+    referenceUnit: 'cup',
+    prefferedUnit: 'gr',
+  }, {
+    name: 'icing sugar',
+    variants: ['confectioner sugar', 'powdered sugar'],
+    equivalences: {
+      cup: 1,
+      gr: 125,
+      oz: 0.27,
+      tbsp: 15,
+      tsp: 45,
+      lb: 0.27
+    },
+    referenceUnit: 'cup',
+    prefferedUnit: 'gr',
+  }, {
+    name: 'caster sugar',
+    variants: ['fine sugar'],
+    equivalences: {
+      cup: 1,
+      gr: 225,
+      oz: 7.9,
+      tbsp: 15,
+      tsp: 5,
+    },
+    referenceUnit: 'cup',
+    prefferedUnit: 'gr',
+  }, {
+    name: 'brown sugar',
+    equivalences: {
+      cup: 1,
+      gr: 200,
+      oz: 7.05,
+      lb: 0.44,
+      tbsp: 14,
+      tsp: 42,
+    },
+    referenceUnit: 'cup',
+    prefferedUnit: 'gr',
+  }, {
+    name: 'yeast',
+    equivalences: {
+      gr: 2.83,
+      tsp: 1,
+    },
+    referenceUnit: 'tsp',
+    prefferedUnit: 'gr',
+  }, {
+    name: 'yogurt',
+    variants: ['plain yogurt'],
+    equivalences: {
+      cup: 1,
+      gr: 245,
+      oz: 8.64,
+      tbsp: 16,
+      tsp: 48,
+    },
+    referenceUnit: 'cup',
+    prefferedUnit: 'gr',
+  },  {
+    name: 'cocoa powder',
+    variants: ['cocoa'],
+    equivalences: {
+      cup: 1,
+      gr: 118,
+      tbsp: 16,
+      tsp: 48,
+    },
+    referenceUnit: 'cup',
+    prefferedUnit: 'gr',
+  }];
+
+  return db.insertMany(ingredients)
+    .then(dbIngredient => res.json(dbIngredient))
+}
+
+export {
+  create,
+}

+ 31 - 0
ktchn/src/ingredients/model.ts

@@ -0,0 +1,31 @@
+import  { ObjectID } from 'mongodb';
+
+type Equivalences = {
+  cup?: number,
+  tbsp?: number,
+  tsp?: number,
+  stick?: number,
+  gr?: number,
+  ml?: number,
+  oz?: number,
+  lb?: number,
+};
+
+export interface Ingredient {
+  name: string,
+  variants?: string[],
+  equivalences: Equivalences,
+  prefferedUnit: keyof Equivalences,
+  referenceUnit: keyof Equivalences,
+  translation?: {
+    english?: string,
+    dutch?: string,
+    spanish?: string
+  }
+}
+
+export interface IngredientDB extends Ingredient {
+  _id: ObjectID,
+}
+
+export default Ingredient;

+ 23 - 0
ktchn/src/ingredients/routes.ts

@@ -0,0 +1,23 @@
+import { Request, Response, Router, NextFunction } from 'express';
+import { Db } from 'mongodb';
+import { mongoService, IMongoService } from '../mongo';
+import piddleware from '../promise-all-middleware';
+import { create } from './controller'
+
+class IngredientRoutes {
+  public router: Router;
+  private IngredientDB: IMongoService;
+
+  public constructor(db: Db) {
+    this.router = Router();
+    this.IngredientDB = mongoService(db)('ingredients');
+    this.init();
+  }
+
+  private init() {
+    this.router.get('/all/');
+    this.router.get('/create', piddleware([create(this.IngredientDB)]))
+  }
+}
+
+export default IngredientRoutes;

+ 2 - 0
ktchn/src/mongo.ts

@@ -6,6 +6,7 @@ export interface IDBDocument<T extends {}> {
 
 export interface IMongoService {
   insertOne<T>(data: T): Promise<IDBDocument<T>>,
+  insertMany<T>(data: T[]): Promise<IDBDocument<T>[]|any[]>,
   findOne<T>(query: FilterQuery<T>): Promise<IDBDocument<T>>,
   findOneById<T>(idParam: string): Promise<IDBDocument<T>>,
   find<T>(query: FilterQuery<T>): Promise<IDBDocument<T>[]>,
@@ -16,6 +17,7 @@ export const mongoService = (db: Db) => (col: string) => {
 
   return {
     insertOne: <T>(data: T): Promise<IDBDocument<T>> => collection.insertOne(data).then(insertOneResult => insertOneResult.ops[0]),
+    insertMany: <T>(data: T[]): Promise<any[]|IDBDocument<T>[]> => collection.insertMany(data).then(insertManyResult => insertManyResult.ops),
     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(),

+ 2 - 1
ktchn/src/server.ts

@@ -5,7 +5,7 @@ import logger from "morgan";
 import express, { Application } from "express";
 import rootRouter from "./routes/root";
 import recipeRouter from "./recipes/routes";
-import nano from 'nano';
+import ingredientsRouter from './ingredients/routes';
 import dotenv from 'dotenv';
 import MongoClient from 'mongodb';
 import { Response } from 'express';
@@ -45,6 +45,7 @@ class App {
   
   private routes(db: MongoClient.Db): void {
     this.app.use("/recipes", new recipeRouter(db).router);
+    this.app.use("/ingredients", new ingredientsRouter(db).router);
     this.app.use("/", rootRouter);
   }