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] refactor: 코드 리팩토링(#12) #13

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion backend/src/main/java/corea/domain/JoinInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public class JoinInfo {
private long roomId;

public JoinInfo(final long memberId, final long roomId) {
this(null,memberId,roomId);
this(null, memberId, roomId);
}
}
5 changes: 3 additions & 2 deletions backend/src/main/java/corea/dto/JoinInfoResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ public record JoinInfoResponse(
long id,
long memberId,
long roomId
){
public static JoinInfoResponse from(final JoinInfo joinInfo){
) {

public static JoinInfoResponse from(final JoinInfo joinInfo) {
return new JoinInfoResponse(
joinInfo.getId(),
joinInfo.getMemberId(),
Expand Down
3 changes: 2 additions & 1 deletion backend/src/main/java/corea/dto/RoomCreateRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public Room toEntity() {
matchingSize,
keyword,
submissionDeadline,
reviewDeadline);
reviewDeadline
);
}
}
3 changes: 1 addition & 2 deletions backend/src/main/java/corea/dto/RoomResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ public record RoomResponse(
String keyword,
LocalDateTime submissionDeadline,
LocalDateTime reviewDeadline

) {

public static RoomResponse from(final Room room,final String memberEmail) {
public static RoomResponse of(final Room room, final String memberEmail) {
return new RoomResponse(
room.getId(),
room.getTitle(),
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/java/corea/member/domain/Matching.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

import static lombok.AccessLevel.PROTECTED;

@NoArgsConstructor(access = PROTECTED)
@Component
@NoArgsConstructor(access = PROTECTED)
public class Matching {

public Map<Long, List<Long>> matchGroup(final List<Member> members, final int matchingSize) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package corea.member.service;

import corea.domain.Member;
import corea.member.domain.Matching;
import corea.member.entity.MatchedGroup;
import corea.domain.Member;
import corea.member.repository.MatchedGroupRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down
35 changes: 16 additions & 19 deletions backend/src/main/java/corea/service/RoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,27 @@ public class RoomService {

public RoomResponse create(final RoomCreateRequest request) {
final Room room = roomRepository.save(request.toEntity());
final long memberId = request.memberId();
final Member member = getMember(memberId);
return RoomResponse.from(room, member.getEmail());
return toRoomResponse(room);
}

public RoomResponse findOne(final long id) {
final Room room = roomRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException(String.format("%d에 해당하는 방이 없습니다.", id)));
final Member member = getMember(room.getMemberId());

return RoomResponse.from(room, member.getEmail());
}

private Member getMember(final long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new IllegalArgumentException(String.format("%d에 해당하는 멤버가 없습니다.", memberId)));
final Room room = getRoom(id);
return toRoomResponse(room);
}

public RoomResponses findAll() {
final List<Room> rooms = roomRepository.findAll();

return rooms.stream()
.map(room -> {
final long memberId = room.getMemberId();
final Member member = getMember(memberId);
return RoomResponse.from(room, member.getEmail());
})
.map(this::toRoomResponse)
.collect(collectingAndThen(toList(), RoomResponses::new));
}

private RoomResponse toRoomResponse(final Room room) {
final Member member = getMember(room.getMemberId());
return RoomResponse.of(room, member.getEmail());
}

public JoinInfoResponse join(final long roomId, final long memberId) {
final Room room = getRoom(roomId);
final Member member = getMember(memberId);
Expand All @@ -67,8 +59,13 @@ public JoinInfoResponse join(final long roomId, final long memberId) {
return JoinInfoResponse.from(joinInfoRepository.save(joinInfo));
}

private Member getMember(final long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new IllegalArgumentException(String.format("해당 Id의 멤버가 없습니다. 입력된 Id=%d", memberId)));
}

public Room getRoom(final long roomId) {
return roomRepository.findById(roomId)
.orElseThrow(() -> new IllegalArgumentException(String.format("%d에 해당하는 방이 없습니다.", roomId)));
.orElseThrow(() -> new IllegalArgumentException(String.format("해당 Id의 방이 없습니다. 입력된 Id=%d", roomId)));
}
}
10 changes: 10 additions & 0 deletions backend/src/test/java/corea/fixture/MemberFixture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package corea.fixture;

import corea.domain.Member;

public class MemberFixture {

public static Member MEMBER_PORORO() {
return new Member("[email protected]");
}
}
6 changes: 4 additions & 2 deletions backend/src/test/java/corea/fixture/RoomFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import java.time.LocalDateTime;

public class RoomFixture {
public static RoomCreateRequest CREATE_REQUEST(final long memberId){

public static RoomCreateRequest ROOM_CREATE_REQUEST(final long memberId){
return new RoomCreateRequest(
"레이싱 카와 함께하는 TDD",
memberId,
Expand All @@ -18,7 +19,8 @@ public static RoomCreateRequest CREATE_REQUEST(final long memberId){
LocalDateTime.now().plusDays(14)
);
}
public static Room GET_DOMAIN(final long memberId){

public static Room ROOM_RACING_CAR(final long memberId){
return new Room(
"레이싱 카와 함께하는 TDD",
memberId,
Expand Down
4 changes: 2 additions & 2 deletions backend/src/test/java/corea/member/domain/MatchingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ class MatchingTest {

@Test
@DisplayName("멤버 리스트를 받아서 매칭 결과를 반환한다.")
void matchGroup(){
void matchGroup() {
List<Member> members = List.of(
new Member(1L, "[email protected]"),
new Member(2L, "[email protected]"),
new Member(3L, "[email protected]"),
new Member(4L, "[email protected]")
);
);
int matchingSize = 2;

Map<Long, List<Long>> results = matching.matchGroup(members, matchingSize);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
package corea.member.service;

import config.ServiceTest;
import corea.domain.Member;
import corea.member.repository.MatchedGroupRepository;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
@ServiceTest
class MatchingServiceTest {

private final MatchingService matchingService;
private final MatchedGroupRepository matchedGroupRepository;
@Autowired
MatchingService matchingService;

@Autowired
public MatchingServiceTest(MatchingService matchingService, MatchedGroupRepository matchedGroupRepository) {
this.matchingService = matchingService;
this.matchedGroupRepository = matchedGroupRepository;
}
MatchedGroupRepository matchedGroupRepository;

@Test
@DisplayName("멤버 리스트를 받아 매칭 결과를 반환한다.")
Expand All @@ -38,5 +35,4 @@ void matchMaking() {

assertThat(matchedGroupRepository.findAll()).hasSize(4);
}

}
Loading
Loading