Skip to content

Commit

Permalink
Merge pull request #53 from onetime-with-members/feature/#52/login-us…
Browse files Browse the repository at this point in the history
…er-schedule

[feat] : 로그인 유저가 이벤트 생성 & 스케줄 등록을 할 수 있다
  • Loading branch information
bbbang105 authored Sep 30, 2024
2 parents cb2e91f + 311415a commit c91933f
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 28 deletions.
11 changes: 9 additions & 2 deletions src/main/java/side/onetime/controller/EventController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ public class EventController {
// 이벤트 생성 API
@PostMapping
public ResponseEntity<ApiResponse<EventDto.CreateEventResponse>> 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);
}

Expand Down
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
14 changes: 13 additions & 1 deletion src/main/java/side/onetime/domain/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -40,12 +44,20 @@ public class Event extends BaseEntity {
@OneToMany(mappedBy = "event",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Member> members;

@OneToMany(mappedBy = "event",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Schedule> 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;
}
}
7 changes: 6 additions & 1 deletion src/main/java/side/onetime/domain/Selection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
8 changes: 8 additions & 0 deletions src/main/java/side/onetime/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import lombok.NoArgsConstructor;
import side.onetime.global.common.dao.BaseEntity;

import java.util.List;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
Expand All @@ -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<Event> events;

@OneToMany(mappedBy = "user",cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Selection> selections;

@Builder
public User(String name, String email, String nickname, String provider, String providerId) {
this.name = name;
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);
}
34 changes: 28 additions & 6 deletions src/main/java/side/onetime/service/EventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand All @@ -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);

Expand All @@ -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<String> ranges, String startTime, String endTime) {
Expand Down
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 c91933f

Please sign in to comment.