Skip to content

Commit

Permalink
Merge pull request #245 from MOONSHOT-Team/feature/#244
Browse files Browse the repository at this point in the history
[Refactor] 예외 클래스 구조 개선
  • Loading branch information
its-sky authored Mar 8, 2024
2 parents 1b2aca6 + 460077b commit b4ed228
Show file tree
Hide file tree
Showing 55 changed files with 305 additions and 468 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import static org.moonshot.keyresult.service.validator.KeyResultValidator.validateKeyResultIndex;
import static org.moonshot.keyresult.service.validator.KeyResultValidator.validateKeyResultPeriod;
import static org.moonshot.log.service.validator.LogValidator.validateLogNum;
import static org.moonshot.response.ErrorType.NOT_FOUND_KEY_RESULT;
import static org.moonshot.response.ErrorType.NOT_FOUND_OBJECTIVE;
import static org.moonshot.response.ErrorType.REQUIRED_KEY_RESULT_VALUE;
import static org.moonshot.task.service.validator.TaskValidator.validateTaskIndex;
import static org.moonshot.user.service.validator.UserValidator.validateUserAuthorization;
import static org.moonshot.validator.IndexValidator.isIndexIncreased;
Expand All @@ -22,9 +25,8 @@
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.moonshot.common.model.Period;
import org.moonshot.exception.keyresult.KeyResultNotFoundException;
import org.moonshot.exception.keyresult.KeyResultRequiredException;
import org.moonshot.exception.objective.ObjectiveNotFoundException;
import org.moonshot.exception.BadRequestException;
import org.moonshot.exception.NotFoundException;
import org.moonshot.keyresult.dto.request.KeyResultCreateRequestDto;
import org.moonshot.keyresult.dto.request.KeyResultCreateRequestInfoDto;
import org.moonshot.keyresult.dto.request.KeyResultModifyRequestDto;
Expand Down Expand Up @@ -83,7 +85,7 @@ public void createInitKRWithObjective(final Objective objective, final List<KeyR

public void createKeyResult(final KeyResultCreateRequestDto request, final Long userId) {
Objective objective = objectiveRepository.findObjectiveAndUserById(request.objectiveId())
.orElseThrow(ObjectiveNotFoundException::new);
.orElseThrow(() -> new NotFoundException(NOT_FOUND_OBJECTIVE));
validateUserAuthorization(objective.getUser().getId(), userId);

List<KeyResult> krList = keyResultRepository.findAllByObjective(objective);
Expand All @@ -105,7 +107,7 @@ public void createKeyResult(final KeyResultCreateRequestDto request, final Long

public void deleteKeyResult(final Long keyResultId, final Long userId) {
KeyResult keyResult = keyResultRepository.findKeyResultAndObjective(keyResultId)
.orElseThrow(KeyResultNotFoundException::new);
.orElseThrow(() -> new NotFoundException(NOT_FOUND_KEY_RESULT));
validateUserAuthorization(keyResult.getObjective().getUser().getId(), userId);

logRepository.deleteAllInBatch(logRepository.findAllByKeyResult(keyResult));
Expand Down Expand Up @@ -135,7 +137,7 @@ public void deleteAllKeyResult(List<Objective> objectiveList) {

public Optional<AchieveResponseDto> modifyKeyResult(final KeyResultModifyRequestDto request, final Long userId) {
KeyResult keyResult = keyResultRepository.findKeyResultAndObjective(request.keyResultId())
.orElseThrow(KeyResultNotFoundException::new);
.orElseThrow(() -> new NotFoundException(NOT_FOUND_KEY_RESULT));
validateUserAuthorization(keyResult.getObjective().getUser().getId(), userId);

if (hasChange(request.title())) {
Expand All @@ -157,7 +159,7 @@ public Optional<AchieveResponseDto> modifyKeyResult(final KeyResultModifyRequest
}

if (request.target() == null || request.logContent() == null){
throw new KeyResultRequiredException();
throw new BadRequestException(REQUIRED_KEY_RESULT_VALUE);
}

Log updateLog = logService.createUpdateLog(request, keyResult.getId());
Expand All @@ -181,7 +183,7 @@ public Optional<AchieveResponseDto> modifyKeyResult(final KeyResultModifyRequest
@Override
public void modifyIdx(final ModifyIndexRequestDto request, final Long userId) {
KeyResult keyResult = keyResultRepository.findKeyResultAndObjective(request.id())
.orElseThrow(KeyResultNotFoundException::new);
.orElseThrow(() -> new NotFoundException(NOT_FOUND_KEY_RESULT));
validateUserAuthorization(keyResult.getObjective().getUser().getId(), userId);

Long krCount = keyResultRepository.countAllByObjectiveId(keyResult.getObjective().getId());
Expand All @@ -203,7 +205,7 @@ public void modifyIdx(final ModifyIndexRequestDto request, final Long userId) {
@Transactional(readOnly = true)
public KRDetailResponseDto getKRDetails(final Long userId, final Long keyResultId) {
KeyResult keyResult = keyResultRepository.findKeyResultAndObjective(keyResultId)
.orElseThrow(KeyResultNotFoundException::new);
.orElseThrow(() -> new NotFoundException(NOT_FOUND_KEY_RESULT));
validateUserAuthorization(keyResult.getObjective().getUser().getId(), userId);

List<Log> logList = logService.getLogList(keyResult);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.moonshot.keyresult.service.converter;

import org.moonshot.exception.global.common.MoonshotException;
import org.moonshot.exception.MoonshotException;
import org.moonshot.keyresult.model.KRState;
import org.moonshot.response.ErrorType;
import org.springframework.core.convert.converter.Converter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,53 @@
package org.moonshot.keyresult.service.validator;

import static org.moonshot.response.ErrorType.ACTIVE_KEY_RESULT_NUMBER_EXCEEDED;
import static org.moonshot.response.ErrorType.INVALID_KEY_RESULT_INDEX;
import static org.moonshot.response.ErrorType.INVALID_KEY_RESULT_PERIOD;

import java.time.LocalDate;
import java.util.List;
import org.moonshot.common.model.Period;
import org.moonshot.exception.keyresult.KeyResultInvalidIndexException;
import org.moonshot.exception.keyresult.KeyResultInvalidPeriodException;
import org.moonshot.exception.keyresult.KeyResultNumberExceededException;
import org.moonshot.exception.BadRequestException;

public class KeyResultValidator {

private static final int ACTIVE_KEY_RESULT_NUMBER = 3;

public static void validateKeyResultIndex(final int index, final int requestIndex) {
if (index != requestIndex) {
throw new KeyResultInvalidIndexException();
throw new BadRequestException(INVALID_KEY_RESULT_INDEX);
}
}

public static void validateKRPeriodWithInObjPeriod(final Period objPeriod, final LocalDate start, final LocalDate end) {
if (start.isBefore(objPeriod.getStartAt()) || end.isAfter(objPeriod.getExpireAt())
|| start.isAfter(objPeriod.getExpireAt()) || start.isAfter(end)) {
throw new KeyResultInvalidPeriodException();
throw new BadRequestException(INVALID_KEY_RESULT_PERIOD);
}
}

public static void validateActiveKRSizeExceeded(final int krListSize) {
if (krListSize >= ACTIVE_KEY_RESULT_NUMBER) {
throw new KeyResultNumberExceededException();
throw new BadRequestException(ACTIVE_KEY_RESULT_NUMBER_EXCEEDED);
}
}

public static void validateIndexUnderMaximum(final int requestIndex, final int totalKRListSize) {
if (requestIndex > totalKRListSize) {
throw new KeyResultInvalidIndexException();
throw new BadRequestException(INVALID_KEY_RESULT_INDEX);
}
}

public static void validateKeyResultPeriod(final Period objPeriod, final LocalDate start, final LocalDate end) {
if (start.isAfter(end) || start.isBefore(objPeriod.getStartAt()) || start.isAfter(objPeriod.getExpireAt())
|| end.isBefore(start) || end.isBefore(objPeriod.getStartAt()) || end.isAfter(objPeriod.getExpireAt())) {
throw new KeyResultInvalidPeriodException();
throw new BadRequestException(INVALID_KEY_RESULT_PERIOD);
}
}

public static void validateIndex(final Long keyResultCount, final Integer requestIndex) {
if (keyResultCount <= requestIndex || requestIndex < 0) {
throw new KeyResultInvalidIndexException();
throw new BadRequestException(INVALID_KEY_RESULT_INDEX);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package org.moonshot.log.service;

import static org.moonshot.keyresult.service.validator.KeyResultValidator.isKeyResultAchieved;
import static org.moonshot.log.service.validator.LogValidator.isCreateLog;
import static org.moonshot.log.service.validator.LogValidator.validateLogNum;
import static org.moonshot.response.ErrorType.NOT_FOUND_KEY_RESULT;
import static org.moonshot.user.service.validator.UserValidator.validateUserAuthorization;

import java.text.NumberFormat;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.moonshot.exception.keyresult.KeyResultNotFoundException;
import org.moonshot.exception.NotFoundException;
import org.moonshot.keyresult.dto.request.KeyResultCreateRequestDto;
import org.moonshot.keyresult.dto.request.KeyResultCreateRequestInfoDto;
import org.moonshot.keyresult.dto.request.KeyResultModifyRequestDto;
Expand All @@ -21,11 +27,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static org.moonshot.keyresult.service.validator.KeyResultValidator.isKeyResultAchieved;
import static org.moonshot.log.service.validator.LogValidator.isCreateLog;
import static org.moonshot.log.service.validator.LogValidator.validateLogNum;
import static org.moonshot.user.service.validator.UserValidator.validateUserAuthorization;


@Service
@Transactional
Expand All @@ -37,7 +38,7 @@ public class LogService {

public Optional<AchieveResponseDto> createRecordLog(final Long userId, final LogCreateRequestDto request) {
KeyResult keyResult = keyResultRepository.findKeyResultAndObjective(request.keyResultId())
.orElseThrow(KeyResultNotFoundException::new);
.orElseThrow(() -> new NotFoundException(NOT_FOUND_KEY_RESULT));
validateUserAuthorization(keyResult.getObjective().getUser().getId(), userId);
Optional<Log> prevLog = logRepository.findLatestLogByKeyResultId(LogState.RECORD, request.keyResultId());
long prevNum = -1;
Expand All @@ -63,7 +64,7 @@ public Optional<AchieveResponseDto> createRecordLog(final Long userId, final Log

public Log createUpdateLog(final KeyResultModifyRequestDto request, final Long keyResultId) {
KeyResult keyResult = keyResultRepository.findById(keyResultId)
.orElseThrow(KeyResultNotFoundException::new);
.orElseThrow(() -> new NotFoundException(NOT_FOUND_KEY_RESULT));
return logRepository.save(Log.builder()
.date(LocalDateTime.now())
.state(LogState.UPDATE)
Expand All @@ -76,7 +77,7 @@ public Log createUpdateLog(final KeyResultModifyRequestDto request, final Long k

public void createKRLog(final Object request, final Long keyResultId) {
KeyResult keyResult = keyResultRepository.findById(keyResultId)
.orElseThrow(KeyResultNotFoundException::new);
.orElseThrow(() -> new NotFoundException(NOT_FOUND_KEY_RESULT));

if (request instanceof KeyResultCreateRequestInfoDto) {
KeyResultCreateRequestInfoDto dto = (KeyResultCreateRequestInfoDto) request;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.moonshot.log.service.validator;

import org.moonshot.exception.log.InvalidLogValueException;
import org.moonshot.exception.BadRequestException;
import org.moonshot.log.model.LogState;
import org.moonshot.response.ErrorType;

public class LogValidator {

public static void validateLogNum(Long requestNum, Long originNum) {
if (requestNum.equals(originNum)) {
throw new InvalidLogValueException();
throw new BadRequestException(ErrorType.INVALID_LOG_VALUE);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.moonshot.objective.controller;

import jakarta.validation.Valid;
import java.security.Principal;
import lombok.RequiredArgsConstructor;
import org.moonshot.jwt.JwtTokenProvider;
import org.moonshot.objective.dto.request.ModifyIndexRequestDto;
import org.moonshot.objective.model.IndexService;
import org.moonshot.objective.service.IndexTargetProvider;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package org.moonshot.objective.service;

import static org.moonshot.objective.service.validator.ObjectiveValidator.*;
import static org.moonshot.user.service.validator.UserValidator.*;
import static org.moonshot.objective.service.validator.ObjectiveValidator.validateActiveObjectiveSizeExceeded;
import static org.moonshot.objective.service.validator.ObjectiveValidator.validateIndexWithInRange;
import static org.moonshot.response.ErrorType.INVALID_EXPIRE_AT;
import static org.moonshot.response.ErrorType.NOT_FOUND_OBJECTIVE;
import static org.moonshot.response.ErrorType.NOT_FOUND_USER;
import static org.moonshot.response.ErrorType.REQUIRED_EXPIRE_AT;
import static org.moonshot.user.service.validator.UserValidator.validateUserAuthorization;
import static org.moonshot.validator.IndexValidator.isIndexIncreased;
import static org.moonshot.validator.IndexValidator.isSameIndex;

Expand All @@ -13,18 +18,19 @@
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.moonshot.common.model.Period;
import org.moonshot.exception.objective.DateInputRequiredException;
import org.moonshot.exception.objective.InvalidExpiredAtException;
import org.moonshot.exception.objective.ObjectiveNotFoundException;
import org.moonshot.exception.user.UserNotFoundException;
import org.moonshot.exception.BadRequestException;
import org.moonshot.exception.NotFoundException;
import org.moonshot.keyresult.service.KeyResultService;
import org.moonshot.objective.dto.request.ModifyIndexRequestDto;
import org.moonshot.objective.dto.request.ModifyObjectiveRequestDto;
import org.moonshot.objective.dto.request.OKRCreateRequestDto;
import org.moonshot.objective.dto.response.DashboardResponseDto;
import org.moonshot.objective.dto.response.HistoryResponseDto;
import org.moonshot.objective.dto.response.ObjectiveGroupByYearDto;
import org.moonshot.objective.model.*;
import org.moonshot.objective.model.Category;
import org.moonshot.objective.model.Criteria;
import org.moonshot.objective.model.IndexService;
import org.moonshot.objective.model.Objective;
import org.moonshot.objective.repository.ObjectiveRepository;
import org.moonshot.user.model.User;
import org.moonshot.user.repository.UserRepository;
Expand All @@ -42,7 +48,7 @@ public class ObjectiveService implements IndexService {

public void createObjective(final Long userId, final OKRCreateRequestDto request) {
User user = userRepository.findById(userId)
.orElseThrow(UserNotFoundException::new);
.orElseThrow(() -> new NotFoundException(NOT_FOUND_USER));

List<Objective> objectives = objectiveRepository.findAllByUserId(userId);
validateActiveObjectiveSizeExceeded(objectives.size());
Expand All @@ -60,7 +66,7 @@ public void createObjective(final Long userId, final OKRCreateRequestDto request

public DashboardResponseDto deleteObjective(final Long userId, final Long objectiveId) {
Objective objective = objectiveRepository.findObjectiveAndUserById(objectiveId)
.orElseThrow(ObjectiveNotFoundException::new);
.orElseThrow(() -> new NotFoundException(NOT_FOUND_OBJECTIVE));
validateUserAuthorization(objective.getUser().getId(), userId);

keyResultService.deleteKeyResult(objective);
Expand All @@ -76,15 +82,15 @@ public void deleteAllObjective(final List<User> userList) {

public void modifyObjective(final Long userId, final ModifyObjectiveRequestDto request) {
Objective objective = objectiveRepository.findObjectiveAndUserById(request.objectiveId())
.orElseThrow(ObjectiveNotFoundException::new);
.orElseThrow(() -> new NotFoundException(NOT_FOUND_OBJECTIVE));
validateUserAuthorization(objective.getUser().getId(), userId);

objective.modifyClosed(request.isClosed());
if (!request.isClosed()) {
if (request.expireAt() == null) {
throw new DateInputRequiredException();
throw new BadRequestException(REQUIRED_EXPIRE_AT);
} else if (request.expireAt().isBefore(LocalDate.now())) {
throw new InvalidExpiredAtException();
throw new BadRequestException(INVALID_EXPIRE_AT);
}
objective.modifyPeriod(Period.of(objective.getPeriod().getStartAt(), request.expireAt()));
}
Expand All @@ -95,13 +101,13 @@ public DashboardResponseDto getObjectiveInDashboard(final Long userId, final Lon
List<Objective> objList = objectiveRepository.findAllByUserId(userId);
if (objList.isEmpty()) {
User user = userRepository.findById(userId)
.orElseThrow(UserNotFoundException::new);
.orElseThrow(() -> new NotFoundException(NOT_FOUND_USER));
return DashboardResponseDto.ofNull(user.getNickname());
}

Long treeId = objectiveId == null ? objList.get(0).getId() : objectiveId;
Objective objective = objectiveRepository.findByIdWithKeyResultsAndTasks(treeId)
.orElseThrow(ObjectiveNotFoundException::new);
.orElseThrow(() -> new NotFoundException(NOT_FOUND_OBJECTIVE));
validateUserAuthorization(objective.getUser().getId(), userId);

return DashboardResponseDto.of(objective, objList, objective.getUser().getNickname());
Expand Down Expand Up @@ -143,7 +149,7 @@ public void modifyIdx(final ModifyIndexRequestDto request, final Long userId) {
validateIndexWithInRange(objectiveCount, request.idx());

Objective objective = objectiveRepository.findObjectiveAndUserById(request.id())
.orElseThrow(ObjectiveNotFoundException::new);
.orElseThrow(() -> new NotFoundException(NOT_FOUND_OBJECTIVE));
validateUserAuthorization(objective.getUser().getId(), userId);

int prevIdx = objective.getIdx();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.moonshot.objective.service.converter;

import org.moonshot.exception.global.common.MoonshotException;
import org.moonshot.exception.MoonshotException;
import org.moonshot.objective.model.Category;
import org.moonshot.response.ErrorType;
import org.springframework.core.convert.converter.Converter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.moonshot.objective.service.converter;

import org.moonshot.exception.global.common.MoonshotException;
import org.moonshot.exception.MoonshotException;
import org.moonshot.objective.model.Criteria;
import org.moonshot.response.ErrorType;
import org.springframework.core.convert.converter.Converter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package org.moonshot.objective.service.validator;

import org.moonshot.exception.objective.ObjectiveInvalidIndexException;
import org.moonshot.exception.objective.ObjectiveNumberExceededException;
import static org.moonshot.response.ErrorType.ACTIVE_OBJECTIVE_NUMBER_EXCEEDED;
import static org.moonshot.response.ErrorType.INVALID_OBJECTIVE_INDEX;

import org.moonshot.exception.BadRequestException;

public class ObjectiveValidator {

private static final int ACTIVE_OBJECTIVE_NUMBER = 10;

public static void validateIndexWithInRange(final Long objectiveCount, final int idx) {
if ((objectiveCount <= idx) || (idx < 0)) {
throw new ObjectiveInvalidIndexException();
throw new BadRequestException(INVALID_OBJECTIVE_INDEX);
}
}

public static void validateActiveObjectiveSizeExceeded(final int objListSize) {
if (objListSize >= ACTIVE_OBJECTIVE_NUMBER) {
throw new ObjectiveNumberExceededException();
throw new BadRequestException(ACTIVE_OBJECTIVE_NUMBER_EXCEEDED);
}
}

Expand Down
Loading

0 comments on commit b4ed228

Please sign in to comment.