Skip to content

Commit f8cd834

Browse files
authored
Merge pull request #21 from hebertzin/refactor/services
refactor/services
2 parents dab6b8b + 8f6500f commit f8cd834

3 files changed

Lines changed: 59 additions & 92 deletions

File tree

src/comments/controller/comments.controller.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class CommentsController {
4141
})
4242
@Get(':id')
4343
async getCommentById(@Param('id') id: string, @Res() res: Response) {
44-
const comment = await this.commentsServices.findCommentById(id);
44+
const comment = await this.commentsServices.findById(id);
4545
return res.status(HttpStatus.OK).json({
4646
message: i18n()['message.comment.get'],
4747
data: { comment },
@@ -62,7 +62,7 @@ export class CommentsController {
6262
})
6363
@Post()
6464
async create(@Body() commentDTO: CommentDTO, @Res() res: Response) {
65-
const comment = await this.commentsServices.createComment(commentDTO);
65+
const comment = await this.commentsServices.save(commentDTO);
6666

6767
return res.status(HttpStatus.CREATED).json({
6868
message: i18n()['message.comment.created'],
@@ -88,10 +88,7 @@ export class CommentsController {
8888
@Body() commentDTO: CommentDTO,
8989
@Res() res: Response,
9090
) {
91-
const comment = await this.commentsServices.findByIdAndUpdateComment(
92-
id,
93-
commentDTO,
94-
);
91+
const comment = await this.commentsServices.update(id, commentDTO);
9592

9693
return res.status(HttpStatus.CREATED).json({
9794
message: i18n()['message.comment.update'],
@@ -113,7 +110,7 @@ export class CommentsController {
113110
})
114111
@Delete(':id')
115112
async deleteComment(@Param('id') id: string, @Res() res: Response) {
116-
await this.commentsServices.findByIdAndDeleteComment(id);
113+
await this.commentsServices.delete(id);
117114

118115
return res.status(HttpStatus.NO_CONTENT);
119116
}

src/comments/services/comments.service.ts

Lines changed: 49 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Comment } from 'src/comments/types/comments';
33
import { PrismaService } from 'src/database/prisma.service';
44
import { Comments } from '@prisma/client';
55
import { LoggerService } from 'src/logger/logger.service';
6-
import { i18n } from 'src/i18n';
76
import { ProjectsService } from 'src/projects/services/projects.service';
87
import { UserService } from 'src/user/services/user.service';
98

@@ -13,81 +12,65 @@ export class CommentsService {
1312
private prismaService: PrismaService,
1413
private userService: UserService,
1514
private projectService: ProjectsService,
16-
private logger: LoggerService,
15+
private logging: LoggerService,
1716
) {}
17+
18+
async save(data: Comment): Promise<Comments> {
19+
this.logging.log(
20+
`[CommentService] Starting to create comment for user ${data.userId} on project ${data.projectId}`,
21+
);
1822

19-
async createComment(data: Comment): Promise<Comments> {
20-
try {
21-
await this.userService.checkUserExistence(data.userId);
22-
await this.projectService.checkProjectExistence(data.projectId);
23-
//add logic to verify question_id later
24-
const createComment = await this.prismaService.comments.create({
25-
data: {
26-
...data,
27-
},
28-
});
29-
return createComment;
30-
} catch (error) {
31-
this.logger.error(`some errror ocurred : ${error.message}`);
32-
throw error;
33-
}
23+
await Promise.all([
24+
this.userService.checkUserExistence(data.userId),
25+
this.projectService.checkProjectExistence(data.projectId),
26+
]);
27+
28+
return this.prismaService.comments.create({ data });
3429
}
3530

36-
async findCommentById(comment_id: string): Promise<Comments | null> {
37-
try {
38-
const comment = await this.prismaService.comments.findUnique({
39-
where: {
40-
id: comment_id,
41-
},
42-
});
43-
if (!comment) {
44-
throw new NotFoundException();
45-
}
31+
async findById(id: string): Promise<Comments> {
32+
this.logging.log(`[CommentService] Finding comment with ID: ${id}`);
4633

47-
return comment;
48-
} catch (error) {
49-
if (error instanceof NotFoundException) {
50-
throw new NotFoundException(i18n()['exception.notFound'], comment_id);
51-
}
34+
const comment = await this.prismaService.comments.findUnique({
35+
where: { id },
36+
});
5237

53-
this.logger.error(`some error ocurred : ${error.message}`);
54-
throw error;
38+
if (!comment) {
39+
this.logging.warn(`[CommentService] Comment not found with ID: ${id}`);
40+
throw new NotFoundException(`Comment with ID ${id} not found`);
5541
}
42+
43+
return comment;
5644
}
5745

58-
async findByIdAndUpdateComment(
59-
comment_id: string,
60-
data: Comment,
61-
): Promise<Comments> {
62-
try {
63-
await this.userService.checkUserExistence(data.userId);
64-
await this.projectService.checkProjectExistence(data.projectId);
65-
const updateComment = await this.prismaService.comments.update({
66-
where: {
67-
id: comment_id,
68-
},
69-
data: {
70-
...data,
71-
},
72-
});
73-
return updateComment;
74-
} catch (error) {
75-
this.logger.error(`some error ocurred : ${error.message}`);
76-
throw error;
77-
}
46+
async update(id: string, data: Comment): Promise<Comments> {
47+
this.logging.log(
48+
`[CommentService] Starting to update comment for user ${data.userId} on project ${data.projectId}`,
49+
);
50+
51+
await Promise.all([
52+
this.userService.checkUserExistence(data.userId),
53+
this.projectService.checkProjectExistence(data.projectId),
54+
]);
55+
56+
const sanitizedData = {
57+
comment: data.comment,
58+
projectId: data.projectId,
59+
questionId: data.questionId,
60+
userId: data.userId,
61+
};
62+
63+
return this.prismaService.comments.update({
64+
where: { id },
65+
data: sanitizedData,
66+
});
7867
}
7968

80-
async findByIdAndDeleteComment(comment_id: string): Promise<Comments | null> {
81-
try {
82-
await this.findCommentById(comment_id);
83-
return await this.prismaService.comments.delete({
84-
where: {
85-
id: comment_id,
86-
},
87-
});
88-
} catch (error) {
89-
this.logger.error(`some error ocurred : ${error.message}`);
90-
throw error;
91-
}
69+
async delete(id: string): Promise<Comments> {
70+
this.logging.log(`[CommentService] Deleting comment ${id}`);
71+
72+
return this.prismaService.comments.delete({
73+
where: { id },
74+
});
9275
}
9376
}

src/user/controller/user.controller.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { UserService } from 'src/user/services/user.service';
2323
@ApiTags('User')
2424
@Controller('/user')
2525
export class UserController {
26-
constructor(private readonly userService: UserService) { }
26+
constructor(private readonly userService: UserService) {}
2727

2828
@ApiResponse({
2929
status: HttpStatus.OK,
@@ -38,10 +38,7 @@ export class UserController {
3838
description: 'Internal server error',
3939
})
4040
@Get(':id')
41-
async findUserById(
42-
@Param('id') id: string,
43-
@Res() res: Response
44-
) {
41+
async findUserById(@Param('id') id: string, @Res() res: Response) {
4542
const user = await this.userService.findUserById(id);
4643
return res.status(HttpStatus.OK).json({
4744
msg: i18n()['message.user.get'],
@@ -62,10 +59,7 @@ export class UserController {
6259
description: 'Internal server error',
6360
})
6461
@Post()
65-
async createUser(
66-
@Body() createUserDto: UserDto,
67-
@Res() res: Response
68-
) {
62+
async createUser(@Body() createUserDto: UserDto, @Res() res: Response) {
6963
const user = await this.userService.createUser(createUserDto);
7064
return res.status(HttpStatus.CREATED).json({
7165
msg: i18n()['message.user.created'],
@@ -88,7 +82,7 @@ export class UserController {
8882
@Delete(':id')
8983
async deleteUser(@Param('id') id: string, @Res() res: Response) {
9084
await this.userService.findUserByIdAndDelete(id);
91-
return res.status(HttpStatus.NO_CONTENT)
85+
return res.status(HttpStatus.NO_CONTENT);
9286
}
9387

9488
@ApiResponse({
@@ -109,22 +103,15 @@ export class UserController {
109103
@Body() editUserDTO: UserDto,
110104
@Res() res: Response,
111105
) {
112-
const user = await this.userService.findUserByIdAndUpdate(
113-
id,
114-
editUserDTO,
115-
);
106+
const user = await this.userService.findUserByIdAndUpdate(id, editUserDTO);
116107
return res.json({
117108
msg: i18n()['message.user.update'],
118109
data: { user },
119110
});
120111
}
121112

122113
@Post('/login')
123-
async login(
124-
@Body() email: string,
125-
password: string,
126-
@Res() res: Response
127-
) {
114+
async login(@Body() email: string, password: string, @Res() res: Response) {
128115
const { token } = await this.userService.auth(email, password);
129116
return res.status(HttpStatus.OK).json({
130117
msg: i18n()['message.user.login'],

0 commit comments

Comments
 (0)