Skip to content

Commit

Permalink
Merge pull request #14 from Princess-in-silvertown/feat/8
Browse files Browse the repository at this point in the history
Feat: ์œ ์ € ์กฐํšŒ ๊ธฐ๋Šฅ ๋ฐ ํ…Œ์ŠคํŠธ ๊ตฌํ˜„
  • Loading branch information
woosung1223 authored Jul 6, 2024
2 parents 6d77d32 + c22cd30 commit b9e9719
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/main/java/slvtwn/khu/toyouserver/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package slvtwn.khu.toyouserver;
package slvtwn.khu.toyouserver.user.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
Expand All @@ -8,6 +8,7 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import slvtwn.khu.toyouserver.BaseTimeEntity;

@Entity
@Table(name = "users")
Expand Down
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) {

}
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 {

}
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 src/main/java/slvtwn/khu/toyouserver/user/service/UserService.java
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());
}
}
8 changes: 6 additions & 2 deletions src/main/resources/application.yml
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

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());
});
}
}

0 comments on commit b9e9719

Please sign in to comment.