Skip to content

Commit

Permalink
🐛 Fix : 스스로 답변하기 예외처리
Browse files Browse the repository at this point in the history
  • Loading branch information
earthkingman authored and nyeoni committed Dec 16, 2021
1 parent b27816c commit 471d131
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
7 changes: 7 additions & 0 deletions backend/src/exception/answer_exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ export class AnswerNotFoundException extends HttpException {
constructor(id: number) {
super(404, `해당 답변글을 찾을 수 없습니다. Answer ${id} not found.`);
}
}


export class AnswerMySelfException extends HttpException {
constructor(id: number) {
super(400, `스스로 답변할 수 없습니다. ${id} bad request.`);
}
}
27 changes: 18 additions & 9 deletions backend/src/service/answer_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Question } from "../entity/question";
import { Answer } from "../entity/answer";
import { User } from "../entity/user";

import { AnswerNotFoundException, AnswerForbiddenException, AnswerBadRequestException } from "../exception/answer_exception";
import { AnswerNotFoundException, AnswerForbiddenException, AnswerMySelfException, AnswerBadRequestException } from "../exception/answer_exception";
import { UserNotFoundException } from "../exception/user_exception";
import { QuestionBadRequestException, QuestionForbiddenException, QuestionNotFoundException } from "../exception/question_exception";
import { DatabaseInternalServerErrorException } from "../exception/server_exception";
Expand All @@ -25,6 +25,15 @@ export class AnswerService {
async post(uploadAnswerInfo) {
const { email, text, questionId, userId } = uploadAnswerInfo;

//자신의 글은 자기가 답변할 수 없습니다.
const questionInfo = await this.questionRepository
.findOne({
where: { id: questionId },
relations: ['user']
});
if (questionInfo.user.id == userId)
throw new AnswerMySelfException(questionId);

const user = await this.userRepository
.findOne({ where: { id: userId } });
if (user === undefined) {
Expand Down Expand Up @@ -142,25 +151,25 @@ export class AnswerService {
}
}

async chooseAnswer(chooseAnswerInfo):Promise<any>{
const { userId, questionId, answerId, answerUserId} = chooseAnswerInfo;
async chooseAnswer(chooseAnswerInfo): Promise<any> {
const { userId, questionId, answerId, answerUserId } = chooseAnswerInfo;
const answer = await this.answerRepository
.findOne({
where: { id: answerId, user: { id: answerUserId }, question: { id: questionId } },
relations: ['user', 'question']
});
const question = await this.questionRepository
.findOne({
where: { id: questionId, user: {id: userId} },
.findOne({
where: { id: questionId, user: { id: userId } },
relations: ['user']
});
if (question === undefined){
if (question === undefined) {
const noAuthQuestion = await this.questionRepository
.findOne({ where: { id: questionId}});
if (noAuthQuestion === undefined){
.findOne({ where: { id: questionId } });
if (noAuthQuestion === undefined) {
throw new QuestionNotFoundException(questionId);
}
else{
else {
throw new QuestionForbiddenException(questionId);
}
}
Expand Down

0 comments on commit 471d131

Please sign in to comment.