From ccf18367bb27c3be28b8584cbff987b0247cb011 Mon Sep 17 00:00:00 2001 From: hebertsanto Date: Fri, 11 Apr 2025 00:32:27 -0300 Subject: [PATCH 1/2] refactor: comment service improve --- .../controller/comments.controller.ts | 8 +- src/comments/services/comments.service.ts | 118 ++++++++---------- src/user/controller/user.controller.ts | 25 +--- 3 files changed, 59 insertions(+), 92 deletions(-) diff --git a/src/comments/controller/comments.controller.ts b/src/comments/controller/comments.controller.ts index 6210dfb..824b6e9 100644 --- a/src/comments/controller/comments.controller.ts +++ b/src/comments/controller/comments.controller.ts @@ -41,7 +41,7 @@ export class CommentsController { }) @Get(':id') async getCommentById(@Param('id') id: string, @Res() res: Response) { - const comment = await this.commentsServices.findCommentById(id); + const comment = await this.commentsServices.findById(id); return res.status(HttpStatus.OK).json({ message: i18n()['message.comment.get'], data: { comment }, @@ -62,7 +62,7 @@ export class CommentsController { }) @Post() async create(@Body() commentDTO: CommentDTO, @Res() res: Response) { - const comment = await this.commentsServices.createComment(commentDTO); + const comment = await this.commentsServices.save(commentDTO); return res.status(HttpStatus.CREATED).json({ message: i18n()['message.comment.created'], @@ -88,7 +88,7 @@ export class CommentsController { @Body() commentDTO: CommentDTO, @Res() res: Response, ) { - const comment = await this.commentsServices.findByIdAndUpdateComment( + const comment = await this.commentsServices.update( id, commentDTO, ); @@ -113,7 +113,7 @@ export class CommentsController { }) @Delete(':id') async deleteComment(@Param('id') id: string, @Res() res: Response) { - await this.commentsServices.findByIdAndDeleteComment(id); + await this.commentsServices.delete(id); return res.status(HttpStatus.NO_CONTENT); } diff --git a/src/comments/services/comments.service.ts b/src/comments/services/comments.service.ts index bf48320..4c66a8a 100644 --- a/src/comments/services/comments.service.ts +++ b/src/comments/services/comments.service.ts @@ -3,7 +3,6 @@ import { Comment } from 'src/comments/types/comments'; import { PrismaService } from 'src/database/prisma.service'; import { Comments } from '@prisma/client'; import { LoggerService } from 'src/logger/logger.service'; -import { i18n } from 'src/i18n'; import { ProjectsService } from 'src/projects/services/projects.service'; import { UserService } from 'src/user/services/user.service'; @@ -13,81 +12,62 @@ export class CommentsService { private prismaService: PrismaService, private userService: UserService, private projectService: ProjectsService, - private logger: LoggerService, - ) {} + private logging: LoggerService, + ) { } + async save(data: Comment): Promise { + this.logging.log(`[CommentService] Starting to create comment for user ${data.userId} on project ${data.projectId}`); + + await Promise.all([ + this.userService.checkUserExistence(data.userId), + this.projectService.checkProjectExistence(data.projectId), + ]); + + return this.prismaService.comments.create({ data }); + } - async createComment(data: Comment): Promise { - try { - await this.userService.checkUserExistence(data.userId); - await this.projectService.checkProjectExistence(data.projectId); - //add logic to verify question_id later - const createComment = await this.prismaService.comments.create({ - data: { - ...data, - }, - }); - return createComment; - } catch (error) { - this.logger.error(`some errror ocurred : ${error.message}`); - throw error; + async findById(id: string): Promise { + this.logging.log(`[CommentService] Finding comment with ID: ${id}`); + + const comment = await this.prismaService.comments.findUnique({ where: { id } }); + + if (!comment) { + this.logging.warn(`[CommentService] Comment not found with ID: ${id}`); + throw new NotFoundException(`Comment with ID ${id} not found`); } + + return comment; } + + async update( + id: string, + data: Comment, + ): Promise { + this.logging.log(`[CommentService] Starting to update comment for user ${data.userId} on project ${data.projectId}`); - async findCommentById(comment_id: string): Promise { - try { - const comment = await this.prismaService.comments.findUnique({ - where: { - id: comment_id, - }, - }); - if (!comment) { - throw new NotFoundException(); - } + await Promise.all([ + this.userService.checkUserExistence(data.userId), + this.projectService.checkProjectExistence(data.projectId), + ]) - return comment; - } catch (error) { - if (error instanceof NotFoundException) { - throw new NotFoundException(i18n()['exception.notFound'], comment_id); - } + const sanitizedData = { + comment: data.comment, + projectId: data.projectId, + questionId: data.questionId, + userId: data.userId + }; - this.logger.error(`some error ocurred : ${error.message}`); - throw error; - } + return this.prismaService.comments.update({ + where: { id }, + data: sanitizedData, + });; } - async findByIdAndUpdateComment( - comment_id: string, - data: Comment, - ): Promise { - try { - await this.userService.checkUserExistence(data.userId); - await this.projectService.checkProjectExistence(data.projectId); - const updateComment = await this.prismaService.comments.update({ - where: { - id: comment_id, - }, - data: { - ...data, - }, - }); - return updateComment; - } catch (error) { - this.logger.error(`some error ocurred : ${error.message}`); - throw error; - } + async delete(id: string): Promise { + this.logging.log(`[CommentService] Deleting comment ${id}`); + + return this.prismaService.comments.delete({ + where: { id }, + }); } - async findByIdAndDeleteComment(comment_id: string): Promise { - try { - await this.findCommentById(comment_id); - return await this.prismaService.comments.delete({ - where: { - id: comment_id, - }, - }); - } catch (error) { - this.logger.error(`some error ocurred : ${error.message}`); - throw error; - } - } -} +} \ No newline at end of file diff --git a/src/user/controller/user.controller.ts b/src/user/controller/user.controller.ts index 6f7828d..7a72628 100644 --- a/src/user/controller/user.controller.ts +++ b/src/user/controller/user.controller.ts @@ -23,7 +23,7 @@ import { UserService } from 'src/user/services/user.service'; @ApiTags('User') @Controller('/user') export class UserController { - constructor(private readonly userService: UserService) { } + constructor(private readonly userService: UserService) {} @ApiResponse({ status: HttpStatus.OK, @@ -38,10 +38,7 @@ export class UserController { description: 'Internal server error', }) @Get(':id') - async findUserById( - @Param('id') id: string, - @Res() res: Response - ) { + async findUserById(@Param('id') id: string, @Res() res: Response) { const user = await this.userService.findUserById(id); return res.status(HttpStatus.OK).json({ msg: i18n()['message.user.get'], @@ -62,10 +59,7 @@ export class UserController { description: 'Internal server error', }) @Post() - async createUser( - @Body() createUserDto: UserDto, - @Res() res: Response - ) { + async createUser(@Body() createUserDto: UserDto, @Res() res: Response) { const user = await this.userService.createUser(createUserDto); return res.status(HttpStatus.CREATED).json({ msg: i18n()['message.user.created'], @@ -88,7 +82,7 @@ export class UserController { @Delete(':id') async deleteUser(@Param('id') id: string, @Res() res: Response) { await this.userService.findUserByIdAndDelete(id); - return res.status(HttpStatus.NO_CONTENT) + return res.status(HttpStatus.NO_CONTENT); } @ApiResponse({ @@ -109,10 +103,7 @@ export class UserController { @Body() editUserDTO: UserDto, @Res() res: Response, ) { - const user = await this.userService.findUserByIdAndUpdate( - id, - editUserDTO, - ); + const user = await this.userService.findUserByIdAndUpdate(id, editUserDTO); return res.json({ msg: i18n()['message.user.update'], data: { user }, @@ -120,11 +111,7 @@ export class UserController { } @Post('/login') - async login( - @Body() email: string, - password: string, - @Res() res: Response - ) { + async login(@Body() email: string, password: string, @Res() res: Response) { const { token } = await this.userService.auth(email, password); return res.status(HttpStatus.OK).json({ msg: i18n()['message.user.login'], From 8f6500fe1313285dc6561a4ee337c9417606bda4 Mon Sep 17 00:00:00 2001 From: hebertsanto Date: Fri, 11 Apr 2025 00:33:14 -0300 Subject: [PATCH 2/2] style: Eslint style --- .../controller/comments.controller.ts | 5 +-- src/comments/services/comments.service.ts | 43 ++++++++++--------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/comments/controller/comments.controller.ts b/src/comments/controller/comments.controller.ts index 824b6e9..2a3dbf4 100644 --- a/src/comments/controller/comments.controller.ts +++ b/src/comments/controller/comments.controller.ts @@ -88,10 +88,7 @@ export class CommentsController { @Body() commentDTO: CommentDTO, @Res() res: Response, ) { - const comment = await this.commentsServices.update( - id, - commentDTO, - ); + const comment = await this.commentsServices.update(id, commentDTO); return res.status(HttpStatus.CREATED).json({ message: i18n()['message.comment.update'], diff --git a/src/comments/services/comments.service.ts b/src/comments/services/comments.service.ts index 4c66a8a..f2250bd 100644 --- a/src/comments/services/comments.service.ts +++ b/src/comments/services/comments.service.ts @@ -13,61 +13,64 @@ export class CommentsService { private userService: UserService, private projectService: ProjectsService, private logging: LoggerService, - ) { } - async save(data: Comment): Promise { - this.logging.log(`[CommentService] Starting to create comment for user ${data.userId} on project ${data.projectId}`); + ) {} + async save(data: Comment): Promise { + this.logging.log( + `[CommentService] Starting to create comment for user ${data.userId} on project ${data.projectId}`, + ); + await Promise.all([ this.userService.checkUserExistence(data.userId), this.projectService.checkProjectExistence(data.projectId), ]); - + return this.prismaService.comments.create({ data }); } async findById(id: string): Promise { this.logging.log(`[CommentService] Finding comment with ID: ${id}`); - - const comment = await this.prismaService.comments.findUnique({ where: { id } }); - + + const comment = await this.prismaService.comments.findUnique({ + where: { id }, + }); + if (!comment) { this.logging.warn(`[CommentService] Comment not found with ID: ${id}`); throw new NotFoundException(`Comment with ID ${id} not found`); } - + return comment; } - - async update( - id: string, - data: Comment, - ): Promise { - this.logging.log(`[CommentService] Starting to update comment for user ${data.userId} on project ${data.projectId}`); + + async update(id: string, data: Comment): Promise { + this.logging.log( + `[CommentService] Starting to update comment for user ${data.userId} on project ${data.projectId}`, + ); await Promise.all([ this.userService.checkUserExistence(data.userId), this.projectService.checkProjectExistence(data.projectId), - ]) + ]); const sanitizedData = { comment: data.comment, projectId: data.projectId, questionId: data.questionId, - userId: data.userId + userId: data.userId, }; return this.prismaService.comments.update({ where: { id }, data: sanitizedData, - });; + }); } async delete(id: string): Promise { this.logging.log(`[CommentService] Deleting comment ${id}`); - + return this.prismaService.comments.delete({ where: { id }, }); } - -} \ No newline at end of file +}