-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): add update notifications and get unread notification count
- Loading branch information
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package me | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/lareii/copl.uk/server/models" | ||
) | ||
|
||
func GetUnreadNotifications(c *fiber.Ctx) error { | ||
user, ok := c.Locals("user").(models.User) | ||
if !ok { | ||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ | ||
"message": "User not authenticated.", | ||
}) | ||
} | ||
|
||
unreads, err := models.GetUnreadNotifications(user.ID) | ||
if err != nil { | ||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ | ||
"message": "Error fetching unread notifications.", | ||
}) | ||
} | ||
|
||
return c.Status(fiber.StatusOK).JSON(fiber.Map{ | ||
"message": "Unread notifications fetched.", | ||
"unreads": unreads, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package me | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/lareii/copl.uk/server/models" | ||
"github.com/lareii/copl.uk/server/utils" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
) | ||
|
||
type UpdateNotificationBody struct { | ||
Read *bool `json:"read" validate:"required"` | ||
} | ||
|
||
func UpdateNotification(c *fiber.Ctx) error { | ||
user, ok := c.Locals("user").(models.User) | ||
if !ok { | ||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ | ||
"message": "User not authenticated.", | ||
}) | ||
} | ||
|
||
var body UpdateNotificationBody | ||
if err := c.BodyParser(&body); err != nil { | ||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ | ||
"message": "Invalid request body.", | ||
}) | ||
} | ||
|
||
if err := utils.Validate.Struct(&body); err != nil { | ||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ | ||
"message": "Missing or invalid fields.", | ||
}) | ||
} | ||
|
||
id := c.Params("id") | ||
notificationID, err := primitive.ObjectIDFromHex(id) | ||
if err != nil { | ||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ | ||
"message": "Invalid notification ID.", | ||
}) | ||
} | ||
|
||
notification, err := models.GetNotificationByID(notificationID) | ||
if err != nil { | ||
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{ | ||
"message": "Error fetching notification.", | ||
}) | ||
} | ||
|
||
if notification.TargetUserID != user.ID { | ||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ | ||
"message": "User not authorized.", | ||
}) | ||
} | ||
|
||
err = models.UpdateNotification(notificationID, *body.Read) | ||
if err != nil { | ||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ | ||
"message": "Error updating notification.", | ||
}) | ||
} | ||
|
||
return c.Status(fiber.StatusOK).JSON(fiber.Map{ | ||
"message": "Notification updated.", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters