diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/common/support/converter/DateConverter.java b/BE/eeos/src/main/java/com/blackcompany/eeos/common/support/converter/DateConverter.java deleted file mode 100644 index 49ab972c..00000000 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/common/support/converter/DateConverter.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.blackcompany.eeos.common.support.converter; - -import java.time.Instant; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.ZoneId; -import java.time.ZoneOffset; -import lombok.experimental.UtilityClass; - -@UtilityClass -public class DateConverter { - private static final String KOREA_ZONE_OFFSET = "+09:00"; - - public static Long toEpochSecond(LocalDateTime localDateTime) { - if (localDateTime == null) { - return null; - } - - return localDateTime.toEpochSecond(ZoneOffset.of(KOREA_ZONE_OFFSET)); - } - - public static Long toEpochSecond(LocalDate localDate) { - if (localDate == null) { - return null; - } - - return localDate.toEpochSecond(LocalTime.parse("00:00:00"), ZoneOffset.of(KOREA_ZONE_OFFSET)); - } - - public static LocalDate toLocalDate(Long epochSecond) { - if (epochSecond == null) { - return null; - } - - return Instant.ofEpochSecond(epochSecond).atZone(ZoneId.of("Asia/Seoul")).toLocalDate(); - } - - public static LocalDateTime toLocalDateTime(Long epochSecond) { - if (epochSecond == null) { - return null; - } - - return Instant.ofEpochSecond(epochSecond) - .atZone(ZoneOffset.of(KOREA_ZONE_OFFSET)) - .toLocalDateTime(); - } -} diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/common/utils/DateConverter.java b/BE/eeos/src/main/java/com/blackcompany/eeos/common/utils/DateConverter.java new file mode 100644 index 00000000..18a52ea7 --- /dev/null +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/common/utils/DateConverter.java @@ -0,0 +1,47 @@ +package com.blackcompany.eeos.common.utils; + +import java.sql.Timestamp; +import java.time.Instant; +import java.time.LocalDate; +import java.time.ZoneId; +import lombok.experimental.UtilityClass; + +@UtilityClass +public class DateConverter { + private static final String KST = "Asia/Seoul"; + + public static Timestamp toEpochSecond(LocalDate localDate) { + if (localDate == null) { + return null; + } + + return Timestamp.valueOf(localDate.atStartOfDay()); + } + + public static Timestamp toEpochSecond(Timestamp epochSecond) { + if (epochSecond == null) { + return null; + } + + LocalDate localDate = toLocalDate(epochSecond); + return toEpochSecond(localDate); + } + + private static LocalDate toLocalDate(Timestamp epochSecond) { + if (epochSecond == null) { + return null; + } + + return epochSecond.toLocalDateTime().toLocalDate(); + } + + public static LocalDate toLocalDate(Long epochMilli) { + if (epochMilli == null) { + return null; + } + + return Instant.ofEpochSecond(epochMilli / 1000) + .atZone(ZoneId.of(KST)) + .toLocalDate(); + } +} diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/health/HealthController.java b/BE/eeos/src/main/java/com/blackcompany/eeos/health/HealthController.java index 261422af..fee88aac 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/health/HealthController.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/health/HealthController.java @@ -15,8 +15,8 @@ @RequestMapping("/api") public class HealthController { - @GetMapping("/health-check") - public ApiResponse> checkHealth() { - return ApiResponseGenerator.success(HttpStatus.OK, MessageCode.CREATE); - } + @GetMapping("/health-check") + public ApiResponse> checkHealth() { + return ApiResponseGenerator.success(HttpStatus.OK, MessageCode.GET); + } } diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/domain/ProgramModel.java b/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/domain/ProgramModel.java index a730501e..67a036f9 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/domain/ProgramModel.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/domain/ProgramModel.java @@ -1,11 +1,11 @@ package com.blackcompany.eeos.program.application.domain; import com.blackcompany.eeos.common.support.AbstractModel; -import com.blackcompany.eeos.common.support.converter.DateConverter; +import com.blackcompany.eeos.common.utils.DateConverter; import com.blackcompany.eeos.program.application.model.EventStatus; import java.sql.Timestamp; import java.time.Instant; -import java.time.LocalDateTime; +import java.time.LocalDate; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; @@ -18,23 +18,23 @@ @NoArgsConstructor @Builder(toBuilder = true) public class ProgramModel implements AbstractModel { - private Long id; - private String title; + private Long id; + private String title; - private String content; + private String content; - private Long userId; + private Long userId; - private Timestamp programDate; - private String eventStatus; + private Timestamp programDate; + private String eventStatus; - public EventStatus calculateEventStatus() { - LocalDateTime nowDate = DateConverter.toLocalDateTime(Instant.now().getEpochSecond()); - LocalDateTime programDate = DateConverter.toLocalDateTime(this.programDate.getTime()); + public EventStatus calculate() { + LocalDate now = DateConverter.toLocalDate(Instant.now().toEpochMilli()); + LocalDate programDate = DateConverter.toLocalDate(this.programDate.getTime()); - if (programDate.isBefore(nowDate)) { - return EventStatus.END; - } - return EventStatus.ING; - } + if (programDate.isBefore(now)) { + return EventStatus.END; + } + return EventStatus.ING; + } } diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/domain/ProgramStatus.java b/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/domain/ProgramStatus.java index 8e535bea..130c65fd 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/domain/ProgramStatus.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/domain/ProgramStatus.java @@ -1,5 +1,8 @@ package com.blackcompany.eeos.program.application.domain; +import com.blackcompany.eeos.attend.application.exception.NotFoundStatusException; +import java.util.Arrays; + public enum ProgramStatus { ACTIVE("active"), END("end"); @@ -10,7 +13,10 @@ public enum ProgramStatus { this.status = status; } - public static boolean isSameStatus(String source, ProgramStatus expected) { - return expected.status.equals(source); + public static ProgramStatus getStatus(String source) { + return Arrays.stream(ProgramStatus.values()) + .filter(status -> status.status.equals(source)) + .findAny() + .orElseThrow(NotFoundStatusException::new); } } diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/model/converter/ProgramRequestConverter.java b/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/model/converter/ProgramRequestConverter.java index 26682959..92d83b8c 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/model/converter/ProgramRequestConverter.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/model/converter/ProgramRequestConverter.java @@ -1,6 +1,7 @@ package com.blackcompany.eeos.program.application.model.converter; import com.blackcompany.eeos.common.support.converter.AbstractDtoConverter; +import com.blackcompany.eeos.common.utils.DateConverter; import com.blackcompany.eeos.program.application.domain.ProgramModel; import com.blackcompany.eeos.program.application.dto.CreateProgramRequest; import com.blackcompany.eeos.program.application.dto.UpdateProgramRequest; @@ -15,7 +16,7 @@ public ProgramModel from(CreateProgramRequest source) { return ProgramModel.builder() .title(source.getTitle()) .content(source.getContent()) - .programDate(source.getProgramDate()) + .programDate(DateConverter.toEpochSecond(source.getProgramDate())) .build(); } diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/service/ActiveProgramStateService.java b/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/service/ActiveProgramStateService.java new file mode 100644 index 00000000..461aaa0b --- /dev/null +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/service/ActiveProgramStateService.java @@ -0,0 +1,18 @@ +package com.blackcompany.eeos.program.application.service; + +import com.blackcompany.eeos.program.persistence.ProgramEntity; +import com.blackcompany.eeos.program.persistence.ProgramRepository; +import java.sql.Timestamp; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; + +@RequiredArgsConstructor +public class ActiveProgramStateService implements ProgramStateService { + private final ProgramRepository programRepository; + + @Override + public Page getPages(Timestamp now, PageRequest pageRequest) { + return programRepository.findAllByIng(now, pageRequest); + } +} diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/service/EndProgramStateService.java b/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/service/EndProgramStateService.java new file mode 100644 index 00000000..10040f6f --- /dev/null +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/service/EndProgramStateService.java @@ -0,0 +1,18 @@ +package com.blackcompany.eeos.program.application.service; + +import com.blackcompany.eeos.program.persistence.ProgramEntity; +import com.blackcompany.eeos.program.persistence.ProgramRepository; +import java.sql.Timestamp; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; + +@RequiredArgsConstructor +public class EndProgramStateService implements ProgramStateService { + private final ProgramRepository programRepository; + + @Override + public Page getPages(Timestamp now, PageRequest pageRequest) { + return programRepository.findAllByEnd(now, pageRequest); + } +} diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/service/ProgramService.java b/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/service/ProgramService.java index 0e7457a7..87f54ad7 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/service/ProgramService.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/service/ProgramService.java @@ -1,7 +1,7 @@ package com.blackcompany.eeos.program.application.service; -import com.blackcompany.eeos.attend.application.exception.NotFoundStatusException; import com.blackcompany.eeos.attend.application.service.CandidateService; +import com.blackcompany.eeos.common.utils.DateConverter; import com.blackcompany.eeos.program.application.domain.ProgramModel; import com.blackcompany.eeos.program.application.domain.ProgramStatus; import com.blackcompany.eeos.program.application.dto.CommandProgramResponse; @@ -15,6 +15,7 @@ import com.blackcompany.eeos.program.application.exception.NotFoundProgramException; import com.blackcompany.eeos.program.application.model.converter.ProgramEntityConverter; import com.blackcompany.eeos.program.application.model.converter.ProgramRequestConverter; +import com.blackcompany.eeos.program.application.support.ProgramStatusFactory; import com.blackcompany.eeos.program.application.usecase.CreateProgramUsecase; import com.blackcompany.eeos.program.application.usecase.GetProgramUsecase; import com.blackcompany.eeos.program.application.usecase.GetProgramsUsecase; @@ -23,7 +24,7 @@ import com.blackcompany.eeos.program.persistence.ProgramRepository; import java.sql.Timestamp; import java.time.LocalDate; -import java.time.ZoneId; +import java.util.Map; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -40,6 +41,7 @@ public class ProgramService private final ProgramRepository programRepository; private final CandidateService candidateService; private final ProgramPageResponseConverter pageResponseConverter; + private final ProgramStatusFactory programStatusFactory; @Override public CommandProgramResponse create(CreateProgramRequest request) { @@ -58,7 +60,7 @@ public GetProgramResponse getProgram(Long id) { programRepository.findById(id).orElseThrow(NotFoundProgramException::new); ProgramModel model = entityConverter.from(programEntity); - return responseConverter.from(model, model.calculateEventStatus()); + return responseConverter.from(model, model.calculate()); } @Override @@ -71,21 +73,15 @@ public CommandProgramResponse update(Long programId, UpdateProgramRequest reques } @Override - public PageResponse getProgram(String status, int size, int page) { - LocalDate currentDate = LocalDate.now(ZoneId.of("Asia/Seoul")); - Timestamp now = Timestamp.from(currentDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); - + public PageResponse getPrograms(String status, int size, int page) { + Timestamp now = DateConverter.toEpochSecond(LocalDate.now()); PageRequest pageRequest = PageRequest.of(page, size); - if (ProgramStatus.isSameStatus(status, ProgramStatus.ACTIVE)) { - Page pages = programRepository.findAllByIng(now, pageRequest); - return pageResponseConverter.from(pages); - } - if (ProgramStatus.isSameStatus(status, ProgramStatus.END)) { - Page pages = programRepository.findAllByEnd(now, pageRequest); - return pageResponseConverter.from(pages); - } + Map programStatusStrategy = programStatusFactory.make(); - throw new NotFoundStatusException(); + ProgramStateService programStateService = + programStatusStrategy.get(ProgramStatus.getStatus(status)); + Page pages = programStateService.getPages(now, pageRequest); + return pageResponseConverter.from(pages); } } diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/service/ProgramStateService.java b/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/service/ProgramStateService.java new file mode 100644 index 00000000..8a365a75 --- /dev/null +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/service/ProgramStateService.java @@ -0,0 +1,10 @@ +package com.blackcompany.eeos.program.application.service; + +import com.blackcompany.eeos.program.persistence.ProgramEntity; +import java.sql.Timestamp; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; + +public interface ProgramStateService { + Page getPages(Timestamp now, PageRequest pageRequest); +} diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/support/ProgramStatusFactory.java b/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/support/ProgramStatusFactory.java new file mode 100644 index 00000000..771599db --- /dev/null +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/support/ProgramStatusFactory.java @@ -0,0 +1,26 @@ +package com.blackcompany.eeos.program.application.support; + +import com.blackcompany.eeos.program.application.domain.ProgramStatus; +import com.blackcompany.eeos.program.application.service.ActiveProgramStateService; +import com.blackcompany.eeos.program.application.service.EndProgramStateService; +import com.blackcompany.eeos.program.application.service.ProgramStateService; +import com.blackcompany.eeos.program.persistence.ProgramRepository; +import java.util.EnumMap; +import java.util.Map; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class ProgramStatusFactory { + private final ProgramRepository programRepository; + + public Map make() { + Map stateServices = new EnumMap<>(ProgramStatus.class); + + stateServices.put(ProgramStatus.ACTIVE, new ActiveProgramStateService(programRepository)); + stateServices.put(ProgramStatus.END, new EndProgramStateService(programRepository)); + + return stateServices; + } +} diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/usecase/GetProgramsUsecase.java b/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/usecase/GetProgramsUsecase.java index b863ad47..a1032015 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/usecase/GetProgramsUsecase.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/program/application/usecase/GetProgramsUsecase.java @@ -5,5 +5,5 @@ public interface GetProgramsUsecase { - PageResponse getProgram(String status, int size, int page); + PageResponse getPrograms(String status, int size, int page); } diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/program/persistence/ProgramRepository.java b/BE/eeos/src/main/java/com/blackcompany/eeos/program/persistence/ProgramRepository.java index a87ede63..b02b44bb 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/program/persistence/ProgramRepository.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/program/persistence/ProgramRepository.java @@ -9,9 +9,11 @@ public interface ProgramRepository extends JpaRepository { - @Query("SELECT p FROM ProgramEntity p WHERE p.programDate < :now ORDER BY p.programDate DESC") + @Query( + "SELECT p FROM ProgramEntity p WHERE p.programDate < :now ORDER BY p.programDate ASC, p.title ASC") Page findAllByEnd(@Param("now") Timestamp now, Pageable pageable); - @Query("SELECT p FROM ProgramEntity p WHERE p.programDate > :now ORDER BY p.programDate DESC") + @Query( + "SELECT p FROM ProgramEntity p WHERE p.programDate >= :now ORDER BY p.programDate DESC, p.title ASC") Page findAllByIng(@Param("now") Timestamp now, Pageable pageable); } diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/program/presentation/ProgramController.java b/BE/eeos/src/main/java/com/blackcompany/eeos/program/presentation/ProgramController.java index a427d999..6e239790 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/program/presentation/ProgramController.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/program/presentation/ProgramController.java @@ -62,7 +62,7 @@ public ApiResponse>> findAll( @RequestParam("programStatus") String status, @RequestParam("size") int size, @RequestParam("page") int page) { - PageResponse response = getProgramsUsecase.getProgram(status, size, page); + PageResponse response = getProgramsUsecase.getPrograms(status, size, page); return ApiResponseGenerator.success(response, HttpStatus.OK, MessageCode.GET); } } diff --git a/BE/eeos/src/main/java/com/blackcompany/eeos/program/presentation/annotation/OverDateValidator.java b/BE/eeos/src/main/java/com/blackcompany/eeos/program/presentation/annotation/OverDateValidator.java index 63742613..39a8565e 100644 --- a/BE/eeos/src/main/java/com/blackcompany/eeos/program/presentation/annotation/OverDateValidator.java +++ b/BE/eeos/src/main/java/com/blackcompany/eeos/program/presentation/annotation/OverDateValidator.java @@ -1,6 +1,6 @@ package com.blackcompany.eeos.program.presentation.annotation; -import com.blackcompany.eeos.common.support.converter.DateConverter; +import com.blackcompany.eeos.common.utils.DateConverter; import java.sql.Timestamp; import java.time.LocalDate; import java.util.Objects; @@ -23,9 +23,6 @@ public boolean isValid(Timestamp value, ConstraintValidatorContext context) { LocalDate now = LocalDate.now(); LocalDate requestDate = DateConverter.toLocalDate(value.getTime()); - if (now.isAfter(requestDate)) { - return false; - } - return true; + return now.isBefore(requestDate) || now.isEqual(requestDate); } }