get delete messages to work

This commit is contained in:
Cubicroot 2021-06-06 21:13:07 +02:00
parent b392ea1b44
commit eebc7f7e31
7 changed files with 103 additions and 18 deletions

5
internal/api/errors.go Normal file
View file

@ -0,0 +1,5 @@
package api
import "errors"
var ErrorMessageNotFound = errors.New("Message not found")

View file

@ -3,6 +3,7 @@ package api
import (
"log"
"net/http"
"net/url"
"strings"
"time"
@ -52,6 +53,7 @@ func (h *NotificationHandler) CreateNotification(ctx *gin.Context) {
}
notification.ID = messageID
notification.UrlEncodedID = url.QueryEscape(messageID)
ctx.JSON(http.StatusOK, &notification)
}
@ -70,5 +72,9 @@ func (h *NotificationHandler) DeleteNotification(ctx *gin.Context) {
Date: time.Now(),
}
h.DP.DeleteNotification(application, &n)
if success := successOrAbort(ctx, http.StatusInternalServerError, h.DP.DeleteNotification(application, &n)); !success {
return
}
ctx.Status(http.StatusOK)
}

View file

@ -11,7 +11,13 @@ import (
func successOrAbort(ctx *gin.Context, code int, err error) bool {
if err != nil {
ctx.AbortWithError(code, err)
// If we know the error force error code
switch err {
case ErrorMessageNotFound:
ctx.AbortWithError(http.StatusNotFound, err)
default:
ctx.AbortWithError(code, err)
}
}
return err == nil