From 6fe6bb382cd6becb4337c5cc7120ba6353a6491b Mon Sep 17 00:00:00 2001 From: hyeonda02 Date: Wed, 31 Jul 2024 05:04:03 +0900 Subject: [PATCH] =?UTF-8?q?Refactor=20:=20Career=20=EC=97=94=ED=8B=B0?= =?UTF-8?q?=ED=8B=B0=EC=97=90=20unknown=20=ED=95=84=EB=93=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95,=20=EC=8A=A4=EC=BC=80=EC=A4=84=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kkijuk/server/career/domain/Career.java | 12 +++++--- .../server/career/dto/CareerRequestDto.java | 11 ++++---- .../server/career/dto/CareerResponseDto.java | 2 +- .../career/dto/converter/CareerConverter.java | 8 +++--- .../career/repository/CareerRepository.java | 3 ++ .../career/scheduler/CareerScheduler.java | 27 ++++++++++++++++++ .../career/service/CareerServiceImpl.java | 16 +++++------ .../career/validation/PeriodValidator.java | 2 +- .../career/service/CareerServiceTest.java | 28 ++++++------------- .../career/service/CareerServiceUnitTest.java | 6 ++-- .../career/validator/PeriodValidatorTest.java | 6 ++-- 11 files changed, 73 insertions(+), 48 deletions(-) create mode 100644 src/main/java/umc/kkijuk/server/career/scheduler/CareerScheduler.java diff --git a/src/main/java/umc/kkijuk/server/career/domain/Career.java b/src/main/java/umc/kkijuk/server/career/domain/Career.java index d093958f..b54a95a7 100644 --- a/src/main/java/umc/kkijuk/server/career/domain/Career.java +++ b/src/main/java/umc/kkijuk/server/career/domain/Career.java @@ -4,6 +4,7 @@ import lombok.*; import umc.kkijuk.server.career.domain.base.BaseEntity; import umc.kkijuk.server.careerdetail.domain.CareerDetail; +import umc.kkijuk.server.member.domain.Member; import java.time.LocalDate; import java.util.ArrayList; @@ -25,8 +26,8 @@ public class Career extends BaseEntity { @Column(name="career_alias", length = 20) private String alias; - @Column(name="career_current") - private Boolean current; + @Column(name="career_unknown") + private Boolean unknown; @Column(name="career_summary",length = 50) private String summary; @@ -49,6 +50,9 @@ public class Career extends BaseEntity { private List careerDetailList = new ArrayList<>(); + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "member_id") + private Member member; public void setName(String name) { this.name = name; @@ -58,8 +62,8 @@ public void setAlias(String alias) { this.alias = alias; } - public void setCurrent(Boolean current) { - this.current = current; + public void setUnknown(Boolean unknown) { + this.unknown = unknown; } public void setSummary(String summary) { diff --git a/src/main/java/umc/kkijuk/server/career/dto/CareerRequestDto.java b/src/main/java/umc/kkijuk/server/career/dto/CareerRequestDto.java index 1b019c42..e9daf5ac 100644 --- a/src/main/java/umc/kkijuk/server/career/dto/CareerRequestDto.java +++ b/src/main/java/umc/kkijuk/server/career/dto/CareerRequestDto.java @@ -32,11 +32,12 @@ public static class CreateCareerDto{ @Schema(description = "활동 내역", example = "주요 활동 내용을 요약하여 작성해주세요. 최대 50자 까지 입력 가능 선택사항입니다.", type = "string") String summary; - @NotNull(message = "활동 여부는 필수 선택 항목입니다.") - @Schema(description = "활동 여부", example = "false", type = "boolean") - Boolean isCurrent; + @NotNull(message = "활동 기간을 알고 있는지 여부를 나타냅니다.") + @Schema(description = "활동 기간 인지 여부", example = "false", type = "boolean") + Boolean isUnknown; + @NotNull(message = "활동 시작 날짜는 필수 입력 항목입니다.") @JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul") @Schema(description = "활동 시작 날짜", example = "2024-04-14", type="string") LocalDate startDate; @@ -64,8 +65,8 @@ public static class UpdateCareerDto{ @Size(max = 50) @Schema(description = "수정된 활동 내역", example = "활동 내역 최대 50자까지 입력 가능(선택사항입니다.)",type = "string") String summary; - @Schema(description = "수정된 활동 여부", example = "false", type = "boolean") - Boolean isCurrent; + @Schema(description = "수정된 활동 기간 인지 여부", example = "false", type = "boolean") + Boolean isUnknown; @JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul") @Schema(description = "수정된 활동 시작 날짜", example = "2024-04-14", type="string") LocalDate startDate; diff --git a/src/main/java/umc/kkijuk/server/career/dto/CareerResponseDto.java b/src/main/java/umc/kkijuk/server/career/dto/CareerResponseDto.java index 8ce11aa9..96f0bd87 100644 --- a/src/main/java/umc/kkijuk/server/career/dto/CareerResponseDto.java +++ b/src/main/java/umc/kkijuk/server/career/dto/CareerResponseDto.java @@ -30,7 +30,7 @@ public static class CareerDto{ String careerName; String alias; String summary; - Boolean isCurrent; + Boolean isUnknown; LocalDate startDate; LocalDate endDate; int year; diff --git a/src/main/java/umc/kkijuk/server/career/dto/converter/CareerConverter.java b/src/main/java/umc/kkijuk/server/career/dto/converter/CareerConverter.java index a0d35e76..9069b353 100644 --- a/src/main/java/umc/kkijuk/server/career/dto/converter/CareerConverter.java +++ b/src/main/java/umc/kkijuk/server/career/dto/converter/CareerConverter.java @@ -17,7 +17,7 @@ public static Career toCareer(CareerRequestDto.CreateCareerDto request){ .summary(request.getSummary()) .startdate(request.getStartDate()) .enddate(request.getEndDate()) - .current(request.getIsCurrent()) + .unknown(request.getIsUnknown()) .enddate(request.getEndDate()) .build(); } @@ -32,7 +32,7 @@ public static CareerResponseDto.CareerDto toCareerDto(Career career) { .careerName(career.getName()) .alias(career.getAlias()) .summary(career.getSummary()) - .isCurrent(career.getCurrent()) + .isUnknown(career.getUnknown()) .startDate(career.getStartdate()) .endDate(career.getEnddate()) .year(career.getYear()) @@ -55,7 +55,7 @@ public static List toCareerGrouped .year(career.getYear()) .startDate(career.getStartdate()) .endDate(career.getEnddate()) - .isCurrent(career.getCurrent()) + .isUnknown(career.getUnknown()) .categoryId(Math.toIntExact(career.getCategory().getId())) .categoryName(career.getCategory().getName()) .build()) @@ -78,7 +78,7 @@ public static List toCareerGroupedByYe .year(career.getYear()) .startDate(career.getStartdate()) .endDate(career.getEnddate()) - .isCurrent(career.getCurrent()) + .isUnknown(career.getUnknown()) .categoryId(Math.toIntExact(career.getCategory().getId())) .categoryName(career.getCategory().getName()) .build()) diff --git a/src/main/java/umc/kkijuk/server/career/repository/CareerRepository.java b/src/main/java/umc/kkijuk/server/career/repository/CareerRepository.java index 40dc69b7..02ec9f52 100644 --- a/src/main/java/umc/kkijuk/server/career/repository/CareerRepository.java +++ b/src/main/java/umc/kkijuk/server/career/repository/CareerRepository.java @@ -3,6 +3,9 @@ import org.springframework.data.jpa.repository.JpaRepository; import umc.kkijuk.server.career.domain.Career; +import java.util.List; + public interface CareerRepository extends JpaRepository { + List findAllByUnknown(boolean isUnknown); } diff --git a/src/main/java/umc/kkijuk/server/career/scheduler/CareerScheduler.java b/src/main/java/umc/kkijuk/server/career/scheduler/CareerScheduler.java new file mode 100644 index 00000000..4f66010e --- /dev/null +++ b/src/main/java/umc/kkijuk/server/career/scheduler/CareerScheduler.java @@ -0,0 +1,27 @@ +package umc.kkijuk.server.career.scheduler; + +import lombok.RequiredArgsConstructor; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import umc.kkijuk.server.career.domain.Career; +import umc.kkijuk.server.career.repository.CareerRepository; + +import java.time.LocalDate; +import java.util.List; + +@Component +@RequiredArgsConstructor +public class CareerScheduler { + private final CareerRepository careerRepository; + @Scheduled(cron = "0 * * * * ?") + public void updateUnkownEndDates() { + List careers = careerRepository.findAllByUnknown(true); + LocalDate now = LocalDate.now(); + for (Career career : careers) { + career.setEnddate(now); + careerRepository.save(career); + } + + } + +} diff --git a/src/main/java/umc/kkijuk/server/career/service/CareerServiceImpl.java b/src/main/java/umc/kkijuk/server/career/service/CareerServiceImpl.java index 2860da17..dbaf0e24 100644 --- a/src/main/java/umc/kkijuk/server/career/service/CareerServiceImpl.java +++ b/src/main/java/umc/kkijuk/server/career/service/CareerServiceImpl.java @@ -30,7 +30,7 @@ public class CareerServiceImpl implements CareerService { @Transactional public Career createCareer(CareerRequestDto.CreateCareerDto request) { Career career = CareerConverter.toCareer(request); - if(career.getCurrent()){ + if(career.getUnknown()){ career.setEnddate(LocalDate.now()); career.setYear(LocalDate.now().getYear()); } @@ -59,8 +59,8 @@ public Career updateCareer(Long careerId, CareerRequestDto.UpdateCareerDto reque if (request.getSummary()!=null) { career.setSummary(request.getSummary()); } - if (request.getIsCurrent()!=null || request.getEndDate()!=null ) { - updateEndDateAndCurrentStatus(career,request.getIsCurrent(),request.getEndDate()); + if (request.getIsUnknown()!=null || request.getEndDate()!=null ) { + updateEndDateAndUnknownStatus(career,request.getIsUnknown(),request.getEndDate()); validatedPeriod(career); } if (request.getStartDate()!=null) { @@ -101,10 +101,10 @@ public Optional findCareer(Long careerId) { - private void updateEndDateAndCurrentStatus(Career career, Boolean isCurrent, LocalDate endDate) { - if (isCurrent != null) { - career.setCurrent(isCurrent); - if (isCurrent) { + private void updateEndDateAndUnknownStatus(Career career, Boolean isUnknown, LocalDate endDate) { + if (isUnknown != null) { + career.setUnknown(isUnknown); + if (isUnknown) { career.setEnddate(LocalDate.now()); career.setYear(LocalDate.now().getYear()); } else { @@ -136,7 +136,7 @@ private void validatedPeriod(Career career) { } private int parsingYear(CareerRequestDto.CreateCareerDto request){ - if(!request.getIsCurrent()){ + if(!request.getIsUnknown()){ return request.getEndDate().getYear(); } return LocalDate.now().getYear(); diff --git a/src/main/java/umc/kkijuk/server/career/validation/PeriodValidator.java b/src/main/java/umc/kkijuk/server/career/validation/PeriodValidator.java index 3b2b5a6b..6ab5ca79 100644 --- a/src/main/java/umc/kkijuk/server/career/validation/PeriodValidator.java +++ b/src/main/java/umc/kkijuk/server/career/validation/PeriodValidator.java @@ -16,7 +16,7 @@ public void initialize(ValidPeriod constraintAnnotation) { } @Override public boolean isValid(CareerRequestDto.CreateCareerDto request, ConstraintValidatorContext context) { - if(request.getIsCurrent() == false){ + if(request.getIsUnknown() == false){ if(request.getEndDate()==null){ context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate(CareerResponseMessage.CAREER_ENDDATE) diff --git a/src/test/java/umc/kkijuk/server/career/service/CareerServiceTest.java b/src/test/java/umc/kkijuk/server/career/service/CareerServiceTest.java index e357aba3..287405af 100644 --- a/src/test/java/umc/kkijuk/server/career/service/CareerServiceTest.java +++ b/src/test/java/umc/kkijuk/server/career/service/CareerServiceTest.java @@ -1,20 +1,14 @@ package umc.kkijuk.server.career.service; -import com.fasterxml.jackson.databind.exc.InvalidFormatException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.dao.DataIntegrityViolationException; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; -import umc.kkijuk.server.common.controller.ExceptionControllerAdvice; import umc.kkijuk.server.common.domian.exception.CareerValidationException; import umc.kkijuk.server.career.controller.response.CareerGroupedByResponse; -import umc.kkijuk.server.career.controller.response.CareerResponse; -import umc.kkijuk.server.career.controller.response.CareerResponseMessage; import umc.kkijuk.server.career.domain.Career; import umc.kkijuk.server.career.domain.Category; import umc.kkijuk.server.career.dto.CareerRequestDto; @@ -22,7 +16,6 @@ import umc.kkijuk.server.career.repository.CareerRepository; import umc.kkijuk.server.career.repository.CategoryRepository; import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException; -import umc.kkijuk.server.common.domian.response.ErrorResponse; import java.time.LocalDate; import java.util.List; @@ -30,7 +23,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; @SpringBootTest @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) @@ -41,8 +33,6 @@ public class CareerServiceTest { private CategoryRepository categoryRepository; @Autowired private CareerRepository careerRepository; - @Autowired - private ExceptionControllerAdvice exceptionControllerAdvice; private Career career1; private Career career2; private Category category1; @@ -66,7 +56,7 @@ void init() { .name("test1") .alias("alias1") .summary("summary1") - .current(false) + .unknown(false) .startdate(LocalDate.of(2024, 4, 10)) .enddate(LocalDate.of(2024, 7, 20)) .year(2024) @@ -77,7 +67,7 @@ void init() { .name("test2") .alias("alias2") .summary("summary2") - .current(false) + .unknown(false) .startdate(LocalDate.of(2024, 4, 10)) .enddate(LocalDate.of(2024, 7, 20)) .year(2024) @@ -94,7 +84,7 @@ void init() { CareerRequestDto.CreateCareerDto careerCreateDto = CareerRequestDto.CreateCareerDto.builder() .careerName("test3") .alias("alias3") - .isCurrent(false) + .isUnknown(false) .summary("summary3") .startDate(LocalDate.of(2024, 4, 10)) .endDate(LocalDate.of(2024, 7, 20)) @@ -126,7 +116,7 @@ void init() { () -> assertThat(updateCareer.getName()).isEqualTo("test2"), () -> assertThat(updateCareer.getAlias()).isEqualTo("alias2"), () -> assertThat(updateCareer.getSummary()).isEqualTo("summary2"), - () -> assertThat(updateCareer.getCurrent()).isEqualTo(false), + () -> assertThat(updateCareer.getUnknown()).isEqualTo(false), () -> assertThat(updateCareer.getStartdate()).isEqualTo(LocalDate.of(2024, 4, 10)), () -> assertThat(updateCareer.getEnddate()).isEqualTo(LocalDate.of(2024, 7, 20)), () -> assertThat(updateCareer.getYear()).isEqualTo(2024), @@ -140,7 +130,7 @@ void init() { .careerName("update test") .summary("update summary") .alias("update alias") - .isCurrent(true) + .isUnknown(true) .category(2) .startDate(LocalDate.of(2021,01,01)) .build(); @@ -153,7 +143,7 @@ void init() { () -> assertThat(updateCareer.getName()).isEqualTo("update test"), () -> assertThat(updateCareer.getSummary()).isEqualTo("update summary"), () -> assertThat(updateCareer.getAlias()).isEqualTo("update alias"), - () -> assertThat(updateCareer.getCurrent()).isEqualTo(true), + () -> assertThat(updateCareer.getUnknown()).isEqualTo(true), () -> assertThat(updateCareer.getStartdate()).isEqualTo(LocalDate.of(2021,01,01)), () -> assertThat(updateCareer.getEnddate()).isEqualTo(LocalDate.now()), () -> assertThat(updateCareer.getCategory().getId()).isEqualTo(2L), @@ -168,7 +158,7 @@ void init() { .careerName("update test") .summary("update summary") .alias("update alias") - .isCurrent(true) + .isUnknown(true) .startDate(LocalDate.of(2021,01,01)) .category(2) .build(); @@ -183,7 +173,7 @@ void init() { .careerName("update test") .summary("update summary") .alias("update alias") - .isCurrent(true) + .isUnknown(true) .startDate(LocalDate.of(2024543,01,01)) .category(Math.toIntExact(category2.getId())) .build(); @@ -200,7 +190,7 @@ void init() { .careerName("update test") .summary("update summary") .alias("update alias") - .isCurrent(false) + .isUnknown(false) .startDate(LocalDate.of(2024,01,01)) .endDate(LocalDate.of(2023,01,01)) .category(Math.toIntExact(category2.getId())) diff --git a/src/test/java/umc/kkijuk/server/career/service/CareerServiceUnitTest.java b/src/test/java/umc/kkijuk/server/career/service/CareerServiceUnitTest.java index b25fc4b6..8c8f296c 100644 --- a/src/test/java/umc/kkijuk/server/career/service/CareerServiceUnitTest.java +++ b/src/test/java/umc/kkijuk/server/career/service/CareerServiceUnitTest.java @@ -51,7 +51,7 @@ void init() { .name("test") .alias("alias") .summary("summary") - .current(false) + .unknown(false) .category(category1) .startdate(LocalDate.of(2024, 4, 10)) .enddate(LocalDate.of(2024, 7, 20)) @@ -63,7 +63,7 @@ void init() { .name("test2") .alias("alias2") .summary("summary2") - .current(false) + .unknown(false) .category(category2) .startdate(LocalDate.of(2024, 4, 10)) .enddate(LocalDate.of(2024, 7, 20)) @@ -109,7 +109,7 @@ void init() { .careerName("update test") .summary("update summary") .alias("update alias") - .isCurrent(true) + .isUnknown(true) .startDate(LocalDate.of(2021, 1, 1)) .category(1) .build(); diff --git a/src/test/java/umc/kkijuk/server/career/validator/PeriodValidatorTest.java b/src/test/java/umc/kkijuk/server/career/validator/PeriodValidatorTest.java index 80051a79..221e6584 100644 --- a/src/test/java/umc/kkijuk/server/career/validator/PeriodValidatorTest.java +++ b/src/test/java/umc/kkijuk/server/career/validator/PeriodValidatorTest.java @@ -32,7 +32,7 @@ void init(){ CareerRequestDto.CreateCareerDto careerDto = CareerRequestDto.CreateCareerDto.builder() .careerName("test") .alias("alias") - .isCurrent(false) + .isUnknown(false) .startDate(LocalDate.of(2024,5,10)) .endDate(LocalDate.of(2024,7,10)) .category(1) @@ -54,7 +54,7 @@ void init(){ CareerRequestDto.CreateCareerDto careerDto = CareerRequestDto.CreateCareerDto.builder() .careerName("test") .alias("alias") - .isCurrent(false) + .isUnknown(false) .startDate(LocalDate.of(2024,7,10)) .endDate(null) .category(1) @@ -79,7 +79,7 @@ void init(){ CareerRequestDto.CreateCareerDto careerDto = CareerRequestDto.CreateCareerDto.builder() .careerName("test") .alias("alias") - .isCurrent(false) + .isUnknown(false) .startDate(LocalDate.of(2024,7,10)) .endDate(LocalDate.of(2024,5,10)) .category(1)