diff --git a/src/main/java/com/activecourses/upwork/controller/user/UserController.java b/src/main/java/com/activecourses/upwork/controller/user/UserController.java index 9b01472..21c460a 100644 --- a/src/main/java/com/activecourses/upwork/controller/user/UserController.java +++ b/src/main/java/com/activecourses/upwork/controller/user/UserController.java @@ -16,7 +16,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; - +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.server.ResponseStatusException; @RestController @RequestMapping("/api/users") @@ -55,4 +56,18 @@ public ResponseEntity getAllUsers( .build()); } + @PreAuthorize("hasRole('ROLE_ADMIN')") + @DeleteMapping + public ResponseEntity DeleteUserbyID( + @RequestParam(value = "id", required = true ) Integer id + ){ + try { + userService.deleteUserbyId(id); + ResponseDto responseDto = new ResponseDto(HttpStatus.OK,true,"User deleted successfully",null); + return new ResponseEntity<>(responseDto, HttpStatus.OK); + } catch (ResponseStatusException e) { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + } } \ No newline at end of file diff --git a/src/main/java/com/activecourses/upwork/dto/ResponseDto.java b/src/main/java/com/activecourses/upwork/dto/ResponseDto.java index 87a52f2..6b8eb8d 100644 --- a/src/main/java/com/activecourses/upwork/dto/ResponseDto.java +++ b/src/main/java/com/activecourses/upwork/dto/ResponseDto.java @@ -1,11 +1,13 @@ package com.activecourses.upwork.dto; +import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import org.springframework.http.HttpStatus; @Data @Builder +@AllArgsConstructor public class ResponseDto { private HttpStatus status; private boolean success; diff --git a/src/main/java/com/activecourses/upwork/repository/user/UserRepository.java b/src/main/java/com/activecourses/upwork/repository/user/UserRepository.java index 5055700..6e59920 100644 --- a/src/main/java/com/activecourses/upwork/repository/user/UserRepository.java +++ b/src/main/java/com/activecourses/upwork/repository/user/UserRepository.java @@ -11,4 +11,5 @@ public interface UserRepository extends JpaRepository { Optional findByEmail(String email); Optional findById(int id); Optional findByVerificationToken(String token); + void deleteById(int id); } \ No newline at end of file diff --git a/src/main/java/com/activecourses/upwork/service/user/UserService.java b/src/main/java/com/activecourses/upwork/service/user/UserService.java index 432fca0..9e36d75 100644 --- a/src/main/java/com/activecourses/upwork/service/user/UserService.java +++ b/src/main/java/com/activecourses/upwork/service/user/UserService.java @@ -4,5 +4,6 @@ public interface UserService { + String deleteUserbyId(Integer id); UserResponseDto getAllUsers(int pageNo, int pageSize, String sortBy, String sortDir); } diff --git a/src/main/java/com/activecourses/upwork/service/user/userServiceImpl.java b/src/main/java/com/activecourses/upwork/service/user/userServiceImpl.java index 5bac67b..5408167 100644 --- a/src/main/java/com/activecourses/upwork/service/user/userServiceImpl.java +++ b/src/main/java/com/activecourses/upwork/service/user/userServiceImpl.java @@ -9,7 +9,9 @@ import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; +import org.springframework.web.server.ResponseStatusException; import java.util.List; @@ -53,5 +55,14 @@ public UserResponseDto getAllUsers(int pageNo, int pageSize, String sortBy, Stri userResponseDto.setTotalPages(pagedResult.getTotalPages()); userResponseDto.setLast(pagedResult.isLast()); return userResponseDto; + + + } + public String deleteUserbyId(Integer id){ + if ( !userRepository.existsById(id)){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND); + } + userRepository.deleteById(id); + return "User deleted successfully"; } }