Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Delete User Account Method #45

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -54,4 +55,19 @@ public ResponseEntity<ResponseDto> getAllUsers(
.status(HttpStatus.OK)
.build());
}

@PreAuthorize("hasRole('ROLE_ADMIN')")
@DeleteMapping
public ResponseEntity <ResponseDto> 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);
}

}
}
2 changes: 2 additions & 0 deletions src/main/java/com/activecourses/upwork/dto/ResponseDto.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public interface UserRepository extends JpaRepository<User, Integer> {
Optional<User> findByEmail(String email);
Optional<User> findById(int id);
Optional<User> findByVerificationToken(String token);
void deleteById(int id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@


public interface UserService {
String deleteUserbyId(Integer id);
UserResponseDto getAllUsers(int pageNo, int pageSize, String sortBy, String sortDir);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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";
}
}