@ -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 }
|
@ -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[];
|
||||
}
|
||||
}
|
Loading…
Reference in new issue