From a87c692a25e8fdf70ec0b0829703147987c9ead7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sun, 3 Nov 2024 17:58:00 +0100 Subject: [PATCH] RLH-4 - Implement /users/delete API --- .../exceptions/RestExceptionHandler.java | 5 +++++ .../releasehive/resources/UserResource.java | 14 +++++++++++--- .../beatrice/releasehive/services/UserService.java | 3 ++- .../releasehive/services/UserServiceImpl.java | 10 ++++++---- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/main/java/wtf/beatrice/releasehive/exceptions/RestExceptionHandler.java b/src/main/java/wtf/beatrice/releasehive/exceptions/RestExceptionHandler.java index d047d97..2c96f7f 100644 --- a/src/main/java/wtf/beatrice/releasehive/exceptions/RestExceptionHandler.java +++ b/src/main/java/wtf/beatrice/releasehive/exceptions/RestExceptionHandler.java @@ -26,6 +26,11 @@ public class RestExceptionHandler LOGGER.error(exception); + if(exception instanceof IllegalArgumentException) { + errorDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(400), exception.getMessage()); + errorDetail.setProperty(DESCRIPTION_PROPERTY, "Invalid argument cannot be parsed"); + } + if(exception instanceof BadRequestException) { errorDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(400), exception.getMessage()); errorDetail.setProperty(DESCRIPTION_PROPERTY, "Bad request"); diff --git a/src/main/java/wtf/beatrice/releasehive/resources/UserResource.java b/src/main/java/wtf/beatrice/releasehive/resources/UserResource.java index 280594b..f0f02a3 100644 --- a/src/main/java/wtf/beatrice/releasehive/resources/UserResource.java +++ b/src/main/java/wtf/beatrice/releasehive/resources/UserResource.java @@ -5,13 +5,12 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import wtf.beatrice.releasehive.models.User; import wtf.beatrice.releasehive.services.UserService; import java.util.List; +import java.util.UUID; @RestController @RequestMapping("/api/v1/users") @@ -43,4 +42,13 @@ public class UserResource return ResponseEntity.ok(users); } + @DeleteMapping( + value = "/delete/{id}", + produces = "application/json") + public ResponseEntity deleteUser(@PathVariable("id") String id) { + UUID uuid = UUID.fromString(id); + boolean deleted = userService.deleteUser(uuid); + return ResponseEntity.ok(deleted); + } + } diff --git a/src/main/java/wtf/beatrice/releasehive/services/UserService.java b/src/main/java/wtf/beatrice/releasehive/services/UserService.java index 2607716..89c98b7 100644 --- a/src/main/java/wtf/beatrice/releasehive/services/UserService.java +++ b/src/main/java/wtf/beatrice/releasehive/services/UserService.java @@ -4,6 +4,7 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException; import wtf.beatrice.releasehive.models.User; import java.util.List; +import java.util.UUID; public interface UserService { @@ -13,5 +14,5 @@ public interface UserService User loadUserByEmail(String email) throws UsernameNotFoundException; - void deleteUser(String email); + boolean deleteUser(UUID id); } diff --git a/src/main/java/wtf/beatrice/releasehive/services/UserServiceImpl.java b/src/main/java/wtf/beatrice/releasehive/services/UserServiceImpl.java index 45ef7db..0f39dbb 100644 --- a/src/main/java/wtf/beatrice/releasehive/services/UserServiceImpl.java +++ b/src/main/java/wtf/beatrice/releasehive/services/UserServiceImpl.java @@ -7,6 +7,7 @@ import wtf.beatrice.releasehive.models.User; import wtf.beatrice.releasehive.repositories.UserRepository; import java.util.List; +import java.util.UUID; @Service public class UserServiceImpl implements UserService @@ -33,10 +34,11 @@ public class UserServiceImpl implements UserService } @Override - public void deleteUser(String email) { - if (userRepository.findByEmail(email).isEmpty()) { - throw new UsernameNotFoundException(email); + public boolean deleteUser(UUID id) { + if (userRepository.findById(id).isEmpty()) { + throw new UsernameNotFoundException(id.toString()); } - userRepository.delete(userRepository.findByEmail(email).get()); + userRepository.delete(userRepository.findById(id).get()); + return true; } }