feat : add exceptions, classes for manipulated object, routes for all ingredients and all recipes, route for one ingredient
parent
5cbf3b03b5
commit
0597c20504
File diff suppressed because it is too large
Load Diff
@ -1,11 +1,29 @@
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import { Router } from "express";
|
||||
import { Exceptions } from "../utils/exception";
|
||||
|
||||
|
||||
const IngredientsController = Router()
|
||||
|
||||
/** To get all ingredients */
|
||||
IngredientsController.get('/', (req, res) => {
|
||||
|
||||
res.status(200)
|
||||
res.send("Liste des ingredients");
|
||||
return res;
|
||||
})
|
||||
|
||||
/** To get one ingredient by id */
|
||||
IngredientsController.get('/:id', (req, res) => {
|
||||
const id = Number(req.params.id);
|
||||
|
||||
if (!Number.isInteger(id)) {
|
||||
throw new Exceptions.BadRequestException('id invalid !')
|
||||
}
|
||||
|
||||
res.send(`Searching for ingredient with id = ${id}...`);
|
||||
res.status(200);
|
||||
|
||||
return res;
|
||||
})
|
||||
|
||||
export { IngredientsController }
|
@ -0,0 +1,4 @@
|
||||
export interface ApiException {
|
||||
error: any
|
||||
status: number
|
||||
}
|
@ -1,4 +1,9 @@
|
||||
export interface Ingredient {
|
||||
id: number,
|
||||
name: string
|
||||
export class Ingredient {
|
||||
readonly id: number;
|
||||
readonly name: string;
|
||||
|
||||
constructor(init_id: number, init_name: string) {
|
||||
this.id = init_id;
|
||||
this.name = init_name;
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
import { ApiException } from '../types/exceptions'
|
||||
|
||||
/**
|
||||
* Classe générique qui sert à créer des erreurs HTTP (ici 400 et 404)
|
||||
*
|
||||
* On précise que notre classe doit correspondre à l'interface `ApiException`
|
||||
*
|
||||
* Les mots clés `readonly` servent de raccourci pour `this.propriété = valeur`,
|
||||
* ils nous empêchent également de mofifier ces valeurs par la suite.
|
||||
*
|
||||
* Ici `this.error = error` et `this.status = status`
|
||||
*/
|
||||
class Exception implements ApiException {
|
||||
constructor(readonly error: any, readonly status: number) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Création d'une 404
|
||||
*/
|
||||
|
||||
export module Exceptions {
|
||||
export class NotFoundException extends Exception {
|
||||
constructor(error: any) {
|
||||
super(error, 404)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Création d'une 400
|
||||
*/
|
||||
export class BadRequestException extends Exception {
|
||||
constructor(error: any) {
|
||||
super(error, 400)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue