Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 1대다 페치조인 페이징처리, 환경 분리 #352

Merged
merged 4 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import org.devcourse.resumeme.business.event.controller.dto.EventUpdateRequest;
import org.devcourse.resumeme.business.event.domain.Event;
import org.devcourse.resumeme.business.event.domain.EventPosition;
import org.devcourse.resumeme.business.event.domain.MenteeToEvent;
import org.devcourse.resumeme.business.event.service.EventPositionService;
import org.devcourse.resumeme.business.event.service.EventService;
import org.devcourse.resumeme.business.event.service.vo.EventUpdateVo;
import org.devcourse.resumeme.business.event.service.vo.EventsFoundCondition;
import org.devcourse.resumeme.business.user.service.UserProvider;
import org.devcourse.resumeme.business.user.service.vo.UserResponse;
import org.devcourse.resumeme.common.response.IdResponse;
Expand All @@ -26,12 +26,8 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.devcourse.resumeme.business.event.service.vo.AuthorizationRole.ALL;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/events")
Expand Down Expand Up @@ -68,20 +64,19 @@ public EventInfoResponse getOne(@PathVariable Long eventId) {

@GetMapping
public EventPageResponse getAll(Pageable pageable) {
Page<Event> pageAbleEvent = eventService.getAllWithPage(new EventsFoundCondition(null, ALL), pageable);

List<Event> events = getEvents(pageAbleEvent);
Page<MenteeToEvent> menteeToEvents = eventService.getAllWithPage(pageable);
List<Event> events = getEvents(menteeToEvents);
List<EventPosition> positions = getPositions(events);
List<UserResponse> mentors = getMentors(events);

return EventPageResponse.of(positions, mentors, pageAbleEvent);
return EventPageResponse.of(positions, mentors, menteeToEvents);
}

private static List<Event> getEvents(Page<Event> events) {
List<Event> content = new ArrayList<>(events.getContent());
Collections.sort(content);

return content;
private List<Event> getEvents(Page<MenteeToEvent> menteeToEvents) {
return menteeToEvents.stream()
.map(MenteeToEvent::getEvent)
.sorted()
.toList();
}

private List<EventPosition> getPositions(List<Event> content) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public EventInfoResponse(Event event, List<EventPosition> positions) {
event.getEventInfo().getStatus().name(), convertToString(positions), new TimeInfo(event.getEventTimeInfo()));
}

public EventInfoResponse(Event event, int size, List<EventPosition> positions) {
this(event.getId(), event.getMentorId(), event.title(), event.content(), event.maximumCount(), size,
event.getEventInfo().getStatus().name(), convertToString(positions), new TimeInfo(event.getEventTimeInfo()));
}

private static List<String> convertToString(List<EventPosition> positions) {
return positions.stream()
.map(eventPosition -> eventPosition.getPosition().name())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import org.devcourse.resumeme.business.event.domain.Event;
import org.devcourse.resumeme.business.event.domain.EventPosition;
import org.devcourse.resumeme.business.event.domain.MenteeToEvent;
import org.devcourse.resumeme.business.user.service.vo.UserResponse;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
Expand All @@ -18,24 +17,23 @@

public record EventPageResponse(List<EventResponse> events, PageableResponse pageData) {

public static EventPageResponse of(List<EventPosition> positions, List<UserResponse> mentors, Page<Event> pageAbleEvent) {
public static EventPageResponse of(List<EventPosition> positions, List<UserResponse> mentors, Page<MenteeToEvent> menteeToEvents) {
Map<Object, List<EventPosition>> positionsMap = positions.stream()
.collect(groupingBy(position -> position.getEvent().getId(), toList()));
Map<Long, UserResponse> mentorsMap = mentors.stream()
.collect(Collectors.toMap(UserResponse::userId, Function.identity()));
Map<Long, List<MenteeToEvent>> menteeToEventMap = menteeToEvents.stream()
.collect(groupingBy(position -> position.getEvent().getId(), toList()));

List<EventResponse> responses = getEvents(pageAbleEvent).stream()
.map(event -> new EventResponse(event, positionsMap.get(event.getId()), mentorsMap.get(event.getMentorId())))
List<Event> events = menteeToEvents.stream()
.map(MenteeToEvent::getEvent)
.toList();

return new EventPageResponse(responses, new PageableResponse(pageAbleEvent));
}

private static List<Event> getEvents(Page<Event> events) {
List<Event> content = new ArrayList<>(events.getContent());
Collections.sort(content);
List<EventResponse> responses = events.stream()
.map(event -> new EventResponse(event, menteeToEventMap.get(event.getId()).size(), positionsMap.get(event.getId()), mentorsMap.get(event.getMentorId())))
.toList();

return content;
return new EventPageResponse(responses, new PageableResponse(menteeToEvents));
}

private record PageableResponse(
Expand All @@ -48,7 +46,7 @@ private record PageableResponse(
long totalElements
) {

private PageableResponse(Page<Event> page) {
private <T> PageableResponse(Page<T> page) {
this(
page.isFirst(),
page.isLast(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

public record EventResponse(EventInfoResponse info, MentorInfo mentorInfo) {

public EventResponse(Event event, List<EventPosition> positions, UserResponse mentor) {
this(new EventInfoResponse(event, positions), new MentorInfo(mentor));
public EventResponse(Event event, int size, List<EventPosition> positions, UserResponse mentor) {
this(new EventInfoResponse(event, size, positions), new MentorInfo(mentor));
}

record MentorInfo(Long mentorId, String nickname, String imageUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import jakarta.persistence.LockModeType;
import org.devcourse.resumeme.business.event.domain.Event;
import org.devcourse.resumeme.business.event.domain.EventStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
Expand All @@ -27,15 +25,6 @@ public interface EventRepository extends JpaRepository<Event, Long> {
@EntityGraph(attributePaths = {"applicants"})
Optional<Event> findWithApplicantsById(Long id);

@EntityGraph(attributePaths = {"applicants"})
Page<Event> findAllByMentorIdOrderByCreatedDateDesc(Long mentorId, Pageable pageable);

@EntityGraph(attributePaths = {"applicants"})
Page<Event> findAllByOrderByCreatedDateDesc(Pageable pageable);

@EntityGraph(attributePaths = {"applicants"})
Page<Event> findAllByApplicantsMenteeIdOrderByCreatedDateDesc(Long menteeId, Pageable pageable);

@Modifying
@Query("update Event e set e.eventInfo.status = :status where e.eventTimeInfo.openDateTime <= :now and e.eventInfo.status = 'READY'")
void openBookedEvent(@Param("status") EventStatus status, @Param("now")LocalDateTime now);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package org.devcourse.resumeme.business.event.repository;

import org.devcourse.resumeme.business.event.domain.MenteeToEvent;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface MenteeToEventRepository extends JpaRepository<MenteeToEvent, Long> {

@EntityGraph(attributePaths = {"event", "event.applicants"})
@EntityGraph(attributePaths = {"event"})
List<MenteeToEvent> findByMenteeId(Long menteeId);

@EntityGraph(attributePaths = {"event"})
List<MenteeToEvent> findByEventMentorId(Long mentorId);

@EntityGraph(attributePaths = {"event"})
Page<MenteeToEvent> findAllByOrderByEventCreatedDateDesc(Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.extern.slf4j.Slf4j;
import org.devcourse.resumeme.business.event.domain.EventStatus;
import org.devcourse.resumeme.business.event.repository.EventRepository;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -13,6 +14,7 @@
@Slf4j
@Service
@Transactional
@Profile({"dev", "prod"})
@RequiredArgsConstructor
public class EventScheduler {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.devcourse.resumeme.business.event.domain.Event;
import org.devcourse.resumeme.business.event.domain.MenteeToEvent;
import org.devcourse.resumeme.business.event.exception.EventException;
import org.devcourse.resumeme.business.event.repository.EventRepository;
import org.devcourse.resumeme.business.event.repository.MenteeToEventRepository;
import org.devcourse.resumeme.business.event.service.listener.EventCreationPublisher;
import org.devcourse.resumeme.business.event.service.vo.EventUpdateVo;
import org.devcourse.resumeme.business.event.service.vo.EventsFoundCondition;
import org.devcourse.resumeme.business.user.service.UserProvider;
import org.devcourse.resumeme.business.user.service.vo.UserResponse;
import org.devcourse.resumeme.global.exception.CustomException;
Expand All @@ -28,6 +29,8 @@ public class EventService {

private final EventCreationPublisher eventCreationPublisher;

private final MenteeToEventRepository menteeToEventRepository;

private final EventRepository eventRepository;

private final UserProvider userProvider;
Expand All @@ -50,12 +53,8 @@ public Event getOne(Long eventId) {
}

@Transactional(readOnly = true)
public Page<Event> getAllWithPage(EventsFoundCondition condition, Pageable pageable) {
return switch (condition.role()) {
case MENTEE -> eventRepository.findAllByApplicantsMenteeIdOrderByCreatedDateDesc(condition.userId(), pageable);
case MENTOR -> eventRepository.findAllByMentorIdOrderByCreatedDateDesc(condition.userId(), pageable);
case ALL -> eventRepository.findAllByOrderByCreatedDateDesc(pageable);
};
public Page<MenteeToEvent> getAllWithPage(Pageable pageable) {
return menteeToEventRepository.findAllByOrderByEventCreatedDateDesc(pageable);
}

public String getOverallReview(Event event, Long resumeId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public class MenteeToEventService {

private final EventRepository eventRepository;

@Transactional(readOnly = true)
public Long getRecord(Long eventId, Long menteeId) {
return menteeToEventRepository.findByMenteeId(menteeId).stream()
return getByMenteeId(menteeId).stream()
.filter(record -> record.getEvent().getId().equals(eventId))
.findFirst()
.map(MenteeToEvent::getId)
Expand Down Expand Up @@ -59,13 +60,18 @@ private void checkCanApply(Long menteeId) {
}

private Long getApplyEventCount(Long menteeId) {
return menteeToEventRepository.findByMenteeId(menteeId).stream()
return getByMenteeId(menteeId).stream()
.filter(MenteeToEvent::isAttending)
.count();
}

@Transactional(readOnly = true)
public List<MenteeToEvent> getByMenteeId(Long menteeId) {
return menteeToEventRepository.findByMenteeId(menteeId);
}

public List<MenteeToEvent> getByMentorId(Long mentorId) {
return menteeToEventRepository.findByEventMentorId(mentorId);
}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
package org.devcourse.resumeme.business.userevent.controller;

import lombok.RequiredArgsConstructor;
import org.devcourse.resumeme.business.event.domain.Event;
import org.devcourse.resumeme.business.event.domain.MenteeToEvent;
import org.devcourse.resumeme.business.event.service.EventService;
import org.devcourse.resumeme.business.event.service.MenteeToEventService;
import org.devcourse.resumeme.business.event.service.vo.AuthorizationRole;
import org.devcourse.resumeme.business.event.service.vo.EventsFoundCondition;
import org.devcourse.resumeme.business.resume.entity.Resume;
import org.devcourse.resumeme.business.resume.service.ResumeService;
import org.devcourse.resumeme.business.user.domain.Role;
import org.devcourse.resumeme.business.user.service.UserProvider;
import org.devcourse.resumeme.business.user.service.vo.UserResponse;
import org.devcourse.resumeme.business.userevent.controller.dto.MenteeEventResponse;
import org.devcourse.resumeme.business.userevent.controller.dto.MentorEventResponse;
import org.devcourse.resumeme.business.userevent.controller.dto.Response;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -29,39 +22,32 @@
@RequestMapping("/api/v1")
public class UserEventController {

private final EventService eventService;

private final ResumeService resumeService;

private final UserProvider userProvider;

private final MenteeToEventService menteeToEventService;

@GetMapping("/{role}/{mentorId}/events")
public List<MentorEventResponse> all(@PathVariable Role role, @PathVariable Long mentorId) {
AuthorizationRole authorizationRole = AuthorizationRole.of(role);
Page<Event> events = eventService.getAllWithPage(new EventsFoundCondition(mentorId, authorizationRole), Pageable.unpaged());
List<Resume> resumes = getResumes(events.getContent());
@GetMapping("/mentors/{mentorId}/events")
public List<MentorEventResponse> all(@PathVariable Long mentorId) {
List<MenteeToEvent> menteeToEvents = menteeToEventService.getByMentorId(mentorId);
List<Resume> resumes = getResumes(menteeToEvents);
List<UserResponse> mentees = getMentees(resumes);

return events.stream()
.map(event -> new MentorEventResponse(event, resumes, mentees))
.toList();
return MentorEventResponse.collect(menteeToEvents, resumes, mentees);
}

@GetMapping("/mentees/{menteeId}/events")
public List<MenteeEventResponse> getOwnEvents(@PathVariable Long menteeId) {
List<MenteeToEvent> menteeToEvents = menteeToEventService.getByMenteeId(menteeId);
List<UserResponse> mentors = getMentors(menteeToEvents);
List<Event> events = getEvents(menteeToEvents);
List<Resume> resumes = getResumes(events);
List<Resume> resumes = getResumes(menteeToEvents);

return new Response(menteeToEvents, mentors, resumes).responses();
}

private List<Resume> getResumes(List<Event> events) {
List<Long> resumeIds = events.stream()
.flatMap(event -> event.getApplicants().stream())
private List<Resume> getResumes(List<MenteeToEvent> menteeToEvents) {
List<Long> resumeIds = menteeToEvents.stream()
.map(MenteeToEvent::getResumeId)
.toList();

Expand All @@ -84,10 +70,4 @@ private List<UserResponse> getMentors(List<MenteeToEvent> byMenteeId) {
return userProvider.getByIds(mentorIds);
}

private List<Event> getEvents(List<MenteeToEvent> menteeToEvents) {
return menteeToEvents.stream()
.map(MenteeToEvent::getEvent)
.toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,26 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;

public record MentorEventResponse(EventInfoResponse info, List<ResumeResponse> resumes) {

public MentorEventResponse(Event event, List<Resume> resumes, List<UserResponse> mentees) {
this(new EventInfoResponse(event, new ArrayList<>()), toResponse(event, resumes, mentees));
public MentorEventResponse(Event event, int size, List<Resume> resumes, List<UserResponse> mentees) {
this(new EventInfoResponse(event, size, new ArrayList<>()), toResponse(event, resumes, mentees));
}

public static List<MentorEventResponse> collect(List<MenteeToEvent> menteeToEvents, List<Resume> resumes, List<UserResponse> mentees) {
Map<Long, List<MenteeToEvent>> menteeToEventMap = menteeToEvents.stream()
.collect(groupingBy(position -> position.getEvent().getId(), toList()));

List<Event> events = menteeToEvents.stream()
.map(MenteeToEvent::getEvent)
.toList();

return events.stream()
.map(event -> new MentorEventResponse(event, menteeToEventMap.get(event.getId()).size(), resumes, mentees))
.toList();
}

public static List<ResumeResponse> toResponse(Event event, List<Resume> resumes, List<UserResponse> mentees) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.devcourse.resumeme.business.event.repository.EventRepository;
import org.devcourse.resumeme.business.resume.repository.ResumeRepository;
import org.devcourse.resumeme.business.user.repository.UserRepository;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

@Component
Expand All @@ -19,7 +18,7 @@ public class AuthorizationResolver {

public boolean resolve(Long userId, Long pathVariable, AuthorizationTarget target) {
return switch (target) {
case EVENTS -> eventRepository.findAllByMentorIdOrderByCreatedDateDesc(userId, Pageable.unpaged()).getContent().stream()
case EVENTS -> eventRepository.findAllByMentorId(userId).stream()
.anyMatch(event -> event.getId().equals(pathVariable));
case RESUMES -> resumeRepository.findAllByMenteeId(userId).stream()
.anyMatch(resume -> resume.getId().equals(pathVariable));
Expand Down
Loading