Skip to content

Commit

Permalink
Merge pull request #56 from onetime-with-members/feature/#54/get-all
Browse files Browse the repository at this point in the history
[feat] : 유저의 요일 & 날짜 스케줄을 조회할 수 있다
  • Loading branch information
bbbang105 authored Oct 1, 2024
2 parents 637d0cb + d3594ff commit 91fbf6e
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 24 deletions.
24 changes: 22 additions & 2 deletions src/main/java/side/onetime/controller/ScheduleController.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ResponseEntity<ApiResponse<List<ScheduleDto.PerDaySchedulesResponse>>> ge
return ApiResponse.onSuccess(SuccessStatus._GET_ALL_DAY_SCHEDULES, perDaySchedulesResponses);
}

// 개인 요일 스케줄 조회 API
// 개인 요일 스케줄 조회 API (비로그인)
@GetMapping("/day/{event_id}/{member_id}")
public ResponseEntity<ApiResponse<ScheduleDto.PerDaySchedulesResponse>> getMemberDaySchedules(
@PathVariable("event_id") String eventId,
Expand All @@ -63,6 +63,16 @@ public ResponseEntity<ApiResponse<ScheduleDto.PerDaySchedulesResponse>> getMembe
return ApiResponse.onSuccess(SuccessStatus._GET_MEMBER_DAY_SCHEDULES, perDaySchedulesResponse);
}

// 개인 요일 스케줄 조회 API (로그인)
@GetMapping("/day/{event_id}/user")
public ResponseEntity<ApiResponse<ScheduleDto.PerDaySchedulesResponse>> getUserDaySchedules(
@PathVariable("event_id") String eventId,
@RequestHeader(value = "Authorization") String authorizationHeader) {

ScheduleDto.PerDaySchedulesResponse perDaySchedulesResponse = scheduleService.getUserDaySchedules(eventId, authorizationHeader);
return ApiResponse.onSuccess(SuccessStatus._GET_USER_DAY_SCHEDULES, perDaySchedulesResponse);
}

// 멤버 필터링 요일 스케줄 조회 API
@GetMapping("/day/action-filtering")
public ResponseEntity<ApiResponse<List<ScheduleDto.PerDaySchedulesResponse>>> getFilteredDaySchedules(
Expand All @@ -81,7 +91,7 @@ public ResponseEntity<ApiResponse<List<ScheduleDto.PerDateSchedulesResponse>>> g
return ApiResponse.onSuccess(SuccessStatus._GET_ALL_DATE_SCHEDULES, perDateSchedulesResponses);
}

// 개인 날짜 스케줄 조회 API
// 개인 날짜 스케줄 조회 API (비로그인)
@GetMapping("/date/{event_id}/{member_id}")
public ResponseEntity<ApiResponse<ScheduleDto.PerDateSchedulesResponse>> getMemberDateSchedules(
@PathVariable("event_id") String eventId,
Expand All @@ -91,6 +101,16 @@ public ResponseEntity<ApiResponse<ScheduleDto.PerDateSchedulesResponse>> getMemb
return ApiResponse.onSuccess(SuccessStatus._GET_MEMBER_DATE_SCHEDULES, perDateSchedulesResponse);
}

// 개인 날짜 스케줄 조회 API (로그인)
@GetMapping("/date/{event_id}/user")
public ResponseEntity<ApiResponse<ScheduleDto.PerDateSchedulesResponse>> getUserDateSchedules(
@PathVariable("event_id") String eventId,
@RequestHeader(value = "Authorization") String authorizationHeader) {

ScheduleDto.PerDateSchedulesResponse perDateSchedulesResponse = scheduleService.getUserDateSchedules(eventId, authorizationHeader);
return ApiResponse.onSuccess(SuccessStatus._GET_USER_DATE_SCHEDULES, perDateSchedulesResponse);
}

