RLH-4 - Implement /users/delete API

This commit is contained in:
Bea 2024-11-03 17:58:00 +01:00
parent c5315daf41
commit a87c692a25
4 changed files with 24 additions and 8 deletions

View File

@ -26,6 +26,11 @@ public class RestExceptionHandler
LOGGER.error(exception); 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) { if(exception instanceof BadRequestException) {
errorDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(400), exception.getMessage()); errorDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(400), exception.getMessage());
errorDetail.setProperty(DESCRIPTION_PROPERTY, "Bad request"); errorDetail.setProperty(DESCRIPTION_PROPERTY, "Bad request");

View File

@ -5,13 +5,12 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import wtf.beatrice.releasehive.models.User; import wtf.beatrice.releasehive.models.User;
import wtf.beatrice.releasehive.services.UserService; import wtf.beatrice.releasehive.services.UserService;
import java.util.List; import java.util.List;
import java.util.UUID;
@RestController @RestController
@RequestMapping("/api/v1/users") @RequestMapping("/api/v1/users")
@ -43,4 +42,13 @@ public class UserResource
return ResponseEntity.ok(users); return ResponseEntity.ok(users);
} }
@DeleteMapping(
value = "/delete/{id}",
produces = "application/json")
public ResponseEntity<Boolean> deleteUser(@PathVariable("id") String id) {
UUID uuid = UUID.fromString(id);
boolean deleted = userService.deleteUser(uuid);
return ResponseEntity.ok(deleted);
}
} }

View File

@ -4,6 +4,7 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
import wtf.beatrice.releasehive.models.User; import wtf.beatrice.releasehive.models.User;
import java.util.List; import java.util.List;
import java.util.UUID;
public interface UserService public interface UserService
{ {
@ -13,5 +14,5 @@ public interface UserService
User loadUserByEmail(String email) throws UsernameNotFoundException; User loadUserByEmail(String email) throws UsernameNotFoundException;
void deleteUser(String email); boolean deleteUser(UUID id);
} }

View File

@ -7,6 +7,7 @@ import wtf.beatrice.releasehive.models.User;
import wtf.beatrice.releasehive.repositories.UserRepository; import wtf.beatrice.releasehive.repositories.UserRepository;
import java.util.List; import java.util.List;
import java.util.UUID;
@Service @Service
public class UserServiceImpl implements UserService public class UserServiceImpl implements UserService
@ -33,10 +34,11 @@ public class UserServiceImpl implements UserService
} }
@Override @Override
public void deleteUser(String email) { public boolean deleteUser(UUID id) {
if (userRepository.findByEmail(email).isEmpty()) { if (userRepository.findById(id).isEmpty()) {
throw new UsernameNotFoundException(email); throw new UsernameNotFoundException(id.toString());
} }
userRepository.delete(userRepository.findByEmail(email).get()); userRepository.delete(userRepository.findById(id).get());
return true;
} }
} }