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: 방 관련 기능 리팩터링 #49

Merged
merged 4 commits into from
Nov 7, 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
25 changes: 14 additions & 11 deletions src/main/java/com/moabam/api/application/RoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
import com.moabam.api.domain.entity.Room;
import com.moabam.api.domain.entity.Routine;
import com.moabam.api.domain.entity.enums.RoomType;
import com.moabam.api.domain.repository.CertificationsMapper;
import com.moabam.api.domain.repository.CertificationsSearchRepository;
import com.moabam.api.domain.repository.ParticipantRepository;
import com.moabam.api.domain.repository.ParticipantSearchRepository;
import com.moabam.api.domain.repository.RoomRepository;
import com.moabam.api.domain.repository.RoutineRepository;
import com.moabam.api.domain.repository.RoutineSearchRepository;
import com.moabam.api.dto.CertificationImageResponse;
import com.moabam.api.dto.CertificationsMapper;
import com.moabam.api.dto.CreateRoomRequest;
import com.moabam.api.dto.EnterRoomRequest;
import com.moabam.api.dto.ModifyRoomRequest;
Expand Down Expand Up @@ -57,7 +57,7 @@ public class RoomService {
private final MemberService memberService;

@Transactional
public void createRoom(Long memberId, CreateRoomRequest createRoomRequest) {
public Long createRoom(Long memberId, CreateRoomRequest createRoomRequest) {
Room room = RoomMapper.toRoomEntity(createRoomRequest);
List<Routine> routines = RoutineMapper.toRoutineEntities(room, createRoomRequest.routines());
Participant participant = Participant.builder()
Expand All @@ -66,9 +66,11 @@ public void createRoom(Long memberId, CreateRoomRequest createRoomRequest) {
.build();

participant.enableManager();
roomRepository.save(room);
Room savedRoom = roomRepository.save(room);
routineRepository.saveAll(routines);
participantRepository.save(participant);

return savedRoom.getId();
}

@Transactional
Expand All @@ -85,6 +87,12 @@ public void modifyRoom(Long memberId, Long roomId, ModifyRoomRequest modifyRoomR
room.changePassword(modifyRoomRequest.password());
room.changeCertifyTime(modifyRoomRequest.certifyTime());
room.changeMaxCount(modifyRoomRequest.maxUserCount());

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

List<Routine> newRoutines = RoutineMapper.toRoutineEntities(room, modifyRoomRequest.routines());
routineRepository.saveAll(newRoutines);
}

@Transactional
Expand Down Expand Up @@ -134,7 +142,7 @@ public RoomDetailsResponse getRoomDetails(Long memberId, Long roomId) {
certificationsSearchRepository.findSortedDailyMemberCertifications(roomId, today);
List<RoutineResponse> routineResponses = getRoutineResponses(roomId);
List<TodayCertificateRankResponse> todayCertificateRankResponses = getTodayCertificateRankResponses(roomId,
routineResponses, dailyMemberCertifications, today);
dailyMemberCertifications, today);
List<LocalDate> certifiedDates = getCertifiedDates(roomId, today);
double completePercentage = calculateCompletePercentage(dailyMemberCertifications.size(),
room.getCurrentUserCount());
Expand Down Expand Up @@ -208,15 +216,10 @@ private List<RoutineResponse> getRoutineResponses(Long roomId) {
}

private List<TodayCertificateRankResponse> getTodayCertificateRankResponses(Long roomId,
List<RoutineResponse> routines, List<DailyMemberCertification> dailyMemberCertifications, LocalDate today) {
List<DailyMemberCertification> dailyMemberCertifications, LocalDate today) {

List<TodayCertificateRankResponse> responses = new ArrayList<>();
List<Long> routineIds = routines.stream()
.map(RoutineResponse::routineId)
.toList();
List<Certification> certifications = certificationsSearchRepository.findCertifications(
routineIds,
today);
List<Certification> certifications = certificationsSearchRepository.findCertifications(roomId, today);
List<Participant> participants = participantSearchRepository.findParticipants(roomId);
List<Member> members = memberService.getRoomMembers(participants.stream()
.map(Participant::getMemberId)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/moabam/api/domain/entity/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class Room extends BaseTimeEntity {
@Column(name = "id")
private Long id;

@Column(name = "title", nullable = false, length = 30)
@Column(name = "title", nullable = false, length = 20)
private String title;

@Column(name = "password", length = 8)
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/com/moabam/api/domain/entity/Routine.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.moabam.api.domain.entity;

import static com.moabam.global.error.model.ErrorMessage.*;
import static java.util.Objects.*;

import org.apache.commons.lang3.StringUtils;

import com.moabam.global.common.entity.BaseTimeEntity;
import com.moabam.global.error.exception.BadRequestException;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -33,17 +37,25 @@ public class Routine extends BaseTimeEntity {
@JoinColumn(name = "room_id", updatable = false)
private Room room;

@Column(name = "content", nullable = false, length = 60)
@Column(name = "content", nullable = false, length = 20)
private String content;

@Builder
private Routine(Long id, Room room, String content) {
this.id = id;
this.room = requireNonNull(room);
this.content = requireNonNull(content);
this.content = validateContent(content);
}

public void changeContent(String content) {
this.content = content;
}

private String validateContent(String content) {
if (StringUtils.isBlank(content) || content.length() > 20) {
throw new BadRequestException(ROUTINE_LENGTH_ERROR);
}

return content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.moabam.api.domain.entity.Certification;
import com.moabam.api.domain.entity.DailyMemberCertification;
import com.moabam.api.domain.entity.DailyRoomCertification;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;

import lombok.RequiredArgsConstructor;
Expand All @@ -25,18 +24,10 @@ public class CertificationsSearchRepository {

private final JPAQueryFactory jpaQueryFactory;

public List<Certification> findCertifications(List<Long> routineIds, LocalDate date) {
BooleanExpression expression = null;

for (Long routineId : routineIds) {
BooleanExpression routineExpression = certification.routine.id.eq(routineId);
expression = expression == null ? routineExpression : expression.or(routineExpression);
}

return jpaQueryFactory
.selectFrom(certification)
public List<Certification> findCertifications(Long roomId, LocalDate date) {
return jpaQueryFactory.selectFrom(certification)
.where(
expression,
certification.routine.room.id.eq(roomId),
certification.createdAt.between(date.atStartOfDay(), date.atTime(LocalTime.MAX))
)
.fetch();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.moabam.api.domain.repository;
package com.moabam.api.dto;

import java.util.ArrayList;
import java.util.List;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/moabam/api/dto/CreateRoomRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;

import com.moabam.api.domain.entity.enums.RoomType;
Expand All @@ -12,7 +13,7 @@
import jakarta.validation.constraints.Size;

public record CreateRoomRequest(
@NotBlank String title,
@NotBlank @Length(max = 20) String title,
@Pattern(regexp = "^(|[0-9]{4,8})$") String password,
@NotNull @Size(min = 1, max = 4) List<String> routines,
@NotNull RoomType roomType,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/moabam/api/dto/ModifyRoomRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import jakarta.validation.constraints.Size;

public record ModifyRoomRequest(
@NotBlank String title,
@NotBlank @Length(max = 20) String title,
@Length(max = 255, message = "방 공지의 길이가 너무 깁니다.") String announcement,
@NotNull @Size(min = 1, max = 4) List<String> routines,
@Pattern(regexp = "^(|\\d{4,8})$") String password,
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/moabam/api/presentation/RoomController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class RoomController {

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public void createRoom(@Valid @RequestBody CreateRoomRequest createRoomRequest) {
roomService.createRoom(1L, createRoomRequest);
public Long createRoom(@Valid @RequestBody CreateRoomRequest createRoomRequest) {
return roomService.createRoom(1L, createRoomRequest);
}

@PutMapping("/{roomId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public enum ErrorMessage {
WRONG_ROOM_PASSWORD("방의 비밀번호가 일치하지 않습니다."),
ROOM_MAX_USER_REACHED("방의 인원수가 찼습니다."),
ROOM_DETAILS_ERROR("방 정보를 불러오는데 실패했습니다."),
ROUTINE_LENGTH_ERROR("루틴의 길이가 잘못 되었습니다."),

LOGIN_FAILED("로그인에 실패했습니다."),
REQUEST_FAILED("네트워크 접근 실패입니다."),
Expand Down
16 changes: 14 additions & 2 deletions src/test/java/com/moabam/api/application/RoomServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.moabam.api.application;

import static com.moabam.api.domain.entity.enums.RoomType.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.*;

import java.util.ArrayList;
Expand All @@ -22,6 +23,7 @@
import com.moabam.api.domain.repository.RoomRepository;
import com.moabam.api.domain.repository.RoutineRepository;
import com.moabam.api.dto.CreateRoomRequest;
import com.moabam.api.dto.RoomMapper;

@ExtendWith(MockitoExtension.class)
class RoomServiceTest {
Expand Down Expand Up @@ -52,13 +54,18 @@ void create_room_no_password_success() {
CreateRoomRequest createRoomRequest = new CreateRoomRequest(
"재윤과 앵맹이의 방임", null, routines, MORNING, 10, 4);

Room expectedRoom = RoomMapper.toRoomEntity(createRoomRequest);
given(roomRepository.save(any(Room.class))).willReturn(expectedRoom);

// when
roomService.createRoom(1L, createRoomRequest);
Long result = roomService.createRoom(1L, createRoomRequest);

// then
verify(roomRepository).save(any(Room.class));
verify(routineRepository).saveAll(ArgumentMatchers.<Routine>anyList());
verify(participantRepository).save(any(Participant.class));
assertThat(result).isEqualTo(expectedRoom.getId());
assertThat(expectedRoom.getPassword()).isNull();
}

@DisplayName("비밀번호 있는 방 생성 성공")
Expand All @@ -72,12 +79,17 @@ void create_room_with_password_success() {
CreateRoomRequest createRoomRequest = new CreateRoomRequest(
"재윤과 앵맹이의 방임", "1234", routines, MORNING, 10, 4);

Room expectedRoom = RoomMapper.toRoomEntity(createRoomRequest);
given(roomRepository.save(any(Room.class))).willReturn(expectedRoom);

// when
roomService.createRoom(1L, createRoomRequest);
Long result = roomService.createRoom(1L, createRoomRequest);

// then
verify(roomRepository).save(any(Room.class));
verify(routineRepository).saveAll(ArgumentMatchers.<Routine>anyList());
verify(participantRepository).save(any(Participant.class));
assertThat(result).isEqualTo(expectedRoom.getId());
assertThat(expectedRoom.getPassword()).isEqualTo("1234");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void create_room_with_password_success(String password) throws Exception {
routines.add("물 마시기");
routines.add("코테 풀기");
CreateRoomRequest createRoomRequest = new CreateRoomRequest(
"비번 있는 재윤과 앵맹이의 방임", password, routines, MORNING, 10, 4);
"비번 있는 재맹의 방임", password, routines, MORNING, 10, 4);
String json = objectMapper.writeValueAsString(createRoomRequest);

// expected
Expand All @@ -141,7 +141,7 @@ void create_room_with_password_success(String password) throws Exception {
.andExpect(status().isCreated())
.andDo(print());
assertThat(roomRepository.findAll()).hasSize(1);
assertThat(roomRepository.findAll().get(0).getTitle()).isEqualTo("비번 있는 재윤과 앵맹이의 방임");
assertThat(roomRepository.findAll().get(0).getTitle()).isEqualTo("비번 있는 재맹의 방임");
assertThat(roomRepository.findAll().get(0).getPassword()).isEqualTo(password);
}

Expand Down