Skip to content

Commit

Permalink
chore: 스케줄 패키지 세분화
Browse files Browse the repository at this point in the history
  • Loading branch information
SangWoon123 committed May 9, 2024
1 parent f908d7c commit 4075e8c
Show file tree
Hide file tree
Showing 21 changed files with 202 additions and 217 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.tukorea.planding.domain.schedule.controller;
package com.tukorea.planding.domain.schedule.attedance.controller;

import com.tukorea.planding.domain.schedule.dto.GroupScheduleAttendanceRequest;
import com.tukorea.planding.domain.schedule.service.GroupScheduleAttendanceService;
import com.tukorea.planding.domain.schedule.attedance.dto.GroupScheduleAttendanceRequest;
import com.tukorea.planding.domain.schedule.attedance.service.GroupScheduleAttendanceService;
import com.tukorea.planding.domain.user.dto.UserInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tukorea.planding.domain.schedule.dto;
package com.tukorea.planding.domain.schedule.attedance.dto;

import com.tukorea.planding.domain.schedule.entity.ScheduleStatus;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tukorea.planding.domain.schedule.repository;
package com.tukorea.planding.domain.schedule.attedance.repository;

import com.tukorea.planding.domain.schedule.entity.GroupScheduleAttendance;
import org.springframework.data.jpa.repository.JpaRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.tukorea.planding.domain.schedule.service;
package com.tukorea.planding.domain.schedule.attedance.service;

import com.tukorea.planding.domain.schedule.dto.GroupScheduleAttendanceRequest;
import com.tukorea.planding.domain.schedule.attedance.dto.GroupScheduleAttendanceRequest;
import com.tukorea.planding.domain.schedule.entity.GroupScheduleAttendance;
import com.tukorea.planding.domain.schedule.entity.Schedule;
import com.tukorea.planding.domain.schedule.repository.GroupScheduleAttendanceRepository;
import com.tukorea.planding.domain.schedule.repository.ScheduleRepository;
import com.tukorea.planding.domain.schedule.attedance.repository.GroupScheduleAttendanceRepository;
import com.tukorea.planding.domain.schedule.common.repository.ScheduleRepository;
import com.tukorea.planding.domain.user.dto.UserInfo;
import com.tukorea.planding.domain.user.entity.User;
import com.tukorea.planding.domain.user.service.UserQueryService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.tukorea.planding.domain.schedule.controller;
package com.tukorea.planding.domain.schedule.common.controller;

import com.tukorea.planding.common.CommonResponse;
import com.tukorea.planding.common.CommonUtils;
import com.tukorea.planding.domain.schedule.entity.ScheduleStatus;
import com.tukorea.planding.domain.schedule.service.ScheduleService;
import com.tukorea.planding.domain.schedule.common.service.ScheduleService;
import com.tukorea.planding.domain.user.dto.UserInfo;
import com.tukorea.planding.domain.schedule.dto.ScheduleRequest;
import com.tukorea.planding.domain.schedule.dto.ScheduleResponse;
import com.tukorea.planding.domain.schedule.common.dto.ScheduleRequest;
import com.tukorea.planding.domain.schedule.common.dto.ScheduleResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tukorea.planding.domain.schedule.dto;
package com.tukorea.planding.domain.schedule.common.dto;

import lombok.Builder;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.tukorea.planding.domain.schedule.dto;
package com.tukorea.planding.domain.schedule.common.dto;

import com.tukorea.planding.domain.schedule.entity.Schedule;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDate;
import java.time.LocalTime;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.tukorea.planding.domain.schedule.common.repository;

