Skip to content

Commit

Permalink
✨ [Feature] 개인일정 기간 조회 API Service 기능 추가 #1061
Browse files Browse the repository at this point in the history
  • Loading branch information
taehyeon committed Jan 7, 2025
1 parent b3ead11 commit 83be3a9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package gg.calendar.api.user.schedule.privateschedule.service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -9,6 +14,7 @@
import gg.calendar.api.user.schedule.privateschedule.controller.request.PrivateScheduleCreateReqDto;
import gg.calendar.api.user.schedule.privateschedule.controller.request.PrivateScheduleUpdateReqDto;
import gg.calendar.api.user.schedule.privateschedule.controller.response.PrivateScheduleDetailResDto;
import gg.calendar.api.user.schedule.privateschedule.controller.response.PrivateSchedulePeriodResDto;
import gg.calendar.api.user.schedule.privateschedule.controller.response.PrivateScheduleUpdateResDto;
import gg.data.calendar.PrivateSchedule;
import gg.data.calendar.PublicSchedule;
Expand Down Expand Up @@ -85,8 +91,24 @@ public PrivateScheduleDetailResDto getPrivateScheduleDetail(UserDto userDto, Lon
return PrivateScheduleDetailResDto.toDto(privateSchedule, scheduleGroup);
}

// public List<PrivateSchedulePeriodResDto> getPrivateSchedulePeriod(UserDto userDto, Long privateScheduleId) {
// }
public List<PrivateSchedulePeriodResDto> getPrivateSchedulePeriod(UserDto userDto, LocalDateTime startTime,
LocalDateTime endTime) {
User user = userRepository.getById(userDto.getId());
List<PrivateSchedule> privateSchedules = privateScheduleRepository.findOverlappingSchedulesByUser(startTime,
endTime, user);
Map<Long, ScheduleGroup> scheduleGroups = scheduleGroupRepository.findByUserId(userDto.getId()).stream()
.collect(Collectors.toMap(ScheduleGroup::getId, Function.identity(), (existing, replacement) -> existing));
List<PrivateSchedulePeriodResDto> response = new ArrayList<>();

for (PrivateSchedule privateSchedule : privateSchedules) {
ScheduleGroup scheduleGroup = scheduleGroups.get(privateSchedule.getGroupId());
if (scheduleGroup == null) {
throw new NotExistException(ErrorCode.SCHEDULE_GROUP_NOT_FOUND);
}
response.add(PrivateSchedulePeriodResDto.toDto(privateSchedule, scheduleGroup));
}
return response;
}

public void validateDetailClassification(DetailClassification classification) {
if (classification != DetailClassification.PRIVATE_SCHEDULE) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package gg.repo.calendar;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import gg.data.calendar.PrivateSchedule;
import gg.data.calendar.PublicSchedule;
import gg.data.user.User;

@Repository
public interface PrivateScheduleRepository extends JpaRepository<PrivateSchedule, Long> {
List<PrivateSchedule> findByPublicSchedule(PublicSchedule publicSchedule);

List<PrivateSchedule> findByUserId(Long userId);
@Query("SELECT pr FROM PrivateSchedule pr "
+ "JOIN pr.publicSchedule pu "
+ "WHERE NOT (pu.startTime > :endTime OR pu.endTime < :startTime) "
+ "AND pr.user = :user")
List<PrivateSchedule> findOverlappingSchedulesByUser(LocalDateTime startTime, LocalDateTime endTime, User user);
}

0 comments on commit 83be3a9

Please sign in to comment.