From b41a6851532b90c83fbe2f35ffcf45b535effc34 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20PAILHAREY?= <rpailharey@grandlyon.com>
Date: Mon, 15 Apr 2024 07:47:33 +0000
Subject: [PATCH] fix: deleting non existing consent returns 404

---
 internal/models/grdfConsent.go | 6 +++++-
 internal/models/sgeConsent.go  | 6 +++++-
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/internal/models/grdfConsent.go b/internal/models/grdfConsent.go
index 0ab5fc1..da7b0b6 100644
--- a/internal/models/grdfConsent.go
+++ b/internal/models/grdfConsent.go
@@ -130,7 +130,11 @@ func (dh *DataHandler) DeleteGrdfConsentById(w http.ResponseWriter, r *http.Requ
 	var consent = GrdfConsent{}
 	err = dh.sqlClient.First(&consent, "id = ?", id).Error
 	if err != nil {
-		http.Error(w, "couldn't find consent", http.StatusInternalServerError)
+		if errors.Is(err, gorm.ErrRecordNotFound) {
+			http.Error(w, "consent not found", http.StatusNotFound)
+			return
+		}
+		http.Error(w, "error while finding consent", http.StatusInternalServerError)
 		log.Println(err.Error())
 		return
 	}
diff --git a/internal/models/sgeConsent.go b/internal/models/sgeConsent.go
index b3278c5..c236666 100644
--- a/internal/models/sgeConsent.go
+++ b/internal/models/sgeConsent.go
@@ -217,7 +217,11 @@ func (dh *DataHandler) DeleteSgeConsentById(w http.ResponseWriter, r *http.Reque
 	var consent = SgeConsent{}
 	err = dh.sqlClient.First(&consent, "id = ?", id).Error
 	if err != nil {
-		http.Error(w, "couldn't find consent", http.StatusInternalServerError)
+		if errors.Is(err, gorm.ErrRecordNotFound) {
+			http.Error(w, "consent not found", http.StatusNotFound)
+			return
+		}
+		http.Error(w, "error while finding consent", http.StatusInternalServerError)
 		log.Println(err.Error())
 		return
 	}
-- 
GitLab