From 7bde31d7200d340fc4e15ffa7d0265aae4412305 Mon Sep 17 00:00:00 2001 From: SofianeLasri <alasri250@gmail.com> Date: Tue, 31 Oct 2023 09:48:47 +0100 Subject: [PATCH] Ajout de routes --- package-lock.json | 75 +++++++++++++++-------- package.json | 2 + src/app.ts | 7 ++- src/infrastructure/express-application.ts | 37 +++++++++-- src/infrastructure/express-router.ts | 29 +++++++++ src/infrastructure/express-server.ts | 18 +++++- src/user.controller.ts | 19 ++++++ src/user.json.service.ts | 44 +++++++++++++ src/user.router.ts | 39 ++++++++++++ src/user.service.ts | 7 +++ src/user/User.ts | 17 +++++ 11 files changed, 261 insertions(+), 33 deletions(-) create mode 100644 src/infrastructure/express-router.ts create mode 100644 src/user.controller.ts create mode 100644 src/user.json.service.ts create mode 100644 src/user.router.ts create mode 100644 src/user.service.ts create mode 100644 src/user/User.ts diff --git a/package-lock.json b/package-lock.json index 9215569..5954e3f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,10 +9,12 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "body-parser": "^1.20.2", "dotenv": "^16.3.1", "express": "^4.18.2" }, "devDependencies": { + "@types/body-parser": "^1.19.3", "@types/express": "^4.17.17", "@types/jest": "^29.5.3", "@types/node": "^20.4.4", @@ -1109,9 +1111,9 @@ } }, "node_modules/@types/body-parser": { - "version": "1.19.2", - "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.2.tgz", - "integrity": "sha512-ALYone6pm6QmwZoAgeyNksccT9Q4AWZQ6PvfwR37GT6r6FWUPguq6sUmNGSMV2Wr761oQoBxwGGa6DR5o1DC9g==", + "version": "1.19.4", + "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.4.tgz", + "integrity": "sha512-N7UDG0/xiPQa2D/XrVJXjkWbpqHCd2sBaB32ggRF2l83RhPfamgKGF8gwwqyksS95qUS5ZYF9aF+lLPRlwI2UA==", "dev": true, "dependencies": { "@types/connect": "*", @@ -1496,12 +1498,12 @@ } }, "node_modules/body-parser": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", - "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "version": "1.20.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", + "integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==", "dependencies": { "bytes": "3.1.2", - "content-type": "~1.0.4", + "content-type": "~1.0.5", "debug": "2.6.9", "depd": "2.0.0", "destroy": "1.2.0", @@ -1509,7 +1511,7 @@ "iconv-lite": "0.4.24", "on-finished": "2.4.1", "qs": "6.11.0", - "raw-body": "2.5.1", + "raw-body": "2.5.2", "type-is": "~1.6.18", "unpipe": "1.0.0" }, @@ -2109,6 +2111,43 @@ "node": ">= 0.10.0" } }, + "node_modules/express/node_modules/body-parser": { + "version": "1.20.1", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", + "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "dependencies": { + "bytes": "3.1.2", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "1.2.0", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "on-finished": "2.4.1", + "qs": "6.11.0", + "raw-body": "2.5.1", + "type-is": "~1.6.18", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8", + "npm": "1.2.8000 || >= 1.4.16" + } + }, + "node_modules/express/node_modules/raw-body": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", + "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "dependencies": { + "bytes": "3.1.2", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/fast-json-stable-stringify": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", @@ -2188,20 +2227,6 @@ "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==", "dev": true }, - "node_modules/fsevents": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", - "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", - "dev": true, - "hasInstallScript": true, - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": "^8.16.0 || ^10.6.0 || >=11.0.0" - } - }, "node_modules/function-bind": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", @@ -3844,9 +3869,9 @@ } }, "node_modules/raw-body": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", - "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz", + "integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==", "dependencies": { "bytes": "3.1.2", "http-errors": "2.0.0", diff --git a/package.json b/package.json index 0b793c1..c389720 100644 --- a/package.json +++ b/package.json @@ -12,10 +12,12 @@ "author": "", "license": "ISC", "dependencies": { + "body-parser": "^1.20.2", "dotenv": "^16.3.1", "express": "^4.18.2" }, "devDependencies": { + "@types/body-parser": "^1.19.3", "@types/express": "^4.17.17", "@types/jest": "^29.5.3", "@types/node": "^20.4.4", diff --git a/src/app.ts b/src/app.ts index 4e386dc..cf9afd3 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,3 +1,6 @@ -import { ExpressApplication } from './infrastructure/express-application'; +import express from 'express'; -new ExpressApplication().bootstrap(); +const app = express(); +const port = 3000; + +app.listen(port, () => console.log(`> Listening on port ${port}`)); \ No newline at end of file diff --git a/src/infrastructure/express-application.ts b/src/infrastructure/express-application.ts index 625bd68..3eb294d 100644 --- a/src/infrastructure/express-application.ts +++ b/src/infrastructure/express-application.ts @@ -1,26 +1,53 @@ import { ExpressServer } from './express-server'; import * as dotenv from 'dotenv'; +import {ExpressRouter} from "./express-router"; +import {UserService} from "../user.service"; +import {UserJsonService} from "../user.json.service"; export class ExpressApplication { - private server: ExpressServer; + private expressRouter!: ExpressRouter; + private port!: string; + private server!: ExpressServer; + private userService!: UserService; constructor() { - this.configureEnvironment(); - - const port = this.getPort(); - this.server = new ExpressServer(port); + this.configureApplication(); } bootstrap(): void { this.server.bootstrap(); } + private configureApplication(): void { + this.configureEnvironment(); + this.configureServerPort(); + this.configureServices(); + this.configureExpressRouter(); + this.configureServer(); + } + private configureEnvironment(): void { dotenv.config({ path: '.env', }); } + private configureServerPort(): void { + this.port = this.getPort(); + } + + private configureServices(): void { + this.userService = new UserJsonService(); + } + + private configureExpressRouter(): void { + this.expressRouter = new ExpressRouter(this.userService); + } + + private configureServer(): void { + this.server = new ExpressServer(this.expressRouter, this.port); + } + private getPort(): string { const port = process.env.PORT; if (!port) { diff --git a/src/infrastructure/express-router.ts b/src/infrastructure/express-router.ts new file mode 100644 index 0000000..0ed4650 --- /dev/null +++ b/src/infrastructure/express-router.ts @@ -0,0 +1,29 @@ +import { Router } from 'express'; +import {UserController} from "../user.controller"; +import {UserRouter} from "../user.router"; +import {UserService} from "../user.service"; + +export class ExpressRouter { + router = Router(); + + private userController!: UserController; + private userRouter!: UserRouter; + + constructor(private userService: UserService) { + this.configureControllers(); + this.configureRouters(); + this.configureRoutes(); + } + + private configureControllers(): void { + this.userController = new UserController(this.userService); + } + + private configureRouters(): void { + this.userRouter = new UserRouter(this.userController); + } + + private configureRoutes(): void { + this.router.use('/user', this.userRouter.router); + } +} \ No newline at end of file diff --git a/src/infrastructure/express-server.ts b/src/infrastructure/express-server.ts index 87561a1..8c5e072 100644 --- a/src/infrastructure/express-server.ts +++ b/src/infrastructure/express-server.ts @@ -1,13 +1,29 @@ import express from 'express'; +import {ExpressRouter} from "./express-router"; +import bodyParser from "body-parser"; export class ExpressServer { private express = express(); - constructor(private port: string) {} + constructor( + private expressRouter: ExpressRouter, + private port: string, + ) { + this.configureBodyParser(); + this.configureRoutes(); + } bootstrap(): void { this.express.listen(this.port, () => { console.log(`> Listening on port ${this.port}`); }); } + + private configureBodyParser(): void { + this.express.use(bodyParser.json()); + } + + private configureRoutes(): void { + this.express.use('/api', this.expressRouter.router); + } } diff --git a/src/user.controller.ts b/src/user.controller.ts new file mode 100644 index 0000000..49d3f51 --- /dev/null +++ b/src/user.controller.ts @@ -0,0 +1,19 @@ +import {User} from './user/User'; +import {UserService} from './user.service'; + +export class UserController { + constructor(private userService: UserService) { + } + + add(name: string, email: string, password: string): User { + return this.userService.add(name, email, password); + } + + getById(id: number): User | null { + return this.userService.getById(id); + } + + getAll(): User[] { + return this.userService.getAll(); + } +} \ No newline at end of file diff --git a/src/user.json.service.ts b/src/user.json.service.ts new file mode 100644 index 0000000..430158e --- /dev/null +++ b/src/user.json.service.ts @@ -0,0 +1,44 @@ +import { User } from "./user/User"; +import { UserService } from "./user.service"; +import fs from 'fs'; + +export class UserJsonService implements UserService { + private users: User[] = []; + private dataFilePath: string = 'users.json'; + + constructor() { + this.loadUsers(); + } + + private saveUsers() { + fs.writeFileSync(this.dataFilePath, JSON.stringify(this.users, null, 2)); + } + + private loadUsers() { + try { + const data = fs.readFileSync(this.dataFilePath, 'utf-8'); + this.users = JSON.parse(data); + } catch (err) { + this.users = []; + } + } + + add(name: string, email: string, password: string): User { + const id = this.users.length + 1; + const user = new User(id, name, email, password); + + this.users.push(user); + this.saveUsers(); + + return user; + } + + getById(id: number): User | null { + const user = this.users.find((u) => u.getId() === id); + return user || null; + } + + getAll(): User[] { + return this.users; + } +} diff --git a/src/user.router.ts b/src/user.router.ts new file mode 100644 index 0000000..c465813 --- /dev/null +++ b/src/user.router.ts @@ -0,0 +1,39 @@ +import {Router} from 'express'; +import {UserController} from './user.controller'; + +export class UserRouter { + router = Router(); + + constructor(private userController: UserController) { + this.configureRoutes(); + } + + private configureRoutes(): void { + this.router.get('/:id', (req, res, next) => { + try { + const result = this.userController.getById(parseInt(req.params.id)); + res.status(200).json(result); + } catch (error: unknown) { + next(error); + } + }); + + this.router.post('/add', (req, res, next) => { + try { + const result = this.userController.add(req.body.name, req.body.email, req.body.password); + res.status(200).json(result); + } catch (error: unknown) { + next(error); + } + }); + + this.router.get('/getAll', (req, res, next) => { + try { + const result = this.userController.getAll(); + res.status(200).json(result); + } catch (error: unknown) { + next(error); + } + }); + } +} \ No newline at end of file diff --git a/src/user.service.ts b/src/user.service.ts new file mode 100644 index 0000000..8bcf6c9 --- /dev/null +++ b/src/user.service.ts @@ -0,0 +1,7 @@ +import { User } from './user/User'; + +export interface UserService { + add(name: string, email: string, password: string): User; + getById(id: number): User | null; + getAll(): User[]; +} \ No newline at end of file diff --git a/src/user/User.ts b/src/user/User.ts new file mode 100644 index 0000000..8636ad3 --- /dev/null +++ b/src/user/User.ts @@ -0,0 +1,17 @@ +export class User { + private id: number; + private name: string; + private email: string; + private password: string; + + constructor(id: number, name: string, email: string, password: string) { + this.id = id; + this.name = name; + this.email = email; + this.password = password; + } + + public getId(): number { + return this.id; + } +} \ No newline at end of file -- GitLab