feat: divide old ingredients controller into new ingredient controller and ingredient gateway

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

@ -4,50 +4,48 @@ import { Exceptions } from "../utils/exception";
import { IIngredient, Ingredient } from "../types/ingredients";
import { pool } from "../database/connection";
import { Query, QueryResult } from "pg";
import { IngredientsGateway } from "../gateways/ingredients.gateway";
const IngredientsController = Router()
const ingredient_gw = new IngredientsGateway()
/** 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);
}
for (let key in results.rows) {
let ingredient:Ingredient = new Ingredient(Number(results.rows[key].id), results.rows[key].name);
ingredients.push(ingredient);
IngredientsController.get('/', async (req, res) => {
try {
const ingredients = await ingredient_gw.getAll()
res.status(200).json(ingredients)
} catch (error) {
const error_error = error as Error
res.status(500).send(error_error.message)
}
res.status(200);
res.json(ingredients);
})
return res;
})
/** To get one ingredient by id */
IngredientsController.get('/:id', (req, res) => {
IngredientsController.get('/:id', async (req, res) => {
const id = Number(req.params.id);
if (!Number.isInteger(id)) {
throw new Exceptions.BadRequestException('id invalid !');
}
pool.query({text:'SELECT * FROM Ingredients WHERE id =$1',
values: [id]}, (error: Error, results: QueryResult) => {
if (error) {
throw(error)
}
try {
const ingredient = await ingredient_gw.findOneById(id)
if (results.rowCount == 0) {
throw new Exceptions.NotFoundException('no ingredient with this id');
if (ingredient == null) {
res.status(404).send('not found')
}
else {
const ingredient_ingredient = ingredient as Ingredient
res.status(200)
res.json(results.rows)
})
return res;
res.status(200).json(ingredient)
}
} catch (error) {
const error_error = error as Error
res.status(500).send(error_error.message)
}
})
export { IngredientsController }

@ -1,11 +1,32 @@
import { Request, Response, NextFunction } from "express";
import { Router } from "express";
import { pool } from "../database/connection";
import { Query, QueryResult } from "pg";
import { IRecipe, Recipe } from "../types/recipes";
import { IngredientsController } from "./ingredients.controller";
import { IIngredient } from "../types/ingredients";
const RecipesController = Router()
RecipesController.get('/', (req, res) => {
res.send("Liste des recettes");
RecipesController.get('/', async (req, res) => {
let recipes:IRecipe[] = []
await pool.query('SELECT * FROM Recipe ORDER BY id', async (error: Error, result: QueryResult) => {
if (error) {
throw error;
}
for (let key in result.rows) {
const current = result.rows[key]
let recipe = new Recipe(current.id, current.name, current.description, current.time_to_cook);
const ingr_ids = current.ingredients
for (let ingr_id in ingr_ids) {
}
}
res.json(result.rows)
})
return res;
})

@ -1,4 +1,7 @@
import { Client } from "pg"
const Pool = require('pg').Pool
export const pool = new Pool({
user: 'rgregnault',
host: 'localhost',
@ -6,3 +9,26 @@ export const pool = new Pool({
password: 'motdepasse',
port: 5432,
})
export class Connection {
public client:Client
clientIsConnected:boolean = false
constructor() {
this.client = new Client({
user: 'rgregnault',
host: 'localhost',
database: 'leftovers',
password: 'motdepasse',
port: 5432,
})
}
public async connect() {
if (!this.clientIsConnected) {
await this.client.connect()
this.clientIsConnected = true
}
}
}

@ -0,0 +1,45 @@
import { QueryResult } from "pg";
import { Ingredient } from "../types/ingredients";
import { Connection } from "../database/connection"
export class IngredientsGateway {
connection:Connection
constructor() {
this.connection = new Connection()
}
async getAll() : Promise<Ingredient[]> {
this.connection.connect()
const res = await this.connection.client.query('SELECT * FROM Ingredients ORDER BY id')
let ingredients:Ingredient[] = []
for (let key in res.rows) {
let ingredient:Ingredient = new Ingredient(Number(res.rows[key].id), res.rows[key].name);
ingredients.push(ingredient);
}
return ingredients
}
async findOneById(id: number) : Promise<any> {
this.connection.connect()
const query = {
text: 'SELECT * FROM Ingredients WHERE id =$1',
values: [id],
}
const res = await this.connection.client.query(query)
if (res.rowCount != 1) {
return null
}
const ingredient = new Ingredient(Number(res.rows[0].id), String(res.rows[0].name))
return ingredient
}
}

@ -1,10 +1,36 @@
import type { IIngredient } from "./ingredients"
export interface IRecipe {
id: number,
name: string,
description: string,
time_to_cook: number,
readonly id: number,
readonly name: string,
readonly description: string,
readonly time_to_cook: number,
ingredients: IIngredient[],
steps: string[]
}
export class Recipe implements IRecipe {
id: number
name: string
description: string
time_to_cook: number
ingredients: IIngredient[]
steps: string[]
constructor(id: number, name: string, description: string, time_to_cook: number) {
this.id = id
this.name = name
this.description = description
this.time_to_cook = time_to_cook
this.ingredients = []
this.steps = []
}
addStep(newStep: string) {
this.steps.push(newStep)
}
addIngredient(newIngredient: IIngredient) {
this.ingredients.push(newIngredient)
}
}
Loading…
Cancel
Save