You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
LeftOvers_Api/API-Project/src/gateways/recipe.gateway.ts

63 lines
2.1 KiB

import { Ingredient } from "../types/ingredients";
import { Recipe } from "../types/recipes"
import { Connection } from "../database/connection"
import { Router } from "express";
import { StepsGateway } from "./steps.gateway";
import { IngredientsGateway } from "./ingredients.gateway";
export class RecipeGateway {
connection:Connection
steps_gw:StepsGateway
ingredient_gw:IngredientsGateway
constructor() {
this.connection = new Connection()
this.steps_gw = new StepsGateway()
this.ingredient_gw = new IngredientsGateway()
}
async getAll() : Promise<Recipe[]> {
this.connection.connect()
const res = await this.connection.client.query('SELECT * FROM Recipes ORDER BY id');
const steps: string[] = [];
let recipes:Recipe[] = []
for (let key in res.rows) {
const steps = await this.steps_gw.getForRecipes(Number(key));
const ingredients = await this.ingredient_gw.findIngredientsForRecipe(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, ingredients);
recipes.push(recipe);
}
console.log(recipes);
return recipes
}
async getById(id: Number) : Promise<Recipe | null>{
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 steps = await this.steps_gw.getForRecipes(id)
const ingredients = await this.ingredient_gw.findIngredientsForRecipe(id)
const recipe = new Recipe(Number(res.rows[0].id),
res.rows[0].name,
res.rows[0].description,
Number(res.rows[0].time),
steps, ingredients);
console.log(ingredients);
return recipe;
}
}