mirror of
https://github.com/pushbits/server.git
synced 2025-05-11 16:07:04 +02:00
add endpoint for delete
This commit is contained in:
parent
3834e90527
commit
b392ea1b44
6 changed files with 65 additions and 34 deletions
|
@ -20,6 +20,17 @@ func getID(ctx *gin.Context) (uint, error) {
|
|||
return id, nil
|
||||
}
|
||||
|
||||
func getMessageID(ctx *gin.Context) (string, error) {
|
||||
id, ok := ctx.MustGet("messageid").(string)
|
||||
if !ok {
|
||||
err := errors.New("an error occured while retrieving messageID from context")
|
||||
ctx.AbortWithError(http.StatusInternalServerError, err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func getApplication(ctx *gin.Context, db Database) (*model.Application, error) {
|
||||
id, err := getID(ctx)
|
||||
if err != nil {
|
||||
|
|
|
@ -8,6 +8,10 @@ type idInURI struct {
|
|||
ID uint `uri:"id" binding:"required"`
|
||||
}
|
||||
|
||||
type messageIdInURI struct {
|
||||
MessageID string `uri:"messageid" binding:"required"`
|
||||
}
|
||||
|
||||
// RequireIDInURI returns a Gin middleware which requires an ID to be supplied in the URI of the request.
|
||||
func RequireIDInURI() gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
|
@ -20,3 +24,16 @@ func RequireIDInURI() gin.HandlerFunc {
|
|||
ctx.Set("id", requestModel.ID)
|
||||
}
|
||||
}
|
||||
|
||||
// RequireMessageIDInURI returns a Gin middleware which requires an messageID to be supplied in the URI of the request.
|
||||
func RequireMessageIDInURI() gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
var requestModel messageIdInURI
|
||||
|
||||
if err := ctx.BindUri(&requestModel); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Set("messageid", requestModel.MessageID)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,8 +18,8 @@ type NotificationDatabase interface {
|
|||
|
||||
// The NotificationDispatcher interface for relaying notifications.
|
||||
type NotificationDispatcher interface {
|
||||
SendNotification(a *model.Application, n *model.Notification) error
|
||||
SendDeleteNotification(a *model.Application, n *model.DeleteNotification) error
|
||||
SendNotification(a *model.Application, n *model.Notification) (id string, err error)
|
||||
DeleteNotification(a *model.Application, n *model.DeleteNotification) error
|
||||
}
|
||||
|
||||
// NotificationHandler holds information for processing requests about notifications.
|
||||
|
@ -39,24 +39,36 @@ func (h *NotificationHandler) CreateNotification(ctx *gin.Context) {
|
|||
application := authentication.GetApplication(ctx)
|
||||
log.Printf("Sending notification for application %s.", application.Name)
|
||||
|
||||
notification.ID = 0
|
||||
notification.ApplicationID = application.ID
|
||||
if strings.TrimSpace(notification.Title) == "" {
|
||||
notification.Title = application.Name
|
||||
}
|
||||
notification.Date = time.Now()
|
||||
|
||||
if success := successOrAbort(ctx, http.StatusInternalServerError, h.DP.SendNotification(application, ¬ification)); !success {
|
||||
messageID, err := h.DP.SendNotification(application, ¬ification)
|
||||
|
||||
if success := successOrAbort(ctx, http.StatusInternalServerError, err); !success {
|
||||
return
|
||||
}
|
||||
|
||||
notification.ID = messageID
|
||||
|
||||
ctx.JSON(http.StatusOK, ¬ification)
|
||||
}
|
||||
|
||||
// DeleteNotification is used to delete (or mark as deleted) a notification for a user
|
||||
func (h *NotificationHandler) DeleteNotification(ctx *gin.Context) {
|
||||
application := authentication.GetApplication(ctx)
|
||||
id, err := getMessageID(ctx)
|
||||
|
||||
n := model.DeleteNotification{}
|
||||
if success := successOrAbort(ctx, http.StatusUnprocessableEntity, err); !success {
|
||||
return
|
||||
}
|
||||
|
||||
h.DP.SendDeleteNotification(application, &n)
|
||||
n := model.DeleteNotification{
|
||||
ID: id,
|
||||
Date: time.Now(),
|
||||
}
|
||||
|
||||
h.DP.DeleteNotification(application, &n)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue