diff options
| author | Bobby <[email protected]> | 2025-12-24 17:17:15 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2025-12-24 17:17:15 +0530 |
| commit | d5ea2aa824eee4b7e2d169d21da0107d057e7bc6 (patch) | |
| tree | e608fea8cf91d6915b7b6ce5eb46896dbdc2ad79 /controllers | |
| parent | b77d75f05fb2059389c05f6c01484e0cd12e796e (diff) | |
| download | lain-d5ea2aa824eee4b7e2d169d21da0107d057e7bc6.tar.xz lain-d5ea2aa824eee4b7e2d169d21da0107d057e7bc6.zip | |
feat: Implement API endpoints for email details and actions, and refactor email preview for client-side rendering with Shadow DOM.
Diffstat (limited to 'controllers')
| -rw-r--r-- | controllers/api.go | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/controllers/api.go b/controllers/api.go new file mode 100644 index 0000000..d952f88 --- /dev/null +++ b/controllers/api.go @@ -0,0 +1,66 @@ +package controllers + +import ( + "lain/services" + "lain/session" + "strconv" + + "github.com/gofiber/fiber/v2" +) + +func GetEmailAPI(context *fiber.Ctx) error { + emailID, err := strconv.ParseUint(context.Params("id"), 10, 32) + if err != nil { + return context.Status(400).JSON(fiber.Map{"error": "Invalid email ID"}) + } + + userEmail, err := session.GetSessionEmail(context) + if err != nil { + return context.Status(401).JSON(fiber.Map{"error": "Unauthorized"}) + } + + email, err := services.GetEmailDetails(userEmail, uint(emailID)) + if err != nil { + return context.Status(404).JSON(fiber.Map{"error": "Email not found"}) + } + + return context.JSON(email) +} + +func ToggleFlagAPI(context *fiber.Ctx) error { + emailID, err := strconv.ParseUint(context.Params("id"), 10, 32) + if err != nil { + return context.Status(400).JSON(fiber.Map{"error": "Invalid email ID"}) + } + + userEmail, err := session.GetSessionEmail(context) + if err != nil { + return context.Status(401).JSON(fiber.Map{"error": "Unauthorized"}) + } + + isFlagged, err := services.ToggleEmailFlag(userEmail, uint(emailID)) + if err != nil { + return context.Status(500).JSON(fiber.Map{"error": "Failed to toggle flag"}) + } + + return context.JSON(fiber.Map{"flagged": isFlagged}) +} + +func MarkEmailAsReadAPI(context *fiber.Ctx) error { + emailID, err := strconv.ParseUint(context.Params("id"), 10, 32) + if err != nil { + return context.Status(400).JSON(fiber.Map{"error": "Invalid email ID"}) + } + + userEmail, err := session.GetSessionEmail(context) + if err != nil { + return context.Status(401).JSON(fiber.Map{"error": "Unauthorized"}) + } + + err = services.MarkEmailAsRead(userEmail, uint(emailID)) + if err != nil { + return context.Status(500).JSON(fiber.Map{"error": "Failed to mark as read"}) + } + + return context.JSON(fiber.Map{"success": true}) +} |
