Skip to content

Commit

Permalink
#52 [feat] : 로그인 유저가 스케줄을 등록할 수 있다
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbang105 committed Sep 30, 2024
1 parent 2a1dd98 commit 311415a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 18 deletions.
22 changes: 16 additions & 6 deletions src/main/java/side/onetime/controller/ScheduleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,28 @@ public class ScheduleController {
// 요일 스케줄 등록 API
@PostMapping("/day")
public ResponseEntity<ApiResponse<SuccessStatus>> createDaySchedules(
@RequestBody ScheduleDto.CreateDayScheduleRequest createDayScheduleRequest) {

scheduleService.createDaySchedules(createDayScheduleRequest);
@RequestBody ScheduleDto.CreateDayScheduleRequest createDayScheduleRequest,
@RequestHeader(value = "Authorization", required = false) String authorizationHeader) {

if (authorizationHeader != null) {
scheduleService.createDaySchedulesForAuthenticatedUser(createDayScheduleRequest, authorizationHeader);
} else {
scheduleService.createDaySchedulesForAnonymousUser(createDayScheduleRequest);
}
return ApiResponse.onSuccess(SuccessStatus._CREATED_DAY_SCHEDULES);
}

// 날짜 스케줄 등록 API
@PostMapping("/date")
public ResponseEntity<ApiResponse<SuccessStatus>> createDateSchedules(
@RequestBody ScheduleDto.CreateDateScheduleRequest createDateScheduleRequest) {

scheduleService.createDateSchedules(createDateScheduleRequest);
@RequestBody ScheduleDto.CreateDateScheduleRequest createDateScheduleRequest,
@RequestHeader(value = "Authorization", required = false) String authorizationHeader) {

if (authorizationHeader != null) {
scheduleService.createDateSchedulesForAuthenticatedUser(createDateScheduleRequest, authorizationHeader);
} else {
scheduleService.createDateSchedulesForAnonymousUser(createDateScheduleRequest);
}
return ApiResponse.onSuccess(SuccessStatus._CREATED_DATE_SCHEDULES);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import side.onetime.domain.Event;
import side.onetime.domain.Member;
import side.onetime.domain.Selection;
import side.onetime.domain.*;

import java.util.List;

public interface SelectionRepository extends JpaRepository<Selection, Long> {
void deleteAllByMember(Member member);
void deleteAllByUserAndScheduleIn(User user, List<Schedule> schedules);
@Query("SELECT s FROM Selection s JOIN FETCH s.schedule sc WHERE sc.event = :event")
List<Selection> findAllSelectionsByEvent(@Param("event") Event event);
}
82 changes: 73 additions & 9 deletions src/main/java/side/onetime/service/ScheduleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import side.onetime.domain.Event;
import side.onetime.domain.Member;
import side.onetime.domain.Schedule;
import side.onetime.domain.Selection;
import side.onetime.domain.*;
import side.onetime.dto.ScheduleDto;
import side.onetime.exception.*;
import side.onetime.repository.EventRepository;
import side.onetime.repository.MemberRepository;
import side.onetime.repository.ScheduleRepository;
import side.onetime.repository.SelectionRepository;
import side.onetime.util.JwtUtil;

import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;

Expand All @@ -25,10 +22,11 @@ public class ScheduleService {
private final MemberRepository memberRepository;
private final ScheduleRepository scheduleRepository;
private final SelectionRepository selectionRepository;
private final JwtUtil jwtUtil;

// 요일 스케줄 등록 메서드
// 요일 스케줄 등록 메서드 (비로그인)
@Transactional
public void createDaySchedules(ScheduleDto.CreateDayScheduleRequest createDayScheduleRequest) {
public void createDaySchedulesForAnonymousUser(ScheduleDto.CreateDayScheduleRequest createDayScheduleRequest) {
Event event = eventRepository.findByEventId(UUID.fromString(createDayScheduleRequest.getEventId()))
.orElseThrow(() -> new EventException(EventErrorResult._NOT_FOUND_EVENT));
Member member = memberRepository.findByMemberId(UUID.fromString(createDayScheduleRequest.getMemberId()))
Expand Down Expand Up @@ -56,9 +54,41 @@ public void createDaySchedules(ScheduleDto.CreateDayScheduleRequest createDaySch
selectionRepository.saveAll(selections);
}

// 날짜 스케줄 등록 메서드
// 요일 스케줄 등록 메서드 (로그인)
@Transactional
public void createDateSchedules(ScheduleDto.CreateDateScheduleRequest createDateScheduleRequest) {
public void createDaySchedulesForAuthenticatedUser(ScheduleDto.CreateDayScheduleRequest createDayScheduleRequest, String authorizationHeader) {
Event event = eventRepository.findByEventId(UUID.fromString(createDayScheduleRequest.getEventId()))
.orElseThrow(() -> new EventException(EventErrorResult._NOT_FOUND_EVENT));
User user = jwtUtil.getUserFromHeader(authorizationHeader);

List<ScheduleDto.DaySchedule> daySchedules = createDayScheduleRequest.getDaySchedules();
List<Selection> newSelections = new ArrayList<>();
List<Schedule> allSchedules = new ArrayList<>();

for (ScheduleDto.DaySchedule daySchedule : daySchedules) {
String day = daySchedule.getDay();
List<String> times = daySchedule.getTimes();
List<Schedule> schedules = scheduleRepository.findAllByEventAndDay(event, day)
.orElseThrow(() -> new ScheduleException(ScheduleErrorResult._NOT_FOUND_DAY_SCHEDULES));

for (Schedule schedule : schedules) {
if (times.contains(schedule.getTime())) {
newSelections.add(Selection.builder()
.user(user)
.schedule(schedule)
.build());
}
}
allSchedules.addAll(schedules);
}
selectionRepository.deleteAllByUserAndScheduleIn(user, allSchedules);
selectionRepository.saveAll(newSelections);
}


// 날짜 스케줄 등록 메서드 (비로그인)
@Transactional
public void createDateSchedulesForAnonymousUser(ScheduleDto.CreateDateScheduleRequest createDateScheduleRequest) {
Event event = eventRepository.findByEventId(UUID.fromString(createDateScheduleRequest.getEventId()))
.orElseThrow(() -> new EventException(EventErrorResult._NOT_FOUND_EVENT));
Member member = memberRepository.findByMemberId(UUID.fromString(createDateScheduleRequest.getMemberId()))
Expand Down Expand Up @@ -86,6 +116,40 @@ public void createDateSchedules(ScheduleDto.CreateDateScheduleRequest createDate
selectionRepository.saveAll(selections);
}

// 날짜 스케줄 등록 메서드 (로그인)
@Transactional
public void createDateSchedulesForAuthenticatedUser(ScheduleDto.CreateDateScheduleRequest createDateScheduleRequest, String authorizationHeader) {
Event event = eventRepository.findByEventId(UUID.fromString(createDateScheduleRequest.getEventId()))
.orElseThrow(() -> new EventException(EventErrorResult._NOT_FOUND_EVENT));
User user = jwtUtil.getUserFromHeader(authorizationHeader);

List<ScheduleDto.DateSchedule> dateSchedules = createDateScheduleRequest.getDateSchedules();
List<Selection> newSelections = new ArrayList<>();
List<Schedule> allSchedules = new ArrayList<>();

for (ScheduleDto.DateSchedule dateSchedule : dateSchedules) {
String date = dateSchedule.getDate();
List<String> times = dateSchedule.getTimes();

List<Schedule> schedules = scheduleRepository.findAllByEventAndDate(event, date)
.orElseThrow(() -> new ScheduleException(ScheduleErrorResult._NOT_FOUND_DATE_SCHEDULES));

for (Schedule schedule : schedules) {
if (times.contains(schedule.getTime())) {
newSelections.add(Selection.builder()
.user(user)
.schedule(schedule)
.build());
}
}

allSchedules.addAll(schedules);
}

selectionRepository.deleteAllByUserAndScheduleIn(user, allSchedules);
selectionRepository.saveAll(newSelections);
}

// 전체 요일 스케줄 반환 메서드
@Transactional
public List<ScheduleDto.PerDaySchedulesResponse> getAllDaySchedules(String eventId) {
Expand Down

0 comments on commit 311415a

Please sign in to comment.