Skip to content

Commit

Permalink
Merge pull request #104 from kssumin/main
Browse files Browse the repository at this point in the history
[BE/FIX] 조회 조건 추가
  • Loading branch information
kssumin authored Nov 28, 2023
2 parents 2c88366 + a1dfc61 commit 697eb78
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 96 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
@RequestMapping("/api")
public class HealthController {

@GetMapping("/health-check")
public ApiResponse<SuccessBody<Void>> checkHealth() {
return ApiResponseGenerator.success(HttpStatus.OK, MessageCode.CREATE);
}
@GetMapping("/health-check")
public ApiResponse<SuccessBody<Void>> checkHealth() {
return ApiResponseGenerator.success(HttpStatus.OK, MessageCode.GET);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<ProgramEntity> getPages(Timestamp now, PageRequest pageRequest) {
return programRepository.findAllByIng(now, pageRequest);
}
}
Original file line number Diff line number Diff line change
@@ -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<ProgramEntity> getPages(Timestamp now, PageRequest pageRequest) {
return programRepository.findAllByEnd(now, pageRequest);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -71,21 +73,15 @@ public CommandProgramResponse update(Long programId, UpdateProgramRequest reques
}

@Override
public PageResponse<GetProgramsResponse> 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<GetProgramsResponse> 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<ProgramEntity> pages = programRepository.findAllByIng(now, pageRequest);
return pageResponseConverter.from(pages);
}
if (ProgramStatus.isSameStatus(status, ProgramStatus.END)) {
Page<ProgramEntity> pages = programRepository.findAllByEnd(now, pageRequest);
return pageResponseConverter.from(pages);
}
Map<ProgramStatus, ProgramStateService> programStatusStrategy = programStatusFactory.make();

throw new NotFoundStatusException();
ProgramStateService programStateService =
programStatusStrategy.get(ProgramStatus.getStatus(status));
Page<ProgramEntity> pages = programStateService.getPages(now, pageRequest);
return pageResponseConverter.from(pages);
}
}
Original file line number Diff line number Diff line change
@@ -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<ProgramEntity> getPages(Timestamp now, PageRequest pageRequest);
}
Original file line number Diff line number Diff line change
@@ -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<ProgramStatus, ProgramStateService> make() {
Map<ProgramStatus, ProgramStateService> stateServices = new EnumMap<>(ProgramStatus.class);

stateServices.put(ProgramStatus.ACTIVE, new ActiveProgramStateService(programRepository));
stateServices.put(ProgramStatus.END, new EndProgramStateService(programRepository));

return stateServices;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

public interface GetProgramsUsecase {

PageResponse<GetProgramsResponse> getProgram(String status, int size, int page);
PageResponse<GetProgramsResponse> getPrograms(String status, int size, int page);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

public interface ProgramRepository extends JpaRepository<ProgramEntity, Long> {

@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<ProgramEntity> 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<ProgramEntity> findAllByIng(@Param("now") Timestamp now, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public ApiResponse<SuccessBody<PageResponse<GetProgramsResponse>>> findAll(
@RequestParam("programStatus") String status,
@RequestParam("size") int size,
@RequestParam("page") int page) {
PageResponse<GetProgramsResponse> response = getProgramsUsecase.getProgram(status, size, page);
PageResponse<GetProgramsResponse> response = getProgramsUsecase.getPrograms(status, size, page);
return ApiResponseGenerator.success(response, HttpStatus.OK, MessageCode.GET);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}

0 comments on commit 697eb78

Please sign in to comment.