From 21e6a2b5f7345e5a527a547c04483c04d4b0678e Mon Sep 17 00:00:00 2001 From: Anna Date: Fri, 18 Oct 2024 00:26:00 +0200 Subject: [PATCH] fix : delete alertId --- .../polytech/controller/AlertResource.java | 22 ++++++++++--------- .../polytech/repository/AlertRepository.java | 13 +++++------ 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/cloud/backend/alert-management/src/main/java/fr/etu/polytech/controller/AlertResource.java b/cloud/backend/alert-management/src/main/java/fr/etu/polytech/controller/AlertResource.java index a4b9161..f1caf13 100644 --- a/cloud/backend/alert-management/src/main/java/fr/etu/polytech/controller/AlertResource.java +++ b/cloud/backend/alert-management/src/main/java/fr/etu/polytech/controller/AlertResource.java @@ -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") @@ -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; @@ -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; + } + } diff --git a/cloud/backend/alert-management/src/main/java/fr/etu/polytech/repository/AlertRepository.java b/cloud/backend/alert-management/src/main/java/fr/etu/polytech/repository/AlertRepository.java index f321d71..88d31cc 100644 --- a/cloud/backend/alert-management/src/main/java/fr/etu/polytech/repository/AlertRepository.java +++ b/cloud/backend/alert-management/src/main/java/fr/etu/polytech/repository/AlertRepository.java @@ -30,12 +30,9 @@ public List 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; - } + + + + + }