import com.tukorea.planding.domain.schedule.entity.Schedule;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ScheduleRepository extends JpaRepository<Schedule, Long> {
List<Schedule> findByGroupRoomId(Long groupRoomId);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tukorea.planding.domain.schedule.repository;
package com.tukorea.planding.domain.schedule.common.repository;

import com.tukorea.planding.domain.schedule.entity.Schedule;
import com.tukorea.planding.domain.user.entity.User;
Expand Down Expand Up @@ -35,4 +35,6 @@ public interface ScheduleRepositoryCustom {
* @return 중복되는 Schedule
*/
List<Schedule> findOverlapSchedules(Long userId, LocalDate date, LocalTime startDate, LocalTime endDate);

List<Schedule> showTodaySchedule(Long userId);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.tukorea.planding.domain.schedule.repository;
package com.tukorea.planding.domain.schedule.common.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.tukorea.planding.domain.schedule.entity.Schedule;
Expand Down Expand Up @@ -37,4 +37,15 @@ public List<Schedule> findOverlapSchedules(Long userId, LocalDate date, LocalTim
.or(schedule.endTime.between(startTime, endTime)))
.fetch();
}

@Override
public List<Schedule> showTodaySchedule(Long userId) {
LocalDate today = LocalDate.now();

return queryFactory
.selectFrom(schedule)
.where(schedule.user.id.eq(userId)
.and(schedule.scheduleDate.eq(today)))
.fetch();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.tukorea.planding.domain.schedule.service;
package com.tukorea.planding.domain.schedule.common.service;

import com.tukorea.planding.domain.schedule.dto.ScheduleRequest;
import com.tukorea.planding.domain.schedule.dto.ScheduleResponse;
import com.tukorea.planding.domain.schedule.common.dto.ScheduleRequest;
import com.tukorea.planding.domain.schedule.common.dto.ScheduleResponse;
import com.tukorea.planding.domain.schedule.entity.Schedule;
import com.tukorea.planding.domain.schedule.repository.ScheduleRepository;
import com.tukorea.planding.domain.schedule.repository.ScheduleRepositoryCustom;
import com.tukorea.planding.domain.schedule.common.repository.ScheduleRepository;
import com.tukorea.planding.domain.schedule.common.repository.ScheduleRepositoryCustom;
import com.tukorea.planding.global.error.BusinessException;
import com.tukorea.planding.global.error.ErrorCode;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.tukorea.planding.domain.schedule.service;
package com.tukorea.planding.domain.schedule.common.service;

import com.tukorea.planding.domain.group.repository.UserGroupMembershipRepositoryCustomImpl;
import com.tukorea.planding.domain.schedule.entity.ScheduleStatus;
import com.tukorea.planding.domain.schedule.repository.ScheduleRepositoryCustomImpl;
import com.tukorea.planding.domain.group.repository.UserGroupRepositoryCustom;
import com.tukorea.planding.domain.schedule.common.repository.ScheduleRepositoryCustomImpl;
import com.tukorea.planding.domain.user.entity.User;
import com.tukorea.planding.domain.user.dto.UserInfo;
import com.tukorea.planding.domain.user.repository.UserRepository;
import com.tukorea.planding.domain.user.service.UserQueryService;
import com.tukorea.planding.global.error.BusinessException;
import com.tukorea.planding.global.error.ErrorCode;
import com.tukorea.planding.domain.schedule.repository.ScheduleRepository;
import com.tukorea.planding.domain.schedule.entity.Schedule;
import com.tukorea.planding.domain.schedule.dto.ScheduleRequest;
import com.tukorea.planding.domain.schedule.dto.ScheduleResponse;
import com.tukorea.planding.domain.schedule.common.dto.ScheduleRequest;
import com.tukorea.planding.domain.schedule.common.dto.ScheduleResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -25,13 +23,13 @@
@RequiredArgsConstructor
public class ScheduleService {

private final ScheduleRepository scheduleRepository;
private final ScheduleRepositoryCustomImpl scheduleRepositoryCustom;
private final UserRepository userRepository;
private final UserGroupMembershipRepositoryCustomImpl userGroupMembershipRepositoryCustomImpl;
private final ScheduleQueryService scheduleQueryService;
private final ScheduleRepositoryCustomImpl scheduleRepositoryCustomImpl;
private final UserQueryService userQueryService;
private final UserGroupRepositoryCustom userGroupRepositoryCustom;

public ScheduleResponse createSchedule(UserInfo userInfo, ScheduleRequest scheduleRequest) {
User user = validateUserByUserCode(userInfo.getUserCode());
User user = userQueryService.getUserByUserCode(userInfo.getUserCode());

Schedule newSchedule = Schedule.builder()
.user(user)
Expand All @@ -43,15 +41,15 @@ public ScheduleResponse createSchedule(UserInfo userInfo, ScheduleRequest schedu
.isComplete(false)
.build();

Schedule save = scheduleRepository.save(newSchedule);
Schedule save = scheduleQueryService.save(newSchedule);

return ScheduleResponse.from(save);
}

public ScheduleResponse getSchedule(Long scheduleId, UserInfo userInfo) {
User user = validateUserByUserCode(userInfo.getUserCode());
Schedule schedule = scheduleRepository.findById(scheduleId)
.orElseThrow(() -> new BusinessException(ErrorCode.SCHEDULE_NOT_FOUND));
User user = userQueryService.getUserByUserCode(userInfo.getUserCode());

Schedule schedule = scheduleQueryService.findScheduleById(scheduleId);

if (!schedule.getUser().getId().equals(user.getId())) {
throw new BusinessException(ErrorCode.UNAUTHORIZED_SCHEDULE);
Expand All @@ -61,20 +59,21 @@ public ScheduleResponse getSchedule(Long scheduleId, UserInfo userInfo) {
}

public void deleteSchedule(UserInfo userInfo, Long scheduleId) {
Schedule schedule = findScheduleById(scheduleId);
Schedule schedule = scheduleQueryService.findScheduleById(scheduleId);

User user = schedule.getUser();

if (!user.getUserCode().equals(userInfo.getUserCode())) {
throw new BusinessException(ErrorCode.UNAUTHORIZED_SCHEDULE);
}

scheduleRepository.delete(schedule);
scheduleQueryService.delete(schedule);
}

public List<ScheduleResponse> getWeekSchedule(LocalDate startDate, LocalDate endDate, UserInfo userInfo) {
User user = validateUserByUserCode(userInfo.getUserCode());
User user = userQueryService.getUserByUserCode(userInfo.getUserCode());

List<Schedule> schedules = scheduleRepositoryCustom.findWeeklyScheduleByUser(startDate, endDate, user);
List<Schedule> schedules = scheduleRepositoryCustomImpl.findWeeklyScheduleByUser(startDate, endDate, user);

List<ScheduleResponse> scheduleResponses = schedules.stream()
.map(ScheduleResponse::from)
Expand All @@ -86,10 +85,10 @@ public List<ScheduleResponse> getWeekSchedule(LocalDate startDate, LocalDate end

public ScheduleResponse updateSchedule(Long scheduleId, ScheduleRequest scheduleRequest, UserInfo userInfo) {
// [1] 유저 확인
User user = validateUserByUserCode(userInfo.getUserCode());
User user = userQueryService.getUserByUserCode(userInfo.getUserCode());

// [2] 스케줄 확인
Schedule schedule = findScheduleById(scheduleId);
Schedule schedule = scheduleQueryService.findScheduleById(scheduleId);

// [3] 유저가 스케줄 업데이트할 수 있는 권한인지 체크
if (!schedule.getUser().getId().equals(user.getId())) {
Expand All @@ -106,14 +105,24 @@ public ScheduleResponse updateSchedule(Long scheduleId, ScheduleRequest schedule
그룹룸 스케줄관련 코드
*/
public ScheduleResponse getGroupSchedule(Long groupRoomId, Long scheduleId, UserInfo userInfo) {
if (!userGroupRepositoryCustom.existsByGroupRoomIdAndUserId(groupRoomId, userInfo.getId())) {
throw new BusinessException(ErrorCode.ACCESS_DENIED);
}

Schedule schedule = scheduleQueryService.findScheduleById(scheduleId);

return ScheduleResponse.from(schedule);
}

public List<ScheduleResponse> getSchedulesByGroupRoom(Long groupRoomId, UserInfo userInfo) {
// [1] 유저가 그룹룸에 접근할 권리가있는지 확인
if (!userGroupMembershipRepositoryCustomImpl.existsByGroupRoomIdAndUserId(groupRoomId, userInfo.getId())) {
if (!userGroupRepositoryCustom.existsByGroupRoomIdAndUserId(groupRoomId, userInfo.getId())) {
throw new BusinessException(ErrorCode.ACCESS_DENIED);
}

// [2] 그룹룸 ID를 기반으로 스케줄을 조회
List<Schedule> schedules = scheduleRepository.findByGroupRoomId(groupRoomId);
List<Schedule> schedules = scheduleQueryService.findByGroupRoomId(groupRoomId);

// [3] dto 반환
return schedules.stream()
Expand All @@ -123,62 +132,29 @@ public List<ScheduleResponse> getSchedulesByGroupRoom(Long groupRoomId, UserInfo

public ScheduleResponse updateScheduleByGroupRoom(Long groupRoomId, Long scheduleId, ScheduleRequest scheduleRequest, UserInfo userInfo) {
// [1] 그룹룸에 수정하려는 유저가 존재하는지 확인
if (!userGroupMembershipRepositoryCustomImpl.existsByGroupRoomIdAndUserId(groupRoomId, userInfo.getId())) {
if (!userGroupRepositoryCustom.existsByGroupRoomIdAndUserId(groupRoomId, userInfo.getId())) {
throw new BusinessException(ErrorCode.ACCESS_DENIED);
}

// [2] 스케줄을 업데이트
Schedule schedule = findScheduleById(scheduleId);
Schedule schedule = scheduleQueryService.findScheduleById(scheduleId);
schedule.update(scheduleRequest.title(), scheduleRequest.content(), scheduleRequest.startTime(), scheduleRequest.endTime());

// [3] dto 반환
return ScheduleResponse.from(schedule);
}


public void deleteScheduleByGroupRoom(Long groupRoomId, Long scheduleId, UserInfo userInfo) {
if (!userGroupMembershipRepositoryCustomImpl.existsByGroupRoomIdAndUserId(groupRoomId, userInfo.getId())) {
if (!userGroupRepositoryCustom.existsByGroupRoomIdAndUserId(groupRoomId, userInfo.getId())) {
throw new BusinessException(ErrorCode.ACCESS_DENIED);
}
scheduleRepository.deleteById(scheduleId);
scheduleQueryService.deleteById(scheduleId);
}

public ScheduleResponse updateScheduleStatus(Long scheduleId, ScheduleStatus status) {
Schedule schedule = scheduleRepository.findById(scheduleId)
.orElseThrow(() -> new BusinessException(ErrorCode.SCHEDULE_NOT_FOUND));

switch (status) {
case POSSIBLE:
schedule.markAsPossible();
break;
case IMPOSSIBLE:
schedule.markAsImpossible();
break;
case UNDECIDED:
schedule.markAsUndecided();
break;
default:
throw new IllegalArgumentException("Invalid status");
}
return ScheduleResponse.from(schedule);
}

/*
공통 로직
*/
public List<ScheduleResponse> findOverlapSchedule(Long userId, ScheduleRequest scheduleRequest) {
List<Schedule> overlapSchedules = scheduleRepositoryCustom.findOverlapSchedules(userId, scheduleRequest.scheduleDate(), scheduleRequest.startTime(), scheduleRequest.endTime());
return overlapSchedules.stream()
.map(ScheduleResponse::from)
.collect(Collectors.toList());
return scheduleQueryService.findOverlapSchedule(userId, scheduleRequest);
}

private User validateUserByUserCode(String userCode) {
return userRepository.findByUserCode(userCode)
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
}

private Schedule findScheduleById(Long scheduleId) {
return scheduleRepository.findById(scheduleId)
.orElseThrow(() -> new BusinessException(ErrorCode.SCHEDULE_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.tukorea.planding.domain.schedule.controller;
package com.tukorea.planding.domain.schedule.group.controller;

import com.tukorea.planding.common.CommonResponse;
import com.tukorea.planding.common.CommonUtils;
import com.tukorea.planding.domain.schedule.service.GroupScheduleService;
import com.tukorea.planding.domain.schedule.dto.ScheduleRequest;
import com.tukorea.planding.domain.schedule.dto.ScheduleResponse;
import com.tukorea.planding.domain.schedule.group.service.GroupScheduleService;
import com.tukorea.planding.domain.schedule.common.dto.ScheduleRequest;
import com.tukorea.planding.domain.schedule.common.dto.ScheduleResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.tukorea.planding.domain.schedule.service;
package com.tukorea.planding.domain.schedule.group.service;

import com.tukorea.planding.domain.group.entity.GroupRoom;
import com.tukorea.planding.domain.group.repository.GroupRoomRepository;
import com.tukorea.planding.domain.group.repository.UserGroupRepositoryCustom;
import com.tukorea.planding.domain.notify.dto.NotificationScheduleRequest;
import com.tukorea.planding.domain.notify.entity.NotificationType;
import com.tukorea.planding.domain.notify.service.NotificationService;
import com.tukorea.planding.domain.schedule.dto.ScheduleRequest;
import com.tukorea.planding.domain.schedule.common.dto.ScheduleRequest;
import com.tukorea.planding.domain.schedule.entity.Schedule;
import com.tukorea.planding.domain.user.entity.User;
import com.tukorea.planding.domain.user.service.UserQueryService;
import com.tukorea.planding.global.error.BusinessException;
import com.tukorea.planding.global.error.ErrorCode;
import com.tukorea.planding.domain.schedule.repository.ScheduleRepository;
import com.tukorea.planding.domain.schedule.dto.ScheduleResponse;
import com.tukorea.planding.domain.schedule.common.repository.ScheduleRepository;
import com.tukorea.planding.domain.schedule.common.dto.ScheduleResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand Down

This file was deleted.

Loading

0 comments on commit 4075e8c

Please sign in to comment.