Skip to content

Commit

Permalink
Merge pull request #133 from onetime-with-members/fix/#130/time-error
Browse files Browse the repository at this point in the history
[fix] : 요일 별 고정 스케줄 조회 시, 시간이 다르게 나오는 문제를 해결한다
  • Loading branch information
bbbang105 authored Dec 10, 2024
2 parents 0035673 + 87e51b1 commit c705ccf
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 102 deletions.
20 changes: 2 additions & 18 deletions src/main/java/side/onetime/domain/FixedEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,16 @@ public class FixedEvent extends BaseEntity {
@Column(name = "title", nullable = false, length = 30)
private String title;

@Column(name = "start_time", nullable = false)
private String startTime;

@Column(name = "end_time", nullable = false)
private String endTime;

@OneToMany(mappedBy = "fixedEvent",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<FixedSelection> fixedSelections;

@Builder
public FixedEvent(User user, String title, String startTime, String endTime) {
public FixedEvent(User user, String title) {
this.user = user;
this.title = title;
this.startTime = startTime;
this.endTime = endTime;
}

public void updateTitle(String title) {
this.title = title;
}

public void updateStartTime(String startTime) {
this.startTime = startTime;
}

public void updateEndTime(String endTime) {
this.endTime = endTime;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,16 @@

import java.util.List;

import static side.onetime.util.DateUtil.addThirtyMinutes;

@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record CreateFixedEventRequest(
@NotBlank(message = "제목은 필수 값입니다.") String title,
@NotNull(message = "스케줄 목록은 필수 값입니다.") List<FixedScheduleResponse> schedules
) {
public FixedEvent toEntity(User user, String startTime, String endTime) {
public FixedEvent toEntity(User user) {
return FixedEvent.builder()
.user(user)
.title(title)
.startTime(startTime)
.endTime(addThirtyMinutes(endTime))
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,14 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import side.onetime.domain.FixedEvent;
import side.onetime.domain.User;
import side.onetime.dto.fixed.response.FixedScheduleResponse;

import java.util.List;

import static side.onetime.util.DateUtil.addThirtyMinutes;

@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record ModifyFixedEventRequest(
String title,
List<FixedScheduleResponse> schedules
) {
public FixedEvent toEntity(User user, String startTime, String endTime) {
return FixedEvent.builder()
.user(user)
.title(title)
.startTime(startTime)
.endTime(addThirtyMinutes(endTime))
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@
@JsonInclude(JsonInclude.Include.NON_NULL)
public record FixedEventDetailResponse(
String title,
String startTime,
String endTime,
List<FixedScheduleResponse> schedules
) {
public static FixedEventDetailResponse of(String title, String startTime, String endTime, List<FixedScheduleResponse> schedules) {
public static FixedEventDetailResponse of(String title, List<FixedScheduleResponse> schedules) {
return new FixedEventDetailResponse(
title,
startTime,
endTime,
schedules
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@
@JsonInclude(JsonInclude.Include.NON_NULL)
public record FixedEventResponse(
Long id,
String startTime,
String endTime,
List<FixedScheduleResponse> schedules
) {
public static FixedEventResponse of(Long id, String startTime, String endTime, List<FixedScheduleResponse> schedules) {
public static FixedEventResponse of(Long id, List<FixedScheduleResponse> schedules) {
return new FixedEventResponse(
id,
startTime,
endTime,
schedules
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ public interface FixedEventRepositoryCustom {
FixedEvent findByUserAndFixedEventIdCustom(User user, Long fixedEventId);
void deleteFixedEventAndSelections(User user, Long fixedEventId);
List<FixedEvent> findFixedEventsByUserAndDay(User user, String day);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,4 @@ public List<FixedEvent> findFixedEventsByUserAndDay(User user, String day) {
.and(fixedSchedule.day.eq(day)))
.fetch();
}

}
}
79 changes: 60 additions & 19 deletions src/main/java/side/onetime/service/FixedEventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import side.onetime.domain.FixedEvent;
import side.onetime.domain.FixedSelection;
import side.onetime.domain.User;
import side.onetime.dto.fixed.request.CreateFixedEventRequest;
import side.onetime.dto.fixed.request.ModifyFixedEventRequest;
import side.onetime.dto.fixed.response.FixedEventByDayResponse;
import side.onetime.exception.CustomException;
import side.onetime.exception.status.FixedErrorStatus;
import side.onetime.repository.FixedEventRepository;
import side.onetime.util.DateUtil;
import side.onetime.util.JwtUtil;

import java.util.List;
Expand All @@ -23,19 +25,32 @@ public class FixedEventService {
private final FixedEventRepository fixedEventRepository;
private final JwtUtil jwtUtil;

// 고정 이벤트 생성 메서드
/**
* 고정 이벤트 생성 메서드.
*
* 유저 인증 정보를 기반으로 새로운 고정 이벤트를 생성하고,
* 관련된 고정 스케줄을 등록합니다.
*
* @param authorizationHeader 인증 토큰
* @param createFixedEventRequest 고정 이벤트 생성 요청 데이터
*/
@Transactional
public void createFixedEvent(String authorizationHeader, CreateFixedEventRequest createFixedEventRequest) {
User user = jwtUtil.getUserFromHeader(authorizationHeader);
List<String> times = createFixedEventRequest.schedules().get(0).times();
String startTime = times.get(0);
String endTime = times.get(times.size() - 1);
FixedEvent fixedEvent = createFixedEventRequest.toEntity(user, startTime, endTime);
FixedEvent fixedEvent = createFixedEventRequest.toEntity(user);
fixedEventRepository.save(fixedEvent);
fixedScheduleService.createFixedSchedules(createFixedEventRequest.schedules(), fixedEvent);
}

// 고정 이벤트 수정 메서드
/**
* 고정 이벤트 수정 메서드.
*
* 고정 이벤트의 제목을 수정합니다.
*
* @param authorizationHeader 인증 토큰
* @param fixedEventId 수정할 고정 이벤트 ID
* @param modifyFixedEventRequest 고정 이벤트 수정 요청 데이터
*/
@Transactional
public void modifyFixedEvent(String authorizationHeader, Long fixedEventId, ModifyFixedEventRequest modifyFixedEventRequest) {
User user = jwtUtil.getUserFromHeader(authorizationHeader);
Expand All @@ -45,7 +60,14 @@ public void modifyFixedEvent(String authorizationHeader, Long fixedEventId, Modi
fixedEventRepository.save(fixedEvent);
}

// 고정 이벤트 & 스케줄 삭제 메서드
/**
* 고정 이벤트 및 관련 스케줄 삭제 메서드.
*
* 고정 이벤트와 해당 이벤트에 관련된 모든 스케줄을 삭제합니다.
*
* @param authorizationHeader 인증 토큰
* @param fixedEventId 삭제할 고정 이벤트 ID
*/
@Transactional
public void removeFixedEvent(String authorizationHeader, Long fixedEventId) {
User user = jwtUtil.getUserFromHeader(authorizationHeader);
Expand All @@ -54,25 +76,44 @@ public void removeFixedEvent(String authorizationHeader, Long fixedEventId) {
fixedEventRepository.deleteFixedEventAndSelections(user, fixedEventId);
}

// 요일 별 고정 이벤트 조회 메서드
/**
* 요일별 고정 이벤트 조회 메서드.
*
* 특정 요일에 해당하는 고정 이벤트 목록을 조회합니다.
*
* @param authorizationHeader 인증 토큰
* @param day 조회할 요일 (예: "mon", "tue" 등)
* @return 고정 이벤트 목록
*/
@Transactional(readOnly = true)
public List<FixedEventByDayResponse> getFixedEventByDay(String authorizationHeader, String day) {
User user = jwtUtil.getUserFromHeader(authorizationHeader);
String koreanDay = convertDayToKorean(day);

List<FixedEvent> fixedEvents = fixedEventRepository.findFixedEventsByUserAndDay(user, koreanDay);

return fixedEvents.stream()
.map(fixedEvent -> FixedEventByDayResponse.of(
fixedEvent.getId(),
fixedEvent.getTitle(),
fixedEvent.getStartTime(),
fixedEvent.getEndTime()
))
return fixedEventRepository.findFixedEventsByUserAndDay(user, koreanDay).stream()
.map(fixedEvent -> {
List<FixedSelection> fixedSelections = fixedEvent.getFixedSelections();
String startTime = fixedSelections.get(0).getFixedSchedule().getTime();
String endTime = fixedSelections.get(fixedSelections.size() - 1).getFixedSchedule().getTime();
return FixedEventByDayResponse.of(
fixedEvent.getId(),
fixedEvent.getTitle(),
startTime,
DateUtil.addThirtyMinutes(endTime)
);
})
.toList();
}

// 영어 요일 -> 한글 요일 변환 메서드
/**
* 영어 요일 -> 한글 요일 변환 메서드.
*
* 영어로 입력된 요일을 한글 요일로 변환합니다.
* 입력이 올바르지 않을 경우 예외를 발생시킵니다.
*
* @param day 영어 요일 (예: "mon", "tue" 등)
* @return 한글 요일 (예: "월", "화" 등)
*/
private String convertDayToKorean(String day) {
return switch (day.toLowerCase()) {
case "mon" -> "월";
Expand All @@ -85,4 +126,4 @@ private String convertDayToKorean(String day) {
default -> throw new CustomException(FixedErrorStatus._IS_NOT_RIGHT_DAY);
};
}
}
}
Loading

0 comments on commit c705ccf

Please sign in to comment.