Skip to content

Commit

Permalink
feat(#41): 좋아요 조회 시, 액세스 토큰 값을 받아서 해당 유저가 좋아요를 눌렀는지 판별하는 코드 추가. 만약 비로…
Browse files Browse the repository at this point in the history
…그인으로 토큰값이 없을 경우에도 로직을 실행하도록 catch로 핸들링
  • Loading branch information
hobiJeong committed Oct 27, 2023
1 parent 33fb5cd commit ccdce30
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 12 deletions.
29 changes: 24 additions & 5 deletions src/boards/controllers/boards-like.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {
Headers,
} from '@nestjs/common';
import { BoardsLikeService } from '../services/boards-like.service';
import { Users } from 'src/common/decorators/user.decorator';
import { User } from 'src/users/entities/user.entity';
import { ApiTags } from '@nestjs/swagger';
import { ApiAddBoardLike } from '../swagger-decorators/add-board-like.decorator';
import { ApiGetBoardLikeCount } from '../swagger-decorators/get-board-like-count.decorator';
Expand All @@ -39,9 +37,30 @@ export class BoardsLikeController {
}

@ApiGetBoardLikeCount()
@Get('/like')
async getBoardsLike(@Query('boardId') boardId: number) {
return this.boardsLikeService.getBoardLikes(boardId);
@Get('like')
async getBoardsLike(
@Headers('access_token') accessToken: string,
@Query('boardId', ParseIntPipe) boardId: number,
) {
try {
const userId = await this.tokenService.decodeToken(accessToken);

return this.boardsLikeService.getBoardLikesAndIsLike(boardId, userId);
} catch (error) {
if (
(error.status === 401 &&
error.message === '유효하지 않은 토큰입니다.') ||
(error.status === 403 && error.message === '만료된 토큰입니다.') ||
(error.status === 404 &&
error.message === '사용자를 찾을 수 없습니다.') ||
(error.status === 411 &&
error.message === '토큰이 제공되지 않았습니다.')
) {
return this.boardsLikeService.getBoardLikes(boardId);
}
console.error(error);
throw error;
}
}

@ApiDeleteBoardLike()
Expand Down
11 changes: 9 additions & 2 deletions src/boards/repository/boards-like.repository.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Injectable } from '@nestjs/common';
import { EntityManager } from 'typeorm';
import { EntityManager, Equal } from 'typeorm';
import { BoardLike } from '../entities/board-like.entity';

@Injectable()
export class BoardsLikeRepository {
constructor(private entityManager: EntityManager) {}

async addBoardLike(boardId: number, userId: number) {
const boardLike = new BoardLike();
boardLike.boardId = boardId;
Expand All @@ -22,6 +23,12 @@ export class BoardsLikeRepository {
return likesCount;
}

async isBoardLike(boardId: number, userId: number) {
return this.entityManager.findOne(BoardLike, {
where: { boardId: Equal(boardId), userId: Equal(userId) },
});
}

async deleteBoardLike(boardId: number, userId: number) {
this.entityManager.delete(BoardLike, {
boardId: boardId,
Expand All @@ -30,4 +37,4 @@ export class BoardsLikeRepository {

return { success: true, msg: '좋아요 삭제 성공', isLike: false };
}
}
}
33 changes: 31 additions & 2 deletions src/boards/services/boards-like.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,43 @@ export class BoardsLikeService {
return returnedLike;
}

async getBoardLikesAndIsLike(boardId: number, userId: number) {
const board = await this.entityManager.findOne(Board, {
where: { id: boardId },
});

if (!board) {
throw new NotFoundException('해당 게시글이 없습니다.');
}

const boardLikesCount =
await this.boardsLikeRepositry.getBoardLikesCount(boardId);

const isBoardLike = await this.boardsLikeRepositry.isBoardLike(
boardId,
userId,
);

if (!isBoardLike) {
return { isLike: false, boardLikesCount };
}

return { isLike: true, boardLikesCount };
}

async getBoardLikes(boardId: number) {
const board = await this.entityManager.findOne(Board, {
where: { id: boardId },
});

if (!board) {
throw new Error('해당 게시글이 없습니다. ');
throw new NotFoundException('해당 게시글이 없습니다.');
}
return this.boardsLikeRepositry.getBoardLikesCount(boardId);

const boardLikesCount =
await this.boardsLikeRepositry.getBoardLikesCount(boardId);

return { isLike: false, boardLikesCount };
}

async deleteBoardLike(boardId: number, userId: number) {
Expand Down
32 changes: 29 additions & 3 deletions src/boards/swagger-decorators/get-board-like-count.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,42 @@ export function ApiGetBoardLikeCount() {
return applyDecorators(
ApiOperation({
summary: '해당 게시글 좋아요 개수 조회',
description: 'Param - board-id',
description: 'Headers - access_token, Param - board-id',
}),
ApiResponse({
status: 200,
description:
'좋아요 조회 성공. 실제 response 값은 count 없이 숫자만 들어옴.',
'좋아요 조회 성공. 토큰이 담겨서 왔고, 해당 유저가 좋아요를 누른 경우',
content: {
JSON: {
example: {
count: 1,
isLike: true,
boardLikesCount: 2,
},
},
},
}),
ApiResponse({
status: 200,
description:
'좋아요 조회 성공. 토큰이 없거나, 해당 유저가 좋아요를 누르지 않은 경우',
content: {
JSON: {
example: {
isLike: true,
boardLikesCount: 2,
},
},
},
}),
ApiResponse({
status: 404,
description: '해당 보드 id가 존재하지 않는 경우',
content: {
JSON: {
example: {
isLike: true,
boardLikesCount: 2,
},
},
},
Expand Down

0 comments on commit ccdce30

Please sign in to comment.