From 3770e9b8fe3b6ba4f72ece2d381eb7d86259efd9 Mon Sep 17 00:00:00 2001 From: bbbang105 <2018111366@dgu.ac.kr> Date: Tue, 1 Oct 2024 03:24:17 +0900 Subject: [PATCH 1/3] =?UTF-8?q?#52=20[feat]=20:=20=EC=97=B0=EA=B4=80?= =?UTF-8?q?=EA=B4=80=EA=B3=84=EB=A5=BC=20=EC=B6=94=EA=B0=80=ED=95=9C?= =?UTF-8?q?=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/side/onetime/domain/Event.java | 14 +++++++++++++- src/main/java/side/onetime/domain/Selection.java | 7 ++++++- src/main/java/side/onetime/domain/User.java | 8 ++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/java/side/onetime/domain/Event.java b/src/main/java/side/onetime/domain/Event.java index ec858a3..88a069a 100644 --- a/src/main/java/side/onetime/domain/Event.java +++ b/src/main/java/side/onetime/domain/Event.java @@ -24,6 +24,10 @@ public class Event extends BaseEntity { @Column(name = "events_uuid", columnDefinition = "BINARY(16)", unique = true) private UUID eventId; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "users_id", foreignKey = @ForeignKey(name = "events_fk_users_id")) + private User user; + @Column(name = "title", nullable = false, length = 30) private String title; @@ -40,12 +44,20 @@ public class Event extends BaseEntity { @OneToMany(mappedBy = "event",cascade = CascadeType.ALL, fetch = FetchType.LAZY) private List members; + @OneToMany(mappedBy = "event",cascade = CascadeType.ALL, fetch = FetchType.LAZY) + private List schedules; + @Builder - public Event(UUID eventId, String title, String startTime, String endTime, Category category) { + public Event(UUID eventId, User user, String title, String startTime, String endTime, Category category) { this.eventId = eventId; + this.user = user; this.title = title; this.startTime = startTime; this.endTime = endTime; this.category = category; } + + public void addUser(User user) { + this.user = user; + } } \ No newline at end of file diff --git a/src/main/java/side/onetime/domain/Selection.java b/src/main/java/side/onetime/domain/Selection.java index 1237613..22c86f8 100644 --- a/src/main/java/side/onetime/domain/Selection.java +++ b/src/main/java/side/onetime/domain/Selection.java @@ -21,13 +21,18 @@ public class Selection extends BaseEntity { @JoinColumn(name = "members_id", foreignKey = @ForeignKey(name = "selections_fk_members_id")) private Member member; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "users_id", foreignKey = @ForeignKey(name = "selections_fk_users_id")) + private User user; + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "schedules_id", foreignKey = @ForeignKey(name = "selections_fk_schedules_id")) private Schedule schedule; @Builder - public Selection(Member member, Schedule schedule) { + public Selection(Member member, User user, Schedule schedule) { this.member = member; + this.user = user; this.schedule = schedule; } } \ No newline at end of file diff --git a/src/main/java/side/onetime/domain/User.java b/src/main/java/side/onetime/domain/User.java index f47ad1d..c7a9729 100644 --- a/src/main/java/side/onetime/domain/User.java +++ b/src/main/java/side/onetime/domain/User.java @@ -7,6 +7,8 @@ import lombok.NoArgsConstructor; import side.onetime.global.common.dao.BaseEntity; +import java.util.List; + @Entity @NoArgsConstructor(access = AccessLevel.PROTECTED) @Getter @@ -32,6 +34,12 @@ public class User extends BaseEntity { @Column(name = "provider_id", nullable = false, length = 50) private String providerId; + @OneToMany(mappedBy = "user",cascade = CascadeType.ALL, fetch = FetchType.LAZY) + private List events; + + @OneToMany(mappedBy = "user",cascade = CascadeType.ALL, fetch = FetchType.LAZY) + private List selections; + @Builder public User(String name, String email, String nickname, String provider, String providerId) { this.name = name; From 2a1dd98e3feab83f01f17303b226d87d859abf41 Mon Sep 17 00:00:00 2001 From: bbbang105 <2018111366@dgu.ac.kr> Date: Tue, 1 Oct 2024 03:25:21 +0900 Subject: [PATCH 2/3] =?UTF-8?q?#52=20[feat]=20:=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=9D=B8=20=EC=9C=A0=EC=A0=80=EA=B0=80=20=EC=9D=B4=EB=B2=A4?= =?UTF-8?q?=ED=8A=B8=EB=A5=BC=20=EC=83=9D=EC=84=B1=ED=95=A0=20=EC=88=98=20?= =?UTF-8?q?=EC=9E=88=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../onetime/controller/EventController.java | 11 ++++-- .../side/onetime/service/EventService.java | 34 +++++++++++++++---- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/main/java/side/onetime/controller/EventController.java b/src/main/java/side/onetime/controller/EventController.java index 17aa2b4..89b4d1a 100644 --- a/src/main/java/side/onetime/controller/EventController.java +++ b/src/main/java/side/onetime/controller/EventController.java @@ -19,9 +19,16 @@ public class EventController { // 이벤트 생성 API @PostMapping public ResponseEntity> createEvent( - @RequestBody EventDto.CreateEventRequest createEventRequest) { + @RequestBody EventDto.CreateEventRequest createEventRequest, + @RequestHeader(value = "Authorization", required = false) String authorizationHeader) { + + EventDto.CreateEventResponse createEventResponse; + if (authorizationHeader != null) { + createEventResponse = eventService.createEventForAuthenticatedUser(createEventRequest, authorizationHeader); + } else { + createEventResponse = eventService.createEventForAnonymousUser(createEventRequest); + } - EventDto.CreateEventResponse createEventResponse = eventService.createEvent(createEventRequest); return ApiResponse.onSuccess(SuccessStatus._CREATED_EVENT, createEventResponse); } diff --git a/src/main/java/side/onetime/service/EventService.java b/src/main/java/side/onetime/service/EventService.java index 8a951be..cc37c19 100644 --- a/src/main/java/side/onetime/service/EventService.java +++ b/src/main/java/side/onetime/service/EventService.java @@ -3,10 +3,7 @@ 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.EventDto; import side.onetime.exception.EventErrorResult; import side.onetime.exception.EventException; @@ -17,6 +14,7 @@ import side.onetime.repository.ScheduleRepository; import side.onetime.repository.SelectionRepository; import side.onetime.util.DateUtil; +import side.onetime.util.JwtUtil; import java.time.LocalTime; import java.util.*; @@ -29,10 +27,11 @@ public class EventService { private final EventRepository eventRepository; private final ScheduleRepository scheduleRepository; private final SelectionRepository selectionRepository; + private final JwtUtil jwtUtil; - // 이벤트 생성 메서드 + // 이벤트 생성 메서드 (비로그인) @Transactional - public EventDto.CreateEventResponse createEvent(EventDto.CreateEventRequest createEventRequest) { + public EventDto.CreateEventResponse createEventForAnonymousUser(EventDto.CreateEventRequest createEventRequest) { Event event = createEventRequest.to(); eventRepository.save(event); @@ -51,6 +50,29 @@ public EventDto.CreateEventResponse createEvent(EventDto.CreateEventRequest crea return EventDto.CreateEventResponse.of(event); } + // 이벤트 생성 메서드 (로그인) + @Transactional + public EventDto.CreateEventResponse createEventForAuthenticatedUser(EventDto.CreateEventRequest createEventRequest, String authorizationHeader) { + User user = jwtUtil.getUserFromHeader(authorizationHeader); + Event event = createEventRequest.to(); + event.addUser(user); + eventRepository.save(event); + + if (createEventRequest.getCategory().equals(Category.DATE)) { + if (!isDateFormat(createEventRequest.getRanges().get(0))) { + throw new EventException(EventErrorResult._IS_NOT_DATE_FORMAT); + } + createAndSaveDateSchedules(event, createEventRequest.getRanges(), createEventRequest.getStartTime(), createEventRequest.getEndTime()); + } else { + if (isDateFormat(createEventRequest.getRanges().get(0))) { + throw new EventException(EventErrorResult._IS_NOT_DAY_FORMAT); + } + createAndSaveDaySchedules(event, createEventRequest.getRanges(), createEventRequest.getStartTime(), createEventRequest.getEndTime()); + } + + return EventDto.CreateEventResponse.of(event); + } + // 날짜 스케줄을 생성하고 저장하는 메서드 @Transactional protected void createAndSaveDateSchedules(Event event, List ranges, String startTime, String endTime) { From 311415aacbd55a39eec18f3b7978a5be4776b0e3 Mon Sep 17 00:00:00 2001 From: bbbang105 <2018111366@dgu.ac.kr> Date: Tue, 1 Oct 2024 03:25:44 +0900 Subject: [PATCH 3/3] =?UTF-8?q?#52=20[feat]=20:=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=9D=B8=20=EC=9C=A0=EC=A0=80=EA=B0=80=20=EC=8A=A4=EC=BC=80?= =?UTF-8?q?=EC=A4=84=EC=9D=84=20=EB=93=B1=EB=A1=9D=ED=95=A0=20=EC=88=98=20?= =?UTF-8?q?=EC=9E=88=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ScheduleController.java | 22 +++-- .../repository/SelectionRepository.java | 5 +- .../side/onetime/service/ScheduleService.java | 82 +++++++++++++++++-- 3 files changed, 91 insertions(+), 18 deletions(-) diff --git a/src/main/java/side/onetime/controller/ScheduleController.java b/src/main/java/side/onetime/controller/ScheduleController.java index 903f597..265dc3f 100644 --- a/src/main/java/side/onetime/controller/ScheduleController.java +++ b/src/main/java/side/onetime/controller/ScheduleController.java @@ -19,18 +19,28 @@ public class ScheduleController { // 요일 스케줄 등록 API @PostMapping("/day") public ResponseEntity> 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> 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); } diff --git a/src/main/java/side/onetime/repository/SelectionRepository.java b/src/main/java/side/onetime/repository/SelectionRepository.java index 66d9a29..6f01c69 100644 --- a/src/main/java/side/onetime/repository/SelectionRepository.java +++ b/src/main/java/side/onetime/repository/SelectionRepository.java @@ -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 { void deleteAllByMember(Member member); + void deleteAllByUserAndScheduleIn(User user, List schedules); @Query("SELECT s FROM Selection s JOIN FETCH s.schedule sc WHERE sc.event = :event") List findAllSelectionsByEvent(@Param("event") Event event); } \ No newline at end of file diff --git a/src/main/java/side/onetime/service/ScheduleService.java b/src/main/java/side/onetime/service/ScheduleService.java index d77349f..0998a74 100644 --- a/src/main/java/side/onetime/service/ScheduleService.java +++ b/src/main/java/side/onetime/service/ScheduleService.java @@ -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; @@ -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())) @@ -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 daySchedules = createDayScheduleRequest.getDaySchedules(); + List newSelections = new ArrayList<>(); + List allSchedules = new ArrayList<>(); + + for (ScheduleDto.DaySchedule daySchedule : daySchedules) { + String day = daySchedule.getDay(); + List times = daySchedule.getTimes(); + List 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())) @@ -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 dateSchedules = createDateScheduleRequest.getDateSchedules(); + List newSelections = new ArrayList<>(); + List allSchedules = new ArrayList<>(); + + for (ScheduleDto.DateSchedule dateSchedule : dateSchedules) { + String date = dateSchedule.getDate(); + List times = dateSchedule.getTimes(); + + List 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 getAllDaySchedules(String eventId) {