Skip to content

Commit

Permalink
Refactor : Career 엔티티에 unknown 필드 수정, 스케줄 클래스 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonda02 committed Jul 30, 2024
1 parent 0bc59a8 commit 6fe6bb3
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 48 deletions.
12 changes: 8 additions & 4 deletions src/main/java/umc/kkijuk/server/career/domain/Career.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -49,6 +50,9 @@ public class Career extends BaseEntity {
private List<CareerDetail> careerDetailList = new ArrayList<>();


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

public void setName(String name) {
this.name = name;
Expand All @@ -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) {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/umc/kkijuk/server/career/dto/CareerRequestDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static class CareerDto{
String careerName;
String alias;
String summary;
Boolean isCurrent;
Boolean isUnknown;
LocalDate startDate;
LocalDate endDate;
int year;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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())
Expand All @@ -55,7 +55,7 @@ public static List<CareerResponseDto.CareerGroupedByCategoryDto> 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())
Expand All @@ -78,7 +78,7 @@ public static List<CareerResponseDto.CareerGroupedByYearDto> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Career, Long> {
List<Career> findAllByUnknown(boolean isUnknown);
}
Original file line number Diff line number Diff line change
@@ -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<Career> careers = careerRepository.findAllByUnknown(true);
LocalDate now = LocalDate.now();
for (Career career : careers) {
career.setEnddate(now);
careerRepository.save(career);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -101,10 +101,10 @@ public Optional<Career> 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 {
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
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;
import umc.kkijuk.server.career.dto.CareerResponseDto;
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;
import java.util.Optional;

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)
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -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),
Expand All @@ -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();
Expand All @@ -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),
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 6fe6bb3

Please sign in to comment.