update: ingredients controller send an array of ingredient instead of pure json

pull/2/head
Rémi REGNAULT 1 year ago
parent 337fd3ce96
commit df10669f3e

@ -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;
})

@ -2,3 +2,13 @@ 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
}
}
Loading…
Cancel
Save