// 멤버 필터링 날짜 스케줄 조회 API
@GetMapping("/date/action-filtering")
public ResponseEntity<ApiResponse<List<ScheduleDto.PerDateSchedulesResponse>>> getFilteredDateSchedules(
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/side/onetime/dto/EventDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import side.onetime.domain.Event;
import side.onetime.domain.Member;
import side.onetime.domain.Schedule;
import side.onetime.domain.User;
import side.onetime.global.common.constant.Category;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -91,10 +93,18 @@ public static GetEventResponse of(Event event, List<String> ranges) {
public static class GetParticipantsResponse {
private List<String> names;

public static GetParticipantsResponse of(List<Member> members) {
List<String> names = members.stream()
public static GetParticipantsResponse of(List<Member> members, List<User> users) {
List<String> names = new ArrayList<>();

// 멤버 이름 추가
names.addAll(members.stream()
.map(Member::getName)
.collect(Collectors.toList());
.collect(Collectors.toList()));

// 유저 닉네임 추가
names.addAll(users.stream()
.map(User::getNickname)
.collect(Collectors.toList()));

return GetParticipantsResponse.builder()
.names(names)
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/side/onetime/dto/ScheduleDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import side.onetime.domain.Member;
import side.onetime.domain.Selection;

import java.util.ArrayList;
Expand Down Expand Up @@ -98,9 +97,9 @@ public static class PerDaySchedulesResponse {
@JsonProperty("schedules")
private List<DaySchedule> daySchedules;

public static PerDaySchedulesResponse of(Member member, List<DaySchedule> daySchedules) {
public static PerDaySchedulesResponse of(String name, List<DaySchedule> daySchedules) {
return PerDaySchedulesResponse.builder()
.name(member.getName())
.name(name)
.daySchedules(daySchedules)
.build();
}
Expand All @@ -117,9 +116,9 @@ public static class PerDateSchedulesResponse {
@JsonProperty("schedules")
private List<DateSchedule> dateSchedules;

public static PerDateSchedulesResponse of(Member member, List<DateSchedule> dateSchedules) {
public static PerDateSchedulesResponse of(String name, List<DateSchedule> dateSchedules) {
return PerDateSchedulesResponse.builder()
.name(member.getName())
.name(name)
.dateSchedules(dateSchedules)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ public enum SuccessStatus implements BaseCode {
_CREATED_DAY_SCHEDULES(HttpStatus.CREATED, "201", "요일 스케줄 등록에 성공했습니다."),
_CREATED_DATE_SCHEDULES(HttpStatus.CREATED, "201", "날짜 스케줄 등록에 성공했습니다."),
_GET_ALL_DAY_SCHEDULES(HttpStatus.OK, "200", "전체 요일 스케줄 조회에 성공했습니다."),
_GET_MEMBER_DAY_SCHEDULES(HttpStatus.OK, "200", "개인 요일 스케줄 조회에 성공했습니다."),
_GET_MEMBER_DAY_SCHEDULES(HttpStatus.OK, "200", "개인(비로그인) 요일 스케줄 조회에 성공했습니다."),
_GET_USER_DAY_SCHEDULES(HttpStatus.OK, "200", "개인(로그인) 요일 스케줄 조회에 성공했습니다."),
_GET_ALL_DATE_SCHEDULES(HttpStatus.OK, "200", "전체 날짜 스케줄 조회에 성공했습니다."),
_GET_MEMBER_DATE_SCHEDULES(HttpStatus.OK, "200", "개인 날짜 스케줄 조회에 성공했습니다."),
_GET_MEMBER_DATE_SCHEDULES(HttpStatus.OK, "200", "개인(비로그인)날짜 스케줄 조회에 성공했습니다."),
_GET_USER_DATE_SCHEDULES(HttpStatus.OK, "200", "개인(로그인) 날짜 스케줄 조회에 성공했습니다."),
_GET_FILTERED_DAY_SCHEDULES(HttpStatus.OK, "200", "멤버 필터링 요일 스케줄 조회에 성공했습니다."),
_GET_FILTERED_DATE_SCHEDULES(HttpStatus.OK, "200", "멤버 필터링 날짜 스케줄 조회에 성공했습니다."),
// URL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package side.onetime.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import side.onetime.domain.Event;
import side.onetime.domain.EventParticipation;

import java.util.List;

public interface EventParticipationRepository extends JpaRepository<EventParticipation,Long> {
List<EventParticipation> findAllByEvent(Event event);
}
38 changes: 34 additions & 4 deletions src/main/java/side/onetime/service/EventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,16 @@ public EventDto.GetEventResponse getEvent(String eventId) {
public EventDto.GetParticipantsResponse getParticipants(String eventId) {
Event event = eventRepository.findByEventId(UUID.fromString(eventId))
.orElseThrow(() -> new EventException(EventErrorResult._NOT_FOUND_EVENT));

// 이벤트에 참여하는 모든 멤버
List<Member> members = event.getMembers();
// 이벤트에 참여하는 모든 유저
List<EventParticipation> eventParticipations = eventParticipationRepository.findAllByEvent(event);
List<User> users = eventParticipations.stream()
.map(EventParticipation::getUser)
.toList();

return EventDto.GetParticipantsResponse.of(members);
return EventDto.GetParticipantsResponse.of(members, users);
}

// 가장 많이 되는 시간 조회 메서드
Expand All @@ -142,31 +149,54 @@ public List<EventDto.GetMostPossibleTime> getMostPossibleTime(String eventId) {
Event event = eventRepository.findByEventId(UUID.fromString(eventId))
.orElseThrow(() -> new EventException(EventErrorResult._NOT_FOUND_EVENT));

// 이벤트에 참여하는 모든 멤버
List<Member> members = event.getMembers();
List<String> allMembersName = members.stream()
.map(Member::getName)
.toList();

// 이벤트에 참여하는 모든 유저
List<EventParticipation> eventParticipations = eventParticipationRepository.findAllByEvent(event);
List<User> users = eventParticipations.stream()
.map(EventParticipation::getUser)
.toList();
List<String> allUserNicknames = users.stream()
.map(User::getNickname)
.toList();

List<Selection> selections = selectionRepository.findAllSelectionsByEvent(event);

// 스케줄과 선택된 참여자 이름 매핑
Map<Schedule, List<String>> scheduleToNamesMap = buildScheduleToNamesMap(selections);

int mostPossibleCnt = scheduleToNamesMap.values().stream()
.mapToInt(List::size)
.max()
.orElse(0);

List<EventDto.GetMostPossibleTime> mostPossibleTimes = buildMostPossibleTimes(scheduleToNamesMap, mostPossibleCnt, allMembersName, event.getCategory());
// 멤버와 유저 전체 이름 합치기
List<String> allParticipants = new ArrayList<>(allMembersName);
allParticipants.addAll(allUserNicknames);

List<EventDto.GetMostPossibleTime> mostPossibleTimes = buildMostPossibleTimes(scheduleToNamesMap, mostPossibleCnt, allParticipants, event.getCategory());

return DateUtil.sortMostPossibleTimes(mostPossibleTimes, event.getCategory());
}

// 스케줄과 선택된 참여자 이름 매핑
// 스케줄과 선택된 참여자 이름 매핑 (멤버 이름 / 유저 닉네임)
private Map<Schedule, List<String>> buildScheduleToNamesMap(List<Selection> selections) {
return selections.stream()
.collect(Collectors.groupingBy(
Selection::getSchedule,
LinkedHashMap::new,
Collectors.mapping(selection -> selection.getMember().getName(), Collectors.toList())
Collectors.mapping(selection -> {
if (selection.getMember() != null) {
return selection.getMember().getName();
} else if (selection.getUser() != null) {
return selection.getUser().getNickname();
}
return null;
}, Collectors.toList())
));
}

Expand Down
Loading

0 comments on commit 91fbf6e

Please sign in to comment.