Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
delete user account
  • Loading branch information
Seifeldahshan committed Oct 15, 2024
1 parent 9af6ef1 commit cdc53b4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
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 @@ -55,4 +56,18 @@ public ResponseEntity<ResponseDto> getAllUsers(
.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";
}
}

0 comments on commit cdc53b4

Please sign in to comment.