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

[feat] : 유저는 스케줄 등록 시 전체 고정 스케줄을 불러올 수 있다 #94

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import side.onetime.dto.fixedEvent.request.CreateFixedEventRequest;
import side.onetime.dto.fixed.request.CreateFixedEventRequest;
import side.onetime.dto.fixed.response.FixedEventResponse;
import side.onetime.global.common.ApiResponse;
import side.onetime.global.common.status.SuccessStatus;
import side.onetime.service.FixedEventService;
import side.onetime.service.FixedScheduleService;

import java.util.List;

@RestController
@RequestMapping("/api/v1/fixed-events")
@RequestMapping("/api/v1/fixed-schedules")
@RequiredArgsConstructor
public class FixedEventController {
public class FixedController {
private final FixedEventService fixedEventService;
private final FixedScheduleService fixedScheduleService;

// 고정 이벤트 생성 및 고정 스케줄 등록 API
@PostMapping
Expand All @@ -25,4 +30,15 @@ public ResponseEntity<ApiResponse<Object>> createFixedEvent(

return ApiResponse.onSuccess(SuccessStatus._CREATED_FIXED_SCHEDULE);
}

// 전체 고정 스케줄 조회 API
@GetMapping
public ResponseEntity<ApiResponse<List<FixedEventResponse>>> getAllFixedSchedules(
@RequestHeader("Authorization") String authorizationHeader) {

List<FixedEventResponse> fixedEventResponses = fixedScheduleService.getAllFixedSchedules(authorizationHeader);

return ApiResponse.onSuccess(SuccessStatus._GET_ALL_FIXED_SCHEDULES, fixedEventResponses);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package side.onetime.dto.fixedEvent.request;
package side.onetime.dto.fixed.request;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
Expand All @@ -7,7 +7,7 @@
import jakarta.validation.constraints.NotNull;
import side.onetime.domain.FixedEvent;
import side.onetime.domain.User;
import side.onetime.dto.fixedEvent.response.FixedScheduleResponse;
import side.onetime.dto.fixed.response.FixedScheduleResponse;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package side.onetime.dto.fixed.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import java.util.List;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record FixedEventResponse(
String title,
String startTime,
String endTime,
List<FixedScheduleResponse> schedules
) {
public static FixedEventResponse of (String title, String startTime, String endTime, List<FixedScheduleResponse> schedules) {
return new FixedEventResponse(
title,
startTime,
endTime,
schedules
);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package side.onetime.dto.fixedEvent.response;
package side.onetime.dto.fixed.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
Expand All @@ -11,4 +11,10 @@
public record FixedScheduleResponse(
String timePoint,
List<String> times
) {}
) {
public static FixedScheduleResponse of(String timePoint, List<String> times) {
return new FixedScheduleResponse(
timePoint,
times);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

@Getter
@RequiredArgsConstructor
public enum FixedScheduleErrorStatus implements BaseErrorCode {
_NOT_FOUND_FIXED_SCHEDULES(HttpStatus.NOT_FOUND, "FIXED-SCHEDULE-001", "스케줄을 가져오는 데 실패했습니다."),
public enum FixedErrorStatus implements BaseErrorCode {
_NOT_FOUND_FIXED_SCHEDULES(HttpStatus.NOT_FOUND, "FIXED-001", "고정 스케줄 목록을 가져오는 데 실패했습니다."),
_NOT_FOUND_FIXED_EVENTS(HttpStatus.NOT_FOUND, "FIXED-002", "고정 이벤트 목록을 가져오는 데 실패했습니다."),
_NOT_FOUND_FIXED_SELECTIONS(HttpStatus.NOT_FOUND, "FIXED-003", "고정 스케줄 선택 목록을 가져오는 데 실패했습니다."),
;

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ public enum SuccessStatus implements BaseCode {
_GET_USER_PROFILE(HttpStatus.OK, "200", "유저 정보 조회에 성공했습니다."),
_UPDATE_USER_PROFILE(HttpStatus.OK, "200", "유저 정보 수정에 성공했습니다."),
_WITHDRAW_SERVICE(HttpStatus.OK, "200", "유저 서비스 탈퇴에 성공했습니다."),
// FixedEvent
// Fixed
_CREATED_FIXED_SCHEDULE(HttpStatus.CREATED, "201", "고정 스케줄 등록에 성공했습니다."),
_GET_ALL_FIXED_SCHEDULES(HttpStatus.OK, "200", "전체 고정 스케줄 조회에 성공했습니다."),
;

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.data.jpa.repository.JpaRepository;
import side.onetime.domain.FixedEvent;
import side.onetime.repository.custom.FixedEventRepositoryCustom;

public interface FixedEventRepository extends JpaRepository<FixedEvent, Long> {
public interface FixedEventRepository extends JpaRepository<FixedEvent, Long>, FixedEventRepositoryCustom {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package side.onetime.repository.custom;

import side.onetime.domain.FixedEvent;
import side.onetime.domain.User;

import java.util.List;

public interface FixedEventRepositoryCustom {
List<FixedEvent> findAllByUser(User user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package side.onetime.repository.custom;

import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import side.onetime.domain.FixedEvent;
import side.onetime.domain.User;

import java.util.List;

import static side.onetime.domain.QFixedEvent.fixedEvent;
import static side.onetime.domain.QFixedSchedule.fixedSchedule;
import static side.onetime.domain.QFixedSelection.fixedSelection;

@RequiredArgsConstructor
public class FixedEventRepositoryImpl implements FixedEventRepositoryCustom {

private final JPAQueryFactory queryFactory;

// 유저 고정 스케줄 목록 조회
@Override
public List<FixedEvent> findAllByUser(User user) {
return queryFactory.selectFrom(fixedEvent)
.leftJoin(fixedEvent.fixedSelections, fixedSelection)
.fetchJoin()
.leftJoin(fixedSelection.fixedSchedule, fixedSchedule)
.fetchJoin()
.where(fixedEvent.user.eq(user))
.fetch();
}
}
2 changes: 1 addition & 1 deletion src/main/java/side/onetime/service/FixedEventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.springframework.transaction.annotation.Transactional;
import side.onetime.domain.FixedEvent;
import side.onetime.domain.User;
import side.onetime.dto.fixedEvent.request.CreateFixedEventRequest;
import side.onetime.dto.fixed.request.CreateFixedEventRequest;
import side.onetime.repository.FixedEventRepository;
import side.onetime.util.JwtUtil;

Expand Down
50 changes: 46 additions & 4 deletions src/main/java/side/onetime/service/FixedScheduleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,29 @@
import side.onetime.domain.FixedEvent;
import side.onetime.domain.FixedSchedule;
import side.onetime.domain.FixedSelection;
import side.onetime.dto.fixedEvent.request.CreateFixedEventRequest;
import side.onetime.dto.fixedEvent.response.FixedScheduleResponse;
import side.onetime.domain.User;
import side.onetime.dto.fixed.request.CreateFixedEventRequest;
import side.onetime.dto.fixed.response.FixedEventResponse;
import side.onetime.dto.fixed.response.FixedScheduleResponse;
import side.onetime.exception.CustomException;
import side.onetime.exception.status.FixedScheduleErrorStatus;
import side.onetime.exception.status.FixedErrorStatus;
import side.onetime.repository.FixedEventRepository;
import side.onetime.repository.FixedScheduleRepository;
import side.onetime.repository.FixedSelectionRepository;
import side.onetime.util.JwtUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class FixedScheduleService {
private final FixedEventRepository fixedEventRepository;
private final FixedScheduleRepository fixedScheduleRepository;
private final FixedSelectionRepository fixedSelectionRepository;
private final JwtUtil jwtUtil;

// 고정 스케줄 등록 메서드
@Transactional
Expand All @@ -32,7 +40,7 @@ public void createFixedSchedules(CreateFixedEventRequest createFixedEventRequest
String day = fixedScheduleResponse.timePoint();
List<String> times = fixedScheduleResponse.times();
List<FixedSchedule> fixedSchedules = fixedScheduleRepository.findAllByDay(day)
.orElseThrow(() -> new CustomException(FixedScheduleErrorStatus._NOT_FOUND_FIXED_SCHEDULES));
.orElseThrow(() -> new CustomException(FixedErrorStatus._NOT_FOUND_FIXED_SCHEDULES));

for (FixedSchedule fixedSchedule : fixedSchedules) {
if (times.contains(fixedSchedule.getTime())) {
Expand All @@ -45,4 +53,38 @@ public void createFixedSchedules(CreateFixedEventRequest createFixedEventRequest
}
fixedSelectionRepository.saveAll(fixedSelections);
}

// 전체 고정 스케줄 조회 메서드
@Transactional(readOnly = true)
public List<FixedEventResponse> getAllFixedSchedules(String authorizationHeader) {
User user = jwtUtil.getUserFromHeader(authorizationHeader);

// 유저의 고정 이벤트와 해당 이벤트에 대한 고정 선택 및 고정 스케줄을 QueryDSL로 조회
List<FixedEvent> fixedEvents = fixedEventRepository.findAllByUser(user);

if (fixedEvents.isEmpty()) {
throw new CustomException(FixedErrorStatus._NOT_FOUND_FIXED_EVENTS);
}

List<FixedEventResponse> fixedEventResponses = new ArrayList<>();
for (FixedEvent fixedEvent : fixedEvents) {
// 각 이벤트에 대한 고정 선택을 그룹화하여 요일별 스케줄을 생성
Map<String, List<String>> groupedSchedules = fixedEvent.getFixedSelections().stream()
.collect(Collectors.groupingBy(
selection -> selection.getFixedSchedule().getDay(),
Collectors.mapping(selection -> selection.getFixedSchedule().getTime(), Collectors.toList())
));

// 고정 스케줄 정보 생성
List<FixedScheduleResponse> scheduleResponses = groupedSchedules.entrySet().stream()
.map(entry -> FixedScheduleResponse.of(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());

// 고정 이벤트 정보 생성
FixedEventResponse fixedEventResponse = FixedEventResponse.of(fixedEvent.getTitle(), fixedEvent.getStartTime(), fixedEvent.getEndTime(), scheduleResponses);
fixedEventResponses.add(fixedEventResponse);
}

return fixedEventResponses;
}
}
Loading