Skip to content

Commit

Permalink
fix : delete alertId
Browse files Browse the repository at this point in the history
  • Loading branch information
annadiplacido committed Oct 17, 2024
1 parent bf36c85 commit 21e6a2b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,9 @@ public Alert updateAlert(@PathParam("alertId") @Pattern(regexp = "^[0-9a-fA-F]{2
@DELETE
@Path("/{alertId}")
@Produces(MediaType.APPLICATION_JSON)
public void deleteAlert(@PathParam("alertId") @Pattern(regexp = "^[0-9a-fA-F]{24}$", message = alertIdErrorMessage) String alertId) throws ResourceNotFoundException, IncorrectRequestException {
validateId(alertId);
boolean isDeleted = alertRepository.deleteByIdR(new ObjectId(alertId));
if (!isDeleted) {
throw new ResourceNotFoundException("Alert with ID " + alertId + " not found.");
}
public void deleteAlert(@PathParam("alertId") @Pattern(regexp = "^[0-9a-fA-F]{24}$", message = alertIdErrorMessage) String alertId) throws ResourceNotFoundException {
Alert alert = findAlertByAlertId(alertId);
alertRepository.delete(alert);
}
@DELETE
@Path("/deleteAll")
Expand All @@ -150,10 +147,7 @@ public void deleteAllAlerts() {
@Path("/{alertId}/mark-treated")
@Produces(MediaType.APPLICATION_JSON)
public Alert markAlertAsTreated(@PathParam("alertId") @Pattern(regexp = "^[0-9a-fA-F]{24}$", message = alertIdErrorMessage) String alertId) throws IncorrectRequestException, ResourceNotFoundException {
Alert alert = alertRepository.findByAlertId(new ObjectId(alertId));
if (alert == null) {
throw new ResourceNotFoundException("Alert with ID " + alertId + " not found.");
}
Alert alert = findAlertByAlertId(alertId);
alert.setTreated(true);
alertRepository.update(alert);
return alert;
Expand Down Expand Up @@ -215,6 +209,14 @@ public void validateGatewayId(String gatewayId) throws IncorrectRequestException
}
}

public Alert findAlertByAlertId(String alertId) throws ResourceNotFoundException {
Alert alert = alertRepository.findByAlertId(new ObjectId(alertId));
if (alert == null) {
throw new ResourceNotFoundException("Alert with ID " + alertId + " not found.");
}
return alert;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ public List<Alert> findBySeverity(Severity severity) {
public Alert findByAlertId(ObjectId alertId) {
return find("alertId", alertId).firstResult();
}
public boolean deleteByIdR(ObjectId id) {
Alert alert = findById(id);
if (alert != null) {
delete(alert);
return true;
}
return false;
}





}

0 comments on commit 21e6a2b

Please sign in to comment.