Skip to content
Open
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 @@ -17,6 +17,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.server.ResponseStatusException;

@Tag(name = "User", description = "User API")
@RestController
Expand Down Expand Up @@ -87,4 +89,18 @@ public ResponseEntity<?> updateUserProfile(
}

}
@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
Expand Up @@ -3,9 +3,11 @@
import lombok.Builder;
import lombok.Data;
import org.springframework.http.HttpStatus;
import lombok.AllArgsConstructor;

@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,6 @@ 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 @@ -7,5 +7,6 @@
public interface UserService {
UserResponseDto getAllUsers(int pageNo, int pageSize, String sortBy, String sortDir);
User findByEmail(String email);
String deleteUserbyId(Integer id);

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.springframework.data.domain.Sort;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

Expand Down Expand Up @@ -59,6 +61,12 @@ public User findByEmail(String email) {
return userRepository.findByEmail(email).
orElseThrow(()->new UsernameNotFoundException("User Not Found"));
}


@Override
public String deleteUserbyId(Integer id){
if ( !userRepository.existsById(id)){
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
userRepository.deleteById(id);
return "User deleted successfully";
}
}