From 1ee11b6db86a191a5dfcce55f4c9f6e698ae4154 Mon Sep 17 00:00:00 2001 From: Seokyun Ha Date: Sun, 1 Sep 2024 19:51:50 +0900 Subject: [PATCH] Fix/notice like (#121) --- .../noticeLike/noticeLike.controller.spec.ts | 8 ++--- src/popo/noticeLike/noticeLike.controller.ts | 35 +++++++------------ src/popo/noticeLike/noticeLike.dto.ts | 2 +- src/popo/noticeLike/noticeLike.entity.ts | 2 +- src/popo/noticeLike/noticeLike.service.ts | 8 ++--- 5 files changed, 22 insertions(+), 33 deletions(-) diff --git a/src/popo/noticeLike/noticeLike.controller.spec.ts b/src/popo/noticeLike/noticeLike.controller.spec.ts index 018f4af..9fdc422 100644 --- a/src/popo/noticeLike/noticeLike.controller.spec.ts +++ b/src/popo/noticeLike/noticeLike.controller.spec.ts @@ -30,7 +30,7 @@ describe('NoticeLikeController', () => { it('should count likes', async () => { const result = 1; - const noticeId = '1'; + const noticeId = 1; noticeLikeService.countLikes.mockResolvedValue(result); expect(await controller.countLikes(noticeId)).toBe(result); @@ -55,7 +55,7 @@ describe('NoticeLikeController', () => { it('should throw error when delete if it is not liked', async () => { const userId = '1'; - const noticeId = '1'; + const noticeId = 1; noticeLikeService.findByUserIdAndNoticeId.mockResolvedValue(null); await expect(controller.delete(userId, noticeId)).rejects.toThrow(); @@ -63,11 +63,11 @@ describe('NoticeLikeController', () => { it('should delete like', async () => { const userId = '1'; - const noticeId = '1'; + const noticeId = 1; const like: NoticeLike = { id: 1, user_id: '1', - notice_id: '1', + notice_id: 1, created_at: new Date(), }; const mockedDeletedResult = { affected: 1, raw: null }; diff --git a/src/popo/noticeLike/noticeLike.controller.ts b/src/popo/noticeLike/noticeLike.controller.ts index a9d5c6e..3538ac9 100644 --- a/src/popo/noticeLike/noticeLike.controller.ts +++ b/src/popo/noticeLike/noticeLike.controller.ts @@ -4,10 +4,9 @@ import { Controller, Delete, Get, - HttpException, - HttpStatus, Post, Query, + Req, UseGuards, } from '@nestjs/common'; import { ApiBody, ApiTags } from '@nestjs/swagger'; @@ -15,13 +14,11 @@ import { ApiBody, ApiTags } from '@nestjs/swagger'; import { JwtAuthGuard } from 'src/auth/guards/jwt-auth.guard'; import { NoticeLikeDto } from './noticeLike.dto'; import { NoticeLikeService } from './noticeLike.service'; -import { QueryFailedError } from 'typeorm'; import { NoticeLike } from './noticeLike.entity'; +import { JwtPayload } from 'src/auth/strategies/jwt.payload'; const Message = { FAIL_LIKE_DELETION_NEVER_LIKED: 'There is no record of liking the post.', - FAIL_DUPLICATE_ENTRY: 'Duplicate entry detected', - FAIL_INTERNAL_SERVER: 'Internal server error', }; @ApiTags('Notice Like') @@ -32,33 +29,25 @@ export class NoticeLikeController { @Post() @UseGuards(JwtAuthGuard) @ApiBody({ type: NoticeLikeDto }) - async create(@Body() dto: NoticeLikeDto): Promise { - try { - return await this.noticeLikeService.save(dto); - } catch (e) { - if (e instanceof QueryFailedError) { - throw new HttpException( - Message.FAIL_DUPLICATE_ENTRY, - HttpStatus.CONFLICT, - ); - } else { - throw new HttpException( - Message.FAIL_INTERNAL_SERVER, - HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + async create(@Body() dto: NoticeLikeDto, @Req() req): Promise { + const user = req.user as JwtPayload; + + if (user.uuid != dto.user_id) { + throw new BadRequestException('User ID does not match.'); } + + return this.noticeLikeService.save(dto); } @Get('count') - countLikes(@Query('notice_id') notice_id: string): Promise { + countLikes(@Query('notice_id') notice_id: number): Promise { return this.noticeLikeService.countLikes(notice_id); } @Get('status') async getStatus( @Query('user_id') user_id: string, - @Query('notice_id') notice_id: string, + @Query('notice_id') notice_id: number, ): Promise { return (await this.noticeLikeService.findByUserIdAndNoticeId( user_id, @@ -72,7 +61,7 @@ export class NoticeLikeController { @UseGuards(JwtAuthGuard) async delete( @Query('user_id') user_id: string, - @Query('notice_id') notice_id: string, + @Query('notice_id') notice_id: number, ) { const target = await this.noticeLikeService.findByUserIdAndNoticeId( user_id, diff --git a/src/popo/noticeLike/noticeLike.dto.ts b/src/popo/noticeLike/noticeLike.dto.ts index 2c067a9..5bdece6 100644 --- a/src/popo/noticeLike/noticeLike.dto.ts +++ b/src/popo/noticeLike/noticeLike.dto.ts @@ -1,4 +1,4 @@ export class NoticeLikeDto { readonly user_id: string; - readonly notice_id: string; + readonly notice_id: number; } diff --git a/src/popo/noticeLike/noticeLike.entity.ts b/src/popo/noticeLike/noticeLike.entity.ts index c345fa6..2e85997 100644 --- a/src/popo/noticeLike/noticeLike.entity.ts +++ b/src/popo/noticeLike/noticeLike.entity.ts @@ -16,7 +16,7 @@ export class NoticeLike { user_id: string; @Column({ nullable: false }) - notice_id: string; + notice_id: number; @CreateDateColumn() created_at: Date; diff --git a/src/popo/noticeLike/noticeLike.service.ts b/src/popo/noticeLike/noticeLike.service.ts index d7106ed..82c5361 100644 --- a/src/popo/noticeLike/noticeLike.service.ts +++ b/src/popo/noticeLike/noticeLike.service.ts @@ -16,23 +16,23 @@ export class NoticeLikeService { return this.noticeLikeRepo.save(dto); } - findByUserIdAndNoticeId(user_id: string, notice_id: string) { + findByUserIdAndNoticeId(user_id: string, notice_id: number) { return this.noticeLikeRepo.findOne({ where: { user_id: user_id, notice_id: notice_id }, }); } - findAllByNoticeId(notice_id: string) { + findAllByNoticeId(notice_id: number) { return this.noticeLikeRepo.find({ where: { notice_id: notice_id }, }); } - countLikes(notice_id: string) { + countLikes(notice_id: number) { return this.noticeLikeRepo.count({ where: { notice_id: notice_id } }); } - delete(user_id: string, notice_id: string) { + delete(user_id: string, notice_id: number) { return this.noticeLikeRepo.delete({ user_id: user_id, notice_id: notice_id,