-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
167 additions
and
116 deletions.
There are no files selected for viewing
47 changes: 25 additions & 22 deletions
47
src/main/java/slvtwn/khu/toyouserver/application/GroupService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<GroupMemberResponse> findAllMembers(final long groupId) { | ||
final var group = groupRepository.getById(groupId); | ||
return List.of(new GroupMemberResponse(1L, 1L, "user1", "profile1")); | ||
} | ||
} | ||
return new GroupResponse(group.getId(), group.getName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...youserver/persistance/BaseTimeEntity.java → ...hu/toyouserver/common/BaseTimeEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/slvtwn/khu/toyouserver/common/ToyouResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package slvtwn.khu.toyouserver.common; | ||
|
||
// TODO: 효율적인 예외처리 Wrapping 과정에 대해 고민 | ||
public record ToyouResponse<T>(String code, T data) { | ||
|
||
public static <T> ToyouResponse<T> success(T data) { | ||
return new ToyouResponse<>("SUCCESS", data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
src/main/java/slvtwn/khu/toyouserver/domain/GroupRepository.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
src/main/java/slvtwn/khu/toyouserver/domain/UserRepository.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package slvtwn.khu.toyouserver.dto; | ||
|
||
public record GroupResponse(Long id, String name) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package slvtwn.khu.toyouserver.dto; | ||
|
||
public record UserResponse(Long id, String name, String imageUrl) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/slvtwn/khu/toyouserver/persistance/GroupRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Group, Long> { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/slvtwn/khu/toyouserver/persistance/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<User, Long> { | ||
} |
17 changes: 8 additions & 9 deletions
17
src/main/java/slvtwn/khu/toyouserver/presentation/GroupController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 등록 필요 | ||
} | ||
} |
7 changes: 0 additions & 7 deletions
7
src/main/java/slvtwn/khu/toyouserver/presentation/GroupResponse.java
This file was deleted.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
src/main/java/slvtwn/khu/toyouserver/presentation/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<UserResponse> findUser(@PathVariable Long userId) { | ||
UserResponse userResponse = userService.findUser(userId); | ||
|
||
return ToyouResponse.success(userResponse); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
|
42 changes: 42 additions & 0 deletions
42
src/test/java/slvtwn/khu/toyouserver/user/service/UserServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
}); | ||
} | ||
} |