Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/comments/controller/comments.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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'],
Expand All @@ -88,10 +88,7 @@ export class CommentsController {
@Body() commentDTO: CommentDTO,
@Res() res: Response,
) {
const comment = await this.commentsServices.findByIdAndUpdateComment(
id,
commentDTO,
);
const comment = await this.commentsServices.update(id, commentDTO);

return res.status(HttpStatus.CREATED).json({
message: i18n()['message.comment.update'],
Expand All @@ -113,7 +110,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);
}
Expand Down
115 changes: 49 additions & 66 deletions src/comments/services/comments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -13,81 +12,65 @@ export class CommentsService {
private prismaService: PrismaService,
private userService: UserService,
private projectService: ProjectsService,
private logger: LoggerService,
private logging: LoggerService,
) {}

async save(data: Comment): Promise<Comments> {
this.logging.log(
`[CommentService] Starting to create comment for user ${data.userId} on project ${data.projectId}`,
);

async createComment(data: Comment): Promise<Comments> {
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;
}
await Promise.all([
this.userService.checkUserExistence(data.userId),
this.projectService.checkProjectExistence(data.projectId),
]);

return this.prismaService.comments.create({ data });
}

async findCommentById(comment_id: string): Promise<Comments | null> {
try {
const comment = await this.prismaService.comments.findUnique({
where: {
id: comment_id,
},
});
if (!comment) {
throw new NotFoundException();
}
async findById(id: string): Promise<Comments> {
this.logging.log(`[CommentService] Finding comment with ID: ${id}`);

return comment;
} catch (error) {
if (error instanceof NotFoundException) {
throw new NotFoundException(i18n()['exception.notFound'], comment_id);
}
const comment = await this.prismaService.comments.findUnique({
where: { id },
});

this.logger.error(`some error ocurred : ${error.message}`);
throw error;
if (!comment) {
this.logging.warn(`[CommentService] Comment not found with ID: ${id}`);
throw new NotFoundException(`Comment with ID ${id} not found`);
}

return comment;
}

async findByIdAndUpdateComment(
comment_id: string,
data: Comment,
): Promise<Comments> {
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 update(id: string, data: Comment): Promise<Comments> {
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,
};

return this.prismaService.comments.update({
where: { id },
data: sanitizedData,
});
}

async findByIdAndDeleteComment(comment_id: string): Promise<Comments | null> {
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;
}
async delete(id: string): Promise<Comments> {
this.logging.log(`[CommentService] Deleting comment ${id}`);

return this.prismaService.comments.delete({
where: { id },
});
}
}
25 changes: 6 additions & 19 deletions src/user/controller/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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'],
Expand All @@ -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'],
Expand All @@ -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({
Expand All @@ -109,22 +103,15 @@ 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 },
});
}

@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'],
Expand Down