diff --git a/API-Project/src/controllers/ingredients.controller.ts b/API-Project/src/controllers/ingredients.controller.ts index 12d2dab..0f0a5f5 100644 --- a/API-Project/src/controllers/ingredients.controller.ts +++ b/API-Project/src/controllers/ingredients.controller.ts @@ -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; }) @@ -39,7 +42,7 @@ IngredientsController.get('/:id', (req, res) => { if (results.rowCount == 0) { throw new Exceptions.NotFoundException('no ingredient with this id'); } - + res.status(200) res.json(results.rows) }) diff --git a/API-Project/src/types/ingredients.ts b/API-Project/src/types/ingredients.ts index 6caa43b..9e696d3 100644 --- a/API-Project/src/types/ingredients.ts +++ b/API-Project/src/types/ingredients.ts @@ -1,4 +1,14 @@ export interface IIngredient { readonly id: number; readonly name: string; +} + +export class Ingredient implements IIngredient { + id: number; + name: string; + + constructor (id: number, name: string) { + this.id = id + this.name = name + } } \ No newline at end of file