From 9825bacc375c75c1b1dbbeb3fe47f6dcdb8801d3 Mon Sep 17 00:00:00 2001 From: rawdaGastan Date: Mon, 13 Jan 2025 15:11:40 +0200 Subject: [PATCH] support an endpoint to seen all notifications --- server/app/app.go | 1 + server/app/notification_handler.go | 28 +++++++++++++++++++++++ server/docs/docs.go | 36 ++++++++++++++++++++++++++++++ server/docs/swagger.yaml | 24 ++++++++++++++++++++ server/models/notification.go | 5 +++++ 5 files changed, 94 insertions(+) diff --git a/server/app/app.go b/server/app/app.go index da38e776..60c30364 100644 --- a/server/app/app.go +++ b/server/app/app.go @@ -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") diff --git a/server/app/notification_handler.go b/server/app/notification_handler.go index c3e7dfc9..2e6e1547 100644 --- a/server/app/notification_handler.go +++ b/server/app/notification_handler.go @@ -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() +} diff --git a/server/docs/docs.go b/server/docs/docs.go index f497c792..8f3a40ba 100644 --- a/server/docs/docs.go +++ b/server/docs/docs.go @@ -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}": { diff --git a/server/docs/swagger.yaml b/server/docs/swagger.yaml index ba534271..fd510e69 100644 --- a/server/docs/swagger.yaml +++ b/server/docs/swagger.yaml @@ -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: diff --git a/server/models/notification.go b/server/models/notification.go index 1bf0f4ae..e6d66d23 100644 --- a/server/models/notification.go +++ b/server/models/notification.go @@ -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