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 { Request, Response, NextFunction } from "express";
import { Router } from "express"; import { Router } from "express";
import { Exceptions } from "../utils/exception"; import { Exceptions } from "../utils/exception";
import { IIngredient } from "../types/ingredients"; import { IIngredient, Ingredient } from "../types/ingredients";
import { pool } from "../database/connection"; import { pool } from "../database/connection";
import { QueryResult } from "pg"; import { Query, QueryResult } from "pg";
const IngredientsController = Router() const IngredientsController = Router()
/** To get all ingredients */ /** To get all ingredients */
IngredientsController.get('/', (req, res) => { IngredientsController.get('/', (req, res) => {
let ingredients:Ingredient[] = []
pool.query({text:'SELECT * FROM Ingredients ORDER BY id'}, (error: Error, results: QueryResult) => { pool.query({text:'SELECT * FROM Ingredients ORDER BY id'}, (error: Error, results: QueryResult) => {
if (error) { 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.status(200);
res.json(results.rows); res.json(ingredients);
}) })
return res; return res;
}) })
@ -39,7 +42,7 @@ IngredientsController.get('/:id', (req, res) => {
if (results.rowCount == 0) { if (results.rowCount == 0) {
throw new Exceptions.NotFoundException('no ingredient with this id'); throw new Exceptions.NotFoundException('no ingredient with this id');
} }
res.status(200) res.status(200)
res.json(results.rows) res.json(results.rows)
}) })

@ -1,4 +1,14 @@
export interface IIngredient { export interface IIngredient {
readonly id: number; readonly id: number;
readonly name: string; 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