diff --git a/src/database/repositories/comment.repository.ts b/src/database/repositories/comment.repository.ts index 309185f..a971643 100644 --- a/src/database/repositories/comment.repository.ts +++ b/src/database/repositories/comment.repository.ts @@ -15,6 +15,7 @@ import { UserProfileDto } from 'src/common/dtos/user-profile.dto'; import { RequestRandomCommentDto } from 'src/users/dtos/RandomComment.request.dto'; import { ResponseRandomCommentDto } from 'src/users/dtos/RandomComment.response.dto'; import { ResponseCommentNumDto } from 'src/users/dtos/CommentNum.response.dto'; +import { ResponseRandomCommentUserDto } from 'src/users/dtos/RandomCommentUser.response.dto'; @Injectable() export class CommentRepository { @@ -109,6 +110,33 @@ export class CommentRepository { return plainToInstance(ResponseRandomCommentDto, entities); } + // 댓글 랜덤 조회(유저 정보 포함) + async getRandomCommentUser(requestRandomCommentDto: RequestRandomCommentDto) { + const { take } = requestRandomCommentDto; + const queryBuilder = this.commentRepository.createQueryBuilder('comment'); + + queryBuilder + .leftJoinAndSelect('comment.user', 'user') + .orderBy('RANDOM()') + .limit(take); + + + const { entities } = await queryBuilder.getRawAndEntities(); + console.log(entities); + const comments = entities.map(function(comment) { + const userInfo = plainToInstance(UserProfileDto, comment.user); + const ret_comment = { + id: comment.id, + content: comment.content, + nickName: comment.nickName, + createdAt: comment.createdAt, + userInfo, + } + return ret_comment; + }) + return plainToInstance(ResponseRandomCommentUserDto, comments); + } + // 댓글 삭제 async deleteComment(id: number) { const comment = await this.commentRepository.findOne({ where: { id: id } }); diff --git a/src/users/dtos/RandomCommentUser.response.dto.ts b/src/users/dtos/RandomCommentUser.response.dto.ts new file mode 100644 index 0000000..c4c3747 --- /dev/null +++ b/src/users/dtos/RandomCommentUser.response.dto.ts @@ -0,0 +1,25 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { Expose } from 'class-transformer'; +import { UserProfileDto } from 'src/common/dtos/user-profile.dto'; + +export class ResponseRandomCommentUserDto { + @ApiProperty({ description: '댓글 고유 아이디', type: Number}) + @Expose() + id: number; + + @ApiProperty({ description: '댓글 내옹', type: String }) + @Expose() + content: string; + + @ApiProperty({ description: '익명 닉네임', type: String }) + @Expose() + nickName: string; + + @ApiProperty({ description: '응원 코멘트 생성 일자', type: Date }) + @Expose() + createdAt: Date; + + @ApiProperty({ description: '유저 정보', type: UserProfileDto }) + @Expose() + userInfo: UserProfileDto +} \ No newline at end of file diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 8f50dce..96d5429 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -40,6 +40,7 @@ import { RequestRandomCommentDto } from './dtos/RandomComment.request.dto'; import { ResponseRandomCommentDto } from './dtos/RandomComment.response.dto'; import { UserFindDto } from './dtos/UserFind.dto'; import { ResponseCommentNumDto } from './dtos/CommentNum.response.dto'; +import { ResponseRandomCommentUserDto } from './dtos/RandomCommentUser.response.dto'; @ApiTags('users') @ApiBearerAuth('accessToken') @@ -186,6 +187,22 @@ export class UsersController { return await this.userService.getRandomComment(requestRandomCommentDto); } + // 댓글 랜덤 조회(유저 정보 포함) + @ApiOperation({ summary: '유저 정보 포함 댓글 랜덤 조회' }) + @SuccessResponse(HttpStatus.OK, [ + { + model: ResponseRandomCommentUserDto, + exampleDescription: '유저 정보 포함 댓글 랜덤 조회 성공 시', + exampleTitle: '댓글 랜덤 조회' + } + ]) + @Get('/random/comment/userInfo') + async getRandomCommentUser( + @Query() requestRandomCommentDto: RequestRandomCommentDto + ) { + return await this.userService.getRandomCommentUser(requestRandomCommentDto); + } + // 응원 댓글 삭제(관리자용) @ApiOperation({ summary: '[어드민] 응원 댓글 삭제' }) @SuccessResponse(HttpStatus.OK, [ diff --git a/src/users/users.service.ts b/src/users/users.service.ts index b1868b1..06c5bf4 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -14,7 +14,6 @@ import { ScrollOptionsDto } from './dtos/Scroll/ScrollOptions.dto'; import { ResponseScrollCommentsDto } from './dtos/Scroll/ScrollComments.response.dto'; import { RequestRandomCommentDto } from './dtos/RandomComment.request.dto'; import { UserFindDto } from './dtos/UserFind.dto'; -import { ResponseCommentNumDto } from './dtos/CommentNum.response.dto'; @Injectable() export class UsersService { @@ -107,6 +106,11 @@ export class UsersService { ); } + // 댓글 랜덤 조회(유저 정보 포함) + async getRandomCommentUser(requestRandomCommentDto: RequestRandomCommentDto) { + return await this.commentRepository.getRandomCommentUser(requestRandomCommentDto); + } + // 댓글 삭제 async deleteComment(id: number) { return await this.commentRepository.deleteComment(id);