Skip to content

Commit

Permalink
feat: 예외 enum 클래스 추가 (#135)
Browse files Browse the repository at this point in the history
* feat: exception enum 클래스 추가

* feat: controller advice 추가

* feat: member domain 권한 초기화
  • Loading branch information
simhani1 committed Aug 15, 2023
1 parent 295029a commit 0caa5f5
Show file tree
Hide file tree
Showing 30 changed files with 464 additions and 109 deletions.
32 changes: 8 additions & 24 deletions src/main/java/com/ohdab/classroom/domain/Classroom.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.ohdab.classroom.domain.classroomInfo.ClassroomInfo;
import com.ohdab.classroom.exception.NoStudentException;
import com.ohdab.core.baseentity.BaseEntity;
import com.ohdab.core.exception.ExceptionEnum;
import com.ohdab.member.domain.student.studentid.StudentId;
import com.ohdab.member.domain.teacher.teacherid.TeacherId;
import com.ohdab.workbook.domain.workbookid.WorkbookId;
import io.jsonwebtoken.lang.Assert;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
Expand Down Expand Up @@ -41,48 +43,30 @@ public class Classroom extends BaseEntity {

@Builder
public Classroom(ClassroomInfo classroomInfo, TeacherId teacher) {
setClassroomInfo(classroomInfo);
setTeacher(teacher);
}

private void setClassroomInfo(ClassroomInfo classroomInfo) {
if (classroomInfo == null) {
throw new IllegalStateException("ClassroomInfo cannot be null");
}
Assert.notNull(classroomInfo, ExceptionEnum.IS_NULL.getMessage());
Assert.notNull(teacher, ExceptionEnum.IS_NULL.getMessage());
this.classroomInfo = classroomInfo;
}

private void setTeacher(TeacherId teacher) {
if (teacher == null) {
throw new IllegalStateException("Teacher cannot be null");
}
this.teacher = teacher;
}

public void addStudent(StudentId student) {
if (student == null) {
throw new IllegalStateException("Student cannot be null.");
}
Assert.notNull(student, ExceptionEnum.IS_NULL.getMessage());
this.students.add(student);
}

public void addWorkbook(WorkbookId workbook) {
if (workbook == null) {
throw new IllegalStateException("Workbook cannot be null.");
}
Assert.notNull(workbook, ExceptionEnum.IS_NULL.getMessage());
this.workbooks.add(workbook);
}

public void deleteStudent(long studentId) {
if (!students.removeIf(student -> student.getId() == studentId)) {
throw new NoStudentException("교실에 존재하지 않는 학생입니다.");
throw new NoStudentException(ExceptionEnum.NO_STUDENT.getMessage());
}
}

public void updateClassroomInfo(ClassroomInfo classroomInfo) {
if (classroomInfo == null) {
throw new IllegalStateException("ClassroomInfo cannot be null");
}
Assert.notNull(classroomInfo, ExceptionEnum.IS_NULL.getMessage());
this.classroomInfo = classroomInfo;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.ohdab.classroom.repository.ClassroomRepository;
import com.ohdab.classroom.service.dto.ClassroomDto;
import com.ohdab.classroom.service.usecase.AddClassroomUsecase;
import com.ohdab.core.exception.ExceptionEnum;
import com.ohdab.member.domain.teacher.teacherid.TeacherId;
import com.ohdab.member.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -45,7 +46,7 @@ public void addClassroom(ClassroomDto.Request classroomReqDto) {

private void throwIfTeacherDoesNotExist(long teacherId) {
if (!memberRepository.existsById(teacherId)) {
throw new NoTeacherException("cannot find teacher by id : " + teacherId);
throw new NoTeacherException(ExceptionEnum.NO_TEACHER.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.ohdab.classroom.repository.ClassroomRepository;
import com.ohdab.classroom.service.mapper.ClassroomDetailServiceMapper;
import com.ohdab.classroom.service.usecase.ClassroomDetailUsecase;
import com.ohdab.core.exception.ExceptionEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -23,7 +24,10 @@ public ClassroomDetailDtoResponse getClassroomDetailById(long classroomId) {
Classroom classroom =
classroomRepository
.findById(classroomId)
.orElseThrow(() -> new NoClassroomException("교실이 존재하지 않습니다."));
.orElseThrow(
() ->
new NoClassroomException(
ExceptionEnum.NO_CLASSROOM.getMessage()));
return ClassroomDetailServiceMapper.classroomToClassroomDetail(classroom);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.ohdab.classroom.repository.ClassroomRepository;
import com.ohdab.classroom.service.mapper.ClassroomDetailServiceMapper;
import com.ohdab.classroom.service.usecase.FindClassroomDetailUsecase;
import com.ohdab.core.exception.ExceptionEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -23,7 +24,10 @@ public ClassroomDetailDtoResponse getClassroomDetailById(long classroomId) {
Classroom classroom =
classroomRepository
.findById(classroomId)
.orElseThrow(() -> new NoClassroomException("존재하지 않는 교실입니다."));
.orElseThrow(
() ->
new NoClassroomException(
ExceptionEnum.NO_CLASSROOM.getMessage()));
return ClassroomDetailServiceMapper.classroomToClassroomDetail(classroom);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.ohdab.classroom.repository.ClassroomRepository;
import com.ohdab.classroom.service.dto.ClassroomWorkbookDto;
import com.ohdab.classroom.service.usecase.GetWorkbookListUsecase;
import com.ohdab.core.exception.ExceptionEnum;
import com.ohdab.workbook.domain.Workbook;
import com.ohdab.workbook.repository.WorkbookRepository;
import java.util.List;
Expand All @@ -31,7 +32,7 @@ public List<ClassroomWorkbookDto.Response> getWorkbookListByClassroomId(long cla

private void throwIfUnknownClassroomId(long classroomId) {
if (!classroomRepository.existsById(classroomId)) {
throw new NoClassroomException("Cannot find classroom with id \"" + classroomId + "\"");
throw new NoClassroomException(ExceptionEnum.NO_CLASSROOM.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.ohdab.classroom.service.dto.ClassroomWorkbookUpdateDto.Request;
import com.ohdab.classroom.service.helper.ClassroomHelperService;
import com.ohdab.classroom.service.usecase.UpdateWorkbookInfoUsecase;
import com.ohdab.core.exception.ExceptionEnum;
import com.ohdab.workbook.domain.Workbook;
import com.ohdab.workbook.domain.workbookInfo.WorkbookInfo;
import com.ohdab.workbook.exception.NoWorkbookException;
Expand Down Expand Up @@ -30,7 +31,7 @@ public void updateWorkbookInfo(Request updateWorkbookReq) {

private void throwIfUnknownWorkbookId(long id) {
if (!workbookRepository.existsById(id)) {
throw new NoWorkbookException("No Workbook with id \"" + id + "\"");
throw new NoWorkbookException(ExceptionEnum.NO_WORKBOOK.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import com.ohdab.classroom.domain.Classroom;
import com.ohdab.classroom.domain.classroomInfo.Grade;
import com.ohdab.classroom.domain.classroomid.ClassroomId;
import com.ohdab.classroom.exception.DuplicatedWorkbookException;
import com.ohdab.classroom.exception.NoClassroomException;
import com.ohdab.classroom.exception.NoGradeException;
import com.ohdab.classroom.repository.ClassroomRepository;
import com.ohdab.core.exception.ExceptionEnum;
import com.ohdab.workbook.exception.DuplicatedWorkbookException;
import com.ohdab.workbook.repository.WorkbookRepository;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand All @@ -18,7 +19,7 @@ public static Grade findGradeByString(String stringGrade) {
try {
return Grade.valueOfLabel(stringGrade);
} catch (NullPointerException e) {
throw new NoGradeException("Cannot find Grade : " + stringGrade, e);
throw new NoGradeException(ExceptionEnum.NO_GRADE.getMessage());
} catch (ClassCastException e) {
throw new IllegalArgumentException("인자의 자료형이 잘못되었습니다.");
}
Expand All @@ -29,20 +30,13 @@ public static Classroom findExistingClassroom(
return classroomRepository
.findById(classroomId)
.orElseThrow(
() ->
new NoClassroomException(
"반을 찾을 수 없습니다. classroomId: " + classroomId));
() -> new NoClassroomException(ExceptionEnum.NO_CLASSROOM.getMessage()));
}

public static void throwIfDuplicatedWorkbookName(
WorkbookRepository workbookRepository, ClassroomId classroomId, String name) {
if (workbookRepository.existsByClassroomIdAndWorkbookInfoName(classroomId, name)) {
throw new DuplicatedWorkbookException(
"Duplicated workbook name \""
+ name
+ "\" in classroom with id \""
+ classroomId
+ "\"");
throw new DuplicatedWorkbookException(ExceptionEnum.DUPLICATED_WORKBOOK.getMessage());
}
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/ohdab/core/exception/ExceptionEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.ohdab.core.exception;

import lombok.Getter;

@Getter
public enum ExceptionEnum {

// 600 - classroom
NO_CLASSROOM(600, "존재하지 않는 교실입니다."),
NO_GRADE(601, "존재하지 않는 학년입니다."),
NO_STUDENT(602, "존재하지 않는 학생입니다."),
NO_TEACHER(603, "존재하지 않는 선생님입니다."),

// 700 - member
ALREADY_WITHDRAWL(700, "이미 탈퇴한 계정입니다."),
DUPLICATED_MEMBER(701, "이미 존재하는 회원입니다."),
NO_MEMBER(702, "존재하지 않는 회원입니다."),
MEMBER_CONTENT_OVERFLOW(703, "최대 글자수를 초과했습니다."),
NO_AUTHORITY(704, "권한정보가 누락되었습니다."),

// 800 - mistakeNote
NO_MISTAKE_NOTE(800, "존재하지 않는 오답노트입니다."),
NO_NUMBERS_WRONG_N_TIMES(801, ""),
NUMBER_IS_OUT_OF_RANGE(802, "N번 이상 틀린 문제가 없습니다."),
MISTAKE_NUMBERS_SIZE(803, "기록 가능한 문제수를 초과했습니다."),

// 900 - workbook
WORKBOOK_CONTENT_OVERFLOW(900, "최대 글자수를 초과했습니다."),
DUPLICATED_WORKBOOK(901, "이미 존재하는 교재입니다."),
INVALID_WORKBOOK_NUMBER_RANGE(902, "허용 범위를 벗어난 문제 번호입니다."),
NO_WORKBOOK(903, "존재하지 않는 교재입니다."),

// 1000 - null
IS_NULL(1000, "필수 입력값이 누락되었습니다."),
;

private int errorCode;
private String message;

ExceptionEnum(int errorCode, String message) {
this.errorCode = errorCode;
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.ohdab.core.exception.handler;

import com.ohdab.classroom.exception.NoClassroomException;
import com.ohdab.classroom.exception.NoGradeException;
import com.ohdab.classroom.exception.NoStudentException;
import com.ohdab.classroom.exception.NoTeacherException;
import com.ohdab.core.exception.ExceptionEnum;
import com.ohdab.core.template.ErrorMessage;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class ClassroomExceptionHandler {

@ExceptionHandler
public ResponseEntity<ErrorMessage> noClassroomException(NoClassroomException e) {
return ResponseEntity.badRequest()
.body(
ErrorMessage.builder()
.errorCode(ExceptionEnum.NO_CLASSROOM.getErrorCode())
.message(e.getMessage())
.build());
}

@ExceptionHandler
public ResponseEntity<ErrorMessage> noGradeException(NoGradeException e) {
return ResponseEntity.badRequest()
.body(
ErrorMessage.builder()
.errorCode(ExceptionEnum.NO_GRADE.getErrorCode())
.message(e.getMessage())
.build());
}

@ExceptionHandler
public ResponseEntity<ErrorMessage> noStudentException(NoStudentException e) {
return ResponseEntity.badRequest()
.body(
ErrorMessage.builder()
.errorCode(ExceptionEnum.NO_STUDENT.getErrorCode())
.message(e.getMessage())
.build());
}

@ExceptionHandler
public ResponseEntity<ErrorMessage> noTeacherException(NoTeacherException e) {
return ResponseEntity.badRequest()
.body(
ErrorMessage.builder()
.errorCode(ExceptionEnum.NO_TEACHER.getErrorCode())
.message(e.getMessage())
.build());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.ohdab.core.exception.handler;

import com.ohdab.core.exception.ExceptionEnum;
import com.ohdab.core.template.ErrorMessage;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class CommonExceptionHandler {

@ExceptionHandler
public ResponseEntity<ErrorMessage> illegalArgumentException(IllegalArgumentException e) {
return ResponseEntity.badRequest()
.body(
ErrorMessage.builder()
.errorCode(ExceptionEnum.IS_NULL.getErrorCode())
.message(e.getMessage())
.build());
}
}
Loading

0 comments on commit 0caa5f5

Please sign in to comment.