|
|
|
@ -1,24 +1,27 @@
|
|
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
|
import { Router } from "express";
|
|
|
|
|
import { Exceptions } from "../utils/exception";
|
|
|
|
|
import { IIngredient } from "../types/ingredients";
|
|
|
|
|
import { IIngredient, Ingredient } from "../types/ingredients";
|
|
|
|
|
import { pool } from "../database/connection";
|
|
|
|
|
import { QueryResult } from "pg";
|
|
|
|
|
import { Query, QueryResult } from "pg";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const IngredientsController = Router()
|
|
|
|
|
|
|
|
|
|
/** To get all ingredients */
|
|
|
|
|
IngredientsController.get('/', (req, res) => {
|
|
|
|
|
let ingredients:Ingredient[] = []
|
|
|
|
|
pool.query({text:'SELECT * FROM Ingredients ORDER BY id'}, (error: Error, results: QueryResult) => {
|
|
|
|
|
if (error) {
|
|
|
|
|
throw(error)
|
|
|
|
|
throw(error);
|
|
|
|
|
}
|
|
|
|
|
for (let key in results.rows) {
|
|
|
|
|
let ingredient:Ingredient = new Ingredient(Number(results.rows[key].id), results.rows[key].name);
|
|
|
|
|
ingredients.push(ingredient);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.status(200);
|
|
|
|
|
res.json(results.rows);
|
|
|
|
|
res.json(ingredients);
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|