Skip to content

Commit

Permalink
#90 [feat] : 유저는 마이페이지에서 고정 스케줄을 수정할 수 있다
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbang105 committed Oct 23, 2024
1 parent 544047c commit a4978ad
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 8 deletions.
22 changes: 20 additions & 2 deletions src/main/java/side/onetime/controller/FixedController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import side.onetime.dto.fixed.request.CreateFixedEventRequest;
import side.onetime.dto.fixed.request.ModifyFixedEventRequest;
import side.onetime.dto.fixed.response.FixedEventDetailResponse;
import side.onetime.dto.fixed.response.FixedEventResponse;
import side.onetime.global.common.ApiResponse;
Expand Down Expand Up @@ -46,10 +47,27 @@ public ResponseEntity<ApiResponse<List<FixedEventResponse>>> getAllFixedSchedule
@GetMapping("/{id}")
public ResponseEntity<ApiResponse<FixedEventDetailResponse>> getFixedScheduleDetail(
@RequestHeader("Authorization") String authorizationHeader,
@PathVariable("id") Long fixedScheduleId) {
@PathVariable("id") Long fixedEventId) {

FixedEventDetailResponse fixedEventDetailResponse = fixedScheduleService.getFixedScheduleDetail(authorizationHeader, fixedScheduleId);
FixedEventDetailResponse fixedEventDetailResponse = fixedScheduleService.getFixedScheduleDetail(authorizationHeader, fixedEventId);

return ApiResponse.onSuccess(SuccessStatus._GET_FIXED_SCHEDULE_DETAIL, fixedEventDetailResponse);
}

