From 52b192d20584c7180ef1e4ae7316349964996782 Mon Sep 17 00:00:00 2001 From: jenny <88226911+kmjenny@users.noreply.github.com> Date: Thu, 23 May 2024 17:44:56 +0900 Subject: [PATCH 1/8] [feat] #21 Room List API --- .../server/controller/RoomController.java | 13 +- .../com/telepigeon/server/domain/Answer.java | 1 + .../com/telepigeon/server/domain/Profile.java | 15 ++- .../com/telepigeon/server/domain/Users.java | 17 +++ .../server/dto/room/response/RoomListDto.java | 42 +++++++ .../server/repository/ProfileRepository.java | 2 + .../server/repository/RoomRepository.java | 5 + .../service/profile/ProfileRetriever.java | 10 ++ .../server/service/room/RoomRetriever.java | 3 + .../server/service/room/RoomSaver.java | 1 + .../server/service/room/RoomService.java | 34 +++++- .../server/service/user/UserRetriever.java | 19 +++ src/main/resources/application-example.yml | 19 --- .../server/roomTest/RoomServiceTest.java | 114 +++++++++++++++--- src/test/resources/application-test.yml | 15 +++ 15 files changed, 261 insertions(+), 49 deletions(-) create mode 100644 src/main/java/com/telepigeon/server/dto/room/response/RoomListDto.java create mode 100644 src/main/java/com/telepigeon/server/service/user/UserRetriever.java delete mode 100644 src/main/resources/application-example.yml create mode 100644 src/test/resources/application-test.yml diff --git a/src/main/java/com/telepigeon/server/controller/RoomController.java b/src/main/java/com/telepigeon/server/controller/RoomController.java index 909f583..249624a 100644 --- a/src/main/java/com/telepigeon/server/controller/RoomController.java +++ b/src/main/java/com/telepigeon/server/controller/RoomController.java @@ -3,16 +3,16 @@ import com.telepigeon.server.annotation.UserId; import com.telepigeon.server.domain.Room; import com.telepigeon.server.dto.room.request.RoomCreateDto; +import com.telepigeon.server.dto.room.response.RoomListDto; import com.telepigeon.server.service.room.RoomService; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; +import org.w3c.dom.stylesheets.LinkStyle; import java.net.URI; +import java.util.List; @RestController @RequiredArgsConstructor @@ -29,4 +29,9 @@ public ResponseEntity createRoom( URI location = URI.create("/rooms/" + createdRoom.getId()); return ResponseEntity.created(location).build(); } + + @GetMapping("/rooms") + public ResponseEntity> getAllRooms(@UserId Long userId) { + return ResponseEntity.ok(roomService.getAllRooms(userId)); + } } diff --git a/src/main/java/com/telepigeon/server/domain/Answer.java b/src/main/java/com/telepigeon/server/domain/Answer.java index 7b44262..0fb50e5 100644 --- a/src/main/java/com/telepigeon/server/domain/Answer.java +++ b/src/main/java/com/telepigeon/server/domain/Answer.java @@ -13,6 +13,7 @@ @Table(name="answer") @NoArgsConstructor(access=AccessLevel.PROTECTED) public class Answer { + @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; diff --git a/src/main/java/com/telepigeon/server/domain/Profile.java b/src/main/java/com/telepigeon/server/domain/Profile.java index 80efef6..bb80eac 100644 --- a/src/main/java/com/telepigeon/server/domain/Profile.java +++ b/src/main/java/com/telepigeon/server/domain/Profile.java @@ -48,9 +48,10 @@ public class Profile { private Room room; @Builder - private Profile(Users user, Room room) { + private Profile(Users user, Room room, Relation relation) { this.user = user; this.room = room; + this.relation = relation; } public static Profile create( @@ -62,4 +63,16 @@ public static Profile create( .room(room) .build(); } + + public static Profile createTest( + Users user, + Room room, + Relation relation + ) { + return Profile.builder() + .user(user) + .room(room) + .relation(relation) + .build(); + } } diff --git a/src/main/java/com/telepigeon/server/domain/Users.java b/src/main/java/com/telepigeon/server/domain/Users.java index 54a10ce..c9d868a 100644 --- a/src/main/java/com/telepigeon/server/domain/Users.java +++ b/src/main/java/com/telepigeon/server/domain/Users.java @@ -2,6 +2,7 @@ import jakarta.persistence.*; import lombok.AccessLevel; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -26,4 +27,20 @@ public class Users { private LocalDateTime updatedAt; + @Builder + private Users(Long id, String name) { + this.id = id; + this.name = name; + } + + public static Users create( + Long id, + String name + ) { + return Users.builder() + .id(id) + .name(name) + .build(); + } + } diff --git a/src/main/java/com/telepigeon/server/dto/room/response/RoomListDto.java b/src/main/java/com/telepigeon/server/dto/room/response/RoomListDto.java new file mode 100644 index 0000000..8c29bdd --- /dev/null +++ b/src/main/java/com/telepigeon/server/dto/room/response/RoomListDto.java @@ -0,0 +1,42 @@ +package com.telepigeon.server.dto.room.response; + +import com.telepigeon.server.domain.Answer; +import com.telepigeon.server.domain.Profile; +import com.telepigeon.server.domain.Room; + +public record RoomListDto( + Long roomId, + String name, + String opponentNickname, + String myRelation, + String opponentRelation, + Integer emotion, + Integer sentence +) { + public static RoomListDto of(Room room, Profile myProfile, Profile opponentProfile, Answer myAnswer, Answer opponentAnswer) { + boolean myState = myAnswer.getContent() != null; + boolean opponentState = opponentAnswer.getContent() != null; + + // 감정 측정 시 업데이트 + int emotion = 0; + + int sentence; + if (myState && opponentState) { + sentence = 0; + } else if (myState) { + sentence = 1; + } else { + sentence = 2; + } + + return new RoomListDto( + room.getId(), + room.getName(), + opponentProfile.getUser().getName(), + myProfile.getRelation().getContent(), + opponentProfile.getRelation().getContent(), + emotion, + sentence + ); + } +} diff --git a/src/main/java/com/telepigeon/server/repository/ProfileRepository.java b/src/main/java/com/telepigeon/server/repository/ProfileRepository.java index 7cf8066..57be615 100644 --- a/src/main/java/com/telepigeon/server/repository/ProfileRepository.java +++ b/src/main/java/com/telepigeon/server/repository/ProfileRepository.java @@ -5,9 +5,11 @@ import com.telepigeon.server.domain.Users; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; import java.util.Optional; public interface ProfileRepository extends JpaRepository { Optional findByUserAndRoom(Users user, Room room); Optional findByUserNotAndRoom(Users user, Room room); + List findByUserId(Long userId); } diff --git a/src/main/java/com/telepigeon/server/repository/RoomRepository.java b/src/main/java/com/telepigeon/server/repository/RoomRepository.java index ded9af6..a34e909 100644 --- a/src/main/java/com/telepigeon/server/repository/RoomRepository.java +++ b/src/main/java/com/telepigeon/server/repository/RoomRepository.java @@ -1,9 +1,14 @@ package com.telepigeon.server.repository; +import com.telepigeon.server.domain.Profile; import com.telepigeon.server.domain.Room; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; +import java.util.Optional; + public interface RoomRepository extends JpaRepository { boolean existsByName(String name); boolean existsByCode(String code); + } diff --git a/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java b/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java index 835fc45..ce32d59 100644 --- a/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java +++ b/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java @@ -9,6 +9,8 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; +import java.util.List; + @Component @RequiredArgsConstructor public class ProfileRetriever { @@ -25,4 +27,12 @@ public Profile findByUserNotAndRoom(Users user, Room room) { .orElseThrow(() -> new NotFoundException(NotFoundErrorCode.PROFILE_NOT_FOUND)); } + public List findByUserId(final long userId) { + List profileList = profileRepository.findByUserId(userId); + if (profileList.isEmpty()) { + throw new NotFoundException(NotFoundErrorCode.PROFILE_NOT_FOUND); + } + return profileList; + } + } diff --git a/src/main/java/com/telepigeon/server/service/room/RoomRetriever.java b/src/main/java/com/telepigeon/server/service/room/RoomRetriever.java index 56b1a84..6cb0157 100644 --- a/src/main/java/com/telepigeon/server/service/room/RoomRetriever.java +++ b/src/main/java/com/telepigeon/server/service/room/RoomRetriever.java @@ -5,8 +5,11 @@ import com.telepigeon.server.exception.code.NotFoundErrorCode; import com.telepigeon.server.repository.RoomRepository; import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.util.List; + @Component @RequiredArgsConstructor public class RoomRetriever { diff --git a/src/main/java/com/telepigeon/server/service/room/RoomSaver.java b/src/main/java/com/telepigeon/server/service/room/RoomSaver.java index 7492c3b..a0f1cab 100644 --- a/src/main/java/com/telepigeon/server/service/room/RoomSaver.java +++ b/src/main/java/com/telepigeon/server/service/room/RoomSaver.java @@ -3,6 +3,7 @@ import com.telepigeon.server.domain.Room; import com.telepigeon.server.repository.RoomRepository; import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component diff --git a/src/main/java/com/telepigeon/server/service/room/RoomService.java b/src/main/java/com/telepigeon/server/service/room/RoomService.java index 316846d..14f3d0d 100644 --- a/src/main/java/com/telepigeon/server/service/room/RoomService.java +++ b/src/main/java/com/telepigeon/server/service/room/RoomService.java @@ -1,17 +1,25 @@ package com.telepigeon.server.service.room; +import com.telepigeon.server.domain.Answer; import com.telepigeon.server.domain.Profile; import com.telepigeon.server.domain.Room; import com.telepigeon.server.domain.Users; import com.telepigeon.server.dto.room.request.RoomCreateDto; +import com.telepigeon.server.dto.room.response.RoomListDto; +import com.telepigeon.server.repository.AnswerRepository; import com.telepigeon.server.repository.RoomRepository; -import com.telepigeon.server.repository.UserRepository; +import com.telepigeon.server.service.answer.AnswerRetriever; +import com.telepigeon.server.service.profile.ProfileRetriever; import com.telepigeon.server.service.profile.ProfileSaver; +import com.telepigeon.server.service.user.UserRetriever; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; import java.util.Random; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor @@ -19,14 +27,14 @@ public class RoomService { private final RoomRepository roomRepository; private final RoomSaver roomSaver; - private final RoomRetriever roomRetriever; - private final RoomRemover roomRemover; - private final UserRepository userRepository; private final ProfileSaver profileSaver; + private final UserRetriever userRetriever; + private final ProfileRetriever profileRetriever; + private final AnswerRetriever answerRetriever; @Transactional public Room createRoom(final RoomCreateDto roomCreateDto, final Long userId){ - Users user = userRepository.findByIdOrThrow(userId); + Users user = userRetriever.findById(userId); String code = createCode(); @@ -56,4 +64,20 @@ private String createCode() { return code; } + + public List getAllRooms(final Long userId) { + Users user = userRetriever.findById(userId); + List profileList = profileRetriever.findByUserId(userId); + List roomList = profileList.stream().map(Profile::getRoom).toList(); + + return roomList.stream().map(room -> { + Profile myProfile = profileRetriever.findByUserAndRoom(user, room); + Profile opponentProfile = profileRetriever.findByUserNotAndRoom(user, room); + Answer myAnswer = answerRetriever.findFirstByProfile(myProfile); + Answer opponentAnswer = answerRetriever.findFirstByProfile(opponentProfile); + + return RoomListDto.of(room, myProfile, opponentProfile, myAnswer, opponentAnswer); + }).collect(Collectors.toList()); + + } } diff --git a/src/main/java/com/telepigeon/server/service/user/UserRetriever.java b/src/main/java/com/telepigeon/server/service/user/UserRetriever.java new file mode 100644 index 0000000..0fd71aa --- /dev/null +++ b/src/main/java/com/telepigeon/server/service/user/UserRetriever.java @@ -0,0 +1,19 @@ +package com.telepigeon.server.service.user; + +import com.telepigeon.server.domain.Users; +import com.telepigeon.server.exception.NotFoundException; +import com.telepigeon.server.exception.code.NotFoundErrorCode; +import com.telepigeon.server.repository.UserRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class UserRetriever { + private final UserRepository userRepository; + + public Users findById(final long id) { + return userRepository.findById(id) + .orElseThrow(() -> new NotFoundException(NotFoundErrorCode.USER_NOT_FOUND)); + } +} diff --git a/src/main/resources/application-example.yml b/src/main/resources/application-example.yml deleted file mode 100644 index 1a50108..0000000 --- a/src/main/resources/application-example.yml +++ /dev/null @@ -1,19 +0,0 @@ -spring: - web: - resources: - add-mappings: false # Disable static resource mapping (For NoHandlerFoundException) - datasource: - url: ${mysql_url} - username: ${mysql_username} - password: ${mysql_password} - driver-class-name: com.mysql.cj.jdbc.Driver - jpa: - hibernate: - ddl-auto: none - properties: - hibernate: - dialect: org.hibernate.dialect.MySQL8Dialect - data: - redis: - host: ${redis_host} - port: ${redis_port} \ No newline at end of file diff --git a/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java b/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java index 345412d..35ef25d 100644 --- a/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java +++ b/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java @@ -1,63 +1,85 @@ package com.telepigeon.server.roomTest; -import com.telepigeon.server.domain.Profile; -import com.telepigeon.server.domain.Room; -import com.telepigeon.server.domain.Users; +import com.telepigeon.server.domain.*; +import com.telepigeon.server.dto.answer.request.AnswerCreateDto; import com.telepigeon.server.dto.room.request.RoomCreateDto; +import com.telepigeon.server.dto.room.response.RoomListDto; +import com.telepigeon.server.dto.type.Relation; import com.telepigeon.server.repository.RoomRepository; import com.telepigeon.server.repository.UserRepository; +import com.telepigeon.server.service.answer.AnswerRetriever; +import com.telepigeon.server.service.profile.ProfileRetriever; import com.telepigeon.server.service.profile.ProfileSaver; import com.telepigeon.server.service.room.RoomRetriever; import com.telepigeon.server.service.room.RoomSaver; import com.telepigeon.server.service.room.RoomService; +import com.telepigeon.server.service.user.UserRetriever; +import jakarta.transaction.Transactional; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; +import org.mockito.*; import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit.jupiter.SpringExtension; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; +@SpringBootTest @ExtendWith(MockitoExtension.class) public class RoomServiceTest { - @Mock - private RoomRepository roomRepository = Mockito.mock(RoomRepository.class); + @MockBean + private RoomRepository roomRepository; - @Mock - private UserRepository userRepository = Mockito.mock(UserRepository.class); + @MockBean + private UserRepository userRepository; - @InjectMocks + @Autowired private RoomService roomService; - @Mock + @MockBean private RoomSaver roomSaver; - @Mock + @MockBean private ProfileSaver profileSaver; + @MockBean + private UserRetriever userRetriever; + + @MockBean + private ProfileRetriever profileRetriever; + + @MockBean + private AnswerRetriever answerRetriever; + @Test @DisplayName("Room DB에 저장 확인") public void checkRoomInDB() { RoomCreateDto roomCreateDto = new RoomCreateDto("test"); Long userId = 1L; - Room room = Room.create(roomCreateDto, "code"); + Room room = Room.create(roomCreateDto, "code"); Users user = Mockito.mock(Users.class); - when(userRepository.findByIdOrThrow(userId)).thenReturn(user); + + when(userRetriever.findById(userId)).thenReturn(user); when(roomRepository.existsByCode(any(String.class))).thenReturn(false); when(roomSaver.save(any(Room.class))).thenReturn(room); - Profile savedProfile = Profile.create(user, room); - when(profileSaver.save(any(Profile.class))).thenReturn(savedProfile); - Room createdRoom = roomService.createRoom(roomCreateDto, userId); + Profile savedProfile = Profile.create(user, createdRoom); + Mockito.when(profileSaver.save(any(Profile.class))).thenReturn(savedProfile); + // 방이 올바르게 생성되었는지 확인 Assertions.assertEquals(room.getName(), createdRoom.getName()); @@ -84,4 +106,56 @@ public void checkRoomToDB() { Assertions.assertTrue(isCheck); } + @Test + @DisplayName("DB 저장 후 목록 확인") + public void checkRoomList() { + Long userId1 = 1L; + Long userId2 = 2L; + + // User 생성 + Users user1 = Users.create(userId1, "user1"); + Users user2 = Users.create(userId2, "user2"); + + // Room 생성 + RoomCreateDto roomCreateDto = new RoomCreateDto("test"); + Room createdRoom = Room.create(roomCreateDto, "code"); + when(roomSaver.save(any(Room.class))).thenReturn(createdRoom); + + // User1 Profile 생성 + Profile profile1 = Profile.createTest(user1, createdRoom, Relation.valueOf("CHILD")); + when(profileSaver.save(profile1)).thenReturn(profile1); + + // User2 Profile 생성 + Profile profile2 = Profile.createTest(user2, createdRoom, Relation.valueOf("MOTHER")); + when(profileSaver.save(profile2)).thenReturn(profile2); + + Question question1 = Question.create("question1", profile1); + Question question2 = Question.create("question2", profile2); + + AnswerCreateDto answerCreateDto = new AnswerCreateDto("answer1",null); + Answer ans1 = Answer.create(answerCreateDto, question1, profile1); + + AnswerCreateDto answerCreateDto2 = new AnswerCreateDto("answer2",null); + Answer ans2 = Answer.create(answerCreateDto2, question2, profile2); + + when(profileRetriever.findByUserId(userId1)).thenReturn(Collections.singletonList(profile1)); + when(userRetriever.findById(userId1)).thenReturn(user1); + when(profileRetriever.findByUserAndRoom(user1, createdRoom)).thenReturn(profile1); + when(profileRetriever.findByUserNotAndRoom(user1, createdRoom)).thenReturn(profile2); + when(answerRetriever.findFirstByProfile(profile1)).thenReturn(ans1); + when(answerRetriever.findFirstByProfile(profile2)).thenReturn(ans2); + + List roomList = roomService.getAllRooms(userId1); + + roomList.forEach(dto -> { + System.out.println("Room Id : " + dto.roomId()); + System.out.println("Room Name : " + dto.name()); + System.out.println("opponentNickname : " + dto.opponentNickname()); + System.out.println("myRelation : " + dto.myRelation()); + System.out.println("opponentRelation : " + dto.opponentRelation()); + System.out.println("sentence : " + dto.sentence()); + System.out.println("emotion : " + dto.emotion()); + }); + } + } diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml new file mode 100644 index 0000000..c436672 --- /dev/null +++ b/src/test/resources/application-test.yml @@ -0,0 +1,15 @@ +spring: + web: + resources: + add-mappings: false + datasource: + url: ${mysql_url} + username: ${mysql_username} + password: ${mysql_password} + driver-class-name: com.mysql.cj.jdbc.Driver + jpa: + hibernate: + ddl-auto: create-drop + properties: + hibernate: + dialect: org.hibernate.dialect.MySQL8Dialect \ No newline at end of file From 3d5b8a7b2bf7263788ce7b340daaf9599f51d177 Mon Sep 17 00:00:00 2001 From: jenny <88226911+kmjenny@users.noreply.github.com> Date: Thu, 23 May 2024 17:50:49 +0900 Subject: [PATCH 2/8] =?UTF-8?q?[add]=20#21=20test.yml=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application-test.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/resources/application-test.yml diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml new file mode 100644 index 0000000..1a50108 --- /dev/null +++ b/src/main/resources/application-test.yml @@ -0,0 +1,19 @@ +spring: + web: + resources: + add-mappings: false # Disable static resource mapping (For NoHandlerFoundException) + datasource: + url: ${mysql_url} + username: ${mysql_username} + password: ${mysql_password} + driver-class-name: com.mysql.cj.jdbc.Driver + jpa: + hibernate: + ddl-auto: none + properties: + hibernate: + dialect: org.hibernate.dialect.MySQL8Dialect + data: + redis: + host: ${redis_host} + port: ${redis_port} \ No newline at end of file From 983e829c6ea91d24ba709dafff80b6aed30b682b Mon Sep 17 00:00:00 2001 From: jenny <88226911+kmjenny@users.noreply.github.com> Date: Sat, 25 May 2024 18:35:16 +0900 Subject: [PATCH 3/8] =?UTF-8?q?[feat]=20#21=20Room=20=EC=A0=95=EB=B3=B4=20?= =?UTF-8?q?=EA=B0=80=EC=A0=B8=EC=98=A4=EA=B8=B0=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/controller/RoomController.java | 6 +++++ .../server/dto/room/response/RoomInfoDto.java | 15 +++++++++++ .../server/service/room/RoomService.java | 10 ++++++-- .../server/roomTest/RoomServiceTest.java | 25 +++++++++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/telepigeon/server/dto/room/response/RoomInfoDto.java diff --git a/src/main/java/com/telepigeon/server/controller/RoomController.java b/src/main/java/com/telepigeon/server/controller/RoomController.java index 249624a..cd3bc6a 100644 --- a/src/main/java/com/telepigeon/server/controller/RoomController.java +++ b/src/main/java/com/telepigeon/server/controller/RoomController.java @@ -3,6 +3,7 @@ import com.telepigeon.server.annotation.UserId; import com.telepigeon.server.domain.Room; import com.telepigeon.server.dto.room.request.RoomCreateDto; +import com.telepigeon.server.dto.room.response.RoomInfoDto; import com.telepigeon.server.dto.room.response.RoomListDto; import com.telepigeon.server.service.room.RoomService; import jakarta.validation.Valid; @@ -34,4 +35,9 @@ public ResponseEntity createRoom( public ResponseEntity> getAllRooms(@UserId Long userId) { return ResponseEntity.ok(roomService.getAllRooms(userId)); } + + @GetMapping("/rooms/{roomId}/info") + public ResponseEntity getRoomInfo(@PathVariable Long roomId) { + return ResponseEntity.ok(roomService.getRoomInfo(roomId)); + } } diff --git a/src/main/java/com/telepigeon/server/dto/room/response/RoomInfoDto.java b/src/main/java/com/telepigeon/server/dto/room/response/RoomInfoDto.java new file mode 100644 index 0000000..6882850 --- /dev/null +++ b/src/main/java/com/telepigeon/server/dto/room/response/RoomInfoDto.java @@ -0,0 +1,15 @@ +package com.telepigeon.server.dto.room.response; + +import com.telepigeon.server.domain.Room; + +public record RoomInfoDto( + String code, + String name +) { + public static RoomInfoDto of(Room room) { + return new RoomInfoDto( + room.getCode(), + room.getName() + ); + } +} diff --git a/src/main/java/com/telepigeon/server/service/room/RoomService.java b/src/main/java/com/telepigeon/server/service/room/RoomService.java index 14f3d0d..131d6c3 100644 --- a/src/main/java/com/telepigeon/server/service/room/RoomService.java +++ b/src/main/java/com/telepigeon/server/service/room/RoomService.java @@ -5,8 +5,8 @@ import com.telepigeon.server.domain.Room; import com.telepigeon.server.domain.Users; import com.telepigeon.server.dto.room.request.RoomCreateDto; +import com.telepigeon.server.dto.room.response.RoomInfoDto; import com.telepigeon.server.dto.room.response.RoomListDto; -import com.telepigeon.server.repository.AnswerRepository; import com.telepigeon.server.repository.RoomRepository; import com.telepigeon.server.service.answer.AnswerRetriever; import com.telepigeon.server.service.profile.ProfileRetriever; @@ -14,7 +14,6 @@ import com.telepigeon.server.service.user.UserRetriever; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @@ -31,6 +30,7 @@ public class RoomService { private final UserRetriever userRetriever; private final ProfileRetriever profileRetriever; private final AnswerRetriever answerRetriever; + private final RoomRetriever roomRetriever; @Transactional public Room createRoom(final RoomCreateDto roomCreateDto, final Long userId){ @@ -80,4 +80,10 @@ public List getAllRooms(final Long userId) { }).collect(Collectors.toList()); } + + public RoomInfoDto getRoomInfo(final Long roomId) { + Room room = roomRetriever.findById(roomId); + + return RoomInfoDto.of(room); + } } diff --git a/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java b/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java index 35ef25d..b63307c 100644 --- a/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java +++ b/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java @@ -3,6 +3,7 @@ import com.telepigeon.server.domain.*; import com.telepigeon.server.dto.answer.request.AnswerCreateDto; import com.telepigeon.server.dto.room.request.RoomCreateDto; +import com.telepigeon.server.dto.room.response.RoomInfoDto; import com.telepigeon.server.dto.room.response.RoomListDto; import com.telepigeon.server.dto.type.Relation; import com.telepigeon.server.repository.RoomRepository; @@ -62,6 +63,9 @@ public class RoomServiceTest { @MockBean private AnswerRetriever answerRetriever; + @MockBean + private RoomRetriever roomRetriever; + @Test @DisplayName("Room DB에 저장 확인") public void checkRoomInDB() { @@ -158,4 +162,25 @@ public void checkRoomList() { }); } + @Test + @DisplayName("Room Info 가져오기") + public void getRoomInfoTest() { + Long roomId = 1L; + Room room = Mockito.mock(Room.class); + when(roomSaver.save(any(Room.class))).thenReturn(room); + when(room.getId()).thenReturn(roomId); + when(room.getName()).thenReturn("roomName"); + when(room.getCode()).thenReturn("roomCode"); + + when(roomRetriever.findById(roomId)).thenReturn(room); + + RoomInfoDto roomInfoDto = roomService.getRoomInfo(room.getId()); + + Assertions.assertEquals(room.getName(), "roomName"); + Assertions.assertEquals(room.getCode(), "roomCode"); + + System.out.println("Room name : " + roomInfoDto.name()); + System.out.println("Room code : " + roomInfoDto.code()); + } + } From 17c18236cc6f865d3fb3f76b30fe73368dd24dac Mon Sep 17 00:00:00 2001 From: jenny <88226911+kmjenny@users.noreply.github.com> Date: Sat, 25 May 2024 19:06:42 +0900 Subject: [PATCH 4/8] =?UTF-8?q?[feat]=20#21=20Room=20=EC=9E=85=EC=9E=A5=20?= =?UTF-8?q?API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/controller/RoomController.java | 12 ++++++++ .../server/dto/room/request/RoomEnterDto.java | 9 ++++++ .../server/repository/RoomRepository.java | 3 ++ .../server/service/room/RoomRetriever.java | 5 ++++ .../server/service/room/RoomService.java | 10 +++++++ .../server/roomTest/RoomServiceTest.java | 28 +++++++++++++++++++ 6 files changed, 67 insertions(+) create mode 100644 src/main/java/com/telepigeon/server/dto/room/request/RoomEnterDto.java diff --git a/src/main/java/com/telepigeon/server/controller/RoomController.java b/src/main/java/com/telepigeon/server/controller/RoomController.java index cd3bc6a..1dcc6e8 100644 --- a/src/main/java/com/telepigeon/server/controller/RoomController.java +++ b/src/main/java/com/telepigeon/server/controller/RoomController.java @@ -1,8 +1,10 @@ package com.telepigeon.server.controller; import com.telepigeon.server.annotation.UserId; +import com.telepigeon.server.domain.Profile; import com.telepigeon.server.domain.Room; import com.telepigeon.server.dto.room.request.RoomCreateDto; +import com.telepigeon.server.dto.room.request.RoomEnterDto; import com.telepigeon.server.dto.room.response.RoomInfoDto; import com.telepigeon.server.dto.room.response.RoomListDto; import com.telepigeon.server.service.room.RoomService; @@ -40,4 +42,14 @@ public ResponseEntity> getAllRooms(@UserId Long userId) { public ResponseEntity getRoomInfo(@PathVariable Long roomId) { return ResponseEntity.ok(roomService.getRoomInfo(roomId)); } + + @PatchMapping("/rooms/entrance") + public ResponseEntity enterRoom( + @UserId Long userId, + @Valid @RequestBody RoomEnterDto roomEnterDto + ) { + Profile createProfile = roomService.enterRoom(roomEnterDto, userId); + URI location = URI.create("/profiles/" + createProfile.getId()); + return ResponseEntity.created(location).build(); + } } diff --git a/src/main/java/com/telepigeon/server/dto/room/request/RoomEnterDto.java b/src/main/java/com/telepigeon/server/dto/room/request/RoomEnterDto.java new file mode 100644 index 0000000..80f1954 --- /dev/null +++ b/src/main/java/com/telepigeon/server/dto/room/request/RoomEnterDto.java @@ -0,0 +1,9 @@ +package com.telepigeon.server.dto.room.request; + +import jakarta.validation.constraints.NotNull; + +public record RoomEnterDto( + @NotNull + String code +) { +} diff --git a/src/main/java/com/telepigeon/server/repository/RoomRepository.java b/src/main/java/com/telepigeon/server/repository/RoomRepository.java index a34e909..58174b7 100644 --- a/src/main/java/com/telepigeon/server/repository/RoomRepository.java +++ b/src/main/java/com/telepigeon/server/repository/RoomRepository.java @@ -4,6 +4,7 @@ import com.telepigeon.server.domain.Room; import org.springframework.data.jpa.repository.JpaRepository; +import javax.swing.text.html.Option; import java.util.List; import java.util.Optional; @@ -11,4 +12,6 @@ public interface RoomRepository extends JpaRepository { boolean existsByName(String name); boolean existsByCode(String code); + Optional findByCode(String code); + } diff --git a/src/main/java/com/telepigeon/server/service/room/RoomRetriever.java b/src/main/java/com/telepigeon/server/service/room/RoomRetriever.java index 6cb0157..2d56e73 100644 --- a/src/main/java/com/telepigeon/server/service/room/RoomRetriever.java +++ b/src/main/java/com/telepigeon/server/service/room/RoomRetriever.java @@ -24,4 +24,9 @@ public Room findById(final long id) { return roomRepository.findById(id) .orElseThrow(() -> new NotFoundException(NotFoundErrorCode.ROOM_NOT_FOUND)); } + + public Room findByCode(final String code) { + return roomRepository.findByCode(code) + .orElseThrow(() -> new NotFoundException(NotFoundErrorCode.ROOM_NOT_FOUND)); + } } diff --git a/src/main/java/com/telepigeon/server/service/room/RoomService.java b/src/main/java/com/telepigeon/server/service/room/RoomService.java index 131d6c3..5fcac72 100644 --- a/src/main/java/com/telepigeon/server/service/room/RoomService.java +++ b/src/main/java/com/telepigeon/server/service/room/RoomService.java @@ -5,6 +5,7 @@ import com.telepigeon.server.domain.Room; import com.telepigeon.server.domain.Users; import com.telepigeon.server.dto.room.request.RoomCreateDto; +import com.telepigeon.server.dto.room.request.RoomEnterDto; import com.telepigeon.server.dto.room.response.RoomInfoDto; import com.telepigeon.server.dto.room.response.RoomListDto; import com.telepigeon.server.repository.RoomRepository; @@ -86,4 +87,13 @@ public RoomInfoDto getRoomInfo(final Long roomId) { return RoomInfoDto.of(room); } + + @Transactional + public Profile enterRoom(final RoomEnterDto roomEnterDto, Long userId) { + Users user = userRetriever.findById(userId); + Room room = roomRetriever.findByCode(roomEnterDto.code()); + + Profile profile = Profile.create(user, room); + return profileSaver.save(profile); + } } diff --git a/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java b/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java index b63307c..439bc63 100644 --- a/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java +++ b/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java @@ -3,6 +3,7 @@ import com.telepigeon.server.domain.*; import com.telepigeon.server.dto.answer.request.AnswerCreateDto; import com.telepigeon.server.dto.room.request.RoomCreateDto; +import com.telepigeon.server.dto.room.request.RoomEnterDto; import com.telepigeon.server.dto.room.response.RoomInfoDto; import com.telepigeon.server.dto.room.response.RoomListDto; import com.telepigeon.server.dto.type.Relation; @@ -183,4 +184,31 @@ public void getRoomInfoTest() { System.out.println("Room code : " + roomInfoDto.code()); } + @Test + @DisplayName("Room 입장하기") + public void enterRoomTest() { + Long roomId = 1L; + String code = "123456abc"; + Room room = Mockito.mock(Room.class); + when(roomSaver.save(any(Room.class))).thenReturn(room); + when(room.getId()).thenReturn(roomId); + when(room.getCode()).thenReturn(code); + + Long userId = 1L; + Users user = Mockito.mock(Users.class); + when(userRetriever.findById(userId)).thenReturn(user); + + RoomEnterDto roomEnterDto = new RoomEnterDto(code); + when(roomRetriever.findByCode(code)).thenReturn(room); + + Profile savedProfile = Profile.create(user, room); + Mockito.when(profileSaver.save(any(Profile.class))).thenReturn(savedProfile); + + Profile profile = roomService.enterRoom(roomEnterDto, userId); + + Assertions.assertEquals(profile.getUser(), user); + Assertions.assertEquals(profile.getRoom().getId(), roomId); + Assertions.assertEquals(profile.getRoom().getCode(), code); + } + } From 4faef98b0cd8624d19b1f7fdeae8abb81ae2d3f5 Mon Sep 17 00:00:00 2001 From: jenny <88226911+kmjenny@users.noreply.github.com> Date: Sat, 25 May 2024 22:48:18 +0900 Subject: [PATCH 5/8] [feat] #21 Room Remove API --- .../server/controller/RoomController.java | 10 ++- .../exception/code/NotFoundErrorCode.java | 3 +- .../server/repository/AnswerRepository.java | 2 + .../server/repository/QuestionRepository.java | 3 + .../server/repository/WorryRepository.java | 11 +++ .../server/service/answer/AnswerRemover.java | 16 ++++ .../service/answer/AnswerRetriever.java | 6 ++ .../service/profile/ProfileRemover.java | 16 ++++ .../service/profile/ProfileRetriever.java | 4 +- .../service/question/QuestionRemover.java | 17 ++++ .../service/question/QuestionRetriever.java | 6 ++ .../server/service/room/RoomService.java | 39 +++++++-- .../server/service/worry/WorryRemover.java | 17 ++++ .../server/service/worry/WorryRetriever.java | 22 +++++ .../server/service/worry/WorrySaver.java | 17 ++++ .../server/roomTest/RoomServiceTest.java | 80 ++++++++++++++++++- 16 files changed, 258 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/telepigeon/server/repository/WorryRepository.java create mode 100644 src/main/java/com/telepigeon/server/service/answer/AnswerRemover.java create mode 100644 src/main/java/com/telepigeon/server/service/profile/ProfileRemover.java create mode 100644 src/main/java/com/telepigeon/server/service/question/QuestionRemover.java create mode 100644 src/main/java/com/telepigeon/server/service/worry/WorryRemover.java create mode 100644 src/main/java/com/telepigeon/server/service/worry/WorryRetriever.java create mode 100644 src/main/java/com/telepigeon/server/service/worry/WorrySaver.java diff --git a/src/main/java/com/telepigeon/server/controller/RoomController.java b/src/main/java/com/telepigeon/server/controller/RoomController.java index 1dcc6e8..947565d 100644 --- a/src/main/java/com/telepigeon/server/controller/RoomController.java +++ b/src/main/java/com/telepigeon/server/controller/RoomController.java @@ -43,7 +43,7 @@ public ResponseEntity getRoomInfo(@PathVariable Long roomId) { return ResponseEntity.ok(roomService.getRoomInfo(roomId)); } - @PatchMapping("/rooms/entrance") + @PostMapping("/rooms/entrance") public ResponseEntity enterRoom( @UserId Long userId, @Valid @RequestBody RoomEnterDto roomEnterDto @@ -52,4 +52,12 @@ public ResponseEntity enterRoom( URI location = URI.create("/profiles/" + createProfile.getId()); return ResponseEntity.created(location).build(); } + + @DeleteMapping("/rooms/{roomId}") + public ResponseEntity deleteRoom( + @UserId Long userId, + @PathVariable Long roomId + ) { + return ResponseEntity.ok(roomService.deleteRoom(roomId, userId)); + } } diff --git a/src/main/java/com/telepigeon/server/exception/code/NotFoundErrorCode.java b/src/main/java/com/telepigeon/server/exception/code/NotFoundErrorCode.java index e0b8bda..da93e3c 100644 --- a/src/main/java/com/telepigeon/server/exception/code/NotFoundErrorCode.java +++ b/src/main/java/com/telepigeon/server/exception/code/NotFoundErrorCode.java @@ -10,10 +10,11 @@ public enum NotFoundErrorCode implements DefaultErrorCode{ NOT_FOUND_END_POINT(HttpStatus.NOT_FOUND, "error", "존재하지 않는 API입니다."), NOT_FOUND_HURRY(HttpStatus.NOT_FOUND, "error", "존재하지 않는 재촉하기 입니다."), - NOT_FOUND_FILE(HttpStatus.NOT_FOUND, "error", "존재하지 않는 파일 입니다."), + NOT_FOUND_FILE(HttpStatus.NOT_FOUND, "error", "존재하지 않는 파일입니다."), ROOM_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 방입니다."), USER_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 사용자입니다."), PROFILE_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 프로필입니다."), + WORRY_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 걱정입니다"), ; @JsonIgnore diff --git a/src/main/java/com/telepigeon/server/repository/AnswerRepository.java b/src/main/java/com/telepigeon/server/repository/AnswerRepository.java index 64dfa38..174f0ac 100644 --- a/src/main/java/com/telepigeon/server/repository/AnswerRepository.java +++ b/src/main/java/com/telepigeon/server/repository/AnswerRepository.java @@ -17,5 +17,7 @@ List findAllByProfileInAndCreatedAtBetweenOrderByCreatedAt( ); Optional findFirstByProfileOrderByCreatedAtDesc(Profile profile); + List findByProfile(Profile profile); + Boolean existsByQuestion(Question question); } diff --git a/src/main/java/com/telepigeon/server/repository/QuestionRepository.java b/src/main/java/com/telepigeon/server/repository/QuestionRepository.java index 18812a4..c5314cc 100644 --- a/src/main/java/com/telepigeon/server/repository/QuestionRepository.java +++ b/src/main/java/com/telepigeon/server/repository/QuestionRepository.java @@ -4,8 +4,11 @@ import com.telepigeon.server.domain.Question; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; import java.util.Optional; public interface QuestionRepository extends JpaRepository { Optional findFirstByProfileOrderByCreatedAtDesc(Profile profile); + + List findByProfile(Profile profile); } diff --git a/src/main/java/com/telepigeon/server/repository/WorryRepository.java b/src/main/java/com/telepigeon/server/repository/WorryRepository.java new file mode 100644 index 0000000..71289d1 --- /dev/null +++ b/src/main/java/com/telepigeon/server/repository/WorryRepository.java @@ -0,0 +1,11 @@ +package com.telepigeon.server.repository; + +import com.telepigeon.server.domain.Profile; +import com.telepigeon.server.domain.Worry; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface WorryRepository extends JpaRepository { + List findByProfile(Profile profile); +} diff --git a/src/main/java/com/telepigeon/server/service/answer/AnswerRemover.java b/src/main/java/com/telepigeon/server/service/answer/AnswerRemover.java new file mode 100644 index 0000000..4879133 --- /dev/null +++ b/src/main/java/com/telepigeon/server/service/answer/AnswerRemover.java @@ -0,0 +1,16 @@ +package com.telepigeon.server.service.answer; + +import com.telepigeon.server.domain.Answer; +import com.telepigeon.server.repository.AnswerRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class AnswerRemover { + private final AnswerRepository answerRepository; + + public void remove(final Answer answer) { + answerRepository.delete(answer); + } +} diff --git a/src/main/java/com/telepigeon/server/service/answer/AnswerRetriever.java b/src/main/java/com/telepigeon/server/service/answer/AnswerRetriever.java index 5fc9979..68f128f 100644 --- a/src/main/java/com/telepigeon/server/service/answer/AnswerRetriever.java +++ b/src/main/java/com/telepigeon/server/service/answer/AnswerRetriever.java @@ -3,6 +3,8 @@ import com.telepigeon.server.domain.Answer; import com.telepigeon.server.domain.Profile; import com.telepigeon.server.domain.Question; +import com.telepigeon.server.exception.NotFoundException; +import com.telepigeon.server.exception.code.NotFoundErrorCode; import com.telepigeon.server.repository.AnswerRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; @@ -35,6 +37,10 @@ public Answer findFirstByProfile(final Profile profile){ ); } + public List findAllByProfile(final Profile profile) { + return answerRepository.findByProfile(profile); + } + public boolean existsByQuestion(final Question question){ return answerRepository.existsByQuestion(question); } diff --git a/src/main/java/com/telepigeon/server/service/profile/ProfileRemover.java b/src/main/java/com/telepigeon/server/service/profile/ProfileRemover.java new file mode 100644 index 0000000..704a6aa --- /dev/null +++ b/src/main/java/com/telepigeon/server/service/profile/ProfileRemover.java @@ -0,0 +1,16 @@ +package com.telepigeon.server.service.profile; + +import com.telepigeon.server.domain.Profile; +import com.telepigeon.server.repository.ProfileRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class ProfileRemover { + private final ProfileRepository profileRepository; + + public void remove(final Profile profile) { + profileRepository.delete(profile); + } +} diff --git a/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java b/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java index ce32d59..a695f1e 100644 --- a/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java +++ b/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java @@ -17,12 +17,12 @@ public class ProfileRetriever { private final ProfileRepository profileRepository; - public Profile findByUserAndRoom(Users user, Room room) { + public Profile findByUserAndRoom(final Users user, final Room room) { return profileRepository.findByUserAndRoom(user, room) .orElseThrow(() -> new NotFoundException(NotFoundErrorCode.PROFILE_NOT_FOUND)); } - public Profile findByUserNotAndRoom(Users user, Room room) { + public Profile findByUserNotAndRoom(final Users user, final Room room) { return profileRepository.findByUserNotAndRoom(user, room) .orElseThrow(() -> new NotFoundException(NotFoundErrorCode.PROFILE_NOT_FOUND)); } diff --git a/src/main/java/com/telepigeon/server/service/question/QuestionRemover.java b/src/main/java/com/telepigeon/server/service/question/QuestionRemover.java new file mode 100644 index 0000000..8845675 --- /dev/null +++ b/src/main/java/com/telepigeon/server/service/question/QuestionRemover.java @@ -0,0 +1,17 @@ +package com.telepigeon.server.service.question; + +import com.telepigeon.server.domain.Question; +import com.telepigeon.server.repository.QuestionRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class QuestionRemover { + + private final QuestionRepository questionRepository; + + public void remove(final Question question) { + questionRepository.delete(question); + } +} diff --git a/src/main/java/com/telepigeon/server/service/question/QuestionRetriever.java b/src/main/java/com/telepigeon/server/service/question/QuestionRetriever.java index 9b94a4b..7ce51f8 100644 --- a/src/main/java/com/telepigeon/server/service/question/QuestionRetriever.java +++ b/src/main/java/com/telepigeon/server/service/question/QuestionRetriever.java @@ -6,6 +6,8 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; +import java.util.List; + @Component @RequiredArgsConstructor @@ -18,4 +20,8 @@ public Question findFirstByProfile(final Profile profile){ RuntimeException::new // Hurry merge 후 NotFoundException으로 바꿀 예정(충돌 때문) ); } + + public List findAllByProfile(final Profile profile) { + return questionRepository.findByProfile(profile); + } } diff --git a/src/main/java/com/telepigeon/server/service/room/RoomService.java b/src/main/java/com/telepigeon/server/service/room/RoomService.java index 5fcac72..11532d9 100644 --- a/src/main/java/com/telepigeon/server/service/room/RoomService.java +++ b/src/main/java/com/telepigeon/server/service/room/RoomService.java @@ -1,18 +1,23 @@ package com.telepigeon.server.service.room; -import com.telepigeon.server.domain.Answer; -import com.telepigeon.server.domain.Profile; -import com.telepigeon.server.domain.Room; -import com.telepigeon.server.domain.Users; +import com.telepigeon.server.domain.*; import com.telepigeon.server.dto.room.request.RoomCreateDto; import com.telepigeon.server.dto.room.request.RoomEnterDto; import com.telepigeon.server.dto.room.response.RoomInfoDto; import com.telepigeon.server.dto.room.response.RoomListDto; +import com.telepigeon.server.repository.AnswerRepository; +import com.telepigeon.server.repository.QuestionRepository; import com.telepigeon.server.repository.RoomRepository; +import com.telepigeon.server.service.answer.AnswerRemover; import com.telepigeon.server.service.answer.AnswerRetriever; +import com.telepigeon.server.service.profile.ProfileRemover; import com.telepigeon.server.service.profile.ProfileRetriever; import com.telepigeon.server.service.profile.ProfileSaver; +import com.telepigeon.server.service.question.QuestionRemover; +import com.telepigeon.server.service.question.QuestionRetriever; import com.telepigeon.server.service.user.UserRetriever; +import com.telepigeon.server.service.worry.WorryRemover; +import com.telepigeon.server.service.worry.WorryRetriever; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -32,6 +37,12 @@ public class RoomService { private final ProfileRetriever profileRetriever; private final AnswerRetriever answerRetriever; private final RoomRetriever roomRetriever; + private final QuestionRetriever questionRetriever; + private final WorryRetriever worryRetriever; + private final ProfileRemover profileRemover; + private final QuestionRemover questionRemover; + private final WorryRemover worryRemover; + private final AnswerRemover answerRemover; @Transactional public Room createRoom(final RoomCreateDto roomCreateDto, final Long userId){ @@ -89,11 +100,29 @@ public RoomInfoDto getRoomInfo(final Long roomId) { } @Transactional - public Profile enterRoom(final RoomEnterDto roomEnterDto, Long userId) { + public Profile enterRoom(final RoomEnterDto roomEnterDto, final Long userId) { Users user = userRetriever.findById(userId); Room room = roomRetriever.findByCode(roomEnterDto.code()); Profile profile = Profile.create(user, room); return profileSaver.save(profile); } + + @Transactional + public Room deleteRoom(final Long roomId, final Long userId) { + Room room = roomRetriever.findById(roomId); + Users user = userRetriever.findById(userId); + + Profile profile = profileRetriever.findByUserAndRoom(user, room); + List answerList = answerRetriever.findAllByProfile(profile); + List questionList = questionRetriever.findAllByProfile(profile); + List worryList = worryRetriever.findAllByProfile(profile); + + profileRemover.remove(profile); + answerList.forEach(answerRemover::remove); + questionList.forEach(questionRemover::remove); + worryList.forEach(worryRemover::remove); + + return room; + } } diff --git a/src/main/java/com/telepigeon/server/service/worry/WorryRemover.java b/src/main/java/com/telepigeon/server/service/worry/WorryRemover.java new file mode 100644 index 0000000..17d1134 --- /dev/null +++ b/src/main/java/com/telepigeon/server/service/worry/WorryRemover.java @@ -0,0 +1,17 @@ +package com.telepigeon.server.service.worry; + +import com.telepigeon.server.domain.Worry; +import com.telepigeon.server.repository.WorryRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class WorryRemover { + + private final WorryRepository worryRepository; + + public void remove(final Worry worry) { + worryRepository.delete(worry); + } +} diff --git a/src/main/java/com/telepigeon/server/service/worry/WorryRetriever.java b/src/main/java/com/telepigeon/server/service/worry/WorryRetriever.java new file mode 100644 index 0000000..a1bb006 --- /dev/null +++ b/src/main/java/com/telepigeon/server/service/worry/WorryRetriever.java @@ -0,0 +1,22 @@ +package com.telepigeon.server.service.worry; + +import com.telepigeon.server.domain.Profile; +import com.telepigeon.server.domain.Worry; +import com.telepigeon.server.exception.NotFoundException; +import com.telepigeon.server.exception.code.NotFoundErrorCode; +import com.telepigeon.server.repository.WorryRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +@RequiredArgsConstructor +public class WorryRetriever { + + private final WorryRepository worryRepository; + + public List findAllByProfile(final Profile profile) { + return worryRepository.findByProfile(profile); + } +} diff --git a/src/main/java/com/telepigeon/server/service/worry/WorrySaver.java b/src/main/java/com/telepigeon/server/service/worry/WorrySaver.java new file mode 100644 index 0000000..ad80ab8 --- /dev/null +++ b/src/main/java/com/telepigeon/server/service/worry/WorrySaver.java @@ -0,0 +1,17 @@ +package com.telepigeon.server.service.worry; + +import com.telepigeon.server.domain.Worry; +import com.telepigeon.server.repository.WorryRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class WorrySaver { + + private final WorryRepository worryRepository; + + public Worry create(final Worry worry){ + return worryRepository.save(worry); + } +} diff --git a/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java b/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java index 439bc63..5c49f84 100644 --- a/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java +++ b/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java @@ -7,15 +7,22 @@ import com.telepigeon.server.dto.room.response.RoomInfoDto; import com.telepigeon.server.dto.room.response.RoomListDto; import com.telepigeon.server.dto.type.Relation; -import com.telepigeon.server.repository.RoomRepository; -import com.telepigeon.server.repository.UserRepository; +import com.telepigeon.server.repository.*; +import com.telepigeon.server.service.answer.AnswerRemover; import com.telepigeon.server.service.answer.AnswerRetriever; +import com.telepigeon.server.service.answer.AnswerSaver; +import com.telepigeon.server.service.profile.ProfileRemover; import com.telepigeon.server.service.profile.ProfileRetriever; import com.telepigeon.server.service.profile.ProfileSaver; +import com.telepigeon.server.service.question.QuestionRemover; +import com.telepigeon.server.service.question.QuestionRetriever; +import com.telepigeon.server.service.question.QuestionSaver; import com.telepigeon.server.service.room.RoomRetriever; import com.telepigeon.server.service.room.RoomSaver; import com.telepigeon.server.service.room.RoomService; import com.telepigeon.server.service.user.UserRetriever; +import com.telepigeon.server.service.worry.WorryRemover; +import com.telepigeon.server.service.worry.WorryRetriever; import jakarta.transaction.Transactional; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -67,6 +74,36 @@ public class RoomServiceTest { @MockBean private RoomRetriever roomRetriever; + @MockBean + private WorryRemover worryRemover; + + @MockBean + private ProfileRemover profileRemover; + + @MockBean + private AnswerRemover answerRemover; + + @MockBean + private QuestionRemover questionRemover; + + @MockBean + private QuestionRetriever questionRetriever; + + @MockBean + private AnswerRepository answerRepository; + + @MockBean + private QuestionRepository questionRepository; + + @MockBean + private ProfileRepository profileRepository; + + @MockBean + private WorryRetriever worryRetriever; + + @MockBean + private WorryRepository worryRepository; + @Test @DisplayName("Room DB에 저장 확인") public void checkRoomInDB() { @@ -211,4 +248,43 @@ public void enterRoomTest() { Assertions.assertEquals(profile.getRoom().getCode(), code); } + @Test + @DisplayName("Room 삭제하기") + public void deleteRoom() { + Long roomId = 1L; + Long userId = 1L; + + Room room = Mockito.mock(Room.class); + Users user = Mockito.mock(Users.class); + Profile profile = Mockito.mock(Profile.class); + Answer answer = Mockito.mock(Answer.class); + Question question = Mockito.mock(Question.class); + Worry worry = Mockito.mock(Worry.class); + + when(roomRetriever.findById(roomId)).thenReturn(room); + when(userRetriever.findById(userId)).thenReturn(user); + when(profileRetriever.findByUserAndRoom(user, room)).thenReturn(profile); + when(answerRetriever.findAllByProfile(profile)).thenReturn(List.of(answer)); + when(questionRetriever.findAllByProfile(profile)).thenReturn(List.of(question)); + when(worryRetriever.findAllByProfile(profile)).thenReturn(List.of(worry)); + + when(answerRepository.findAll()).thenReturn(Collections.emptyList()); + when(questionRepository.findAll()).thenReturn(Collections.emptyList()); + when(profileRepository.findAll()).thenReturn(Collections.emptyList()); + when(worryRepository.findAll()).thenReturn(Collections.emptyList()); + + Room deletedRoom = roomService.deleteRoom(roomId, userId); + + verify(profileRemover).remove(profile); + verify(answerRemover).remove(answer); + verify(questionRemover).remove(question); + verify(worryRemover).remove(worry); + + Assertions.assertTrue(answerRepository.findAll().isEmpty(), "Answer repository should be empty"); + Assertions.assertTrue(questionRepository.findAll().isEmpty(), "Question repository should be empty"); + Assertions.assertTrue(worryRepository.findAll().isEmpty(), "Worry repository should be empty"); + Assertions.assertTrue(profileRepository.findAll().isEmpty(), "Profile repository should be empty"); + } + + } From 76da5a0843defebb499dabf89726128184d5f1fb Mon Sep 17 00:00:00 2001 From: jenny <88226911+kmjenny@users.noreply.github.com> Date: Tue, 28 May 2024 14:35:10 +0900 Subject: [PATCH 6/8] =?UTF-8?q?[fix]=20#21=20Review=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/controller/RoomController.java | 6 +- .../dto/room/request/RoomCreateDto.java | 2 - .../server/dto/room/response/RoomListDto.java | 54 +++++---- .../server/repository/AnswerRepository.java | 2 +- .../server/repository/ProfileRepository.java | 6 +- .../server/repository/QuestionRepository.java | 2 +- .../server/repository/RoomRepository.java | 6 +- .../server/repository/WorryRepository.java | 2 +- .../service/answer/AnswerRetriever.java | 4 +- .../service/profile/ProfileRetriever.java | 14 +-- .../service/question/QuestionRetriever.java | 2 +- .../server/service/room/RoomRetriever.java | 5 +- .../server/service/room/RoomSaver.java | 1 - .../server/service/room/RoomService.java | 51 +++++++-- .../server/service/worry/WorryRetriever.java | 4 +- .../server/roomTest/RoomServiceTest.java | 103 +++++++++--------- 16 files changed, 137 insertions(+), 127 deletions(-) diff --git a/src/main/java/com/telepigeon/server/controller/RoomController.java b/src/main/java/com/telepigeon/server/controller/RoomController.java index 947565d..4b4904f 100644 --- a/src/main/java/com/telepigeon/server/controller/RoomController.java +++ b/src/main/java/com/telepigeon/server/controller/RoomController.java @@ -15,6 +15,7 @@ import org.w3c.dom.stylesheets.LinkStyle; import java.net.URI; +import java.util.Collections; import java.util.List; @RestController @@ -35,7 +36,7 @@ public ResponseEntity createRoom( @GetMapping("/rooms") public ResponseEntity> getAllRooms(@UserId Long userId) { - return ResponseEntity.ok(roomService.getAllRooms(userId)); + return ResponseEntity.ok(Collections.singletonList(roomService.getAllRooms(userId))); } @GetMapping("/rooms/{roomId}/info") @@ -49,8 +50,7 @@ public ResponseEntity enterRoom( @Valid @RequestBody RoomEnterDto roomEnterDto ) { Profile createProfile = roomService.enterRoom(roomEnterDto, userId); - URI location = URI.create("/profiles/" + createProfile.getId()); - return ResponseEntity.created(location).build(); + return ResponseEntity.created(URI.create("/profiles/" + createProfile.getId())).build(); } @DeleteMapping("/rooms/{roomId}") diff --git a/src/main/java/com/telepigeon/server/dto/room/request/RoomCreateDto.java b/src/main/java/com/telepigeon/server/dto/room/request/RoomCreateDto.java index a0d4fcd..ae00eee 100644 --- a/src/main/java/com/telepigeon/server/dto/room/request/RoomCreateDto.java +++ b/src/main/java/com/telepigeon/server/dto/room/request/RoomCreateDto.java @@ -1,8 +1,6 @@ package com.telepigeon.server.dto.room.request; import com.telepigeon.server.domain.Room; -import jakarta.annotation.Nullable; -import jakarta.validation.Valid; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; diff --git a/src/main/java/com/telepigeon/server/dto/room/response/RoomListDto.java b/src/main/java/com/telepigeon/server/dto/room/response/RoomListDto.java index 8c29bdd..44fbb49 100644 --- a/src/main/java/com/telepigeon/server/dto/room/response/RoomListDto.java +++ b/src/main/java/com/telepigeon/server/dto/room/response/RoomListDto.java @@ -4,39 +4,35 @@ import com.telepigeon.server.domain.Profile; import com.telepigeon.server.domain.Room; +import java.util.List; + public record RoomListDto( - Long roomId, - String name, - String opponentNickname, - String myRelation, - String opponentRelation, - Integer emotion, - Integer sentence + List rooms ) { - public static RoomListDto of(Room room, Profile myProfile, Profile opponentProfile, Answer myAnswer, Answer opponentAnswer) { - boolean myState = myAnswer.getContent() != null; - boolean opponentState = opponentAnswer.getContent() != null; - // 감정 측정 시 업데이트 - int emotion = 0; + public static RoomListDto of(List rooms) { + return new RoomListDto(rooms); + } - int sentence; - if (myState && opponentState) { - sentence = 0; - } else if (myState) { - sentence = 1; - } else { - sentence = 2; + public record RoomDto( + Long roomId, + String name, + String opponentNickname, + String myRelation, + String opponentRelation, + Integer emotion, + Integer sentence + ) { + public static RoomDto of( + Long roomId, + String name, + String opponentNickname, + String myRelation, + String opponentRelation, + Integer emotion, + Integer sentence + ) { + return new RoomDto(roomId, name, opponentNickname, myRelation,opponentRelation, emotion, sentence); } - - return new RoomListDto( - room.getId(), - room.getName(), - opponentProfile.getUser().getName(), - myProfile.getRelation().getContent(), - opponentProfile.getRelation().getContent(), - emotion, - sentence - ); } } diff --git a/src/main/java/com/telepigeon/server/repository/AnswerRepository.java b/src/main/java/com/telepigeon/server/repository/AnswerRepository.java index 174f0ac..36bd960 100644 --- a/src/main/java/com/telepigeon/server/repository/AnswerRepository.java +++ b/src/main/java/com/telepigeon/server/repository/AnswerRepository.java @@ -17,7 +17,7 @@ List findAllByProfileInAndCreatedAtBetweenOrderByCreatedAt( ); Optional findFirstByProfileOrderByCreatedAtDesc(Profile profile); - List findByProfile(Profile profile); + List findAllByProfile(Profile profile); Boolean existsByQuestion(Question question); } diff --git a/src/main/java/com/telepigeon/server/repository/ProfileRepository.java b/src/main/java/com/telepigeon/server/repository/ProfileRepository.java index 57be615..eb42c36 100644 --- a/src/main/java/com/telepigeon/server/repository/ProfileRepository.java +++ b/src/main/java/com/telepigeon/server/repository/ProfileRepository.java @@ -9,7 +9,7 @@ import java.util.Optional; public interface ProfileRepository extends JpaRepository { - Optional findByUserAndRoom(Users user, Room room); - Optional findByUserNotAndRoom(Users user, Room room); - List findByUserId(Long userId); + Optional findAllByUserAndRoom(Users user, Room room); + Optional findAllByUserNotAndRoom(Users user, Room room); + List findAllByUserId(Long userId); } diff --git a/src/main/java/com/telepigeon/server/repository/QuestionRepository.java b/src/main/java/com/telepigeon/server/repository/QuestionRepository.java index c5314cc..2fba26c 100644 --- a/src/main/java/com/telepigeon/server/repository/QuestionRepository.java +++ b/src/main/java/com/telepigeon/server/repository/QuestionRepository.java @@ -10,5 +10,5 @@ public interface QuestionRepository extends JpaRepository { Optional findFirstByProfileOrderByCreatedAtDesc(Profile profile); - List findByProfile(Profile profile); + List findAllByProfile(Profile profile); } diff --git a/src/main/java/com/telepigeon/server/repository/RoomRepository.java b/src/main/java/com/telepigeon/server/repository/RoomRepository.java index 58174b7..7d599d5 100644 --- a/src/main/java/com/telepigeon/server/repository/RoomRepository.java +++ b/src/main/java/com/telepigeon/server/repository/RoomRepository.java @@ -1,17 +1,13 @@ package com.telepigeon.server.repository; -import com.telepigeon.server.domain.Profile; import com.telepigeon.server.domain.Room; import org.springframework.data.jpa.repository.JpaRepository; - -import javax.swing.text.html.Option; -import java.util.List; import java.util.Optional; public interface RoomRepository extends JpaRepository { boolean existsByName(String name); boolean existsByCode(String code); - Optional findByCode(String code); + Optional findAllByCode(String code); } diff --git a/src/main/java/com/telepigeon/server/repository/WorryRepository.java b/src/main/java/com/telepigeon/server/repository/WorryRepository.java index 71289d1..7826ed1 100644 --- a/src/main/java/com/telepigeon/server/repository/WorryRepository.java +++ b/src/main/java/com/telepigeon/server/repository/WorryRepository.java @@ -7,5 +7,5 @@ import java.util.List; public interface WorryRepository extends JpaRepository { - List findByProfile(Profile profile); + List findAllByProfile(Profile profile); } diff --git a/src/main/java/com/telepigeon/server/service/answer/AnswerRetriever.java b/src/main/java/com/telepigeon/server/service/answer/AnswerRetriever.java index 68f128f..a3b9987 100644 --- a/src/main/java/com/telepigeon/server/service/answer/AnswerRetriever.java +++ b/src/main/java/com/telepigeon/server/service/answer/AnswerRetriever.java @@ -3,8 +3,6 @@ import com.telepigeon.server.domain.Answer; import com.telepigeon.server.domain.Profile; import com.telepigeon.server.domain.Question; -import com.telepigeon.server.exception.NotFoundException; -import com.telepigeon.server.exception.code.NotFoundErrorCode; import com.telepigeon.server.repository.AnswerRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; @@ -38,7 +36,7 @@ public Answer findFirstByProfile(final Profile profile){ } public List findAllByProfile(final Profile profile) { - return answerRepository.findByProfile(profile); + return answerRepository.findAllByProfile(profile); } public boolean existsByQuestion(final Question question){ diff --git a/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java b/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java index a695f1e..4a018d9 100644 --- a/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java +++ b/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java @@ -18,21 +18,21 @@ public class ProfileRetriever { private final ProfileRepository profileRepository; public Profile findByUserAndRoom(final Users user, final Room room) { - return profileRepository.findByUserAndRoom(user, room) + return profileRepository.findAllByUserAndRoom(user, room) .orElseThrow(() -> new NotFoundException(NotFoundErrorCode.PROFILE_NOT_FOUND)); } public Profile findByUserNotAndRoom(final Users user, final Room room) { - return profileRepository.findByUserNotAndRoom(user, room) + return profileRepository.findAllByUserNotAndRoom(user, room) .orElseThrow(() -> new NotFoundException(NotFoundErrorCode.PROFILE_NOT_FOUND)); } public List findByUserId(final long userId) { - List profileList = profileRepository.findByUserId(userId); - if (profileList.isEmpty()) { - throw new NotFoundException(NotFoundErrorCode.PROFILE_NOT_FOUND); - } - return profileList; + return profileRepository.findAllByUserId(userId); + } + + public List findAll() { + return profileRepository.findAll(); } } diff --git a/src/main/java/com/telepigeon/server/service/question/QuestionRetriever.java b/src/main/java/com/telepigeon/server/service/question/QuestionRetriever.java index 7ce51f8..ea1b404 100644 --- a/src/main/java/com/telepigeon/server/service/question/QuestionRetriever.java +++ b/src/main/java/com/telepigeon/server/service/question/QuestionRetriever.java @@ -22,6 +22,6 @@ public Question findFirstByProfile(final Profile profile){ } public List findAllByProfile(final Profile profile) { - return questionRepository.findByProfile(profile); + return questionRepository.findAllByProfile(profile); } } diff --git a/src/main/java/com/telepigeon/server/service/room/RoomRetriever.java b/src/main/java/com/telepigeon/server/service/room/RoomRetriever.java index 2d56e73..bc09c4c 100644 --- a/src/main/java/com/telepigeon/server/service/room/RoomRetriever.java +++ b/src/main/java/com/telepigeon/server/service/room/RoomRetriever.java @@ -5,11 +5,8 @@ import com.telepigeon.server.exception.code.NotFoundErrorCode; import com.telepigeon.server.repository.RoomRepository; import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.util.List; - @Component @RequiredArgsConstructor public class RoomRetriever { @@ -26,7 +23,7 @@ public Room findById(final long id) { } public Room findByCode(final String code) { - return roomRepository.findByCode(code) + return roomRepository.findAllByCode(code) .orElseThrow(() -> new NotFoundException(NotFoundErrorCode.ROOM_NOT_FOUND)); } } diff --git a/src/main/java/com/telepigeon/server/service/room/RoomSaver.java b/src/main/java/com/telepigeon/server/service/room/RoomSaver.java index a0f1cab..7492c3b 100644 --- a/src/main/java/com/telepigeon/server/service/room/RoomSaver.java +++ b/src/main/java/com/telepigeon/server/service/room/RoomSaver.java @@ -3,7 +3,6 @@ import com.telepigeon.server.domain.Room; import com.telepigeon.server.repository.RoomRepository; import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component diff --git a/src/main/java/com/telepigeon/server/service/room/RoomService.java b/src/main/java/com/telepigeon/server/service/room/RoomService.java index 11532d9..eec1d9d 100644 --- a/src/main/java/com/telepigeon/server/service/room/RoomService.java +++ b/src/main/java/com/telepigeon/server/service/room/RoomService.java @@ -5,8 +5,6 @@ import com.telepigeon.server.dto.room.request.RoomEnterDto; import com.telepigeon.server.dto.room.response.RoomInfoDto; import com.telepigeon.server.dto.room.response.RoomListDto; -import com.telepigeon.server.repository.AnswerRepository; -import com.telepigeon.server.repository.QuestionRepository; import com.telepigeon.server.repository.RoomRepository; import com.telepigeon.server.service.answer.AnswerRemover; import com.telepigeon.server.service.answer.AnswerRetriever; @@ -18,7 +16,7 @@ import com.telepigeon.server.service.user.UserRetriever; import com.telepigeon.server.service.worry.WorryRemover; import com.telepigeon.server.service.worry.WorryRetriever; -import jakarta.transaction.Transactional; +import org.springframework.transaction.annotation.*; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -77,22 +75,53 @@ private String createCode() { return code; } - public List getAllRooms(final Long userId) { + @Transactional(readOnly = true) + public RoomListDto getAllRooms(final Long userId) { Users user = userRetriever.findById(userId); List profileList = profileRetriever.findByUserId(userId); List roomList = profileList.stream().map(Profile::getRoom).toList(); - return roomList.stream().map(room -> { - Profile myProfile = profileRetriever.findByUserAndRoom(user, room); - Profile opponentProfile = profileRetriever.findByUserNotAndRoom(user, room); - Answer myAnswer = answerRetriever.findFirstByProfile(myProfile); - Answer opponentAnswer = answerRetriever.findFirstByProfile(opponentProfile); + List roomDtos = roomList.stream() + .map(room -> createRoomDto(user, room)) + .toList(); - return RoomListDto.of(room, myProfile, opponentProfile, myAnswer, opponentAnswer); - }).collect(Collectors.toList()); + return RoomListDto.of(roomDtos); + } + @Transactional(readOnly = true) + public RoomListDto.RoomDto createRoomDto(Users user, Room room) { + Profile myProfile = profileRetriever.findByUserAndRoom(user, room); + Profile opponentProfile = profileRetriever.findByUserNotAndRoom(user, room); + Answer myAnswer = answerRetriever.findFirstByProfile(myProfile); + Answer opponentAnswer = answerRetriever.findFirstByProfile(opponentProfile); + + boolean myState = myAnswer.getContent() != null; + boolean opponentState = opponentAnswer.getContent() != null; + + // 감정 측정 시 업데이트 + int emotion = 0; + + int sentence; + if (myState && opponentState) { + sentence = 0; + } else if (myState) { + sentence = 1; + } else { + sentence = 2; + } + + return RoomListDto.RoomDto.of( + room.getId(), + room.getName(), + opponentProfile.getUser().getName(), + myProfile.getRelation().getContent(), + opponentProfile.getRelation().getContent(), + emotion, + sentence + ); } + @Transactional(readOnly = true) public RoomInfoDto getRoomInfo(final Long roomId) { Room room = roomRetriever.findById(roomId); diff --git a/src/main/java/com/telepigeon/server/service/worry/WorryRetriever.java b/src/main/java/com/telepigeon/server/service/worry/WorryRetriever.java index a1bb006..c17dab2 100644 --- a/src/main/java/com/telepigeon/server/service/worry/WorryRetriever.java +++ b/src/main/java/com/telepigeon/server/service/worry/WorryRetriever.java @@ -2,8 +2,6 @@ import com.telepigeon.server.domain.Profile; import com.telepigeon.server.domain.Worry; -import com.telepigeon.server.exception.NotFoundException; -import com.telepigeon.server.exception.code.NotFoundErrorCode; import com.telepigeon.server.repository.WorryRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; @@ -17,6 +15,6 @@ public class WorryRetriever { private final WorryRepository worryRepository; public List findAllByProfile(final Profile profile) { - return worryRepository.findByProfile(profile); + return worryRepository.findAllByProfile(profile); } } diff --git a/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java b/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java index 5c49f84..13b8261 100644 --- a/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java +++ b/src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java @@ -10,22 +10,18 @@ import com.telepigeon.server.repository.*; import com.telepigeon.server.service.answer.AnswerRemover; import com.telepigeon.server.service.answer.AnswerRetriever; -import com.telepigeon.server.service.answer.AnswerSaver; import com.telepigeon.server.service.profile.ProfileRemover; import com.telepigeon.server.service.profile.ProfileRetriever; import com.telepigeon.server.service.profile.ProfileSaver; import com.telepigeon.server.service.question.QuestionRemover; import com.telepigeon.server.service.question.QuestionRetriever; -import com.telepigeon.server.service.question.QuestionSaver; import com.telepigeon.server.service.room.RoomRetriever; import com.telepigeon.server.service.room.RoomSaver; import com.telepigeon.server.service.room.RoomService; import com.telepigeon.server.service.user.UserRetriever; import com.telepigeon.server.service.worry.WorryRemover; import com.telepigeon.server.service.worry.WorryRetriever; -import jakarta.transaction.Transactional; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; +import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -34,9 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -50,9 +44,6 @@ public class RoomServiceTest { @MockBean private RoomRepository roomRepository; - @MockBean - private UserRepository userRepository; - @Autowired private RoomService roomService; @@ -105,54 +96,51 @@ public class RoomServiceTest { private WorryRepository worryRepository; @Test - @DisplayName("Room DB에 저장 확인") + @DisplayName("DB에 Room이 저장되는지 확인") public void checkRoomInDB() { + // Given RoomCreateDto roomCreateDto = new RoomCreateDto("test"); Long userId = 1L; Room room = Room.create(roomCreateDto, "code"); Users user = Mockito.mock(Users.class); + Profile profile = Profile.create(user, room); when(userRetriever.findById(userId)).thenReturn(user); when(roomRepository.existsByCode(any(String.class))).thenReturn(false); when(roomSaver.save(any(Room.class))).thenReturn(room); + when(profileRetriever.findByUserAndRoom(any(Users.class), any(Room.class))).thenReturn(profile); + // When Room createdRoom = roomService.createRoom(roomCreateDto, userId); + Profile createdProfile = profileRetriever.findByUserAndRoom(user, createdRoom); - Profile savedProfile = Profile.create(user, createdRoom); - Mockito.when(profileSaver.save(any(Profile.class))).thenReturn(savedProfile); - + // Then // 방이 올바르게 생성되었는지 확인 - Assertions.assertEquals(room.getName(), createdRoom.getName()); - - // 프로필이 올바르게 생성되었는지 확인 - Assertions.assertEquals(user, savedProfile.getUser()); - Assertions.assertEquals(room, savedProfile.getRoom()); - - // 정보 출력해 확인 - System.out.println("Room name : " + createdRoom.getName()); - System.out.println("Room code : " + createdRoom.getCode()); - System.out.println("Room created time : " + createdRoom.getCreatedAt()); - System.out.println("Profile room : " + savedProfile.getRoom()); - System.out.println("Profile user : " + savedProfile.getUser()); + Assertions.assertThat(room.getName()).isEqualTo(createdRoom.getName()); + Assertions.assertThat(createdRoom.getCode()).isEqualTo(room.getCode()); + Assertions.assertThat(createdProfile.getUser()).isEqualTo(user); + Assertions.assertThat(createdProfile.getRoom()).isEqualTo(createdRoom); } @Test - @DisplayName("Room DB에서 꺼내오기 확인") + @DisplayName("DB에 저장된 Room을 꺼내와서 확인하기") public void checkRoomToDB() { RoomRetriever roomRetriever = new RoomRetriever(roomRepository); Room room = Room.create(new RoomCreateDto("name"), "code"); Mockito.doAnswer(invocation -> true).when(roomRepository).existsByName(room.getName()); RoomCreateDto roomCreateDto = RoomCreateDto.of(room); boolean isCheck = roomRetriever.existsByName(roomCreateDto.name()); - Assertions.assertTrue(isCheck); + Assertions.assertThat(isCheck).isTrue(); } @Test - @DisplayName("DB 저장 후 목록 확인") + @DisplayName("Room을 저장한 후에 List로 불러오기") public void checkRoomList() { + // Given Long userId1 = 1L; Long userId2 = 2L; + Long roomId = 1L; // User 생성 Users user1 = Users.create(userId1, "user1"); @@ -187,22 +175,27 @@ public void checkRoomList() { when(answerRetriever.findFirstByProfile(profile1)).thenReturn(ans1); when(answerRetriever.findFirstByProfile(profile2)).thenReturn(ans2); - List roomList = roomService.getAllRooms(userId1); - - roomList.forEach(dto -> { - System.out.println("Room Id : " + dto.roomId()); - System.out.println("Room Name : " + dto.name()); - System.out.println("opponentNickname : " + dto.opponentNickname()); - System.out.println("myRelation : " + dto.myRelation()); - System.out.println("opponentRelation : " + dto.opponentRelation()); - System.out.println("sentence : " + dto.sentence()); - System.out.println("emotion : " + dto.emotion()); - }); + // When + RoomListDto roomListDto = roomService.getAllRooms(userId1); + List roomList = roomListDto.rooms(); + + // Then + Assertions.assertThat(roomList).isNotEmpty(); + + for (RoomListDto.RoomDto room : roomList) { + Assertions.assertThat(room.name()).isNotBlank(); + Assertions.assertThat(room.opponentNickname()).isNotBlank(); + Assertions.assertThat(room.myRelation()).isNotBlank(); + Assertions.assertThat(room.opponentRelation()).isNotBlank(); + Assertions.assertThat(room.emotion()).isNotNull(); + Assertions.assertThat(room.sentence()).isNotNull(); + } } @Test @DisplayName("Room Info 가져오기") public void getRoomInfoTest() { + // Given Long roomId = 1L; Room room = Mockito.mock(Room.class); when(roomSaver.save(any(Room.class))).thenReturn(room); @@ -212,18 +205,18 @@ public void getRoomInfoTest() { when(roomRetriever.findById(roomId)).thenReturn(room); + // When RoomInfoDto roomInfoDto = roomService.getRoomInfo(room.getId()); - Assertions.assertEquals(room.getName(), "roomName"); - Assertions.assertEquals(room.getCode(), "roomCode"); - - System.out.println("Room name : " + roomInfoDto.name()); - System.out.println("Room code : " + roomInfoDto.code()); + // Then + Assertions.assertThat(room.getName()).isEqualTo(roomInfoDto.name()); + Assertions.assertThat(room.getCode()).isEqualTo(roomInfoDto.code()); } @Test @DisplayName("Room 입장하기") public void enterRoomTest() { + // Given Long roomId = 1L; String code = "123456abc"; Room room = Mockito.mock(Room.class); @@ -241,16 +234,19 @@ public void enterRoomTest() { Profile savedProfile = Profile.create(user, room); Mockito.when(profileSaver.save(any(Profile.class))).thenReturn(savedProfile); + // When Profile profile = roomService.enterRoom(roomEnterDto, userId); - Assertions.assertEquals(profile.getUser(), user); - Assertions.assertEquals(profile.getRoom().getId(), roomId); - Assertions.assertEquals(profile.getRoom().getCode(), code); + // Then + Assertions.assertThat(profile.getUser()).isEqualTo(user); + Assertions.assertThat(profile.getRoom().getId()).isEqualTo(roomId); + Assertions.assertThat(profile.getRoom().getCode()).isEqualTo(code); } @Test @DisplayName("Room 삭제하기") public void deleteRoom() { + // Given Long roomId = 1L; Long userId = 1L; @@ -273,17 +269,20 @@ public void deleteRoom() { when(profileRepository.findAll()).thenReturn(Collections.emptyList()); when(worryRepository.findAll()).thenReturn(Collections.emptyList()); + // When Room deletedRoom = roomService.deleteRoom(roomId, userId); + // Then + // Remover가 작동했는지 확인 verify(profileRemover).remove(profile); verify(answerRemover).remove(answer); verify(questionRemover).remove(question); verify(worryRemover).remove(worry); - Assertions.assertTrue(answerRepository.findAll().isEmpty(), "Answer repository should be empty"); - Assertions.assertTrue(questionRepository.findAll().isEmpty(), "Question repository should be empty"); - Assertions.assertTrue(worryRepository.findAll().isEmpty(), "Worry repository should be empty"); - Assertions.assertTrue(profileRepository.findAll().isEmpty(), "Profile repository should be empty"); + Assertions.assertThat(answerRepository.findAll()).isEmpty(); + Assertions.assertThat(questionRepository.findAll()).isEmpty(); + Assertions.assertThat(worryRepository.findAll()).isEmpty(); + Assertions.assertThat(profileRepository.findAll()).isEmpty(); } From 99cce52fe0643b8e2cf1f7eabce61c241fa4bb28 Mon Sep 17 00:00:00 2001 From: jenny <88226911+kmjenny@users.noreply.github.com> Date: Tue, 28 May 2024 14:55:16 +0900 Subject: [PATCH 7/8] =?UTF-8?q?[fix]=20#21=20Controller=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/telepigeon/server/controller/RoomController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/telepigeon/server/controller/RoomController.java b/src/main/java/com/telepigeon/server/controller/RoomController.java index 4b4904f..5b88d85 100644 --- a/src/main/java/com/telepigeon/server/controller/RoomController.java +++ b/src/main/java/com/telepigeon/server/controller/RoomController.java @@ -35,8 +35,8 @@ public ResponseEntity createRoom( } @GetMapping("/rooms") - public ResponseEntity> getAllRooms(@UserId Long userId) { - return ResponseEntity.ok(Collections.singletonList(roomService.getAllRooms(userId))); + public ResponseEntity getAllRooms(@UserId Long userId) { + return ResponseEntity.ok(roomService.getAllRooms(userId)); } @GetMapping("/rooms/{roomId}/info") From 8aee4b07ad7526c1d9c76b7dd04ee89745bdb2fa Mon Sep 17 00:00:00 2001 From: jenny <88226911+kmjenny@users.noreply.github.com> Date: Tue, 28 May 2024 19:29:27 +0900 Subject: [PATCH 8/8] =?UTF-8?q?[fix]=20#21=20repository=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/telepigeon/server/repository/ProfileRepository.java | 4 ++-- .../java/com/telepigeon/server/repository/RoomRepository.java | 2 +- .../telepigeon/server/service/profile/ProfileRetriever.java | 4 ++-- .../com/telepigeon/server/service/room/RoomRetriever.java | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/telepigeon/server/repository/ProfileRepository.java b/src/main/java/com/telepigeon/server/repository/ProfileRepository.java index eb42c36..20d67b5 100644 --- a/src/main/java/com/telepigeon/server/repository/ProfileRepository.java +++ b/src/main/java/com/telepigeon/server/repository/ProfileRepository.java @@ -9,7 +9,7 @@ import java.util.Optional; public interface ProfileRepository extends JpaRepository { - Optional findAllByUserAndRoom(Users user, Room room); - Optional findAllByUserNotAndRoom(Users user, Room room); + Optional findByUserAndRoom(Users user, Room room); + Optional findByUserNotAndRoom(Users user, Room room); List findAllByUserId(Long userId); } diff --git a/src/main/java/com/telepigeon/server/repository/RoomRepository.java b/src/main/java/com/telepigeon/server/repository/RoomRepository.java index 7d599d5..543f9dc 100644 --- a/src/main/java/com/telepigeon/server/repository/RoomRepository.java +++ b/src/main/java/com/telepigeon/server/repository/RoomRepository.java @@ -8,6 +8,6 @@ public interface RoomRepository extends JpaRepository { boolean existsByName(String name); boolean existsByCode(String code); - Optional findAllByCode(String code); + Optional findByCode(String code); } diff --git a/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java b/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java index 4a018d9..96c4277 100644 --- a/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java +++ b/src/main/java/com/telepigeon/server/service/profile/ProfileRetriever.java @@ -18,12 +18,12 @@ public class ProfileRetriever { private final ProfileRepository profileRepository; public Profile findByUserAndRoom(final Users user, final Room room) { - return profileRepository.findAllByUserAndRoom(user, room) + return profileRepository.findByUserAndRoom(user, room) .orElseThrow(() -> new NotFoundException(NotFoundErrorCode.PROFILE_NOT_FOUND)); } public Profile findByUserNotAndRoom(final Users user, final Room room) { - return profileRepository.findAllByUserNotAndRoom(user, room) + return profileRepository.findByUserNotAndRoom(user, room) .orElseThrow(() -> new NotFoundException(NotFoundErrorCode.PROFILE_NOT_FOUND)); } diff --git a/src/main/java/com/telepigeon/server/service/room/RoomRetriever.java b/src/main/java/com/telepigeon/server/service/room/RoomRetriever.java index bc09c4c..c564480 100644 --- a/src/main/java/com/telepigeon/server/service/room/RoomRetriever.java +++ b/src/main/java/com/telepigeon/server/service/room/RoomRetriever.java @@ -23,7 +23,7 @@ public Room findById(final long id) { } public Room findByCode(final String code) { - return roomRepository.findAllByCode(code) + return roomRepository.findByCode(code) .orElseThrow(() -> new NotFoundException(NotFoundErrorCode.ROOM_NOT_FOUND)); } }