-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
184 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
@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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
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, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,3 @@ export const typeormDevelopConfig: TypeOrmModuleOptions = { | |
//logging: true, | ||
synchronize: true, | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters