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

[BE] 방 수정 기능 & 스케줄러 부분 분리 및 리팩토링(#605) #606

Merged
merged 7 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
86 changes: 86 additions & 0 deletions backend/src/main/java/corea/room/service/RoomAutomaticService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package corea.room.service;

import corea.room.domain.Room;
import corea.scheduler.domain.AutomaticMatching;
import corea.scheduler.domain.AutomaticUpdate;
import corea.scheduler.domain.ScheduleStatus;
import corea.scheduler.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class RoomAutomaticService {

private final AutomaticUpdateWriter automaticUpdateWriter;
private final AutomaticUpdateReader automaticUpdateReader;

private final AutomaticMatchingWriter automaticMatchingWriter;
private final AutomaticMatchingReader automaticMatchingReader;

private final AutomaticMatchingService automaticMatchingService;
private final AutomaticUpdateService automaticUpdateService;

@Transactional
public void updateTime(Room updateRoom) {
AutomaticMatching automaticMatching = automaticMatchingReader.findWithRoom(updateRoom);
AutomaticUpdate automaticUpdate = automaticUpdateReader.findWithRoom(updateRoom);

automaticMatchingWriter.updateTime(automaticMatching, updateRoom.getRecruitmentDeadline());
automaticUpdateWriter.updateTime(automaticUpdate, updateRoom.getReviewDeadline());

automaticMatchingService.modifyTask(updateRoom);
automaticUpdateService.modifyTask(updateRoom);
}

@Transactional
public void createAutomatic(Room room) {
automaticMatchingWriter.create(room);
automaticUpdateWriter.create(room);

automaticMatchingService.matchOnRecruitmentDeadline(room);
automaticUpdateService.updateAtReviewDeadline(room);
}

@Transactional
public void deleteAutomatic(Room room) {
AutomaticMatching automaticMatching = automaticMatchingReader.findWithRoom(room);
AutomaticUpdate automaticUpdate = automaticUpdateReader.findWithRoom(room);

automaticMatchingWriter.delete(automaticMatching);
automaticUpdateWriter.delete(automaticUpdate);

automaticMatchingService.cancel(room.getId());
automaticUpdateService.cancel(room.getId());
}

@EventListener(ApplicationReadyEvent.class)
public void schedulePendingAutomaticMatching() {
List<AutomaticMatching> matchings = automaticMatchingReader.findByStatus(ScheduleStatus.PENDING);

log.info("{}개의 방에 대해 자동 매칭 재예약 시작", matchings.size());

matchings.forEach(automaticMatchingService::matchOnRecruitmentDeadline);

log.info("{}개의 방에 대해 자동 매칭 재예약 완료", matchings.size());
}

@EventListener(ApplicationReadyEvent.class)
public void schedulePendingAutomaticUpdate() {
List<AutomaticUpdate> updates = automaticUpdateReader.findByStatus(ScheduleStatus.PENDING);

log.info("{}개의 방에 대해 자동 상태 업데이트 재예약 시작", updates.size());

updates.forEach(automaticUpdateService::updateAtReviewDeadline);

log.info("{}개의 방에 대해 자동 상태 업데이트 재예약 완료", updates.size());
}
}
18 changes: 4 additions & 14 deletions backend/src/main/java/corea/room/service/RoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,10 @@
import corea.participation.domain.ParticipationStatus;
import corea.participation.repository.ParticipationRepository;
import corea.room.domain.Room;
import corea.room.domain.RoomClassification;
import corea.room.domain.RoomStatus;
import corea.room.dto.*;
import corea.room.repository.RoomRepository;
import corea.scheduler.domain.AutomaticMatching;
import corea.scheduler.domain.AutomaticUpdate;
import corea.scheduler.repository.AutomaticMatchingRepository;
import corea.scheduler.repository.AutomaticUpdateRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -47,8 +39,8 @@ public class RoomService {
private final MatchResultRepository matchResultRepository;
private final ParticipationRepository participationRepository;
private final FailedMatchingRepository failedMatchingRepository;
private final AutomaticMatchingRepository automaticMatchingRepository;
private final AutomaticUpdateRepository automaticUpdateRepository;
private final RoomAutomaticService roomAutomaticService;


@Transactional
public RoomResponse create(long memberId, RoomCreateRequest request) {
Expand All @@ -60,8 +52,7 @@ public RoomResponse create(long memberId, RoomCreateRequest request) {
Participation participation = new Participation(room, manager);

participationRepository.save(participation);
automaticMatchingRepository.save(new AutomaticMatching(room.getId(), request.recruitmentDeadline()));
automaticUpdateRepository.save(new AutomaticUpdate(room.getId(), request.reviewDeadline()));
roomAutomaticService.createAutomatic(room);

return RoomResponse.of(room, participation.getMemberRole(), ParticipationStatus.MANAGER);
}
Expand Down Expand Up @@ -131,8 +122,7 @@ public void delete(long roomId, long memberId) {

roomRepository.delete(room);
participationRepository.deleteAllByRoomId(roomId);
automaticMatchingRepository.deleteByRoomId(roomId);
automaticUpdateRepository.deleteByRoomId(roomId);
roomAutomaticService.deleteAutomatic(room);
}

private void validateDeletionAuthority(Room room, long memberId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package corea.scheduler.service;

import corea.exception.CoreaException;
import corea.exception.ExceptionType;
import corea.room.domain.Room;
import corea.scheduler.domain.AutomaticMatching;
import corea.scheduler.domain.ScheduleStatus;
import corea.scheduler.repository.AutomaticMatchingRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class AutomaticMatchingReader {

private final AutomaticMatchingRepository automaticMatchingRepository;

public AutomaticMatching findWithRoom(Room room) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P5

네이밍 얘기가 나온 김에

아래 메서드가 findByXXX이니
위 메서드도 findByRoom이 되어도 좋겠네요~

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위에 코멘트가 안달려서 밑에 남깁니당.

제안: findMatchingsByStatus

ReaderWriter 는 최대한 Reader 와 Writer 인걸 명확하게 사용하고 싶어요.
( AutomaticMatchingReader 라는 클래스만 봐도 AutomaticMatching 을 읽을거라 기대 )
그렇기에 findByStatus 만 해도 충분할 거 같다는 생각이 드는데 어떤가요?

return automaticMatchingRepository.findByRoomId(room.getId())
.orElseThrow(() -> new CoreaException(ExceptionType.AUTOMATIC_MATCHING_NOT_FOUND));
}

public List<AutomaticMatching> findByStatus(ScheduleStatus status) {
return automaticMatchingRepository.findAllByStatus(status);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

하나의 값을 반환할것 같아요. 메서드명도 같이 수정해주면 좋을 듯 합니다~

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제안: findMatchingsByStatus

Copy link
Contributor

@youngsu5582 youngsu5582 Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제안: findMatchingsByStatus

ReaderWriter 는 최대한 Reader 와 Writer 인걸 명확하게 사용하고 싶어요.
( AutomaticMatchingReader 라는 클래스만 봐도 AutomaticMatching 을 읽을거라 기대 )
그렇기에 findAllByStatus 만 해도 충분할 거 같다는 생각이 드는데 어떤가요?

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package corea.scheduler.service;

import corea.room.domain.Room;
import corea.scheduler.domain.AutomaticMatching;
import corea.scheduler.repository.AutomaticMatchingRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Component
@RequiredArgsConstructor
@Transactional
public class AutomaticMatchingWriter {

private final AutomaticMatchingRepository automaticMatchingRepository;

public AutomaticMatching updateTime(AutomaticMatching automaticMatching, LocalDateTime matchingStartTime) {
AutomaticMatching updateEntity = new AutomaticMatching(
automaticMatching.getId(),
automaticMatching.getRoomId(),
matchingStartTime,
automaticMatching.getStatus()
);
return automaticMatchingRepository.save(updateEntity);
}

public AutomaticMatching create(Room room) {
AutomaticMatching entity = new AutomaticMatching(room.getId(),room.getRecruitmentDeadline());
return automaticMatchingRepository.save(entity);
}

public void delete(AutomaticMatching automaticMatching) {
automaticMatchingRepository.delete(automaticMatching);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package corea.scheduler.service;

import corea.exception.CoreaException;
import corea.exception.ExceptionType;
import corea.room.domain.Room;
import corea.scheduler.domain.AutomaticUpdate;
import corea.scheduler.domain.ScheduleStatus;
import corea.scheduler.repository.AutomaticUpdateRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class AutomaticUpdateReader {

private final AutomaticUpdateRepository automaticUpdateRepository;

public AutomaticUpdate findWithRoom(Room room) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네이밍 관련 코멘트 동일합니다.

findByRoom

return automaticUpdateRepository.findByRoomId(room.getId())
.orElseThrow(() -> new CoreaException(ExceptionType.AUTOMATIC_UPDATE_NOT_FOUND));
}

public List<AutomaticUpdate> findByStatus(ScheduleStatus status) {
return automaticUpdateRepository.findAllByStatus(status)
.stream()
.toList();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거 붙은 이유가 뭔가요?? 🧐

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package corea.scheduler.service;

import corea.room.domain.Room;
import corea.scheduler.domain.AutomaticUpdate;
import corea.scheduler.repository.AutomaticUpdateRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Component
@RequiredArgsConstructor
@Transactional
public class AutomaticUpdateWriter {

private final AutomaticUpdateRepository automaticUpdateRepository;

public AutomaticUpdate updateTime(AutomaticUpdate automaticUpdate, LocalDateTime reviewDeadline) {
AutomaticUpdate updateEntity = new AutomaticUpdate(
automaticUpdate.getId(),
automaticUpdate.getRoomId(),
reviewDeadline,
automaticUpdate.getStatus()
);
return automaticUpdateRepository.save(updateEntity);
}

public AutomaticUpdate create(Room room) {
AutomaticUpdate createEntity = new AutomaticUpdate(
room.getId(),
room.getReviewDeadline()
);
return automaticUpdateRepository.save(createEntity);
}

public void delete(AutomaticUpdate automaticUpdate) {
automaticUpdateRepository.delete(automaticUpdate);
}
}