-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: notification channels(discord) and apis
- Loading branch information
1 parent
830618c
commit 3a61fdd
Showing
27 changed files
with
2,164 additions
and
101 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
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,181 @@ | ||
package controllers | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/chamanbravo/upstat/queries" | ||
"github.com/chamanbravo/upstat/serializers" | ||
"github.com/chamanbravo/upstat/utils" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
// @Tags Notifications | ||
// @Accept json | ||
// @Produce json | ||
// @Param body body serializers.NotificationCreateIn true "Body" | ||
// @Success 200 {object} serializers.SuccessResponse | ||
// @Failure 400 {object} serializers.ErrorResponse | ||
// @Router /api/notifications/create [post] | ||
func CreateNotification(c *fiber.Ctx) error { | ||
notificationChannel := new(serializers.NotificationCreateIn) | ||
if err := c.BodyParser(notificationChannel); err != nil { | ||
fmt.Print(notificationChannel) | ||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ | ||
"message": err.Error(), | ||
}) | ||
} | ||
|
||
errors := utils.BodyValidator.Validate(notificationChannel) | ||
if len(errors) > 0 { | ||
return c.Status(400).JSON(errors) | ||
} | ||
|
||
err := queries.CreateNotificationChannel(notificationChannel) | ||
if err != nil { | ||
return c.Status(500).JSON(fiber.Map{ | ||
"errors": err.Error(), | ||
}) | ||
} | ||
|
||
return c.JSON(fiber.Map{ | ||
"message": "Notification channel created.", | ||
}) | ||
} | ||
|
||
// @Tags Notifications | ||
// @Accept json | ||
// @Produce json | ||
// @Success 200 {object} serializers.NotificationListOut | ||
// @Failure 400 {object} serializers.ErrorResponse | ||
// @Router /api/notifications/list [get] | ||
func ListNotificationsChannel(c *fiber.Ctx) error { | ||
notifications, err := queries.ListNotificationChannel() | ||
if err != nil { | ||
return c.Status(500).JSON(fiber.Map{ | ||
"errors": err.Error(), | ||
}) | ||
} | ||
|
||
return c.JSON(fiber.Map{ | ||
"message": "Notifications channels list", | ||
"notifications": notifications, | ||
}) | ||
} | ||
|
||
// @Accept json | ||
// @Produce json | ||
// @Param id path string true "Notification Channel ID" | ||
// @Success 200 {object} serializers.SuccessResponse | ||
// @Failure 400 {object} serializers.ErrorResponse | ||
// @Router /api/notifications/delete/{id} [delete] | ||
func DeleteNotificationChannel(c *fiber.Ctx) error { | ||
idParam := c.Params("id") | ||
if idParam == "" { | ||
return c.Status(400).JSON(fiber.Map{ | ||
"message": "ID parameter is missing", | ||
}) | ||
} | ||
|
||
id, err := strconv.Atoi(idParam) | ||
if err != nil { | ||
return c.Status(400).JSON(fiber.Map{ | ||
"message": "Invalid ID parameter", | ||
}) | ||
} | ||
|
||
err = queries.DeleteNotificationChannel(id) | ||
if err != nil { | ||
return c.JSON(fiber.Map{ | ||
"error": err.Error(), | ||
"message": "Unable to delete a notification channel", | ||
}) | ||
} | ||
|
||
return c.JSON(fiber.Map{ | ||
"message": "Notifications channels deleted", | ||
}) | ||
} | ||
|
||
// @Tags Notifications | ||
// @Accept json | ||
// @Produce json | ||
// @Param id path string true "Notification Channel ID" | ||
// @Param body body serializers.NotificationCreateIn true "Body" | ||
// @Success 200 {object} serializers.SuccessResponse | ||
// @Success 400 {object} serializers.ErrorResponse | ||
// @Router /api/notifications/update/{id} [put] | ||
func UpdateNotificationChannel(c *fiber.Ctx) error { | ||
idParam := c.Params("id") | ||
if idParam == "" { | ||
return c.Status(400).JSON(fiber.Map{ | ||
"message": "ID parameter is missing", | ||
}) | ||
} | ||
|
||
notificationChannel := new(serializers.NotificationCreateIn) | ||
if err := c.BodyParser(notificationChannel); err != nil { | ||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ | ||
"message": err.Error(), | ||
}) | ||
} | ||
|
||
errors := utils.BodyValidator.Validate(notificationChannel) | ||
if len(errors) > 0 { | ||
return c.Status(400).JSON(errors) | ||
} | ||
|
||
id, err := strconv.Atoi(idParam) | ||
if err != nil { | ||
return c.Status(400).JSON(fiber.Map{ | ||
"message": "Invalid ID parameter", | ||
}) | ||
} | ||
|
||
err = queries.UpdateNotificationById(id, notificationChannel) | ||
if err != nil { | ||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ | ||
"message": err.Error(), | ||
}) | ||
} | ||
|
||
return c.Status(200).JSON(fiber.Map{ | ||
"message": "Successfully updated.", | ||
}) | ||
|
||
} | ||
|
||
// @Tags Notifications | ||
// @Accept json | ||
// @Produce json | ||
// @Param id path string true "Notification Channel ID" | ||
// @Success 200 {object} serializers.NotificationChannelInfo | ||
// @Success 400 {object} serializers.ErrorResponse | ||
// @Router /api/notifications/info/{id} [get] | ||
func NotificationChannelInfo(c *fiber.Ctx) error { | ||
idParam := c.Params("id") | ||
if idParam == "" { | ||
return c.Status(400).JSON(fiber.Map{ | ||
"message": "ID parameter is missing", | ||
}) | ||
} | ||
|
||
id, err := strconv.Atoi(idParam) | ||
if err != nil { | ||
return c.Status(400).JSON(fiber.Map{ | ||
"message": "Invalid ID parameter", | ||
}) | ||
} | ||
|
||
notification, err := queries.FindNotificationById(id) | ||
if err != nil { | ||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ | ||
"message": err.Error(), | ||
}) | ||
} | ||
|
||
return c.Status(200).JSON(fiber.Map{ | ||
"message": "success", | ||
"notification": notification, | ||
}) | ||
} |
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
Oops, something went wrong.