Skip to content

Commit

Permalink
답변 같기 가져오기
Browse files Browse the repository at this point in the history
  • Loading branch information
overthestream committed Jan 27, 2024
1 parent 28e9dbd commit 74bb315
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/blurting/blurting.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ export class BlurtingController {
description: '마지막 Q&A 정보 반환',
type: BlurtingPageDto,
})
async getBlurting(@Req() req: Request, @Res() res: Response) {
async getBlurting(@Req() req: Request) {
const { id } = req.user as JwtPayload;
const user = await this.userService.findUserByVal('id', id);
const blurtingPage = await this.blurtingService.getBlurting(
id,
user.group,
0,
);
return res.json(blurtingPage);
return blurtingPage;
}

@UseGuards(AuthGuard('access'))
Expand Down
2 changes: 1 addition & 1 deletion src/blurting/blurting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export class BlurtingService {
const answers = await this.answerRepository.find({
where: { question: question },
order: { postedAt: 'ASC' },
relations: ['question', 'user'],
relations: ['question', 'user', 'reply', 'reply.user'],
});

const answersDto = await Promise.all(
Expand Down
23 changes: 23 additions & 0 deletions src/dtos/blurtingPage.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,30 @@ import {
BlurtingAnswerEntity,
BlurtingGroupEntity,
BlurtingQuestionEntity,
ReplyEntity,
UserEntity,
} from 'src/entities';
import { ApiProperty } from '@nestjs/swagger';
import { Mbti, Sex } from 'src/common/enums';

export class ReplyDto {
@ApiProperty({ description: '답글 작성 유저 아이디' })
writerUserId: number;
@ApiProperty({ description: '답글 작성 유저 닉네임' })
writerUserName: string;
@ApiProperty({ description: '답글 내용' })
content: string;
@ApiProperty({ description: '답글 작성 시간 ( 한국 시간 기준 )' })
createdAt: Date;
static toDto(entity: ReplyEntity): ReplyDto {
return {
writerUserId: entity.user.id,
writerUserName: entity.user.userNickname,
content: entity.content,
createdAt: new Date(entity.createdAt.getTime() + 1000 * 60 * 60 * 9),
};
}
}
export class BlurtingAnswerDto {
@IsNumber()
@ApiProperty({ description: 'userId' })
Expand Down Expand Up @@ -54,6 +73,9 @@ export class BlurtingAnswerDto {
@ApiProperty({ description: '답변 ID' })
id: number;

@ApiProperty({ description: '답글들', type: ReplyDto, isArray: true })
reply: ReplyDto[];

static ToDto(
answerEntity: BlurtingAnswerEntity,
room: string,
Expand All @@ -72,6 +94,7 @@ export class BlurtingAnswerDto {
room: room ?? null,
likes,
ilike,
reply: answerEntity.reply.map((e) => ReplyDto.toDto(e)),
};
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/entities/blurtingAnswer.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import { BlurtingQuestionEntity, UserEntity } from '../entities';
import {
Column,
Entity,
ManyToOne,
OneToMany,
PrimaryGeneratedColumn,
} from 'typeorm';
import { BlurtingQuestionEntity, ReplyEntity, UserEntity } from '../entities';
import { Sex } from 'src/common/enums';

@Entity()
Expand Down Expand Up @@ -28,4 +34,7 @@ export class BlurtingAnswerEntity {

@Column({ default: 0 })
allLikes: number;

@OneToMany(() => ReplyEntity, (reply) => reply.answer)
reply: ReplyEntity[];
}

0 comments on commit 74bb315

Please sign in to comment.