Skip to content

Commit

Permalink
의존성 분리 및 코드 정리 #349
Browse files Browse the repository at this point in the history
  • Loading branch information
Juhongseok authored Dec 3, 2023
2 parents c2a58f2 + dda15a5 commit b4acf86
Show file tree
Hide file tree
Showing 43 changed files with 274 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import org.devcourse.resumeme.business.event.domain.EventPosition;
import org.devcourse.resumeme.business.event.service.EventPositionService;
import org.devcourse.resumeme.business.event.service.EventService;
import org.devcourse.resumeme.business.event.service.vo.AllEventFilter;
import org.devcourse.resumeme.business.event.service.vo.EventUpdateVo;
import org.devcourse.resumeme.business.user.entity.User;
import org.devcourse.resumeme.business.user.entity.UserService;
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;
import org.devcourse.resumeme.global.auth.model.jwt.JwtUser;
import org.springframework.data.domain.Page;
Expand All @@ -37,7 +37,7 @@ public class EventController {

private final EventService eventService;

private final UserService userService;
private final UserProvider userProvider;

private final EventPositionService eventPositionService;

Expand Down Expand Up @@ -66,11 +66,11 @@ public EventInfoResponse getOne(@PathVariable Long eventId) {

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

List<Event> events = getEvents(pageAbleEvent);
List<EventPosition> positions = getPositions(events);
List<User> mentors = getMentors(events);
List<UserResponse> mentors = getMentors(events);

return EventPageResponse.of(positions, mentors, pageAbleEvent);
}
Expand All @@ -90,12 +90,12 @@ private List<EventPosition> getPositions(List<Event> content) {
return eventPositionService.getAll(eventIds);
}

private List<User> getMentors(List<Event> events) {
private List<UserResponse> getMentors(List<Event> events) {
List<Long> mentorIds = events.stream()
.map(Event::getMentorId)
.toList();

return userService.getByIds(mentorIds);
return userProvider.getByIds(mentorIds);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import org.devcourse.resumeme.business.event.service.MenteeToEventService;
import org.devcourse.resumeme.business.event.service.vo.AcceptMenteeToEvent;
import org.devcourse.resumeme.business.event.service.vo.ApplyUpdateVo;
import org.devcourse.resumeme.business.resume.service.ComponentService;
import org.devcourse.resumeme.business.resume.service.ResumeService;
import org.devcourse.resumeme.business.resume.service.ResumeProvider;
import org.devcourse.resumeme.business.snapshot.service.SnapshotCapture;
import org.devcourse.resumeme.common.response.IdResponse;
import org.devcourse.resumeme.global.auth.model.jwt.JwtUser;
Expand All @@ -25,9 +24,7 @@
@RequestMapping("/api/v1/appliments/events")
public class MenteeToEventController {

private final ResumeService resumeService;

private final ComponentService componentService;
private final ResumeProvider resumeProvider;

private final MenteeToEventService applyService;

Expand All @@ -42,8 +39,7 @@ public IdResponse getParticipantRecord(@PathVariable Long eventId, @Authenticati

@PostMapping("/{eventId}")
public void applyEvent(@PathVariable Long eventId, @RequestBody ApplyToEventRequest request, @AuthenticationPrincipal JwtUser user) {
Long copyResumeId = resumeService.copyResume(request.resumeId());
componentService.copy(request.resumeId(), copyResumeId);
Long copyResumeId = resumeProvider.copy(request.resumeId());
applyService.acceptMentee(new AcceptMenteeToEvent(eventId, user.id(), copyResumeId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import org.devcourse.resumeme.business.event.domain.Event;
import org.devcourse.resumeme.business.event.domain.EventPosition;
import org.devcourse.resumeme.business.user.domain.mentor.Mentor;
import org.devcourse.resumeme.business.user.entity.User;
import org.devcourse.resumeme.business.user.service.vo.UserResponse;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;

Expand All @@ -19,12 +18,11 @@

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

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

List<EventResponse> responses = getEvents(pageAbleEvent).stream()
.map(event -> new EventResponse(event, positionsMap.get(event.getId()), mentorsMap.get(event.getMentorId())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

import org.devcourse.resumeme.business.event.domain.Event;
import org.devcourse.resumeme.business.event.domain.EventPosition;
import org.devcourse.resumeme.business.user.domain.mentor.Mentor;
import org.devcourse.resumeme.business.user.service.vo.UserResponse;

import java.util.List;

public record EventResponse(EventInfoResponse info, MentorInfo mentorInfo) {

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

record MentorInfo(Long mentorId, String nickname, String imageUrl) {

MentorInfo(Mentor mentor) {
this(mentor.getId(), mentor.getRequiredInfo().getNickname(), mentor.getImageUrl());
MentorInfo(UserResponse mentor) {
this(mentor.userId(), mentor.nickname(), mentor.imageUrl());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.devcourse.resumeme.business.event.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.devcourse.resumeme.business.event.domain.EventStatus;
import org.devcourse.resumeme.business.event.repository.EventRepository;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
public class EventScheduler {

private final EventRepository eventRepository;

@Scheduled(cron = "0 0/1 * * * *")
public void openBookedEvents() {
log.info("scheduler 실행 됨");
eventRepository.openBookedEvent(EventStatus.OPEN, LocalDateTime.now());
eventRepository.closeApplyToEvent(EventStatus.CLOSE, LocalDateTime.now());
eventRepository.finishEvent(EventStatus.FINISH, LocalDateTime.now());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.devcourse.resumeme.business.event.domain.Event;
import org.devcourse.resumeme.business.event.domain.EventStatus;
import org.devcourse.resumeme.business.event.exception.EventException;
import org.devcourse.resumeme.business.event.repository.EventRepository;
import org.devcourse.resumeme.business.event.service.vo.AllEventFilter;
import org.devcourse.resumeme.business.event.service.listener.EventCreationPublisher;
import org.devcourse.resumeme.business.event.service.vo.EventUpdateVo;
import org.devcourse.resumeme.business.user.domain.mentor.Mentor;
import org.devcourse.resumeme.business.user.entity.User;
import org.devcourse.resumeme.business.user.entity.UserService;
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;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

import static org.devcourse.resumeme.business.event.service.EventCreation.*;
import static org.devcourse.resumeme.business.event.service.listener.EventCreation.EventNoticeInfo;
import static org.devcourse.resumeme.global.exception.ExceptionCode.EVENT_NOT_FOUND;
import static org.devcourse.resumeme.global.exception.ExceptionCode.RESUME_NOT_FOUND;

Expand All @@ -34,15 +30,14 @@ public class EventService {

private final EventRepository eventRepository;

private final UserService userService;
private final UserProvider userProvider;

public Long create(Event event) {
eventRepository.findAllByMentorId(event.getMentorId())
.forEach(Event::checkOpen);
Event savedEvent = eventRepository.save(event);
User user = userService.getOne(savedEvent.getMentorId());
Mentor mentor = Mentor.of(user);
EventNoticeInfo eventNoticeInfo = new EventNoticeInfo(savedEvent, mentor);
UserResponse user = userProvider.getOne(savedEvent.getMentorId());
EventNoticeInfo eventNoticeInfo = new EventNoticeInfo(savedEvent, user);
eventCreationPublisher.publishEventCreation(eventNoticeInfo);

return savedEvent.getId();
Expand All @@ -55,16 +50,12 @@ public Event getOne(Long eventId) {
}

@Transactional(readOnly = true)
public Page<Event> getAllWithPage(AllEventFilter filter, Pageable pageable) {
if (filter.mentorId() != null) {
return eventRepository.findAllByMentorIdOrderByCreatedDateDesc(filter.mentorId(), pageable);
}

if (filter.menteeId() != null) {
return eventRepository.findAllByApplicantsMenteeIdOrderByCreatedDateDesc(filter.menteeId(), pageable);
}

return eventRepository.findAllByOrderByCreatedDateDesc(pageable);
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 String getOverallReview(Event event, Long resumeId) {
Expand All @@ -85,11 +76,4 @@ public void checkCommentAvailableDate(Long eventId) {
event.checkDate();
}

@Scheduled(cron = "0 0/1 * * * *")
public void openBookedEvents() {
log.info("scheduler 실행 됨");
eventRepository.openBookedEvent(EventStatus.OPEN, LocalDateTime.now());
eventRepository.closeApplyToEvent(EventStatus.CLOSE, LocalDateTime.now());
eventRepository.finishEvent(EventStatus.FINISH, LocalDateTime.now());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public Long update(Long eventId, ApplyUpdateVo applyUpdateVo) {
.orElseThrow(() -> new EventException(EVENT_NOT_FOUND));

ApplimentUpdate model = applyUpdateVo.toModel();

return model.update(event);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.devcourse.resumeme.business.event.service;
package org.devcourse.resumeme.business.event.service.listener;

import lombok.Getter;
import org.devcourse.resumeme.business.event.domain.Event;
import org.devcourse.resumeme.business.user.domain.mentor.Mentor;
import org.devcourse.resumeme.business.user.service.vo.UserResponse;
import org.springframework.context.ApplicationEvent;

public class EventCreation extends ApplicationEvent {
Expand All @@ -17,8 +17,8 @@ public EventCreation(Object source, EventNoticeInfo eventNoticeInfo) {

public record EventNoticeInfo(Long eventId, Long mentorId, String mentorNickname) {

public EventNoticeInfo(Event event, Mentor mentor) {
this(event.getId(), mentor.getId(), mentor.getNickname());
public EventNoticeInfo(Event event, UserResponse mentor) {
this(event.getId(), mentor.userId(), mentor.nickname());
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.devcourse.resumeme.business.event.service;
package org.devcourse.resumeme.business.event.service.listener;

import lombok.RequiredArgsConstructor;
import org.devcourse.resumeme.business.mail.service.EmailService;
import org.devcourse.resumeme.business.user.domain.mentee.Follow;
import org.devcourse.resumeme.business.user.entity.User;
import org.devcourse.resumeme.business.user.entity.UserService;
import org.devcourse.resumeme.business.user.service.UserProvider;
import org.devcourse.resumeme.business.user.service.mentee.FollowService;
import org.devcourse.resumeme.business.user.service.vo.UserResponse;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

Expand All @@ -21,7 +21,7 @@ public class EventCreationListener implements ApplicationListener<EventCreation>

private final FollowService followService;

private final UserService userService;
private final UserProvider userProvider;

@Override
public void onApplicationEvent(EventCreation eventCreation) {
Expand All @@ -31,8 +31,8 @@ public void onApplicationEvent(EventCreation eventCreation) {
.map(Follow::getMenteeId)
.toList();

List<String> emails = userService.getByIds(followerIds).stream()
.map(User::getEmail)
List<String> emails = userProvider.getByIds(followerIds).stream()
.map(UserResponse::email)
.toList();

emailService.sendEmail(createEventCreationMail(emails, eventCreation.getEventNoticeInfo()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.devcourse.resumeme.business.event.service;
package org.devcourse.resumeme.business.event.service.listener;

import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import static org.devcourse.resumeme.business.event.service.EventCreation.*;
import static org.devcourse.resumeme.business.event.service.listener.EventCreation.*;

@Component
@RequiredArgsConstructor
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.devcourse.resumeme.business.event.service.vo;

import org.devcourse.resumeme.business.user.domain.Role;

public enum AuthorizationRole {
MENTOR,
MENTEE,
ALL;

public static AuthorizationRole of(Role role) {
return switch (role) {
case ROLE_MENTEE -> MENTEE;
case ROLE_PENDING, ROLE_MENTOR -> MENTOR;
case ROLE_ADMIN -> ALL;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.devcourse.resumeme.business.event.service.vo;

public record EventsFoundCondition(Long userId, AuthorizationRole role) {

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package org.devcourse.resumeme.business.mail.service;

import lombok.NoArgsConstructor;
import org.devcourse.resumeme.business.mail.EmailInfo;
import org.devcourse.resumeme.business.user.domain.mentor.Mentor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

import static org.devcourse.resumeme.business.event.service.EventCreation.EventNoticeInfo;
import static lombok.AccessLevel.PRIVATE;
import static org.devcourse.resumeme.business.event.service.listener.EventCreation.EventNoticeInfo;
import static org.devcourse.resumeme.business.mail.EmailType.EVENT_CREATED;
import static org.devcourse.resumeme.business.mail.EmailType.MENTOR_APPROVED;

@Component
@NoArgsConstructor(access = PRIVATE)
public class EmailInfoGenerator {

private static final String BASE_URL = "https://resumeme.vercel.app";
Expand Down
Loading

0 comments on commit b4acf86

Please sign in to comment.