Skip to content

Commit

Permalink
Merge pull request #81 from depromeet/dev
Browse files Browse the repository at this point in the history
🚀 feat(questions) : 채팅 아이디로 질문 가져오는 api 추가
  • Loading branch information
ImNM authored Jun 7, 2022
2 parents e2d1592 + 0915ef0 commit 8f5040d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/apis/questions/questions.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from '@nestjs/swagger';
import { JwtAuthGuard } from 'src/auth/guards/jwt.guard';
import { ReqUser } from 'src/common/decorators/user.decorator';
import { ChatIdDto } from 'src/common/dtos/ChatId.dto';
import { CommentIdDto } from 'src/common/dtos/CommentId.dto';
import { QuestionIdDto } from 'src/common/dtos/QuestionId.dto';
import { MongooseClassSerializerInterceptor } from 'src/common/interceptors/mongooseClassSerializer.interceptor';
Expand Down Expand Up @@ -75,6 +76,27 @@ export class QuestionsController {
// return '';
// }

@ApiOperation({
summary:
'찾고 싶은 질문을 챗 아이디로 찾습니다. 질문의 세부정보를 가져온다. 댓글 목록 포함',
})
@ApiResponse({
status: 200,
description: '요청 성공시',
type: QuestionShowDto,
})
@Get('byChatId/:chatId')
async findQuestionByChatId(
@Param() chatIdDto: ChatIdDto,
@ReqUser() user: User,
): Promise<any> {
return await this.questionService.findQuestionByChatId(
user.userIdDto,
chatIdDto,
user.blockedUserDto,
);
}

@ApiOperation({ summary: '질문의 세부정보를 가져온다. 댓글 목록 포함' })
@ApiResponse({
status: 200,
Expand Down
24 changes: 24 additions & 0 deletions src/apis/questions/questions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { plainToClass, plainToInstance } from 'class-transformer';
import { QUESTION_FIND_FILTER_TYPE } from 'src/common/consts/enum';
import { returnValueToDto } from 'src/common/decorators/returnValueToDto.decorator';
import { BlockedUserDto } from 'src/common/dtos/BlockedUserList.dto';
import { ChatIdDto } from 'src/common/dtos/ChatId.dto';
import { CommentIdDto } from 'src/common/dtos/CommentId.dto';
import { QuestionIdDto } from 'src/common/dtos/QuestionId.dto';
import { RoomIdDto } from 'src/common/dtos/RoomId.dto';
Expand Down Expand Up @@ -119,6 +120,29 @@ export class QuestionsService {
return question;
}

@returnValueToDto(QuestionShowDto)
async findQuestionByChatId(
userIdDto: UserIdDto,
chatIdDto: ChatIdDto,
blockUserListDto: BlockedUserDto,
) {
// 내 아이디 정보를 넣어서 비교로직 추가가 필요함.
await this.checkMyRoom(userIdDto);
const question = await this.questionRepository.getQuestionByChatId(
chatIdDto,
);
if (!question) {
throw new BadRequestException('질문 없음');
}

question.myUserId = userIdDto.userId;
question.commentList = this.filterRemoveBlockedUserFromCommentList(
question.commentList,
blockUserListDto,
);
return question;
}

// 삭제된 여부 똑같이 리턴해주장.
@returnValueToDto(QuestionShowDto)
async deleteQuestionByQuestionId(
Expand Down
4 changes: 2 additions & 2 deletions src/common/dtos/ChatId.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export class ChatIdDto {
}
@ApiProperty({
type: String,
title: ' 아이디',
description: '몽고아이디 형식입니다. (홍익대학교 룸 예시 )',
title: ' 아이디',
description: '찾고 싶은 질문을 챗 아이디로 찾습니다.',
example: '62596e8c4e22b2180fe2a902',
})
@MongoIdValidationTransfrom({ toClassOnly: true })
Expand Down
15 changes: 15 additions & 0 deletions src/repositories/question.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { UserProfileSelect } from 'src/common/dtos/UserProfile.dto';
import { CommentStringDto } from 'src/apis/questions/dto/CommentString.dto';
import { CommentIdDto } from 'src/common/dtos/CommentId.dto';
import { BlockedUserDto } from 'src/common/dtos/BlockedUserList.dto';
import { ChatIdDto } from 'src/common/dtos/ChatId.dto';

@Injectable()
export class QuestionRepository {
Expand Down Expand Up @@ -101,6 +102,20 @@ export class QuestionRepository {
.lean<Question>({ defaults: true });
}

async getQuestionByChatId(chatIdDto: ChatIdDto): Promise<Question | null> {
return await this.questionModel
.findOne({ chat: chatIdDto.chatId })
.populate({
path: 'user',
select: UserProfileSelect,
})
.populate({
path: 'commentList.user',
select: UserProfileSelect,
})
.lean<Question>({ defaults: true });
}

async deleteQuestionsByQuestionId(
questionIdDto: QuestionIdDto,
): Promise<Question> {
Expand Down

0 comments on commit 8f5040d

Please sign in to comment.