Skip to content

Commit

Permalink
feat: check matching, get blurting page by Q no
Browse files Browse the repository at this point in the history
  • Loading branch information
kyeahxx19 committed Nov 22, 2023
1 parent 4b27281 commit bab928e
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 9 deletions.
88 changes: 85 additions & 3 deletions src/blurting/blurting.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ import {
Req,
Res,
Body,
Param,
} from '@nestjs/common';
import { Request, Response } from 'express';
import { BlurtingService } from './blurting.service';
import { AuthGuard } from '@nestjs/passport';
import { JwtPayload } from 'src/interfaces/auth';
import { UserService } from 'src/user/user.service';
import { AnswerDto } from 'src/dtos/blurtingPage.dto';
import { AnswerDto, BlurtingPageDto } from 'src/dtos/blurtingPage.dto';
import {
ApiBadRequestResponse,
ApiConflictResponse,
ApiCreatedResponse,
ApiHeader,
ApiBody,
ApiOperation,
ApiResponse,
} from '@nestjs/swagger';

@Controller('blurting')
Expand All @@ -28,15 +33,92 @@ export class BlurtingController {

@UseGuards(AuthGuard('access'))
@Get()
@ApiHeader({
name: 'authorization',
required: true,
example: 'Bearer asdas.asdasd.asd',
})
@ApiOperation({
summary: '매칭 여부 확인',
description: '블러팅 탭 클릭 시 매칭 여부 반환',
})
@ApiCreatedResponse({
description: '매칭 완료 시 true, 매칭 중이거나 전일 시 false',
})
async getMatching(@Req() req: Request, @Res() res: Response) {
const { id } = req.user as JwtPayload;
const user = await this.userService.findUserByVal('id', id);
if (user.group == null || user.group == undefined) {
return res.send(false);
} else {
return res.send(true);
}
}

@UseGuards(AuthGuard('access'))
@Get('/latest')
@ApiHeader({
name: 'authorization',
required: true,
example: 'Bearer asdas.asdasd.asd',
})
@ApiOperation({
summary: '블러팅 최신 질문 탭',
description: '마지막 질문 관련 정보 및 답변 반환',
})
@ApiResponse({
description: '마지막 Q&A 정보 반환',
type: BlurtingPageDto,
})
async getBlurting(@Req() req: Request, @Res() res: Response) {
const { id } = req.user as JwtPayload;
const user = await this.userService.findUserByVal('id', id);
const blurtingPage = await this.blurtingService.getBlurting(user.group);
const blurtingPage = await this.blurtingService.getBlurting(user.group, 0);
return res.json(blurtingPage);
}

@UseGuards(AuthGuard('access'))
@Get('/:no')
@ApiHeader({
name: 'authorization',
required: true,
example: 'Bearer asdas.asdasd.asd',
})
@ApiOperation({
summary: '블러팅 최신 질문 탭',
description: '마지막 질문 관련 정보 및 답변 반환',
})
@ApiResponse({
description: '마지막 Q&A 정보 반환',
type: BlurtingPageDto,
})
async getBlurtingNo(
@Req() req: Request,
@Param() no: number,
@Res() res: Response,
) {
const { id } = req.user as JwtPayload;
const user = await this.userService.findUserByVal('id', id);
const blurtingPage = await this.blurtingService.getBlurting(user.group, no);
return res.json(blurtingPage);
}

@UseGuards(AuthGuard('access'))
@Post('/answer')
@ApiHeader({
name: 'authorization',
required: true,
example: 'Bearer asdas.asdasd.asd',
})
@ApiBody({
description: '블러팅 답변 정보 json',
type: AnswerDto,
})
@ApiOperation({
summary: '블러팅 답변 업로드',
description: '질문에 대한 답변 등록',
})
@ApiCreatedResponse({ description: '답변 업로드 성공' })
async postAnswer(
@Req() req: Request,
@Body() answerDto: AnswerDto,
Expand All @@ -45,7 +127,7 @@ export class BlurtingController {
const { id } = req.user as JwtPayload;
const { questionId, answer } = answerDto;
if (await this.blurtingService.postAnswer(id, questionId, answer)) {
res.sendStatus(200);
res.sendStatus(201);
}
}

Expand Down
28 changes: 22 additions & 6 deletions src/blurting/blurting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,28 @@ export class BlurtingService {
await this.questionRepository.save(newQuestion);
}

async getBlurting(group: BlurtingGroupEntity): Promise<BlurtingPageDto> {
const question = await this.questionRepository.findOne({
where: { group: group },
order: { no: 'DESC' },
relations: ['group'],
});
async getBlurting(
group: BlurtingGroupEntity,
no: number,
): Promise<BlurtingPageDto> {
let question;
if (no == 0) {
question = await this.questionRepository.findOne({
where: { group: group },
order: { no: 'DESC' },
relations: ['group'],
});
} else {
question = await this.questionRepository.findOne({
where: { group: group, no: no },
relations: ['group'],
});
}

if (!question) {
throw new BadRequestException('invalid question no');
}

const answers = await this.answerRepository.find({
where: { question: question },
order: { postedAt: 'ASC' },
Expand Down

0 comments on commit bab928e

Please sign in to comment.