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] 수정 시, 현재 인원 초기화 + 방 삭제 / 수정 시 오픈 상태일때만 수정하게 변경(#637) #639

Merged
merged 10 commits into from
Oct 21, 2024
21 changes: 21 additions & 0 deletions backend/src/main/java/corea/member/domain/MemberReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package corea.member.domain;

import corea.exception.CoreaException;
import corea.exception.ExceptionType;
import corea.member.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

private final MemberRepository memberRepository;

public Member findOne(long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new CoreaException(ExceptionType.MEMBER_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package corea.participation.domain;

import corea.member.domain.MemberRole;
import corea.participation.repository.ParticipationRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ParticipationReader {

private final ParticipationRepository participationRepository;

public MemberRole findMemberRole(long roomId, long memberId) {
return participationRepository.findByRoomIdAndMemberId(roomId, memberId)
.map(Participation::getMemberRole)
.orElse(MemberRole.NONE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class ParticipationWriter {
private final ParticipationRepository participationRepository;

public Participation create(Room room, Member member, MemberRole memberRole, ParticipationStatus participationStatus) {

return create(room, member, memberRole, participationStatus, room.getMatchingSize());
}

Expand All @@ -45,6 +44,13 @@ public void delete(long roomId, long memberId) {
participationRepository.delete(participation);
}

public void deleteAllByRoom(Room room) {
if (room.isNotOpened()) {
throw new CoreaException(ExceptionType.ROOM_STATUS_INVALID);
}
participationRepository.deleteAllByRoomId(room.getId());
}

private void logCreateParticipation(Participation participation) {
log.info("방에 참가했습니다. id={}, 방 id={}, 참가한 사용자 id={}, 역할={}, 원하는 매칭 인원={}", participation.getId(), participation.getRoomsId(), participation.getMembersId(), participation.getMemberRole(), participation.getMatchingSize());
}
Expand Down
70 changes: 70 additions & 0 deletions backend/src/main/java/corea/room/domain/RoomWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package corea.room.domain;

import corea.exception.CoreaException;
import corea.exception.ExceptionType;
import corea.member.domain.Member;
import corea.participation.domain.ParticipationWriter;
import corea.room.dto.RoomCreateRequest;
import corea.room.dto.RoomUpdateRequest;
import corea.room.repository.RoomRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Component
@RequiredArgsConstructor
@Transactional
public class RoomWriter {

private static final int PLUS_HOURS_TO_MINIMUM_RECRUITMENT_DEADLINE = 1;
private static final int PLUS_DAYS_TO_MINIMUM_REVIEW_DEADLINE = 1;

private final RoomRepository roomRepository;
private final ParticipationWriter participationWriter;

public Room create(Member member, RoomCreateRequest request) {
Copy link
Contributor

Choose a reason for hiding this comment

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

마찬가지로

create, update, delete 모두
member -> manager로 변경되어도 좋을 것 같습니다~

// validateDeadLine(request.recruitmentDeadline(), request.reviewDeadline());
Room room = roomRepository.save(request.toEntity(member));
log.info("방을 생성했습니다. 방 생성자 id={}, 요청한 사용자 id={}", room.getId(), room.getManagerId());
return room;
}

public Room update(Room room, Member member, RoomUpdateRequest request) {
validate(room, member);
return roomRepository.save(request.toEntity(room, member));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Controller 단에서 전달받은 dto를 그대로 전달받았는데, 이 부분 급하다고 하니 TODO로 남기고 넘어가보시죠!~


public void delete(Room room, Member member) {
validate(room, member);
roomRepository.delete(room);
participationWriter.deleteAllByRoom(room);
log.info("방을 삭제했습니다. 방 id={}, 사용자 iD={}", room.getId(), member.getId());
}

private void validate(Room room, Member member) {
if (room.isNotMatchingManager(member.getId())) {
log.warn("인증되지 않은 방 변경 시도 방 생성자 id={}, 요청한 사용자 id={}", room.getId(), member.getId());
throw new CoreaException(ExceptionType.ROOM_MODIFY_AUTHORIZATION_ERROR);
}
if (room.isNotOpened()) {
throw new CoreaException(ExceptionType.ROOM_STATUS_INVALID);
}
}

// private void validateDeadLine(LocalDateTime recruitmentDeadline, LocalDateTime reviewDeadline) {
// LocalDateTime currentDateTime = LocalDateTime.now();
//
// LocalDateTime minimumRecruitmentDeadline = currentDateTime.plusHours(PLUS_HOURS_TO_MINIMUM_RECRUITMENT_DEADLINE);
// if (recruitmentDeadline.isBefore(minimumRecruitmentDeadline)) {
// throw new CoreaException(ExceptionType.INVALID_RECRUITMENT_DEADLINE,
// String.format("모집 마감 시간은 현재 시간보다 %d시간 이후여야 합니다.", PLUS_HOURS_TO_MINIMUM_RECRUITMENT_DEADLINE));
// }
// LocalDateTime minimumReviewDeadline = recruitmentDeadline.plusDays(PLUS_DAYS_TO_MINIMUM_REVIEW_DEADLINE);
// if (reviewDeadline.isBefore(minimumReviewDeadline)) {
// throw new CoreaException(ExceptionType.INVALID_REVIEW_DEADLINE,
// String.format("리뷰 마감 시간은 모집 마감 시간보다 %d일 이후여야 합니다.", PLUS_DAYS_TO_MINIMUM_REVIEW_DEADLINE));
// }
// }
}
77 changes: 19 additions & 58 deletions backend/src/main/java/corea/room/service/RoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
import corea.exception.ExceptionType;
import corea.matchresult.repository.MatchResultRepository;
import corea.member.domain.Member;
import corea.member.domain.MemberReader;
import corea.member.domain.MemberRole;
import corea.member.repository.MemberRepository;
import corea.participation.domain.Participation;
import corea.participation.domain.ParticipationReader;
import corea.participation.domain.ParticipationStatus;
import corea.participation.domain.ParticipationWriter;
import corea.participation.repository.ParticipationRepository;
import corea.room.domain.Room;
import corea.room.domain.RoomReader;
import corea.room.domain.RoomWriter;
import corea.room.dto.*;
import corea.room.repository.RoomRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -29,84 +30,49 @@
@Transactional(readOnly = true)
public class RoomService {

private static final int PLUS_HOURS_TO_MINIMUM_RECRUITMENT_DEADLINE = 1;
private static final int PLUS_DAYS_TO_MINIMUM_REVIEW_DEADLINE = 1;
private static final int RANDOM_DISPLAY_PARTICIPANTS_SIZE = 6;

private final RoomRepository roomRepository;
private final MemberRepository memberRepository;
private final MatchResultRepository matchResultRepository;
private final ParticipationRepository participationRepository;
private final RoomAutomaticService roomAutomaticService;
private final ParticipationWriter participationWriter;
private final MemberReader memberReader;
private final RoomWriter roomWriter;
private final ParticipationReader participationReader;
private final RoomReader roomReader;

@Transactional
public RoomResponse create(long memberId, RoomCreateRequest request) {
// validateDeadLine(request.recruitmentDeadline(), request.reviewDeadline());

Member manager = memberRepository.findById(memberId)
.orElseThrow(() -> new CoreaException(ExceptionType.MEMBER_NOT_FOUND));
Room room = roomRepository.save(request.toEntity(manager));
log.info("방을 생성했습니다. 방 생성자 id={}, 요청한 사용자 id={}", room.getManagerId(), memberId);
Member manager = memberReader.findOne(memberId);
Room room = roomWriter.create(manager, request);

Participation participation = participationWriter.create(room, manager, MemberRole.REVIEWER, ParticipationStatus.MANAGER);

participationRepository.save(participation);
roomAutomaticService.createAutomatic(room);

return RoomResponse.of(room, participation.getMemberRole(), ParticipationStatus.MANAGER);
}

private void validateDeadLine(LocalDateTime recruitmentDeadline, LocalDateTime reviewDeadline) {
LocalDateTime currentDateTime = LocalDateTime.now();

LocalDateTime minimumRecruitmentDeadline = currentDateTime.plusHours(PLUS_HOURS_TO_MINIMUM_RECRUITMENT_DEADLINE);
if (recruitmentDeadline.isBefore(minimumRecruitmentDeadline)) {
throw new CoreaException(ExceptionType.INVALID_RECRUITMENT_DEADLINE,
String.format("모집 마감 시간은 현재 시간보다 %d시간 이후여야 합니다.", PLUS_HOURS_TO_MINIMUM_RECRUITMENT_DEADLINE));
}
LocalDateTime minimumReviewDeadline = recruitmentDeadline.plusDays(PLUS_DAYS_TO_MINIMUM_REVIEW_DEADLINE);
if (reviewDeadline.isBefore(minimumReviewDeadline)) {
throw new CoreaException(ExceptionType.INVALID_REVIEW_DEADLINE,
String.format("리뷰 마감 시간은 모집 마감 시간보다 %d일 이후여야 합니다.", PLUS_DAYS_TO_MINIMUM_REVIEW_DEADLINE));
}
}

@Transactional
public RoomResponse update(long memberId, RoomUpdateRequest request) {
Room room = getRoom(request.roomId());
if (room.isNotMatchingManager(memberId)) {
throw new CoreaException(ExceptionType.MEMBER_IS_NOT_MANAGER);
}
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new CoreaException(ExceptionType.MEMBER_NOT_FOUND));
Room room = roomReader.find(request.roomId());
Member member = memberReader.findOne(memberId);

Room updatedRoom = roomRepository.save(request.toEntity(member));
Participation participation = participationRepository.findByRoomIdAndMemberId(updatedRoom.getId(), memberId)
.orElseThrow(() -> new CoreaException(ExceptionType.NOT_PARTICIPATED_ROOM));
Room updatedRoom = roomWriter.update(room, member, request);
MemberRole memberRole = participationReader.findMemberRole(room.getId(), memberId);

roomAutomaticService.updateTime(updatedRoom);
return RoomResponse.of(updatedRoom, participation.getMemberRole(), ParticipationStatus.MANAGER);
return RoomResponse.of(updatedRoom, memberRole, ParticipationStatus.MANAGER);
}

@Transactional
public void delete(long roomId, long memberId) {
Room room = getRoom(roomId);
validateDeletionAuthority(room, memberId);
Room room = roomReader.find(roomId);
Member member = memberReader.findOne(memberId);

log.info("방을 삭제했습니다. 방 id={}, 사용자 iD={}", roomId, memberId);
roomRepository.delete(room);
participationRepository.deleteAllByRoomId(roomId);
roomWriter.delete(room, member);
roomAutomaticService.deleteAutomatic(room);
}

private void validateDeletionAuthority(Room room, long memberId) {
if (room.isNotMatchingManager(memberId)) {
log.warn("방 삭제 권한이 없습니다. 방 생성자만 방을 삭제할 수 있습니다. 방 생성자 id={}, 요청한 사용자 id={}", room.getManagerId(), memberId);
throw new CoreaException(ExceptionType.ROOM_DELETION_AUTHORIZATION_ERROR);
}
}

public RoomParticipantResponses findParticipants(long roomId, long memberId) {
List<Participation> participants = new ArrayList<>(participationRepository.findAllByRoomId(roomId)
.stream()
Expand Down Expand Up @@ -140,11 +106,6 @@ private RoomParticipantResponse getRoomParticipantResponse(long roomId, Particip
}

public RoomResponse getRoomById(long roomId) {
return RoomResponse.from(getRoom(roomId));
}

private Room getRoom(long roomId) {
return roomRepository.findById(roomId)
.orElseThrow(() -> new CoreaException(ExceptionType.ROOM_NOT_FOUND, String.format("해당 Id의 방이 없습니다. 입력된 Id=%d", roomId)));
return RoomResponse.from(roomReader.find(roomId));
}
}
Loading