From fd2c3f0cabf5aac80847f9db5622da5570162361 Mon Sep 17 00:00:00 2001 From: PgmJun <84304802+PgmJun@users.noreply.github.com> Date: Fri, 21 Apr 2023 12:23:05 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20POST=20Method=20API=20-=20ResponseE?= =?UTF-8?q?ntity=20=EC=83=9D=EC=84=B1=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20#2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserController.java | 10 ++++++++-- .../service/UserService.java | 2 +- .../service/UserServiceImpl.java | 12 ++++++++++-- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/controller/UserController.java b/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/controller/UserController.java index 861753e..578c3a2 100644 --- a/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/controller/UserController.java +++ b/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/controller/UserController.java @@ -19,8 +19,9 @@ public class UserController { @PostMapping("/save") public ResponseEntity saveUser(@RequestBody UserRequestDto userRequestDto) { - userService.saveUser(userRequestDto); - return new ResponseEntity("유저 저장 완료", HttpStatus.OK); + boolean saveResult = userService.saveUser(userRequestDto); + + return generateSaveResponseEntity(saveResult); } @GetMapping("/{userId}") @@ -46,6 +47,11 @@ public ResponseEntity updateUser(@PathVariable Long userId) { + private ResponseEntity generateSaveResponseEntity(boolean saveResult) { + return (saveResult) ? + new ResponseEntity<>("유저에 대한 정보가 성공적으로 저장 되었습니다.", HttpStatus.OK): + new ResponseEntity<>("유저 등록에 실패하였습니다.", HttpStatus.NO_CONTENT); + } private ResponseEntity generateUserResponseEntity(Optional user, Long userId) { return (user.isEmpty()) ? diff --git a/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/service/UserService.java b/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/service/UserService.java index 3581197..c591319 100644 --- a/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/service/UserService.java +++ b/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/service/UserService.java @@ -8,7 +8,7 @@ public interface UserService{ - void saveUser(UserRequestDto userDto); + boolean saveUser(UserRequestDto userDto); Optional findUserById(Long userId); diff --git a/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/service/UserServiceImpl.java b/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/service/UserServiceImpl.java index f8fbefd..b3503f5 100644 --- a/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/service/UserServiceImpl.java +++ b/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/service/UserServiceImpl.java @@ -14,8 +14,16 @@ public class UserServiceImpl implements UserService { private final UserRepository userRepository; @Override - public void saveUser(UserRequestDto userDto) { - userRepository.save(userDto.from()); + public boolean saveUser(UserRequestDto userDto) { + boolean saveResult = true; + + try { + userRepository.save(userDto.from()); + } catch (Exception e) { + saveResult = false; + } + + return saveResult; } @Override