diff --git a/src/main/java/slvtwn/khu/toyouserver/Member.java b/src/main/java/slvtwn/khu/toyouserver/Member.java index 1f6481a..31e675a 100644 --- a/src/main/java/slvtwn/khu/toyouserver/Member.java +++ b/src/main/java/slvtwn/khu/toyouserver/Member.java @@ -9,6 +9,7 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; +import slvtwn.khu.toyouserver.user.domain.User; @Entity @NoArgsConstructor(access = AccessLevel.PROTECTED) diff --git a/src/main/java/slvtwn/khu/toyouserver/common/ToyouResponse.java b/src/main/java/slvtwn/khu/toyouserver/common/ToyouResponse.java new file mode 100644 index 0000000..2a12f77 --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/common/ToyouResponse.java @@ -0,0 +1,9 @@ +package slvtwn.khu.toyouserver.common; + +// TODO: 효율적인 예외처리 Wrapping 과정에 대해 고민 +public record ToyouResponse(String code, T data) { + + public static ToyouResponse success(T data) { + return new ToyouResponse<>("SUCCESS", data); + } +} diff --git a/src/main/java/slvtwn/khu/toyouserver/user/controller/UserController.java b/src/main/java/slvtwn/khu/toyouserver/user/controller/UserController.java new file mode 100644 index 0000000..dde04db --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/user/controller/UserController.java @@ -0,0 +1,25 @@ +package slvtwn.khu.toyouserver.user.controller; + +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; +import slvtwn.khu.toyouserver.common.ToyouResponse; +import slvtwn.khu.toyouserver.user.dto.UserResponse; +import slvtwn.khu.toyouserver.user.service.UserService; + +@RequiredArgsConstructor +@RestController +public class UserController { + + private final UserService userService; + + @GetMapping("/users/{userId}") + public ToyouResponse findUser(@PathVariable Long userId) { + UserResponse userResponse = userService.findUser(userId); + + return ToyouResponse.success(userResponse); + } +} diff --git a/src/main/java/slvtwn/khu/toyouserver/User.java b/src/main/java/slvtwn/khu/toyouserver/user/domain/User.java similarity index 88% rename from src/main/java/slvtwn/khu/toyouserver/User.java rename to src/main/java/slvtwn/khu/toyouserver/user/domain/User.java index 31f9a26..6e3f72e 100644 --- a/src/main/java/slvtwn/khu/toyouserver/User.java +++ b/src/main/java/slvtwn/khu/toyouserver/user/domain/User.java @@ -1,4 +1,4 @@ -package slvtwn.khu.toyouserver; +package slvtwn.khu.toyouserver.user.domain; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; @@ -8,6 +8,7 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; +import slvtwn.khu.toyouserver.BaseTimeEntity; @Entity @Table(name = "users") diff --git a/src/main/java/slvtwn/khu/toyouserver/user/dto/UserResponse.java b/src/main/java/slvtwn/khu/toyouserver/user/dto/UserResponse.java new file mode 100644 index 0000000..8ca04f9 --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/user/dto/UserResponse.java @@ -0,0 +1,5 @@ +package slvtwn.khu.toyouserver.user.dto; + +public record UserResponse(Long id, String name, String imageUrl) { + +} diff --git a/src/main/java/slvtwn/khu/toyouserver/user/exception/UserNotFoundException.java b/src/main/java/slvtwn/khu/toyouserver/user/exception/UserNotFoundException.java new file mode 100644 index 0000000..b8e21a0 --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/user/exception/UserNotFoundException.java @@ -0,0 +1,5 @@ +package slvtwn.khu.toyouserver.user.exception; + +public class UserNotFoundException extends RuntimeException { + +} diff --git a/src/main/java/slvtwn/khu/toyouserver/user/repository/UserRepository.java b/src/main/java/slvtwn/khu/toyouserver/user/repository/UserRepository.java new file mode 100644 index 0000000..670ef93 --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/user/repository/UserRepository.java @@ -0,0 +1,8 @@ +package slvtwn.khu.toyouserver.user.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import slvtwn.khu.toyouserver.user.domain.User; + +public interface UserRepository extends JpaRepository { + +} diff --git a/src/main/java/slvtwn/khu/toyouserver/user/service/UserService.java b/src/main/java/slvtwn/khu/toyouserver/user/service/UserService.java new file mode 100644 index 0000000..5ff3a49 --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/user/service/UserService.java @@ -0,0 +1,24 @@ +package slvtwn.khu.toyouserver.user.service; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import slvtwn.khu.toyouserver.user.domain.User; +import slvtwn.khu.toyouserver.user.dto.UserResponse; +import slvtwn.khu.toyouserver.user.exception.UserNotFoundException; +import slvtwn.khu.toyouserver.user.repository.UserRepository; + +@RequiredArgsConstructor +@Service +@Transactional(readOnly = true) +public class UserService { + + private final UserRepository userRepository; + + public UserResponse findUser(Long userId) { + User user = userRepository.findById(userId) + .orElseThrow(UserNotFoundException::new); + + return new UserResponse(user.getId(), user.getName(), user.getProfilePicture()); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 8a2503e..75f63bc 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,9 +1,13 @@ spring: jpa: - properties: hibernate: format_sql: true - show-sql: true + datasource: + url: jdbc:h2:mem:testdb + driver-class-name: org.h2.Driver + username: sa + password: 1234 + diff --git a/src/test/java/slvtwn/khu/toyouserver/user/service/UserServiceTest.java b/src/test/java/slvtwn/khu/toyouserver/user/service/UserServiceTest.java new file mode 100644 index 0000000..bf5957e --- /dev/null +++ b/src/test/java/slvtwn/khu/toyouserver/user/service/UserServiceTest.java @@ -0,0 +1,44 @@ +package slvtwn.khu.toyouserver.user.service; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.assertj.core.api.Assertions; +import org.assertj.core.api.SoftAssertions; +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.transaction.annotation.Transactional; +import slvtwn.khu.toyouserver.user.domain.User; +import slvtwn.khu.toyouserver.user.dto.UserResponse; +import slvtwn.khu.toyouserver.user.repository.UserRepository; + +@SuppressWarnings("NonAsciiCharacters") +@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +@SpringBootTest +@Transactional +class UserServiceTest { + + @Autowired + private UserRepository userRepository; + @Autowired + private UserService userService; + + @Test + void 유저를_조회할_수_있다() { + // given + User user = new User("teo", "www.profile-picture.com"); + userRepository.save(user); + + // when + UserResponse found = userService.findUser(user.getId()); + + // then + SoftAssertions.assertSoftly(softly -> { + softly.assertThat(user.getId()).isEqualTo(found.id()); + softly.assertThat(user.getName()).isEqualTo(found.name()); + softly.assertThat(user.getProfilePicture()).isEqualTo(found.imageUrl()); + }); + } +}