Skip to content

Commit

Permalink
[v0.1.5] 댓글 랜덤 배포
Browse files Browse the repository at this point in the history
[v0.1.5] 댓글 랜덤 배포
  • Loading branch information
ImNM authored Aug 2, 2022
2 parents fe0a02d + 5f58e6a commit 6a3eebc
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/database/repositories/comment.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { plainToInstance } from 'class-transformer';
import { ResponseCommentDto } from 'src/users/dtos/Comment.response.dto';
import { CommentDto } from 'src/users/dtos/Comment.dto';
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';

@Injectable()
export class CommentRepository {
Expand Down Expand Up @@ -76,6 +78,21 @@ export class CommentRepository {
return new ResponseScrollCommentDto(entities, scrollMetaDto);
}

// 댓글 랜덤 조회
async getRandomComment(requestRandomCommentDto: RequestRandomCommentDto) {
const { take } = requestRandomCommentDto;
const queryBuilder = this.commentRepository.createQueryBuilder('comment');

queryBuilder
.orderBy('RANDOM()')
.limit(take);


const { entities } = await queryBuilder.getRawAndEntities();

return plainToInstance(ResponseRandomCommentDto, entities);
}

// 댓글 삭제
async deleteComment(id: number) {
const comment = await this.commentRepository.findOne({ where: { id: id } });
Expand Down
2 changes: 1 addition & 1 deletion src/database/repositories/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class UserRepository {
const queryBuilder = this.userRepository.createQueryBuilder('user');

queryBuilder
.orderBy('user.createdAt', pageOptionsDto.order)
.orderBy('user.id', pageOptionsDto.order)
.leftJoin('user.ticket', 'ticket')
.addSelect('ticket')
.skip(pageOptionsDto.skip)
Expand Down
8 changes: 8 additions & 0 deletions src/users/dtos/RandomComment.request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';
import { Expose } from 'class-transformer';

export class RequestRandomCommentDto {
@ApiProperty({ description: '가져올 댓글 개수', type: Number })
@Expose()
readonly take: number;
}
21 changes: 21 additions & 0 deletions src/users/dtos/RandomComment.response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ApiProperty } from '@nestjs/swagger';
import { Expose } from 'class-transformer';
import { User } from 'src/database/entities/user.entity';

export class ResponseRandomCommentDto {
@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;
}
23 changes: 23 additions & 0 deletions src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ import { CommentDto } from './dtos/Comment.dto';
import { ResponseScrollCommentsDto } from './dtos/Scroll/ScrollComments.response.dto';
import { SuccessResponse } from 'src/common/decorators/SuccessResponse.decorator';
import { ResponseUserTicketNumDto } from './dtos/UserTicketNum.response.dto';
<<<<<<< HEAD
import { RequestRandomCommentDto } from './dtos/RandomComment.request.dto';
import { ResponseRandomCommentDto } from './dtos/RandomComment.response.dto';

=======
>>>>>>> 9ce53e67e2e7be56a387372b6401fa587ac4d447

@ApiTags('users')
@ApiBearerAuth('accessToken')
Expand Down Expand Up @@ -149,6 +155,23 @@ export class UsersController {
return await this.userService.getAllComment(user.id, scrollOptionsDto);
}

// 댓글 랜덤 조회
@ApiOperation({ summary: '댓글 랜덤 조회' })
@SuccessResponse(HttpStatus.OK, [
{
model: ResponseRandomCommentDto,
exampleDescription:
'댓글 랜덤 조회 성공 시',
exampleTitle: '댓글 랜덤 조회',
}
])
@Get('/random/comment')
async getRandomComment(
@Query() requestRandomCommentDto: RequestRandomCommentDto
) {
return await this.userService.getRandomComment(requestRandomCommentDto);
}

// 응원 댓글 삭제(관리자용)
@ApiOperation({ summary: '[어드민] 응원 댓글 삭제' })
@SuccessResponse(HttpStatus.OK, [
Expand Down
6 changes: 6 additions & 0 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ResponseUserTicketNumDto } from './dtos/UserTicketNum.response.dto';
import { PageDto } from 'src/common/dtos/page/page.dto';
import { ScrollOptionsDto } from './dtos/Scroll/ScrollOptions.dto';
import { ResponseScrollCommentsDto } from './dtos/Scroll/ScrollComments.response.dto';
import { RequestRandomCommentDto } from './dtos/RandomComment.request.dto';

@Injectable()
export class UsersService {
Expand Down Expand Up @@ -80,6 +81,11 @@ export class UsersService {
return new ResponseScrollCommentsDto(final_comments, responseScrollCommentDto.meta);
}

// 댓글 랜덤 조회
async getRandomComment(requestRandomCommentDto: RequestRandomCommentDto) {
return await this.commentRepository.getRandomComment(requestRandomCommentDto);
}

// 댓글 삭제
async deleteComment(id: number) {
return await this.commentRepository.deleteComment(id);
Expand Down

0 comments on commit 6a3eebc

Please sign in to comment.