diff --git a/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java b/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java index 476d0a9..ae06406 100644 --- a/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java +++ b/src/main/java/slvtwn/khu/toyouserver/application/GroupService.java @@ -1,33 +1,36 @@ package slvtwn.khu.toyouserver.application; -import java.util.List; import org.springframework.stereotype.Service; -import slvtwn.khu.toyouserver.domain.GroupRepository; -import slvtwn.khu.toyouserver.presentation.GroupMemberResponse; -import slvtwn.khu.toyouserver.presentation.GroupResponse; +import org.springframework.transaction.annotation.Transactional; +import slvtwn.khu.toyouserver.common.ErrorType; +import slvtwn.khu.toyouserver.domain.Group; +import slvtwn.khu.toyouserver.domain.User; +import slvtwn.khu.toyouserver.dto.GroupResponse; +import slvtwn.khu.toyouserver.exception.ToyouException; +import slvtwn.khu.toyouserver.persistance.GroupRepository; +import slvtwn.khu.toyouserver.persistance.UserRepository; @Service +@Transactional(readOnly = true) public class GroupService { - private final GroupRepository groupRepository; + private final GroupRepository groupRepository; + private final UserRepository userRepository; - private final UserService userService; + public GroupService(GroupRepository groupRepository, UserRepository userRepository) { + this.groupRepository = groupRepository; + this.userRepository = userRepository; + } - public GroupService(GroupRepository groupRepository, UserService userService) { - this.groupRepository = groupRepository; - this.userService = userService; - } + @Transactional + public GroupResponse registerUser(long groupId, long userId) { + Group group = groupRepository.findById(groupId) + .orElseThrow(() -> new ToyouException(ErrorType.GROUP_NOT_FOUND)); + User user = userRepository.findById(userId) + .orElseThrow(() -> new ToyouException(ErrorType.USER_NOT_FOUND)); - public GroupResponse registerUser(final long groupId, final long userId) { - final var group = groupRepository.getById(groupId); - final var user = userService.findById(userId); - // TODO : 그룹에 유저를 가입시킨다. 유저는 멤버로 등록된다. + // TODO : 그룹에 유저를 가입시킨다. 유저는 멤버로 등록된다. // group.addMember(user); - return new GroupResponse(group.getId(), group.getName()); - } - - public List findAllMembers(final long groupId) { - final var group = groupRepository.getById(groupId); - return List.of(new GroupMemberResponse(1L, 1L, "user1", "profile1")); - } -} \ No newline at end of file + return new GroupResponse(group.getId(), group.getName()); + } +} diff --git a/src/main/java/slvtwn/khu/toyouserver/application/UserService.java b/src/main/java/slvtwn/khu/toyouserver/application/UserService.java index 0d04de2..65db4ac 100644 --- a/src/main/java/slvtwn/khu/toyouserver/application/UserService.java +++ b/src/main/java/slvtwn/khu/toyouserver/application/UserService.java @@ -1,9 +1,11 @@ package slvtwn.khu.toyouserver.application; import org.springframework.stereotype.Service; +import slvtwn.khu.toyouserver.common.ErrorType; import slvtwn.khu.toyouserver.domain.User; -import slvtwn.khu.toyouserver.domain.UserRepository; - +import slvtwn.khu.toyouserver.exception.ToyouException; +import slvtwn.khu.toyouserver.persistance.UserRepository; +import slvtwn.khu.toyouserver.dto.UserResponse; @Service public class UserService { @@ -14,7 +16,10 @@ public UserService(UserRepository userRepository) { this.userRepository = userRepository; } - public User findById(final long userId) { - return userRepository.getById(userId); + public UserResponse findUser(Long userId) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new ToyouException(ErrorType.USER_NOT_FOUND)); + + return new UserResponse(user.getId(), user.getName(), user.getProfilePicture()); } } diff --git a/src/main/java/slvtwn/khu/toyouserver/persistance/BaseTimeEntity.java b/src/main/java/slvtwn/khu/toyouserver/common/BaseTimeEntity.java similarity index 93% rename from src/main/java/slvtwn/khu/toyouserver/persistance/BaseTimeEntity.java rename to src/main/java/slvtwn/khu/toyouserver/common/BaseTimeEntity.java index 9cab087..71ec6a0 100644 --- a/src/main/java/slvtwn/khu/toyouserver/persistance/BaseTimeEntity.java +++ b/src/main/java/slvtwn/khu/toyouserver/common/BaseTimeEntity.java @@ -1,4 +1,4 @@ -package slvtwn.khu.toyouserver.persistance; +package slvtwn.khu.toyouserver.common; import jakarta.persistence.Column; import jakarta.persistence.EntityListeners; 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/domain/Comment.java b/src/main/java/slvtwn/khu/toyouserver/domain/Comment.java index 3c9fa8e..7204564 100644 --- a/src/main/java/slvtwn/khu/toyouserver/domain/Comment.java +++ b/src/main/java/slvtwn/khu/toyouserver/domain/Comment.java @@ -9,7 +9,7 @@ import jakarta.persistence.ManyToOne; import lombok.AccessLevel; import lombok.NoArgsConstructor; -import slvtwn.khu.toyouserver.persistance.BaseTimeEntity; +import slvtwn.khu.toyouserver.common.BaseTimeEntity; @Entity @NoArgsConstructor(access = AccessLevel.PROTECTED) diff --git a/src/main/java/slvtwn/khu/toyouserver/domain/Group.java b/src/main/java/slvtwn/khu/toyouserver/domain/Group.java index 7bc4f9e..0cd4871 100644 --- a/src/main/java/slvtwn/khu/toyouserver/domain/Group.java +++ b/src/main/java/slvtwn/khu/toyouserver/domain/Group.java @@ -9,7 +9,7 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; -import slvtwn.khu.toyouserver.persistance.BaseTimeEntity; +import slvtwn.khu.toyouserver.common.BaseTimeEntity; @Entity @Table(name = "groups") diff --git a/src/main/java/slvtwn/khu/toyouserver/domain/GroupRepository.java b/src/main/java/slvtwn/khu/toyouserver/domain/GroupRepository.java deleted file mode 100644 index 3498677..0000000 --- a/src/main/java/slvtwn/khu/toyouserver/domain/GroupRepository.java +++ /dev/null @@ -1,24 +0,0 @@ -package slvtwn.khu.toyouserver.domain; - -import static slvtwn.khu.toyouserver.common.ErrorType.GROUP_NOT_FOUND; - -import java.util.Optional; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; -import org.springframework.transaction.annotation.Transactional; -import slvtwn.khu.toyouserver.exception.ToyouException; - -@Repository -public interface GroupRepository extends JpaRepository { - - @Transactional - Group save(Group group); - - @Transactional(readOnly = true) - Optional findById(long id); - - @Transactional(readOnly = true) - default Group getById(final long id) { - return findById(id).orElseThrow(() -> new ToyouException(GROUP_NOT_FOUND)); - } -} diff --git a/src/main/java/slvtwn/khu/toyouserver/domain/Member.java b/src/main/java/slvtwn/khu/toyouserver/domain/Member.java index a6ade45..c47ecb1 100644 --- a/src/main/java/slvtwn/khu/toyouserver/domain/Member.java +++ b/src/main/java/slvtwn/khu/toyouserver/domain/Member.java @@ -9,26 +9,26 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; -import slvtwn.khu.toyouserver.persistance.BaseTimeEntity; +import slvtwn.khu.toyouserver.common.BaseTimeEntity; @Entity @NoArgsConstructor(access = AccessLevel.PROTECTED) @Getter public class Member extends BaseTimeEntity { - @Id - @GeneratedValue - private Long id; + @Id + @GeneratedValue + private Long id; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "user_id") - private User user; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id") + private User user; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "group_id") - private Group group; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "group_id") + private Group group; - public Member(Group group) { - this.group = group; - } + public Member(Group group) { + this.group = group; + } } diff --git a/src/main/java/slvtwn/khu/toyouserver/domain/RollingPaper.java b/src/main/java/slvtwn/khu/toyouserver/domain/RollingPaper.java index 1c0338a..08b0353 100644 --- a/src/main/java/slvtwn/khu/toyouserver/domain/RollingPaper.java +++ b/src/main/java/slvtwn/khu/toyouserver/domain/RollingPaper.java @@ -10,7 +10,7 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; -import slvtwn.khu.toyouserver.persistance.BaseTimeEntity; +import slvtwn.khu.toyouserver.common.BaseTimeEntity; @Entity @NoArgsConstructor(access = AccessLevel.PROTECTED) diff --git a/src/main/java/slvtwn/khu/toyouserver/domain/User.java b/src/main/java/slvtwn/khu/toyouserver/domain/User.java index fc0306c..270b90a 100644 --- a/src/main/java/slvtwn/khu/toyouserver/domain/User.java +++ b/src/main/java/slvtwn/khu/toyouserver/domain/User.java @@ -8,7 +8,7 @@ import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; -import slvtwn.khu.toyouserver.persistance.BaseTimeEntity; +import slvtwn.khu.toyouserver.common.BaseTimeEntity; @Entity @Table(name = "users") diff --git a/src/main/java/slvtwn/khu/toyouserver/domain/UserRepository.java b/src/main/java/slvtwn/khu/toyouserver/domain/UserRepository.java deleted file mode 100644 index 2a144c9..0000000 --- a/src/main/java/slvtwn/khu/toyouserver/domain/UserRepository.java +++ /dev/null @@ -1,21 +0,0 @@ -package slvtwn.khu.toyouserver.domain; - -import static slvtwn.khu.toyouserver.common.ErrorType.USER_NOT_FOUND; - -import java.util.Optional; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; -import org.springframework.transaction.annotation.Transactional; -import slvtwn.khu.toyouserver.exception.ToyouException; - -@Repository -public interface UserRepository extends JpaRepository { - - @Transactional(readOnly = true) - Optional findById(Long id); - - @Transactional(readOnly = true) - default User getById(Long id) { - return findById(id).orElseThrow(() -> new ToyouException(USER_NOT_FOUND)); - } -} diff --git a/src/main/java/slvtwn/khu/toyouserver/dto/GroupResponse.java b/src/main/java/slvtwn/khu/toyouserver/dto/GroupResponse.java new file mode 100644 index 0000000..5d0c903 --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/dto/GroupResponse.java @@ -0,0 +1,4 @@ +package slvtwn.khu.toyouserver.dto; + +public record GroupResponse(Long id, String name) { +} diff --git a/src/main/java/slvtwn/khu/toyouserver/dto/UserResponse.java b/src/main/java/slvtwn/khu/toyouserver/dto/UserResponse.java new file mode 100644 index 0000000..5ba3fdf --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/dto/UserResponse.java @@ -0,0 +1,4 @@ +package slvtwn.khu.toyouserver.dto; + +public record UserResponse(Long id, String name, String imageUrl) { +} diff --git a/src/main/java/slvtwn/khu/toyouserver/exception/ToyouException.java b/src/main/java/slvtwn/khu/toyouserver/exception/ToyouException.java index 414b487..4950d74 100644 --- a/src/main/java/slvtwn/khu/toyouserver/exception/ToyouException.java +++ b/src/main/java/slvtwn/khu/toyouserver/exception/ToyouException.java @@ -6,7 +6,8 @@ @RequiredArgsConstructor @Getter -public final class ToyouException extends RuntimeException { +public class ToyouException extends RuntimeException { + private final ErrorType errorType; public String code() { diff --git a/src/main/java/slvtwn/khu/toyouserver/persistance/GroupRepository.java b/src/main/java/slvtwn/khu/toyouserver/persistance/GroupRepository.java new file mode 100644 index 0000000..48015a1 --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/persistance/GroupRepository.java @@ -0,0 +1,7 @@ +package slvtwn.khu.toyouserver.persistance; + +import org.springframework.data.jpa.repository.JpaRepository; +import slvtwn.khu.toyouserver.domain.Group; + +public interface GroupRepository extends JpaRepository { +} diff --git a/src/main/java/slvtwn/khu/toyouserver/persistance/UserRepository.java b/src/main/java/slvtwn/khu/toyouserver/persistance/UserRepository.java new file mode 100644 index 0000000..0126b83 --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/persistance/UserRepository.java @@ -0,0 +1,7 @@ +package slvtwn.khu.toyouserver.persistance; + +import org.springframework.data.jpa.repository.JpaRepository; +import slvtwn.khu.toyouserver.domain.User; + +public interface UserRepository extends JpaRepository { +} diff --git a/src/main/java/slvtwn/khu/toyouserver/presentation/GroupController.java b/src/main/java/slvtwn/khu/toyouserver/presentation/GroupController.java index 695c5b9..17c195f 100644 --- a/src/main/java/slvtwn/khu/toyouserver/presentation/GroupController.java +++ b/src/main/java/slvtwn/khu/toyouserver/presentation/GroupController.java @@ -1,21 +1,20 @@ package slvtwn.khu.toyouserver.presentation; +import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import slvtwn.khu.toyouserver.application.GroupService; +import slvtwn.khu.toyouserver.dto.GroupResponse; +@RequiredArgsConstructor @RestController public class GroupController { - private final GroupService groupService; + private final GroupService groupService; - public GroupController(GroupService groupService) { - this.groupService = groupService; - } - - @PostMapping("/groups/{groupId}/members") - public GroupResponse registerMember(@PathVariable final long groupId) { - return groupService.registerMember(groupId); - } + @PostMapping("/groups/{groupId}/members") + public GroupResponse registerMember(@PathVariable long groupId) { + return groupService.registerUser(groupId, 1L); // TODO: user -> argumentResolver 등록 필요 + } } diff --git a/src/main/java/slvtwn/khu/toyouserver/presentation/GroupResponse.java b/src/main/java/slvtwn/khu/toyouserver/presentation/GroupResponse.java deleted file mode 100644 index 238efe7..0000000 --- a/src/main/java/slvtwn/khu/toyouserver/presentation/GroupResponse.java +++ /dev/null @@ -1,7 +0,0 @@ -package slvtwn.khu.toyouserver.presentation; - -public record GroupResponse( - Long id, - - String name) { -} \ No newline at end of file diff --git a/src/main/java/slvtwn/khu/toyouserver/presentation/UserController.java b/src/main/java/slvtwn/khu/toyouserver/presentation/UserController.java new file mode 100644 index 0000000..8c73635 --- /dev/null +++ b/src/main/java/slvtwn/khu/toyouserver/presentation/UserController.java @@ -0,0 +1,23 @@ +package slvtwn.khu.toyouserver.presentation; + +import lombok.RequiredArgsConstructor; +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.application.UserService; +import slvtwn.khu.toyouserver.common.ToyouResponse; +import slvtwn.khu.toyouserver.dto.UserResponse; + +@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/resources/application.yml b/src/main/resources/application.yml index 9d366b8..d279fe1 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,17 +1,16 @@ spring: - data-source: - url: jdbc:h2:mem:test - driver-class-name: org.h2.Driver - username: sa - password: - jpa: - database: mysql +# database: mysql open-in-view: false - show-sql: true - database-platform: org.hibernate.dialect.H2Dialect generate-ddl: true + show-sql: true properties: hibernate: format_sql: true - ddl-auto: update + + datasource: + url: jdbc:h2:mem:testdb + driver-class-name: org.h2.Driver + username: sa + password: + 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..06997ac --- /dev/null +++ b/src/test/java/slvtwn/khu/toyouserver/user/service/UserServiceTest.java @@ -0,0 +1,42 @@ +package slvtwn.khu.toyouserver.user.service; + +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.application.UserService; +import slvtwn.khu.toyouserver.domain.User; +import slvtwn.khu.toyouserver.persistance.UserRepository; +import slvtwn.khu.toyouserver.dto.UserResponse; + +@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()); + }); + } +}