-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: comment entity 설정 * feat: comment repository에 저장하는 로직 추가 * feat: album comment 등록하는 api 추가 * feat: comment 모듈 등록 * feat: 앨범 상세 댓글 불러오는 응답 dto 추가 * feat: 앨범 상세 댓글 불러오기 api 추가
- Loading branch information
Showing
7 changed files
with
159 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'; | ||
import { CommentService } from './comment.service'; | ||
import { ApiBody, ApiOperation, ApiParam } from '@nestjs/swagger'; | ||
import { AlbumCommentResponseDto } from './dto/album-comment-response.dto'; | ||
|
||
@Controller('comment') | ||
export class CommentController { | ||
constructor(private readonly commentService: CommentService) {} | ||
|
||
@ApiOperation({ summary: '댓글 추가' }) | ||
@ApiParam({ name: 'albumId', required: true, description: '앨범 id' }) | ||
@ApiBody({ | ||
description: '댓글 내용', | ||
schema: { type: 'object', properties: { content: { type: 'string' } } }, | ||
}) | ||
@Post('/album/:albumId') | ||
async createComment( | ||
@Param('albumId') albumId: string, | ||
@Body('content') content: string, | ||
): Promise<any> { | ||
const commentResponse = await this.commentService.createComment( | ||
albumId, | ||
content, | ||
); | ||
return { | ||
success: true, | ||
commentResponse, | ||
}; | ||
} | ||
|
||
@ApiOperation({ summary: '댓글 불러오기' }) | ||
@ApiParam({ name: 'albumId', required: true, description: '앨범 id' }) | ||
@Get('/album/:albumId') | ||
async getAlbumComments( | ||
@Param('albumId') albumId: string, | ||
): Promise<AlbumCommentResponseDto> { | ||
return await this.commentService.getAlbumComments(albumId); | ||
} | ||
} |
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 { | ||
Entity, | ||
Column, | ||
PrimaryGeneratedColumn, | ||
ManyToOne, | ||
JoinColumn, | ||
CreateDateColumn, | ||
} from 'typeorm'; | ||
import { Album } from '@/album/album.entity'; | ||
|
||
@Entity() | ||
export class Comment { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column({ name: 'album_id', type: 'char', length: 36, nullable: false }) | ||
albumId: string; | ||
|
||
@ManyToOne(() => Album, { nullable: false }) | ||
@JoinColumn({ name: 'album_id' }) | ||
album: Album; | ||
|
||
@Column({ type: 'varchar', length: 200, nullable: false }) | ||
content: string; | ||
|
||
@CreateDateColumn({ type: 'timestamp', name: 'created_at', nullable: false }) | ||
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 { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { Comment } from './comment.entity'; | ||
import { CommentController } from './comment.controller'; | ||
import { CommentService } from './comment.service'; | ||
import { CommentRepository } from './comment.repository'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([Comment])], | ||
controllers: [CommentController], | ||
providers: [CommentService, CommentRepository], | ||
}) | ||
export class CommentModule {} |
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,34 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectDataSource, InjectRepository } from '@nestjs/typeorm'; | ||
import { Comment } from './comment.entity'; | ||
import { DataSource, Repository } from 'typeorm'; | ||
import { AlbumCommentDto } from './dto/album-comment-response.dto'; | ||
import { plainToInstance } from 'class-transformer'; | ||
|
||
@Injectable() | ||
export class CommentRepository { | ||
constructor( | ||
@InjectRepository(Comment) | ||
private readonly commentRepository: Repository<Comment>, | ||
@InjectDataSource() private readonly dataSource: DataSource, | ||
) {} | ||
|
||
async createComment(commentData: { | ||
albumId: string; | ||
content: string; | ||
}): Promise<Comment> { | ||
return await this.commentRepository.save(commentData); | ||
} | ||
|
||
async getCommentInfos(albumId: string): Promise<AlbumCommentDto[]> { | ||
const commentInfos = await this.dataSource | ||
.createQueryBuilder() | ||
.from(Comment, 'comment') | ||
.select(['album_id as albumId', 'content']) | ||
.where('album_id = :albumId', { albumId }) | ||
.orderBy('created_at') | ||
.getRawMany(); | ||
|
||
return plainToInstance(AlbumCommentDto, commentInfos); | ||
} | ||
} |
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,21 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { CommentRepository } from './comment.repository'; | ||
import { Comment } from './comment.entity'; | ||
import { AlbumCommentResponseDto } from './dto/album-comment-response.dto'; | ||
|
||
@Injectable() | ||
export class CommentService { | ||
constructor(private readonly commentRepository: CommentRepository) {} | ||
|
||
async createComment(albumId: string, content: string): Promise<Comment> { | ||
return await this.commentRepository.createComment({ | ||
albumId, | ||
content, | ||
}); | ||
} | ||
|
||
async getAlbumComments(albumId: string): Promise<AlbumCommentResponseDto> { | ||
const comments = await this.commentRepository.getCommentInfos(albumId); | ||
return new AlbumCommentResponseDto(comments); | ||
} | ||
} |
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,20 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
export class AlbumCommentResponseDto { | ||
@ApiProperty({ type: () => AlbumCommentDto, isArray: true }) | ||
result: { | ||
albumComments: AlbumCommentDto[]; | ||
}; | ||
constructor(albumComments: AlbumCommentDto[]) { | ||
this.result = { | ||
albumComments, | ||
}; | ||
} | ||
} | ||
|
||
export class AlbumCommentDto { | ||
@ApiProperty() | ||
albumId: string; | ||
@ApiProperty() | ||
content: string; | ||
} |