Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE/BUG] term1 오류 해결 #107

Merged
merged 20 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
b29903a
feat : programStatus의 상태에 따라 데이터를 불러오는 ProgramStatusService 구현
kssumin Nov 28, 2023
426af9c
feat : 주어진 source와 맞는 ProgramStatus가지고 오도록 구현
kssumin Nov 28, 2023
699fa8c
feat : ProgramStateService를 가지고 있는 팩터리 구현
kssumin Nov 28, 2023
ca7143d
refactor : ProgramStatusService를 이용하여 데이터를 불러오도록 변경
kssumin Nov 28, 2023
ed86835
refactor : 리스트를 불러옴을 나타낼 수 있도록 메서드명 변경
kssumin Nov 28, 2023
fb935bc
feat : DateTime에서 TimeStamp로 변환하는 converter 구현
kssumin Nov 28, 2023
5241976
feat : source에서 ProgramStatus반환하도록 구현
kssumin Nov 28, 2023
486ad96
chore : dateConverter 위치 변경으로 인한 import 변경
kssumin Nov 28, 2023
9b4724f
refactor : DateConverter 이용하도록 변경
kssumin Nov 28, 2023
0d15cbd
feat : 프로그램 시간 제외 날짜만 포함한 timestamp 반환하도록 변경
kssumin Nov 28, 2023
00a796e
feat : 당일은 active로 반환하도록 구현
kssumin Nov 28, 2023
990a1b2
fix : 현 시간이 요청한 날짜의 이후인지 아닌지 판단하는 로직 변경
kssumin Nov 28, 2023
52cf6f0
fix : health-check시 response code 200으로 변환하도록 구현
kssumin Nov 28, 2023
b362138
chore : 불필요한 converter 메서드 제거
kssumin Nov 28, 2023
02db6a4
fix : 현 날짜와 동일한 날짜의 프로그램 생성 가능하도록 변경
kssumin Nov 28, 2023
f847626
fix : 날짜를 기준으로 비교를 하도록 변경
kssumin Nov 28, 2023
ae881a2
refactor : 메서드명 변경
kssumin Nov 28, 2023
3b393ae
feat : 프로그램 리스트 조회 시 정렬 기준 추가
kssumin Nov 28, 2023
a1dfc61
Merge pull request #25 from kssumin/fix/#97/list-sort
kssumin Nov 28, 2023
697eb78
Merge pull request #104 from kssumin/main
kssumin Nov 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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);
}
}