Skip to content

Commit

Permalink
[add] 기수 수상내역이 있을때 다시 추가시 에러처리 (#134)
Browse files Browse the repository at this point in the history
* [add] 기수 수상내역이 있을때 다시 추가시 에러처리 (#130)

* [chore] spotless 적용

* [docs] 주석 추가
  • Loading branch information
hyunihs authored Aug 7, 2023
1 parent 460ac82 commit 39d2bac
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ceos.backend.domain.awards.exception;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.CONFLICT;

import ceos.backend.global.common.dto.ErrorReason;
import ceos.backend.global.error.BaseErrorCode;
Expand All @@ -12,7 +13,8 @@
@AllArgsConstructor
public enum AwardsErrorCode implements BaseErrorCode {
AWARD_NOT_FOUND(BAD_REQUEST, "AWARD_404_1", "해당 수상이력이 존재하지 않습니다"),
START_DATE_NOT_FOUND(BAD_REQUEST, "AWARD_404_2", "해당 기수의 활동 시작 시기 정보가 존재하지 않습니다");
START_DATE_NOT_FOUND(BAD_REQUEST, "AWARD_404_2", "해당 기수의 활동 시작 시기 정보가 존재하지 않습니다"),
DUPLICATE_GENERATION(CONFLICT, "AWARD_409_1", "해당 기수의 데이터가 이미 존재합니다");

private HttpStatus status;
private String code;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ceos.backend.domain.awards.exception;


import ceos.backend.global.error.BaseErrorException;

public class DuplicateGeneration extends BaseErrorException {
public static final DuplicateGeneration EXCEPTION = new DuplicateGeneration();

private DuplicateGeneration() {
super(AwardsErrorCode.DUPLICATE_GENERATION);
}
}
12 changes: 12 additions & 0 deletions src/main/java/ceos/backend/domain/awards/helper/AwardsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@


import ceos.backend.domain.awards.domain.Awards;
import ceos.backend.domain.awards.domain.StartDate;
import ceos.backend.domain.awards.dto.response.AwardsResponse;
import ceos.backend.domain.awards.exception.DuplicateGeneration;
import ceos.backend.domain.awards.repository.AwardsRepository;
import ceos.backend.domain.awards.repository.StartDateRepository;
import ceos.backend.domain.awards.vo.ProjectInfoVo;
import ceos.backend.domain.project.domain.Project;
import ceos.backend.domain.project.repository.ProjectRepository;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

Expand All @@ -18,6 +22,7 @@ public class AwardsHelper {

private final ProjectRepository projectRepository;
private final AwardsRepository awardsRepository;
private final StartDateRepository startDateRepository;

public List<ProjectInfoVo> getProjectVo(int generation) {
List<Project> projectList = projectRepository.findByGeneration(generation);
Expand All @@ -39,4 +44,11 @@ public List<AwardsResponse> getAwardsDto(int generation) {
}
return awardsResponseList;
}

public void validateGeneration(int generation) {
Optional<StartDate> startDate = startDateRepository.findById(generation);
if (startDate.isPresent()) {
throw DuplicateGeneration.EXCEPTION;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class AwardsService {

@Transactional
public void createAwards(AwardsRequest awardsRequest) {
// 이미 기수의 수상 정보가 있다면 다시 추가할 수 없음
awardsHelper.validateGeneration(awardsRequest.getGeneration());

// 활동 시작 시기 저장
StartDate startDate = StartDate.from(awardsRequest);
startDateRepository.save(startDate);
Expand Down

0 comments on commit 39d2bac

Please sign in to comment.