// 고정 이벤트 또는 스케줄 수정 API
@PatchMapping("/{id}")
public ResponseEntity<ApiResponse<Object>> modifyFixedEvent(
@RequestHeader("Authorization") String authorizationHeader,
@PathVariable("id") Long fixedEventId,
@RequestBody ModifyFixedEventRequest modifyFixedEventRequest) {

if (modifyFixedEventRequest.title() != null) {
fixedEventService.modifyFixedEvent(authorizationHeader, fixedEventId, modifyFixedEventRequest);
}
if (modifyFixedEventRequest.schedules() != null) {
fixedScheduleService.modifyFixedSchedule(authorizationHeader, fixedEventId, modifyFixedEventRequest);
}

return ApiResponse.onSuccess(SuccessStatus._MODIFY_FIXED_SCHEDULE);
}
}
12 changes: 12 additions & 0 deletions src/main/java/side/onetime/domain/FixedEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,16 @@ public FixedEvent(User user, String title, String startTime, String endTime) {
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
@@ -0,0 +1,28 @@
package side.onetime.dto.fixed.request;

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 @@ -48,6 +48,7 @@ public enum SuccessStatus implements BaseCode {
_CREATED_FIXED_SCHEDULE(HttpStatus.CREATED, "201", "고정 스케줄 등록에 성공했습니다."),
_GET_ALL_FIXED_SCHEDULES(HttpStatus.OK, "200", "전체 고정 스케줄 조회에 성공했습니다."),
_GET_FIXED_SCHEDULE_DETAIL(HttpStatus.OK, "200", "특정 고정 스케줄 상세 조회에 성공했습니다."),
_MODIFY_FIXED_SCHEDULE(HttpStatus.OK, "200", "고정 스케줄 수정에 성공했습니다."),
;

private final HttpStatus httpStatus;
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/side/onetime/service/FixedEventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import side.onetime.domain.FixedEvent;
import side.onetime.domain.User;
import side.onetime.dto.fixed.request.CreateFixedEventRequest;
import side.onetime.dto.fixed.request.ModifyFixedEventRequest;
import side.onetime.repository.FixedEventRepository;
import side.onetime.util.JwtUtil;

Expand All @@ -28,6 +29,15 @@ public void createFixedEvent(String authorizationHeader, CreateFixedEventRequest
String endTime = times.get(times.size() - 1);
FixedEvent fixedEvent = createFixedEventRequest.toEntity(user, startTime, endTime);
fixedEventRepository.save(fixedEvent);
fixedScheduleService.createFixedSchedules(createFixedEventRequest, fixedEvent);
fixedScheduleService.createFixedSchedules(createFixedEventRequest.schedules(), fixedEvent);
}

// 고정 이벤트 수정 메서드
@Transactional
public void modifyFixedEvent(String authorizationHeader, Long fixedEventId, ModifyFixedEventRequest modifyFixedEventRequest) {
User user = jwtUtil.getUserFromHeader(authorizationHeader);
FixedEvent fixedEvent = fixedEventRepository.findByUserAndId(user, fixedEventId);
fixedEvent.updateTitle(modifyFixedEventRequest.title());
fixedEventRepository.save(fixedEvent);
}
}
28 changes: 23 additions & 5 deletions src/main/java/side/onetime/service/FixedScheduleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import side.onetime.domain.FixedSchedule;
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.FixedEventDetailResponse;
import side.onetime.dto.fixed.response.FixedEventResponse;
import side.onetime.dto.fixed.response.FixedScheduleResponse;
Expand All @@ -23,6 +23,8 @@
import java.util.Map;
import java.util.stream.Collectors;

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

@Service
@RequiredArgsConstructor
public class FixedScheduleService {
Expand All @@ -33,8 +35,7 @@ public class FixedScheduleService {

// 고정 스케줄 등록 메서드
@Transactional
public void createFixedSchedules(CreateFixedEventRequest createFixedEventRequest, FixedEvent fixedEvent) {
List<FixedScheduleResponse> fixedScheduleResponses = createFixedEventRequest.schedules();
public void createFixedSchedules(List<FixedScheduleResponse> fixedScheduleResponses, FixedEvent fixedEvent) {
List<FixedSelection> fixedSelections = new ArrayList<>();

for (FixedScheduleResponse fixedScheduleResponse : fixedScheduleResponses) {
Expand Down Expand Up @@ -90,11 +91,11 @@ public List<FixedEventResponse> getAllFixedSchedules(String authorizationHeader)

// 특정 고정 스케줄 상세 조회 메서드
@Transactional(readOnly = true)
public FixedEventDetailResponse getFixedScheduleDetail(String authorizationHeader, Long fixedScheduleId) {
public FixedEventDetailResponse getFixedScheduleDetail(String authorizationHeader, Long fixedEventId) {
User user = jwtUtil.getUserFromHeader(authorizationHeader);

// 고정 이벤트 조회
FixedEvent fixedEvent = fixedEventRepository.findByUserAndFixedEventId(user, fixedScheduleId);
FixedEvent fixedEvent = fixedEventRepository.findByUserAndFixedEventIdCustom(user, fixedEventId);
if (fixedEvent == null) {
throw new CustomException(FixedErrorStatus._NOT_FOUND_FIXED_EVENT);
}
Expand All @@ -114,4 +115,21 @@ public FixedEventDetailResponse getFixedScheduleDetail(String authorizationHeade
// 고정 이벤트 상세 정보 반환
return FixedEventDetailResponse.of(fixedEvent.getTitle(), fixedEvent.getStartTime(), fixedEvent.getEndTime(), scheduleResponses);
}

// 고정 스케줄 수정 메서드
@Transactional
public void modifyFixedSchedule(String authorizationHeader, Long fixedEventId, ModifyFixedEventRequest modifyFixedEventRequest) {
User user = jwtUtil.getUserFromHeader(authorizationHeader);
FixedEvent fixedEvent = fixedEventRepository.findByUserAndId(user, fixedEventId);

List<String> times = modifyFixedEventRequest.schedules().get(0).times();
String startTime = times.get(0);
String endTime = times.get(times.size() - 1);
fixedEvent.updateStartTime(startTime);
fixedEvent.updateEndTime(addThirtyMinutes(endTime));
fixedEventRepository.save(fixedEvent);

fixedSelectionRepository.deleteFixedSelectionsByEvent(fixedEvent);
createFixedSchedules(modifyFixedEventRequest.schedules(), fixedEvent);
}
}

0 comments on commit a4978ad

Please sign in to comment.