feat : add exceptions, classes for manipulated object, routes for all ingredients and all recipes, route for one ingredient

pull/1/head
Rémi REGNAULT 1 year ago
parent 5cbf3b03b5
commit 0597c20504

File diff suppressed because it is too large Load Diff

@ -5,7 +5,7 @@
"main": "src/server.ts",
"scripts": {
"start": "node build/server.js",
"build": "tsup src/index.ts --format cjs --clean",
"build": "tsup src/server.ts --format cjs --clean",
"dev": "nodemon --watch src -e js,ts,json --exec \"ts-node src/server.ts\""
},
"author": "",
@ -17,6 +17,7 @@
"morgan": "^1.10.0",
"nodemon": "^3.0.1",
"ts-node": "^10.9.1",
"tsup": "^7.2.0",
"typescript": "^5.2.2"
}
}

@ -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 }

@ -5,7 +5,8 @@ import { Router } from "express";
const RecipesController = Router()
RecipesController.get('/', (req, res) => {
res.send("Liste des recettes");
return res;
})
export { RecipesController }

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

@ -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…
Cancel
Save