From 9eecf45d0da42659ae18dbdd0dafdc1abad8e4f8 Mon Sep 17 00:00:00 2001 From: PgmJun <84304802+PgmJun@users.noreply.github.com> Date: Fri, 21 Apr 2023 12:18:47 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20DELETE=20Method=20-=20=EC=9C=A0?= =?UTF-8?q?=EC=A0=80=20=EC=A0=95=EB=B3=B4=20=EC=82=AD=EC=A0=9C=20#2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserController.java | 13 +++++++++++++ .../repository/UserRepository.java | 2 ++ .../repository/UserRepositoryImpl.java | 6 +++++- .../service/UserService.java | 2 ++ .../service/UserServiceImpl.java | 15 +++++++++++++++ 5 files changed, 37 insertions(+), 1 deletion(-) 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 ae81a6e..861753e 100644 --- a/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/controller/UserController.java +++ b/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/controller/UserController.java @@ -37,6 +37,13 @@ public ResponseEntity updateUser(@PathVariable Long userId, @RequestBody User return generateUpdateResponseEntity(updateResult, userId); } + @DeleteMapping("/delete/{userId}") + public ResponseEntity updateUser(@PathVariable Long userId) { + boolean deleteResult = userService.deleteUser(userId); + + return generateDeleteResponseEntity(deleteResult, userId); + } + @@ -52,4 +59,10 @@ private ResponseEntity generateUpdateResponseEntity(boolean updateResult, Lon new ResponseEntity<>(userId + "번 유저에 대한 정보가 존재하지 않습니다.", HttpStatus.NO_CONTENT); } + private ResponseEntity generateDeleteResponseEntity(boolean deleteResult, Long userId) { + return (deleteResult) ? + new ResponseEntity<>(userId + "번 유저에 대한 정보가 성공적으로 삭제 되었습니다.", HttpStatus.OK): + new ResponseEntity<>(userId + "번 유저에 대한 정보가 존재하지 않습니다.", HttpStatus.NO_CONTENT); + } + } diff --git a/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/repository/UserRepository.java b/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/repository/UserRepository.java index 17a3cb9..caaf21e 100644 --- a/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/repository/UserRepository.java +++ b/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/repository/UserRepository.java @@ -14,4 +14,6 @@ public interface UserRepository { Optional find(Long userId); void update(Long userId, User user); + + void delete(Long userId); } diff --git a/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/repository/UserRepositoryImpl.java b/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/repository/UserRepositoryImpl.java index 3f6d260..189359b 100644 --- a/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/repository/UserRepositoryImpl.java +++ b/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/repository/UserRepositoryImpl.java @@ -32,6 +32,10 @@ public Optional find(Long userId) { @Override public void update(Long userId, User user) { userData.replace(userId, user); - System.out.println("userData.get(userId) = " + userData.get(userId).getName()); + } + + @Override + public void delete(Long userId) { + userData.remove(userId); } } 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 0035e48..3581197 100644 --- a/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/service/UserService.java +++ b/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/service/UserService.java @@ -13,4 +13,6 @@ public interface UserService{ Optional findUserById(Long userId); boolean updateUser(Long userId, UserRequestDto userDto); + + boolean deleteUser(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 43e64fd..f8fbefd 100644 --- a/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/service/UserServiceImpl.java +++ b/secondSeminarAdvancedAssignment/src/main/java/sopt/org/secondSeminarAdvancedAssignment/service/UserServiceImpl.java @@ -41,4 +41,19 @@ public boolean updateUser(Long userId, UserRequestDto updateDto) { return updateResult; } + + @Override + public boolean deleteUser(Long userId) { + boolean deleteResult = true; + + Optional user = userRepository.find(userId); + + if(user.isEmpty()) { + deleteResult = false; + return deleteResult; + } + + userRepository.delete(userId); + return deleteResult; + } }