Skip to content

Commit

Permalink
support an endpoint to seen all notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
rawdaGastan committed Jan 13, 2025
1 parent 26ced02 commit 9825bac
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions server/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func (a *App) registerHandlers() {

notificationRouter.HandleFunc("", WrapFunc(a.ListNotificationsHandler)).Methods("GET", "OPTIONS")
notificationRouter.HandleFunc("/{id}", WrapFunc(a.UpdateNotificationsHandler)).Methods("PUT", "OPTIONS")
notificationRouter.HandleFunc("", WrapFunc(a.SeenNotificationsHandler)).Methods("PUT", "OPTIONS")

regionRouter.HandleFunc("", WrapFunc(a.ListRegionsHandler)).Methods("GET", "OPTIONS")

Expand Down
28 changes: 28 additions & 0 deletions server/app/notification_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,31 @@ func (a *App) UpdateNotificationsHandler(req *http.Request) (interface{}, Respon
Data: nil,
}, Ok()
}

// SeenNotificationsHandler updates notifications for a user to be seen
// Example endpoint: Set user's notifications as seen
// @Summary Set user's notifications as seen
// @Description Set user's notifications as seen
// @Tags Notification
// @Accept json
// @Produce json
// @Security BearerAuth
// @Success 200 {object} Response
// @Failure 400 {object} Response
// @Failure 401 {object} Response
// @Failure 500 {object} Response
// @Router /notification [put]
func (a *App) SeenNotificationsHandler(req *http.Request) (interface{}, Response) {
userID := req.Context().Value(middlewares.UserIDKey("UserID")).(string)

err := a.db.UpdateUserNotification(userID, true)
if err != nil {
log.Error().Err(err).Send()
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

return ResponseMsg{
Message: "Notifications are seen",
Data: nil,
}, Ok()
}
36 changes: 36 additions & 0 deletions server/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,42 @@ const docTemplate = `{
"schema": {}
}
}
},
"put": {
"security": [
{
"BearerAuth": []
}
],
"description": "Set user's notifications as seen",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Notification"
],
"summary": "Set user's notifications as seen",
"responses": {
"200": {
"description": "OK",
"schema": {}
},
"400": {
"description": "Bad Request",
"schema": {}
},
"401": {
"description": "Unauthorized",
"schema": {}
},
"500": {
"description": "Internal Server Error",
"schema": {}
}
}
}
},
"/notification/{id}": {
Expand Down
24 changes: 24 additions & 0 deletions server/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,30 @@ paths:
summary: Lists user's notifications
tags:
- Notification
put:
consumes:
- application/json
description: Set user's notifications as seen
produces:
- application/json
responses:
"200":
description: OK
schema: {}
"400":
description: Bad Request
schema: {}
"401":
description: Unauthorized
schema: {}
"500":
description: Internal Server Error
schema: {}
security:
- BearerAuth: []
summary: Set user's notifications as seen
tags:
- Notification
/notification/{id}:
put:
consumes:
Expand Down
5 changes: 5 additions & 0 deletions server/models/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func (d *DB) UpdateNotification(id int, seen bool) error {
return d.db.Model(&Notification{}).Where("id = ?", id).Updates(map[string]interface{}{"seen": seen}).Error
}

// UpdateUserNotification updates seen field for user notifications
func (d *DB) UpdateUserNotification(userID string, seen bool) error {
return d.db.Model(&Notification{}).Where("user_id = ?", userID).Updates(map[string]interface{}{"seen": seen}).Error
}

// CreateNotification adds a new notification for a user
func (d *DB) CreateNotification(n *Notification) error {
return d.db.Create(&n).Error
Expand Down

0 comments on commit 9825bac

Please sign in to comment.