forked from f-lab-edu/bbaemin
-
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.
User JPA 적용, BindException 처리 (f-lab-edu#26)
* User - JPA 적용 * User - ApiResult 적용 및 Controller 테스트 작성 (cherry picked from commit 4239842) * Validation 체크 및 ControllerExceptionAdvice 생성 (cherry picked from commit e38b6f6) * updateUserInfo - Patch로 변경 * Validation 체크 예외를 MethodArgumentNotValidException으로 변경 * ApiResult - ResultCode.CREATED 추가
- Loading branch information
1 parent
36a105f
commit dec634b
Showing
11 changed files
with
504 additions
and
89 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
bbaemin-api/src/main/java/org/bbaemin/config/advice/ControllerExceptionAdvice.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,21 @@ | ||
package org.bbaemin.config.advice; | ||
|
||
import org.bbaemin.config.response.ApiResult; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import java.util.List; | ||
|
||
@RestControllerAdvice | ||
public class ControllerExceptionAdvice { | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ApiResult<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { | ||
BindingResult bindingResult = e.getBindingResult(); | ||
List<FieldError> fieldErrors = bindingResult.getFieldErrors(); | ||
return ApiResult.badRequest(fieldErrors); | ||
} | ||
} |
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
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: 2 additions & 0 deletions
2
bbaemin-api/src/main/java/org/bbaemin/user/controller/response/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
68 changes: 6 additions & 62 deletions
68
bbaemin-api/src/main/java/org/bbaemin/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 |
---|---|---|
@@ -1,68 +1,12 @@ | ||
package org.bbaemin.user.repository; | ||
|
||
import org.bbaemin.user.vo.User; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
@Transactional(readOnly = true) | ||
@Repository | ||
public interface UserRepository extends JpaRepository<User, Long> { | ||
|
||
@Component | ||
public class UserRepository { | ||
|
||
private static final Map<Long, User> map = new ConcurrentHashMap<>(); | ||
private static final AtomicLong id = new AtomicLong(0L); | ||
|
||
public static AtomicLong getId() { | ||
return id; | ||
} | ||
|
||
static { | ||
map.put(getId().incrementAndGet(), User.builder() | ||
.userId(getId().get()) | ||
.email("user1@email.com") | ||
.nickname("user1") | ||
.image(null) | ||
.phoneNumber("010-1234-5678") | ||
.build()); | ||
map.put(getId().incrementAndGet(), User.builder() | ||
.userId(getId().get()) | ||
.email("user2@email.com") | ||
.nickname("user2") | ||
.image(null) | ||
.phoneNumber("010-1111-2222") | ||
.build()); | ||
} | ||
|
||
public static void clear() { | ||
map.clear(); | ||
} | ||
|
||
public List<User> findAll() { | ||
return new ArrayList<>(map.values()); | ||
} | ||
|
||
public User findById(Long userId) { | ||
return map.get(userId); | ||
} | ||
|
||
public User insert(User user) { | ||
Long userId = getId().incrementAndGet(); | ||
user.setUserId(userId); | ||
map.put(userId, user); | ||
return user; | ||
} | ||
|
||
public User update(User user) { | ||
map.put(user.getUserId(), user); | ||
return user; | ||
} | ||
|
||
public void updateUserDeleted(Long userId) { | ||
User user = map.get(userId); | ||
user.setDeleted(true); | ||
map.put(userId, user); | ||
} | ||
} |
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
Oops, something went wrong.