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

[add] #27 User 부분 추가 #29

Merged
merged 3 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ResponseEntity<Void> createRoom(
@UserId Long userId,
@Valid @RequestBody RoomCreateDto roomCreateDto
) {
Room createdRoom = roomService.createRoom(roomCreateDto);
Room createdRoom = roomService.createRoom(roomCreateDto, userId);
URI location = URI.create("/rooms/" + createdRoom.getId());
return ResponseEntity.created(location).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum NotFoundErrorCode implements DefaultErrorCode{
NOT_FOUND_END_POINT(HttpStatus.NOT_FOUND, "error", "존재하지 않는 API입니다."),
ROOM_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 방입니다."),
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 사용자입니다."),
PROFILE_NOT_FOUND(HttpStatus.NOT_FOUND, "error", "존재하지 않는 프로필입니다."),
;

@JsonIgnore
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.telepigeon.server.repository;

import com.telepigeon.server.domain.Profile;
import com.telepigeon.server.domain.Room;
import com.telepigeon.server.domain.Users;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface ProfileRepository extends JpaRepository<Profile, Long> {
Optional<Profile> findByUserAndRoom(Users user, Room room);
Optional<Profile> findByUserNotAndRoom(Users user, Room room);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.telepigeon.server.service.profile;

import com.telepigeon.server.domain.Profile;
import com.telepigeon.server.domain.Room;
import com.telepigeon.server.domain.Users;
import com.telepigeon.server.exception.NotFoundException;
import com.telepigeon.server.exception.code.NotFoundErrorCode;
import com.telepigeon.server.repository.ProfileRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ProfileRetriever {

private final ProfileRepository profileRepository;

public Profile findByUserAndRoom(Users user, Room room) {
Copy link
Contributor

Choose a reason for hiding this comment

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

감사합니다

return profileRepository.findByUserAndRoom(user, room)
.orElseThrow(() -> new NotFoundException(NotFoundErrorCode.PROFILE_NOT_FOUND));
}

public Profile findByUserNotAndRoom(Users user, Room room) {
return profileRepository.findByUserNotAndRoom(user, room)
.orElseThrow(() -> new NotFoundException(NotFoundErrorCode.PROFILE_NOT_FOUND));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ public class RoomService {
private final ProfileSaver profileSaver;

@Transactional
public Room createRoom(final RoomCreateDto roomCreateDto){
// Users user = userRepository.findByIdOrThrow(userId);
public Room createRoom(final RoomCreateDto roomCreateDto, final Long userId){
Users user = userRepository.findByIdOrThrow(userId);

String code = createCode();

Room room = Room.create(roomCreateDto, code);
Room savedRoom = roomSaver.save(room);

// Profile profile = Profile.create(user, savedRoom);
// Profile savedProfile = profileSaver.save(profile);
Profile profile = Profile.create(user, savedRoom);
Profile savedProfile = profileSaver.save(profile);

return savedRoom;
}
Expand Down
57 changes: 52 additions & 5 deletions src/test/java/com/telepigeon/server/roomTest/RoomServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,76 @@
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.dto.room.request.RoomCreateDto;
import com.telepigeon.server.repository.RoomRepository;
import com.telepigeon.server.repository.UserRepository;
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 org.junit.jupiter.api.Assertions;
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.junit.jupiter.MockitoExtension;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;


@ExtendWith(MockitoExtension.class)
public class RoomServiceTest {

@Mock
private RoomRepository roomRepository = Mockito.mock(RoomRepository.class);

@Mock
private UserRepository userRepository = Mockito.mock(UserRepository.class);

@InjectMocks
private RoomService roomService;

@Mock
private RoomSaver roomSaver;

@Mock
private ProfileSaver profileSaver;

@Test
@DisplayName("Room DB에 저장 확인")
public void checkRoomInDB() {
RoomSaver roomSaver = new RoomSaver(roomRepository);
Room room = Room.create(new RoomCreateDto("name"), "code");
Mockito.doAnswer(invocation -> room).when(roomRepository).save(room);
Room room1 = roomSaver.save(room);
Assertions.assertEquals(room.getName(), room1.getName());
RoomCreateDto roomCreateDto = new RoomCreateDto("test");
Long userId = 1L;
Room room = Room.create(roomCreateDto, "code");

Users user = Mockito.mock(Users.class);
when(userRepository.findByIdOrThrow(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);

// 방이 올바르게 생성되었는지 확인
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());
}

@Test
Expand Down
Loading