-
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.
Merge pull request #14 from Princess-in-silvertown/feat/8
Feat: ์ ์ ์กฐํ ๊ธฐ๋ฅ ๋ฐ ํ ์คํธ ๊ตฌํ
- Loading branch information
Showing
10 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
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); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/slvtwn/khu/toyouserver/user/controller/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,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<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
5 changes: 5 additions & 0 deletions
5
src/main/java/slvtwn/khu/toyouserver/user/dto/UserResponse.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,5 @@ | ||
package slvtwn.khu.toyouserver.user.dto; | ||
|
||
public record UserResponse(Long id, String name, String imageUrl) { | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/slvtwn/khu/toyouserver/user/exception/UserNotFoundException.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,5 @@ | ||
package slvtwn.khu.toyouserver.user.exception; | ||
|
||
public class UserNotFoundException extends RuntimeException { | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/slvtwn/khu/toyouserver/user/repository/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,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<User, Long> { | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/slvtwn/khu/toyouserver/user/service/UserService.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,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()); | ||
} | ||
} |
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,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 | ||
|
44 changes: 44 additions & 0 deletions
44
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,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()); | ||
}); | ||
} | ||
} |