Skip to content

Commit

Permalink
앨범 상세 코멘트 API (#146)
Browse files Browse the repository at this point in the history
* feat: comment entity 설정

* feat: comment repository에 저장하는 로직 추가

* feat: album comment 등록하는 api 추가

* feat: comment 모듈 등록

* feat: 앨범 상세 댓글 불러오는 응답 dto 추가

* feat: 앨범 상세 댓글 불러오기 api 추가
  • Loading branch information
Kontae authored Nov 28, 2024
1 parent 32feb79 commit 2b00fdb
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 1 deletion.
5 changes: 4 additions & 1 deletion server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { MusicRepository } from '@/music/music.repository';
import { RoomModule } from '@/room/room.module';
import { SchedulerService } from './common/scheduler/scheduler.service';
import { ScheduleModule } from '@nestjs/schedule';
import { Comment } from './comment/comment.entity';
import { CommentModule } from './comment/comment.module';

@Module({
imports: [
Expand All @@ -32,14 +34,15 @@ import { ScheduleModule } from '@nestjs/schedule';
AlbumModule,
SongModule,
RoomModule,
CommentModule,
TypeOrmModule.forRoot({
type: 'mysql',
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT),
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
entities: [Album, Song],
entities: [Album, Song, Comment],
}),
],
controllers: [AppController],
Expand Down
39 changes: 39 additions & 0 deletions server/src/comment/comment.controller.ts
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);
}
}
28 changes: 28 additions & 0 deletions server/src/comment/comment.entity.ts
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;
}
13 changes: 13 additions & 0 deletions server/src/comment/comment.module.ts
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 {}
34 changes: 34 additions & 0 deletions server/src/comment/comment.repository.ts
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);
}
}
21 changes: 21 additions & 0 deletions server/src/comment/comment.service.ts
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);
}
}
20 changes: 20 additions & 0 deletions server/src/comment/dto/album-comment-response.dto.ts
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;
}

0 comments on commit 2b00fdb

Please sign in to comment.