Skip to content

Commit

Permalink
Fix/notice like (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueHorn07 authored Sep 1, 2024
1 parent d7eb9d9 commit 1ee11b6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/popo/noticeLike/noticeLike.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -55,19 +55,19 @@ 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();
});

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 };
Expand Down
35 changes: 12 additions & 23 deletions src/popo/noticeLike/noticeLike.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@ import {
Controller,
Delete,
Get,
HttpException,
HttpStatus,
Post,
Query,
Req,
UseGuards,
} from '@nestjs/common';
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')
Expand All @@ -32,33 +29,25 @@ export class NoticeLikeController {
@Post()
@UseGuards(JwtAuthGuard)
@ApiBody({ type: NoticeLikeDto })
async create(@Body() dto: NoticeLikeDto): Promise<NoticeLike> {
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<NoticeLike> {
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<number> {
countLikes(@Query('notice_id') notice_id: number): Promise<number> {
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<boolean> {
return (await this.noticeLikeService.findByUserIdAndNoticeId(
user_id,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/popo/noticeLike/noticeLike.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class NoticeLikeDto {
readonly user_id: string;
readonly notice_id: string;
readonly notice_id: number;
}
2 changes: 1 addition & 1 deletion src/popo/noticeLike/noticeLike.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class NoticeLike {
user_id: string;

@Column({ nullable: false })
notice_id: string;
notice_id: number;

@CreateDateColumn()
created_at: Date;
Expand Down
8 changes: 4 additions & 4 deletions src/popo/noticeLike/noticeLike.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1ee11b6

Please sign in to comment.