Skip to content

Commit

Permalink
feat: notification channels(discord) and apis
Browse files Browse the repository at this point in the history
  • Loading branch information
chamanbravo committed Feb 21, 2024
1 parent 830618c commit 3a61fdd
Show file tree
Hide file tree
Showing 27 changed files with 2,164 additions and 101 deletions.
52 changes: 52 additions & 0 deletions controllers/monitor_controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"fmt"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -38,6 +39,13 @@ func CreateMonitor(c *fiber.Ctx) error {
})
}

err = queries.NotificationMonitor(monitor.ID, newMonitor.NotificationChannels)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": err.Error(),
})
}

utils.StartGoroutine(monitor)

return c.Status(200).JSON(fiber.Map{
Expand Down Expand Up @@ -122,6 +130,13 @@ func UpdateMonitor(c *fiber.Ctx) error {
})
}

err = queries.UpdateNotificationMonitorById(id, monitor.NotificationChannels)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": err.Error(),
})
}

return c.Status(200).JSON(fiber.Map{
"message": "success",
})
Expand Down Expand Up @@ -420,3 +435,40 @@ func CertificateExpiryCountDown(c *fiber.Ctx) error {
"daysUntilExpiration": daysUnitlExp,
})
}

// @Tags Monitors
// @Accept json
// @Produce json
// @Param id path string true "Monitor ID"
// @Success 200 {object} serializers.NotificationListOut
// @Success 400 {object} serializers.ErrorResponse
// @Router /api/monitors/{id}/notifications [get]
func NotificationChannelListOfMonitor(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.FindNotificationChannelsByMonitorId(id)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": err.Error(),
})
}

fmt.Println(notification)

return c.Status(200).JSON(fiber.Map{
"message": "success",
"notifications": notification,
})
}
181 changes: 181 additions & 0 deletions controllers/notifications_controller.go
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,
})
}
4 changes: 2 additions & 2 deletions database/migrations/20231229140812_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ CREATE TABLE notifications (
name VARCHAR(32) NOT NULL,
provider VARCHAR(50) NOT NULL,
data json NOT NULL
)
);

CREATE TABLE notifications_monitors (
monitor_id INTEGER REFERENCES monitors(id) ON DELETE CASCADE NOT NULL,
notification_id INTEGER REFERENCES notifications(id) ON DELETE CASCADE NOT NULL,
PRIMARY KEY (monitor_id, notification_id)
)
);
-- +goose StatementEnd

-- +goose Down
Expand Down
Loading

0 comments on commit 3a61fdd

Please sign in to comment.