Skip to content

Commit

Permalink
[v1.0.6] 랜덤 댓글 뽑기 기능(유저 정보 포함)
Browse files Browse the repository at this point in the history
[v1.0.6] 랜덤 댓글 뽑기 기능(유저 정보 포함)
  • Loading branch information
ImNM authored Aug 9, 2022
2 parents ffccd29 + ef3ce4f commit 80232b2
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/database/repositories/comment.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 } });
Expand Down
25 changes: 25 additions & 0 deletions src/users/dtos/RandomCommentUser.response.dto.ts
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 17 additions & 0 deletions src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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, [
Expand Down
6 changes: 5 additions & 1 deletion src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 80232b2

Please sign in to comment.