Skip to content

Commit 3ec39ba

Browse files
authored
Merge pull request #19 from hebertzin/feat/refactor-controllers
feat/refactor controllers
2 parents 34edd2d + 944653d commit 3ec39ba

File tree

13 files changed

+393
-369
lines changed

13 files changed

+393
-369
lines changed

src/comments/controller/comments.controller.ts

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
Post,
88
Put,
99
Res,
10-
Req,
10+
HttpStatus,
1111
} from '@nestjs/common';
1212

1313
import {
@@ -27,93 +27,94 @@ import { i18n } from 'src/i18n';
2727
export class CommentsController {
2828
constructor(private commentsServices: CommentsService) {}
2929

30-
@ApiResponse({ status: 200, description: i18n()['message.comment.get'] })
30+
@ApiResponse({
31+
status: HttpStatus.OK,
32+
description: i18n()['message.comment.get'],
33+
})
3134
@ApiBadRequestResponse({
32-
status: 400,
35+
status: HttpStatus.BAD_REQUEST,
3336
description: 'Bad Request ',
3437
})
3538
@ApiInternalServerErrorResponse({
36-
status: 500,
39+
status: HttpStatus.INTERNAL_SERVER_ERROR,
3740
description: 'Internal server error',
3841
})
39-
@Get('/:id')
40-
async getCommentById(
41-
@Param('id') id: string,
42-
@Res() res: Response,
43-
@Req() req,
44-
) {
45-
const user = req.user;
46-
47-
console.log(user);
48-
const commentFound = await this.commentsServices.findCommentById(id);
49-
50-
return res.status(200).json({
42+
@Get(':id')
43+
async getCommentById(@Param('id') id: string, @Res() res: Response) {
44+
const comment = await this.commentsServices.findCommentById(id);
45+
return res.status(HttpStatus.OK).json({
5146
message: i18n()['message.comment.get'],
52-
comment: commentFound,
47+
data: { comment },
5348
});
5449
}
5550

56-
@ApiResponse({ status: 201, description: i18n()['message.comment.created'] })
51+
@ApiResponse({
52+
status: HttpStatus.CREATED,
53+
description: i18n()['message.comment.created'],
54+
})
5755
@ApiBadRequestResponse({
58-
status: 400,
56+
status: HttpStatus.BAD_REQUEST,
5957
description: 'Bad Request',
6058
})
6159
@ApiInternalServerErrorResponse({
62-
status: 500,
60+
status: HttpStatus.INTERNAL_SERVER_ERROR,
6361
description: 'Internal server error',
6462
})
65-
@Post('')
63+
@Post()
6664
async create(@Body() commentDTO: CommentDTO, @Res() res: Response) {
67-
const commentCreated =
68-
await this.commentsServices.createComment(commentDTO);
65+
const comment = await this.commentsServices.createComment(commentDTO);
6966

70-
return res.status(201).json({
67+
return res.status(HttpStatus.CREATED).json({
7168
message: i18n()['message.comment.created'],
72-
comment: commentCreated,
69+
data: { comment },
7370
});
7471
}
7572

76-
@ApiResponse({ status: 200, description: i18n()['message.comment.update'] })
73+
@ApiResponse({
74+
status: HttpStatus.CREATED,
75+
description: i18n()['message.comment.update'],
76+
})
7777
@ApiBadRequestResponse({
78-
status: 400,
78+
status: HttpStatus.BAD_REQUEST,
7979
description: 'Bad Request : user or post or question does not exist',
8080
})
8181
@ApiInternalServerErrorResponse({
82-
status: 500,
82+
status: HttpStatus.INTERNAL_SERVER_ERROR,
8383
description: 'Internal server error',
8484
})
85-
@Put('/:id')
85+
@Put(':id')
8686
async updateComment(
8787
@Param('id') id: string,
8888
@Body() commentDTO: CommentDTO,
8989
@Res() res: Response,
9090
) {
91-
const updated = await this.commentsServices.findByIdAndUpdateComment(
91+
const comment = await this.commentsServices.findByIdAndUpdateComment(
9292
id,
9393
commentDTO,
9494
);
9595

96-
return res.status(200).json({
96+
return res.status(HttpStatus.CREATED).json({
9797
message: i18n()['message.comment.update'],
98-
comment: updated,
98+
data: { comment },
9999
});
100100
}
101101

102-
@ApiResponse({ status: 200, description: i18n()['message.comment.deleted'] })
102+
@ApiResponse({
103+
status: HttpStatus.NO_CONTENT,
104+
description: i18n()['message.comment.deleted'],
105+
})
103106
@ApiBadRequestResponse({
104-
status: 400,
107+
status: HttpStatus.BAD_REQUEST,
105108
description: 'Bad Request : comment does not exist',
106109
})
107110
@ApiInternalServerErrorResponse({
108-
status: 500,
111+
status: HttpStatus.INTERNAL_SERVER_ERROR,
109112
description: 'Internal server error',
110113
})
111-
@Delete('/:id')
114+
@Delete(':id')
112115
async deleteComment(@Param('id') id: string, @Res() res: Response) {
113116
await this.commentsServices.findByIdAndDeleteComment(id);
114117

115-
return res.status(200).json({
116-
message: i18n()['message.comment.deleted'],
117-
});
118+
return res.status(HttpStatus.NO_CONTENT);
118119
}
119120
}

src/decisions/controller/decisions.controller.ts

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
Controller,
44
Delete,
55
Get,
6+
HttpStatus,
67
Param,
78
Post,
89
Put,
@@ -25,99 +26,96 @@ export class DecisionsController {
2526
constructor(private decisionService: DecisionsService) {}
2627

2728
@ApiResponse({
28-
status: 201,
29+
status: HttpStatus.CREATED,
2930
description: i18n()['message.decisions.created'],
3031
})
3132
@ApiBadRequestResponse({
32-
status: 400,
33+
status: HttpStatus.BAD_REQUEST,
3334
description: 'Bad Request',
3435
})
3536
@ApiInternalServerErrorResponse({
36-
status: 500,
37+
status: HttpStatus.INTERNAL_SERVER_ERROR,
3738
description: 'Internal server error',
3839
})
3940
@Post()
4041
async createDesision(
4142
@Body() createDecision: DecisionDTO,
4243
@Res() res: Response,
4344
) {
44-
const desicion = await this.decisionService.create(createDecision);
45+
const decision = await this.decisionService.create(createDecision);
4546

46-
return res.status(201).json({
47+
return res.status(HttpStatus.CREATED).json({
4748
message: i18n()['message.decisions.created'],
48-
desicion,
49+
data: { decision },
4950
});
5051
}
5152

5253
@ApiResponse({
53-
status: 200,
54+
status: HttpStatus.OK,
5455
description: i18n()['message.decisions.get'],
5556
})
5657
@ApiBadRequestResponse({
57-
status: 400,
58+
status: HttpStatus.BAD_REQUEST,
5859
description: 'Bad Request',
5960
})
6061
@ApiInternalServerErrorResponse({
61-
status: 500,
62+
status: HttpStatus.INTERNAL_SERVER_ERROR,
6263
description: 'Internal server error',
6364
})
64-
@Get('/:id')
65+
@Get(':id')
6566
async findDecisionById(@Param('id') id: string, @Res() res: Response) {
6667
const decision = await this.decisionService.findDecisionById(id);
6768

68-
return res.status(200).json({
69+
return res.status(HttpStatus.OK).json({
6970
message: i18n()['message.decisions.get'],
7071
decision,
7172
});
7273
}
7374

7475
@ApiResponse({
75-
status: 200,
76-
description: i18n()['message.decisions.deleted'],
76+
status: HttpStatus.NO_CONTENT,
77+
description: i18n()['message.decisions.get'],
7778
})
7879
@ApiBadRequestResponse({
79-
status: 400,
80+
status: HttpStatus.BAD_REQUEST,
8081
description: 'Bad Request',
8182
})
8283
@ApiInternalServerErrorResponse({
83-
status: 500,
84+
status: HttpStatus.INTERNAL_SERVER_ERROR,
8485
description: 'Internal server error',
8586
})
86-
@Delete('/:id')
87+
@Delete(':id')
8788
async findByIdAndDelete(@Param('id') id: string, @Res() res: Response) {
8889
await this.decisionService.findDecisionByIdAndDelete(id);
89-
90-
return res.status(200).json({
91-
message: i18n()['message.decisions.deleted'],
92-
});
90+
return res.status(HttpStatus.NO_CONTENT);
9391
}
9492

9593
@ApiResponse({
96-
status: 200,
94+
status: HttpStatus.OK,
9795
description: i18n()['message.decisions.update'],
9896
})
9997
@ApiBadRequestResponse({
100-
status: 400,
98+
status: HttpStatus.BAD_REQUEST,
10199
description: 'Bad Request',
102100
})
103101
@ApiInternalServerErrorResponse({
104-
status: 500,
102+
status: HttpStatus.INTERNAL_SERVER_ERROR,
105103
description: 'Internal server error',
106104
})
107-
@Put('/:id')
105+
@Put(':id')
108106
async findDecisionByIdAndUpdate(
109107
@Param('id') id: string,
110-
@Body() decision: DecisionDTO,
108+
@Body() decisionDTO: DecisionDTO,
111109
@Res() res: Response,
112110
) {
113-
const updateDecision = await this.decisionService.findDecisionByIdAndUpdate(
111+
const decision = await this.decisionService.findDecisionByIdAndUpdate(
114112
id,
115-
decision,
113+
decisionDTO,
116114
);
117115

118-
return res.status(200).json({
116+
return res.status(HttpStatus.OK).json({
119117
message: i18n()['message.decisions.update'],
120-
decision: updateDecision,
118+
data: { decision },
121119
});
122120
}
123121
}

src/follow-project/controller/follow-project.controller.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
Controller,
44
Delete,
55
Get,
6+
HttpStatus,
67
Param,
78
Post,
89
Res,
@@ -23,18 +24,18 @@ export class FollowProjectController {
2324
constructor(private followProjectService: FollowProjectService) {}
2425

2526
@ApiResponse({
26-
status: 200,
27+
status: HttpStatus.OK,
2728
description: i18n()['message.followProject.created'],
2829
})
2930
@ApiBadRequestResponse({
30-
status: 400,
31+
status: HttpStatus.BAD_REQUEST,
3132
description: 'Bad Request',
3233
})
3334
@ApiInternalServerErrorResponse({
34-
status: 500,
35+
status: HttpStatus.INTERNAL_SERVER_ERROR,
3536
description: 'Internal server error',
3637
})
37-
@Post('/:projectId/follow')
38+
@Post(':projectId/follow')
3839
async followProject(
3940
@Body() data: any,
4041
@Res() res: Response,
@@ -45,54 +46,52 @@ export class FollowProjectController {
4546
data.userId,
4647
);
4748

48-
return res.status(200).json({
49-
msg: i18n()['message.followProject.created'],
50-
follow,
49+
return res.status(HttpStatus.OK).json({
50+
message: i18n()['message.followProject.created'],
51+
data: { follow },
5152
});
5253
}
5354

5455
@ApiResponse({
55-
status: 200,
56+
status: HttpStatus.OK,
5657
description: i18n()['message.followProject.all'],
5758
})
5859
@ApiBadRequestResponse({
59-
status: 400,
60+
status: HttpStatus.BAD_REQUEST,
6061
description: 'Bad Request',
6162
})
6263
@ApiInternalServerErrorResponse({
63-
status: 500,
64+
status: HttpStatus.INTERNAL_SERVER_ERROR,
6465
description: 'Internal server error',
6566
})
66-
@Get('/:projectId/all-followers')
67+
@Get(':projectId/followers')
6768
async allFollowers(
6869
@Res() res: Response,
6970
@Param('projectId') projectId: string,
7071
) {
71-
const all =
72+
const followers =
7273
await this.followProjectService.getProjectsUserFollow(projectId);
73-
return res.status(200).json({
74-
msg: i18n()['message.followProject.all'],
75-
all,
74+
return res.status(HttpStatus.OK).json({
75+
message: i18n()['message.followProject.all'],
76+
data: { followers },
7677
});
7778
}
7879

7980
@ApiResponse({
80-
status: 200,
81+
status: HttpStatus.NO_CONTENT,
8182
description: i18n()['message.followProject.deleted'],
8283
})
8384
@ApiBadRequestResponse({
84-
status: 400,
85+
status: HttpStatus.BAD_REQUEST,
8586
description: 'Bad Request',
8687
})
8788
@ApiInternalServerErrorResponse({
88-
status: 500,
89+
status: HttpStatus.INTERNAL_SERVER_ERROR,
8990
description: 'Internal server error',
9091
})
9192
@Delete('/:id/stop')
9293
async stopFollowProject(@Res() res: Response, @Param('id') id: string) {
9394
await this.followProjectService.stopFollowProject(id);
94-
return res.status(200).json({
95-
msg: i18n()['message.followProject.deleted'],
96-
});
95+
return res.status(HttpStatus.NO_CONTENT);
9796
}
9897
}

0 commit comments

Comments
 (0)