add gateway recipes

pull/5/head
Rayhân HASSOU 1 year ago
parent 0ceb125acf
commit f294250be6

@ -3,31 +3,49 @@ import { Router } from "express";
import { pool } from "../database/connection";
import { Query, QueryResult } from "pg";
import { IRecipe, Recipe } from "../types/recipes";
import { Exceptions } from "../utils/exception";
import { IngredientsController } from "./ingredients.controller";
import { IIngredient } from "../types/ingredients";
import { RecipeGateway } from "../gateways/recipe.gateway";
const RecipesController = Router()
const recipes_gw = new RecipeGateway()
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;
try {
const recipes = await recipes_gw.getAll()
res.status(200).json(recipes)
} catch (error) {
const error_error = error as Error
res.status(500).send(error_error.message)
}
})
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) {
RecipesController.get('/:id', async (req, res) => {
const id = Number(req.params.id);
if (!Number.isInteger(id)) {
throw new Exceptions.BadRequestException('id invalid !');
}
try {
const recipe = await recipes_gw.getById(id)
if (recipe == null) {
res.status(404).send('not found')
}
else {
const ingredient_ingredient = recipe as Recipe
res.json(result.rows)
})
return res;
res.status(200).json(recipe)
}
} catch (error) {
const error_error = error as Error
res.status(500).send(error_error.message)
}
})
export { RecipesController }

@ -17,10 +17,10 @@ export class Connection {
constructor() {
this.client = new Client({
user: 'rgregnault',
host: 'localhost',
database: 'leftovers',
password: 'motdepasse',
user: 'leftovers_admin',
host: 'postgresql-leftovers.alwaysdata.net',
database: 'leftovers_recipedb',
password: 'AdmPsswd',
port: 5432,
})
}

@ -1,4 +1,3 @@
import { QueryResult } from "pg";
import { Ingredient } from "../types/ingredients";
import { Connection } from "../database/connection"

@ -0,0 +1,46 @@
import { Ingredient } from "../types/ingredients";
import { Recipe } from "../types/recipes"
import { Connection } from "../database/connection"
export class RecipeGateway {
connection:Connection
constructor() {
this.connection = new Connection()
}
async getAll() : Promise<Recipe[]> {
this.connection.connect()
const res = await this.connection.client.query('SELECT * FROM Recipes ORDER BY id')
let recipes:Recipe[] = []
console.log(res.rows);
for (let key in res.rows) {
let recipe:Recipe = new Recipe(Number(res.rows[key].id), res.rows[key].name, res.rows[key].description, res.rows[key].time);
recipes.push(recipe);
}
return recipes
}
async getById(id: number) : Promise<any>{
this.connection.connect()
const query = {
text: 'SELECT * FROM Recipes WHERE id =$1',
values: [id],
}
const res = await this.connection.client.query(query)
if (res.rowCount != 1) {
return null
}
const recipe = new Recipe(Number(res.rows[0].id), res.rows[0].name, res.rows[0].description, res.rows[0].time);
return recipe;
}
}
Loading…
Cancel
Save