From 961a8b991d995ef1b51bf2aa7e0d27fff82aaa0c Mon Sep 17 00:00:00 2001 From: Hugo Lextrait Date: Fri, 17 Mar 2023 17:30:36 +0100 Subject: [PATCH] :sparkles: created main services and controller for the IUser --- package.json | 4 +- src/app/api/UserController.ts | 60 ++++++++++++++++++++--- src/services/UsersService/UsersService.ts | 34 +++++++++++-- 3 files changed, 84 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 15177a0a..08b167c2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "tezoslink", + "name": "lecoffre", "version": "1.0.0", - "description": "tezosLink project", + "description": "lecoffre project", "_moduleAliases": { "@App": "./dist/app", "@Api": "./dist/app/api", diff --git a/src/app/api/UserController.ts b/src/app/api/UserController.ts index 966aebdc..982eecab 100644 --- a/src/app/api/UserController.ts +++ b/src/app/api/UserController.ts @@ -1,9 +1,9 @@ import { type Response, type Request } from "express"; -import { Controller, Get } from "@ControllerPattern/index"; -// import { IsNotEmpty, IsString, IsUUID } from "class-validator"; +import { Controller, Get, Post, Put } from "@ControllerPattern/index"; import { Service } from "typedi"; import ApiController from "@Common/system/controller-pattern/ApiController"; import UsersService from "@Services/UsersService/UsersService"; +// import { IsNotEmpty, IsString, IsUUID } from "class-validator"; // class Params { // @IsString() @@ -20,15 +20,59 @@ export default class UserController extends ApiController { super(); } + /** + * @description Get all users + * @returns IUser[] list of users + */ @Get("/api/users") protected async get(req: Request, response: Response) { - console.log("UserController > get > response"); - // request: Request - // const query = this.buildQueryAsObject>(request); try { - // await validateOrReject(QueryService.createFrom>(query), { - // forbidUnknownValues: true, - // }); + // TODO + } catch (error) { + this.httpBadRequest(response, error); + return; + } + this.httpSuccess(response, await this.userService.get()); + } + + /** + * @description Create a new user + * @returns IUser created + */ + @Post("/api/users") + protected async post(req: Request, response: Response) { + try { + // TODO + } catch (error) { + this.httpBadRequest(response, error); + return; + } + this.httpSuccess(response, await this.userService.create()); + } + + /** + * @description Modify a specific user by uid + * @returns IUser modified + */ + @Put("/api/users/:uid") + protected async put(req: Request, response: Response) { + try { + // TODO + } catch (error) { + this.httpBadRequest(response, error); + return; + } + this.httpSuccess(response, await this.userService.create()); + } + + /** + * @description Get a specific user by uid + * @returns IUser + */ + @Get("/api/users/:uid") + protected async getOneByUid(req: Request, response: Response) { + try { + // TODO } catch (error) { this.httpBadRequest(response, error); return; diff --git a/src/services/UsersService/UsersService.ts b/src/services/UsersService/UsersService.ts index 7261b9b6..49d7c24b 100644 --- a/src/services/UsersService/UsersService.ts +++ b/src/services/UsersService/UsersService.ts @@ -9,17 +9,19 @@ export default class UsersService extends BaseService { } /** + * @description : Get all users * @returns : T * @throws {Error} If project cannot be created * @param : projectEntity: Partial */ - public async getByUid(uuid: string) { + public async get() { // const user = await this.usersRepository.findOne(uuid); // if (!user) Promise.reject(new Error("Cannot get user by uuid")); - return "User/getByUid > Not implemented yet"; + return { response: "/api/users > GET : All users > Not implemented yet" }; } - + /** + * @description : Create a new user * @returns : T * @throws {Error} If project cannot be created * @param : projectEntity: Partial @@ -27,6 +29,30 @@ export default class UsersService extends BaseService { public async create() { // const project = await this.projectRepository.create(projectEntity); // if (!project) Promise.reject(new Error("Cannot create project")); - // return project; + return { response: "/api/users > POST : Create User > Not implemented yet" }; + } + + /** + * @description : Create a new user + * @returns : T + * @throws {Error} If project cannot be created + * @param : projectEntity: Partial + */ + public async put() { + // const project = await this.projectRepository.create(projectEntity); + // if (!project) Promise.reject(new Error("Cannot create project")); + return { response: "/api/users > POST : Create User > Not implemented yet" }; + } + + /** + * @description : Get a user by uid + * @returns : T + * @throws {Error} If project cannot be created + * @param : projectEntity: Partial + */ + public async getByUid(uuid: string) { + // const user = await this.usersRepository.findOne(uuid); + // if (!user) Promise.reject(new Error("Cannot get user by uuid")); + return { response: "/api/users/:uuid > GET : User by uid > Not implemented yet" }; } }