-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/#93 - 좋아요 기능 구현 #193
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { applyDecorators } from '@nestjs/common'; | ||
import { | ||
ApiBadRequestResponse, | ||
ApiCookieAuth, | ||
ApiOkResponse, | ||
ApiOperation, | ||
} from '@nestjs/swagger'; | ||
import { LikeResponse } from '@/chat/dto/like.response'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export function ToggleLikeApi() { | ||
return applyDecorators( | ||
ApiCookieAuth(), | ||
ApiOperation({ | ||
summary: '채팅 좋아요 토글 API', | ||
description: '채팅 좋아요를 토글한다.', | ||
}), | ||
ApiOkResponse({ | ||
description: '좋아요 성공', | ||
type: LikeResponse, | ||
}), | ||
ApiBadRequestResponse({ | ||
description: '채팅이 존재하지 않음', | ||
example: { | ||
message: 'Chat not found', | ||
error: 'Bad Request', | ||
statusCode: 400, | ||
}, | ||
}), | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { | ||
CreateDateColumn, | ||
Entity, | ||
Index, | ||
JoinColumn, | ||
ManyToOne, | ||
PrimaryGeneratedColumn, | ||
} from 'typeorm'; | ||
import { Chat } from '@/chat/domain/chat.entity'; | ||
import { User } from '@/user/domain/user.entity'; | ||
|
||
@Index('chat_user_unique', ['chat', 'user'], { unique: true }) | ||
Comment on lines
+11
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 인덱스 설정 굿입니다. |
||
@Entity() | ||
export class Like { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@ManyToOne(() => Chat, (chat) => chat.id) | ||
@JoinColumn({ name: 'chat_id' }) | ||
chat: Chat; | ||
|
||
@ManyToOne(() => User, (user) => user.id) | ||
@JoinColumn({ name: 'user_id' }) | ||
user: User; | ||
|
||
@CreateDateColumn({ name: 'created_at' }) | ||
createdAt: Date; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsNumber } from 'class-validator'; | ||
|
||
export class LikeRequest { | ||
@ApiProperty({ | ||
required: true, | ||
type: Number, | ||
description: '좋아요를 누를 채팅의 ID', | ||
example: 1, | ||
}) | ||
@IsNumber() | ||
chatId: number; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
export class LikeResponse { | ||
@ApiProperty({ | ||
type: Number, | ||
description: '좋아요를 누른 채팅의 ID', | ||
example: 1, | ||
}) | ||
chatId: number; | ||
|
||
@ApiProperty({ | ||
type: Number, | ||
description: '채팅의 좋아요 수', | ||
example: 45, | ||
}) | ||
likeCount: number; | ||
|
||
@ApiProperty({ | ||
type: String, | ||
description: '결과 메시지', | ||
example: 'like chat', | ||
}) | ||
message: string; | ||
|
||
@ApiProperty({ | ||
type: Date, | ||
description: '좋아요를 누른 시간', | ||
example: '2021-08-01T00:00:00', | ||
}) | ||
date: Date; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { BadRequestException, Injectable } from '@nestjs/common'; | ||
import { DataSource, EntityManager } from 'typeorm'; | ||
import { Chat } from '@/chat/domain/chat.entity'; | ||
import { Like } from '@/chat/domain/like.entity'; | ||
import { LikeResponse } from '@/chat/dto/like.response'; | ||
|
||
@Injectable() | ||
export class LikeService { | ||
constructor(private readonly dataSource: DataSource) {} | ||
|
||
async toggleLike(userId: number, chatId: number) { | ||
return await this.dataSource.transaction(async (manager) => { | ||
const chat = await this.findChat(chatId, manager); | ||
return await this.saveLike(manager, chat, userId); | ||
}); | ||
} | ||
|
||
private async findChat(chatId: number, manager: EntityManager) { | ||
const chat = await manager.findOne(Chat, { where: { id: chatId } }); | ||
if (!chat) { | ||
throw new BadRequestException('Chat not found'); | ||
} | ||
return chat; | ||
} | ||
|
||
private async saveLike( | ||
manager: EntityManager, | ||
chat: Chat, | ||
userId: number, | ||
): Promise<LikeResponse> { | ||
chat.likeCount += 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. like 카운트를 더해주는 방향으로 구현하셨군요. 혹시 이 부분에 대해 정합성을 검사해주는 로직이 존재할까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아마 주기적으로 스케줄링으로 좋아요 수를 맞출 예정입니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 락도 고민했었는데 성능 문제와 데드락의 위험성이 조금 있어서 제외했습니다! (아마 같은 사용자가 같은 게시물을 엄청 빠르게 토글하면 데드락이 발생할 가능성이 있습니다!) |
||
await Promise.all([ | ||
manager.save(Like, { | ||
user: { id: userId }, | ||
chat, | ||
}), | ||
manager.save(Chat, chat), | ||
]); | ||
return { | ||
likeCount: chat.likeCount, | ||
message: 'like chat', | ||
chatId: chat.id, | ||
date: chat.date.updatedAt, | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,3 @@ export const typeormDevelopConfig: TypeOrmModuleOptions = { | |
//logging: true, | ||
synchronize: true, | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
커스텀 데코레이터 설정 굿입니다.