now the routes for recipes also returns the steps

pull/6/head
Rayhân HASSOU 1 year ago
parent 1203db6a96
commit 7d042d5a17

@ -1,11 +1,6 @@
import { Request, Response, NextFunction } from "express";
import { Router } from "express"; import { Router } from "express";
import { pool } from "../database/connection"; import { Recipe } from "../types/recipes";
import { Query, QueryResult } from "pg";
import { IRecipe, Recipe } from "../types/recipes";
import { Exceptions } from "../utils/exception"; import { Exceptions } from "../utils/exception";
import { IngredientsController } from "./ingredients.controller";
import { IIngredient } from "../types/ingredients";
import { RecipeGateway } from "../gateways/recipe.gateway"; import { RecipeGateway } from "../gateways/recipe.gateway";

@ -0,0 +1,35 @@
import { Router } from "express";
import { Recipe } from "../types/recipes";
import { Exceptions } from "../utils/exception";
import { StepsGateway } from "../gateways/steps.gateway";
const StepsController = Router()
const steps_gw = new StepsGateway()
StepsController.get('/:id', async (req, res) => {
const id = String(req.params.id);
if (!Number.isInteger(id)) {
throw new Exceptions.BadRequestException('id invalid !');
}
try {
const steps = await steps_gw.getForRecipes(Number(id))
if (steps == null) {
res.status(404).send('not found')
}
else {
const steps_steps = steps as string[]
res.status(200).json(steps)
}
} catch (error) {
const error_error = error as Error
res.status(500).send(error_error.message)
}
})
export { StepsController }

@ -23,7 +23,7 @@ export class IngredientsGateway {
return ingredients return ingredients
} }
async findOneById(id: number) : Promise<any> { async findOneById(id: number) : Promise<Ingredient | null> {
this.connection.connect() this.connection.connect()
const query = { const query = {

@ -1,6 +1,8 @@
import { Ingredient } from "../types/ingredients"; import { Ingredient } from "../types/ingredients";
import { Recipe } from "../types/recipes" import { Recipe } from "../types/recipes"
import { Connection } from "../database/connection" import { Connection } from "../database/connection"
import { Router } from "express";
import { StepsGateway } from "./steps.gateway";
export class RecipeGateway { export class RecipeGateway {
connection:Connection connection:Connection
@ -11,22 +13,27 @@ export class RecipeGateway {
async getAll() : Promise<Recipe[]> { async getAll() : Promise<Recipe[]> {
this.connection.connect() this.connection.connect()
const steps_gw = new StepsGateway()
const res = await this.connection.client.query('SELECT * FROM Recipes ORDER BY id') const res = await this.connection.client.query('SELECT * FROM Recipes ORDER BY id');
const steps: string[] = [];
let recipes:Recipe[] = [] let recipes:Recipe[] = []
console.log(res.rows);
for (let key in 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); const steps = await steps_gw.getForRecipes(Number(key));
let recipe:Recipe = new Recipe(Number(res.rows[key].id), res.rows[key].name, res.rows[key].description, res.rows[key].time, steps);
recipes.push(recipe); recipes.push(recipe);
} }
console.log(recipes);
return recipes return recipes
} }
async getById(id: number) : Promise<any>{ async getById(id: Number) : Promise<Recipe | null>{
this.connection.connect() this.connection.connect()
const steps_gw = new StepsGateway()
const query = { const query = {
text: 'SELECT * FROM Recipes WHERE id =$1', text: 'SELECT * FROM Recipes WHERE id =$1',
@ -39,8 +46,11 @@ export class RecipeGateway {
return null return null
} }
const recipe = new Recipe(Number(res.rows[0].id), res.rows[0].name, res.rows[0].description, res.rows[0].time); const steps = await steps_gw.getForRecipes(id)
console.log(steps);
const recipe = new Recipe(Number(res.rows[0].id), res.rows[0].name, res.rows[0].description, res.rows[0].time, steps);
console.log(recipe);
return recipe; return recipe;
} }
} }

@ -0,0 +1,26 @@
import { Recipe } from "../types/recipes"
import { Connection } from "../database/connection"
export class StepsGateway {
connection:Connection
constructor() {
this.connection = new Connection()
}
async getForRecipes(id: Number): Promise<string[]> {
this.connection.connect();
const query = {
text: 'SELECT action FROM Steps WHERE idRecipe = $1 ORDER BY numstep',
values: [id],
};
const res = await this.connection.client.query(query);
const steps = res.rows.map(row => row.action);
return steps as string[];
}
}

@ -1,6 +1,7 @@
import express from "express"; import express from "express";
import { IngredientsController } from "./controllers/ingredients.controller"; import { IngredientsController } from "./controllers/ingredients.controller";
import { RecipesController } from "./controllers/recipes.controller"; import { RecipesController } from "./controllers/recipes.controller";
import { StepsController } from "./controllers/steps.controller";
const app = express(); const app = express();
@ -10,6 +11,7 @@ app.get('/', (req, res) => {
app.use('/ingredients', IngredientsController); app.use('/ingredients', IngredientsController);
app.use('/recipes', RecipesController); app.use('/recipes', RecipesController);
app.use('/steps', StepsController)
const port = process.env.PORT || 3000; const port = process.env.PORT || 3000;

@ -17,13 +17,13 @@ export class Recipe implements IRecipe {
ingredients: IIngredient[] ingredients: IIngredient[]
steps: string[] steps: string[]
constructor(id: number, name: string, description: string, time_to_cook: number) { constructor(id: number, name: string, description: string, time_to_cook: number, steps: string[]) {
this.id = id this.id = id
this.name = name this.name = name
this.description = description this.description = description
this.time_to_cook = time_to_cook this.time_to_cook = time_to_cook
this.ingredients = [] this.ingredients = []
this.steps = [] this.steps = steps;
} }
addStep(newStep: string) { addStep(newStep: string) {

Loading…
Cancel
Save