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: 방/루틴 전체 리팩토링 #143

Merged
merged 11 commits into from
Nov 24, 2023
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.moabam.api.application.room;

import static com.moabam.api.domain.image.ImageType.*;
import static com.moabam.global.error.model.ErrorMessage.*;
import static com.moabam.global.error.model.ErrorMessage.DUPLICATED_DAILY_MEMBER_CERTIFICATION;
import static com.moabam.global.error.model.ErrorMessage.INVALID_CERTIFY_TIME;
import static com.moabam.global.error.model.ErrorMessage.PARTICIPANT_NOT_FOUND;
import static com.moabam.global.error.model.ErrorMessage.ROUTINE_NOT_FOUND;

import java.time.LocalDate;
import java.time.LocalDateTime;
Expand All @@ -12,9 +14,7 @@

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.moabam.api.application.image.ImageService;
import com.moabam.api.application.member.MemberService;
import com.moabam.api.application.room.mapper.CertificationsMapper;
import com.moabam.api.domain.bug.BugType;
Expand Down Expand Up @@ -42,7 +42,9 @@
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class RoomCertificationService {
public class CertificationService {

private static final int REQUIRED_ROOM_CERTIFICATION = 75;

private final RoutineRepository routineRepository;
private final CertificationRepository certificationRepository;
Expand All @@ -51,12 +53,11 @@ public class RoomCertificationService {
private final DailyRoomCertificationRepository dailyRoomCertificationRepository;
private final DailyMemberCertificationRepository dailyMemberCertificationRepository;
private final MemberService memberService;
private final ImageService imageService;
private final ClockHolder clockHolder;

@Transactional
public void certifyRoom(Long memberId, Long roomId, List<MultipartFile> multipartFiles) {
LocalDate today = LocalDate.now();
public void certifyRoom(Long memberId, Long roomId, List<String> imageUrls) {
Copy link
Contributor

Choose a reason for hiding this comment

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

오 분리하셨군요 좋습니다!

LocalDate today = clockHolder.date();
Participant participant = participantSearchRepository.findOne(memberId, roomId)
.orElseThrow(() -> new NotFoundException(PARTICIPANT_NOT_FOUND));
Room room = participant.getRoom();
Expand All @@ -70,47 +71,17 @@ public void certifyRoom(Long memberId, Long roomId, List<MultipartFile> multipar
validateCertifyTime(clockHolder.times(), room.getCertifyTime());
validateAlreadyCertified(memberId, roomId, today);

DailyMemberCertification dailyMemberCertification = CertificationsMapper.toDailyMemberCertification(memberId,
roomId, participant);
dailyMemberCertificationRepository.save(dailyMemberCertification);

member.increaseTotalCertifyCount();
participant.updateCertifyCount();

List<String> result = imageService.uploadImages(multipartFiles, CERTIFICATION);
saveNewCertifications(result, memberId);
certifyMember(memberId, roomId, participant, member, imageUrls);

Optional<DailyRoomCertification> dailyRoomCertification =
certificationsSearchRepository.findDailyRoomCertification(roomId, today);

if (dailyRoomCertification.isEmpty()) {
List<DailyMemberCertification> dailyMemberCertifications =
certificationsSearchRepository.findSortedDailyMemberCertifications(roomId, today);
double completePercentage = calculateCompletePercentage(dailyMemberCertifications.size(),
room.getCurrentUserCount());

if (completePercentage >= 75) {
DailyRoomCertification createDailyRoomCertification = CertificationsMapper.toDailyRoomCertification(
roomId, today);

dailyRoomCertificationRepository.save(createDailyRoomCertification);

int expAppliedRoomLevel = getRoomLevelAfterExpApply(roomLevel, room);

List<Long> memberIds = dailyMemberCertifications.stream()
.map(DailyMemberCertification::getMemberId)
.toList();

memberService.getRoomMembers(memberIds)
.forEach(completedMember -> completedMember.getBug().increaseBug(bugType, expAppliedRoomLevel));

return;
}
certifyRoomIfAvailable(roomId, today, room, bugType, roomLevel);
return;
}

if (dailyRoomCertification.isPresent()) {
member.getBug().increaseBug(bugType, roomLevel);
}
member.getBug().increaseBug(bugType, roomLevel);
}

public boolean existsMemberCertification(Long memberId, Long roomId, LocalDate date) {
Expand Down Expand Up @@ -138,7 +109,17 @@ private void validateAlreadyCertified(Long memberId, Long roomId, LocalDate toda
}
}

private void saveNewCertifications(List<String> imageUrls, Long memberId) {
private void certifyMember(Long memberId, Long roomId, Participant participant, Member member, List<String> urls) {
DailyMemberCertification dailyMemberCertification = CertificationsMapper.toDailyMemberCertification(memberId,
roomId, participant);
dailyMemberCertificationRepository.save(dailyMemberCertification);
member.increaseTotalCertifyCount();
participant.updateCertifyCount();

saveNewCertifications(memberId, urls);
}

private void saveNewCertifications(Long memberId, List<String> imageUrls) {
List<Certification> certifications = new ArrayList<>();

for (String imageUrl : imageUrls) {
Expand All @@ -153,6 +134,23 @@ private void saveNewCertifications(List<String> imageUrls, Long memberId) {
certificationRepository.saveAll(certifications);
}

private void certifyRoomIfAvailable(Long roomId, LocalDate today, Room room, BugType bugType, int roomLevel) {
List<DailyMemberCertification> dailyMemberCertifications =
certificationsSearchRepository.findSortedDailyMemberCertifications(roomId, today);
double completePercentage = calculateCompletePercentage(dailyMemberCertifications.size(),
room.getCurrentUserCount());

if (completePercentage >= REQUIRED_ROOM_CERTIFICATION) {
DailyRoomCertification createDailyRoomCertification = CertificationsMapper.toDailyRoomCertification(
roomId, today);

dailyRoomCertificationRepository.save(createDailyRoomCertification);
int expAppliedRoomLevel = getRoomLevelAfterExpApply(roomLevel, room);

provideBugToCompletedMembers(bugType, dailyMemberCertifications, expAppliedRoomLevel);
}
}

private double calculateCompletePercentage(int certifiedMembersCount, int currentsMembersCount) {
double completePercentage = ((double)certifiedMembersCount / currentsMembersCount) * 100;

Expand All @@ -169,4 +167,14 @@ private int getRoomLevelAfterExpApply(int roomLevel, Room room) {

return room.getLevel();
}

private void provideBugToCompletedMembers(BugType bugType, List<DailyMemberCertification> dailyMemberCertifications,
int expAppliedRoomLevel) {
List<Long> memberIds = dailyMemberCertifications.stream()
.map(DailyMemberCertification::getMemberId)
.toList();
Comment on lines +173 to +175
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
List<Long> memberIds = dailyMemberCertifications.stream()
.map(DailyMemberCertification::getMemberId)
.toList();
List<Long> memberIds = map(dailyMemberCertifications, DailyMemberCertification::getMemberId);

C: StreamUtils 만들어놓은 것 적용할 수 있을듯!

Copy link
Member Author

Choose a reason for hiding this comment

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

XX : 난 안쓰는게 더 조아!

(제 말 아님)


memberService.getRoomMembers(memberIds)
.forEach(completedMember -> completedMember.getBug().increaseBug(bugType, expAppliedRoomLevel));
}
}
90 changes: 36 additions & 54 deletions src/main/java/com/moabam/api/application/room/RoomService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.moabam.api.application.room;

import static com.moabam.api.domain.room.RoomType.*;
import static com.moabam.global.error.model.ErrorMessage.*;
import static com.moabam.api.domain.room.RoomType.MORNING;
import static com.moabam.api.domain.room.RoomType.NIGHT;
import static com.moabam.global.error.model.ErrorMessage.MEMBER_ROOM_EXCEED;
import static com.moabam.global.error.model.ErrorMessage.PARTICIPANT_NOT_FOUND;
import static com.moabam.global.error.model.ErrorMessage.ROOM_EXIT_MANAGER_FAIL;
import static com.moabam.global.error.model.ErrorMessage.ROOM_MAX_USER_REACHED;
import static com.moabam.global.error.model.ErrorMessage.ROOM_MODIFY_UNAUTHORIZED_REQUEST;
import static com.moabam.global.error.model.ErrorMessage.ROOM_NOT_FOUND;
import static com.moabam.global.error.model.ErrorMessage.WRONG_ROOM_PASSWORD;

import java.util.List;

Expand All @@ -10,6 +17,7 @@
import org.springframework.transaction.annotation.Transactional;

import com.moabam.api.application.member.MemberService;
import com.moabam.api.application.room.mapper.ParticipantMapper;
import com.moabam.api.application.room.mapper.RoomMapper;
import com.moabam.api.application.room.mapper.RoutineMapper;
import com.moabam.api.domain.member.Member;
Expand All @@ -21,7 +29,6 @@
import com.moabam.api.domain.room.repository.ParticipantSearchRepository;
import com.moabam.api.domain.room.repository.RoomRepository;
import com.moabam.api.domain.room.repository.RoutineRepository;
import com.moabam.api.domain.room.repository.RoutineSearchRepository;
import com.moabam.api.dto.room.CreateRoomRequest;
import com.moabam.api.dto.room.EnterRoomRequest;
import com.moabam.api.dto.room.ModifyRoomRequest;
Expand All @@ -38,7 +45,6 @@ public class RoomService {

private final RoomRepository roomRepository;
private final RoutineRepository routineRepository;
private final RoutineSearchRepository routineSearchRepository;
private final ParticipantRepository participantRepository;
private final ParticipantSearchRepository participantSearchRepository;
private final MemberService memberService;
Expand All @@ -47,19 +53,15 @@ public class RoomService {
public Long createRoom(Long memberId, String nickname, CreateRoomRequest createRoomRequest) {
Room room = RoomMapper.toRoomEntity(createRoomRequest);
List<Routine> routines = RoutineMapper.toRoutineEntities(room, createRoomRequest.routines());
Participant participant = Participant.builder()
.room(room)
.memberId(memberId)
.build();
Participant participant = ParticipantMapper.toParticipantEntity(room, memberId);

if (!isEnterRoomAvailable(memberId, room.getRoomType())) {
throw new BadRequestException(MEMBER_ROOM_EXCEED);
}

increaseRoomCount(memberId, room.getRoomType());
validateEnteredRoomCount(memberId, room.getRoomType());

Member member = memberService.getById(memberId);
member.enterRoom(room.getRoomType());
participant.enableManager();
room.changeManagerNickname(nickname);

Room savedRoom = roomRepository.save(room);
routineRepository.saveAll(routines);
participantRepository.save(participant);
Expand All @@ -79,7 +81,7 @@ public void modifyRoom(Long memberId, Long roomId, ModifyRoomRequest modifyRoomR
room.changeCertifyTime(modifyRoomRequest.certifyTime());
room.changeMaxCount(modifyRoomRequest.maxUserCount());

List<Routine> routines = routineSearchRepository.findAllByRoomId(roomId);
List<Routine> routines = routineRepository.findAllByRoomId(roomId);
routineRepository.deleteAll(routines);

List<Routine> newRoutines = RoutineMapper.toRoutineEntities(room, modifyRoomRequest.routines());
Expand All @@ -91,13 +93,11 @@ public void enterRoom(Long memberId, Long roomId, EnterRoomRequest enterRoomRequ
Room room = roomRepository.findById(roomId).orElseThrow(() -> new NotFoundException(ROOM_NOT_FOUND));
validateRoomEnter(memberId, enterRoomRequest.password(), room);

Member member = memberService.getById(memberId);
member.enterRoom(room.getRoomType());
room.increaseCurrentUserCount();
increaseRoomCount(memberId, room.getRoomType());

Participant participant = Participant.builder()
.room(room)
.memberId(memberId)
.build();
Participant participant = ParticipantMapper.toParticipantEntity(room, memberId);
participantRepository.save(participant);
}

Expand All @@ -106,11 +106,11 @@ public void exitRoom(Long memberId, Long roomId) {
Participant participant = getParticipant(memberId, roomId);
Room room = participant.getRoom();

if (participant.isManager() && room.getCurrentUserCount() != 1) {
throw new BadRequestException(ROOM_EXIT_MANAGER_FAIL);
}
validateRoomExit(participant, room);

Member member = memberService.getById(memberId);
member.exitRoom(room.getRoomType());

decreaseRoomCount(memberId, room.getRoomType());
participant.removeRoom();
participantRepository.flush();
participantRepository.delete(participant);
Expand All @@ -124,7 +124,7 @@ public void exitRoom(Long memberId, Long roomId) {
}

@Transactional
public void mandateRoomManager(Long managerId, Long roomId, Long memberId) {
public void mandateManager(Long managerId, Long roomId, Long memberId) {
Participant managerParticipant = getParticipant(managerId, roomId);
Participant memberParticipant = getParticipant(memberId, roomId);
validateManagerAuthorization(managerParticipant);
Expand All @@ -141,13 +141,14 @@ public void mandateRoomManager(Long managerId, Long roomId, Long memberId) {
public void deportParticipant(Long managerId, Long roomId, Long memberId) {
Participant managerParticipant = getParticipant(managerId, roomId);
Participant memberParticipant = getParticipant(memberId, roomId);
Room room = managerParticipant.getRoom();

validateManagerAuthorization(managerParticipant);

Room room = managerParticipant.getRoom();
participantRepository.delete(memberParticipant);
room.decreaseCurrentUserCount();
decreaseRoomCount(memberId, room.getRoomType());

Member member = memberService.getById(memberId);
member.exitRoom(room.getRoomType());
}

public void validateRoomById(Long roomId) {
Expand All @@ -168,9 +169,8 @@ private void validateManagerAuthorization(Participant participant) {
}

private void validateRoomEnter(Long memberId, String requestPassword, Room room) {
if (!isEnterRoomAvailable(memberId, room.getRoomType())) {
throw new BadRequestException(MEMBER_ROOM_EXCEED);
}
validateEnteredRoomCount(memberId, room.getRoomType());

if (!StringUtils.isEmpty(requestPassword) && !room.getPassword().equals(requestPassword)) {
throw new BadRequestException(WRONG_ROOM_PASSWORD);
}
Expand All @@ -179,38 +179,20 @@ private void validateRoomEnter(Long memberId, String requestPassword, Room room)
}
}

private boolean isEnterRoomAvailable(Long memberId, RoomType roomType) {
private void validateEnteredRoomCount(Long memberId, RoomType roomType) {
Member member = memberService.getById(memberId);

if (roomType.equals(MORNING) && member.getCurrentMorningCount() >= 3) {
return false;
throw new BadRequestException(MEMBER_ROOM_EXCEED);
}
if (roomType.equals(NIGHT) && member.getCurrentNightCount() >= 3) {
return false;
}

return true;
}

private void increaseRoomCount(Long memberId, RoomType roomType) {
Member member = memberService.getById(memberId);

if (roomType.equals(MORNING)) {
member.enterMorningRoom();
return;
throw new BadRequestException(MEMBER_ROOM_EXCEED);
}

member.enterNightRoom();
}

private void decreaseRoomCount(Long memberId, RoomType roomType) {
Member member = memberService.getById(memberId);

if (roomType.equals(MORNING)) {
member.exitMorningRoom();
return;
private void validateRoomExit(Participant participant, Room room) {
if (participant.isManager() && room.getCurrentUserCount() != 1) {
throw new BadRequestException(ROOM_EXIT_MANAGER_FAIL);
}

member.exitNightRoom();
}
}
Loading