RLH-4 - Implement /users/delete API
This commit is contained in:
parent
c5315daf41
commit
a87c692a25
@ -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");
|
||||
|
@ -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<Boolean> deleteUser(@PathVariable("id") String id) {
|
||||
UUID uuid = UUID.fromString(id);
|
||||
boolean deleted = userService.deleteUser(uuid);
|
||||
return ResponseEntity.ok(deleted);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user