pull/2/head
parent
df10669f3e
commit
acff49b692
@ -1,11 +1,32 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, NextFunction } from "express";
|
||||||
import { Router } 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()
|
const RecipesController = Router()
|
||||||
|
|
||||||
RecipesController.get('/', (req, res) => {
|
RecipesController.get('/', async (req, res) => {
|
||||||
res.send("Liste des recettes");
|
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;
|
return res;
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -1,8 +1,34 @@
|
|||||||
|
import { Client } from "pg"
|
||||||
|
|
||||||
const Pool = require('pg').Pool
|
const Pool = require('pg').Pool
|
||||||
|
|
||||||
export const pool = new Pool({
|
export const pool = new Pool({
|
||||||
user: 'rgregnault',
|
user: 'rgregnault',
|
||||||
host: 'localhost',
|
host: 'localhost',
|
||||||
database: 'leftovers',
|
database: 'leftovers',
|
||||||
password: 'motdepasse',
|
password: 'motdepasse',
|
||||||
port: 5432,
|
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"
|
import type { IIngredient } from "./ingredients"
|
||||||
|
|
||||||
export interface IRecipe {
|
export interface IRecipe {
|
||||||
id: number,
|
readonly id: number,
|
||||||
name: string,
|
readonly name: string,
|
||||||
description: string,
|
readonly description: string,
|
||||||
time_to_cook: number,
|
readonly time_to_cook: number,
|
||||||
ingredients: IIngredient[],
|
ingredients: IIngredient[],
|
||||||
steps: string[]
|
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…
Reference in new issue