Skip to content

Commit

Permalink
feat: status-pages api
Browse files Browse the repository at this point in the history
  • Loading branch information
chamanbravo committed Feb 25, 2024
1 parent f24f85d commit c707fe6
Show file tree
Hide file tree
Showing 12 changed files with 1,222 additions and 3 deletions.
53 changes: 50 additions & 3 deletions controllers/monitor_controller.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package controllers

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

fmt.Println(newMonitor.StatusPages)
err = queries.StatusPageMonitor(monitor.ID, newMonitor.StatusPages)
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 @@ -137,6 +144,13 @@ func UpdateMonitor(c *fiber.Ctx) error {
})
}

err = queries.UpdateStatusPageMonitorById(id, monitor.StatusPages)
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 @@ -465,10 +479,43 @@ func NotificationChannelListOfMonitor(c *fiber.Ctx) error {
})
}

fmt.Println(notification)

return c.Status(200).JSON(fiber.Map{
"message": "success",
"notifications": notification,
})
}

// @Tags Monitors
// @Accept json
// @Produce json
// @Param id path string true "Monitor ID"
// @Success 200 {object} serializers.ListStatusPagesOut
// @Success 400 {object} serializers.ErrorResponse
// @Router /api/monitors/{id}/status-pages [get]
func StatusPagesListOfMonitor(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",
})
}

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

return c.Status(200).JSON(fiber.Map{
"message": "success",
"statusPages": statusPages,
})
}
179 changes: 179 additions & 0 deletions controllers/statuspage_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package controllers

import (
"strconv"

"github.com/chamanbravo/upstat/queries"
"github.com/chamanbravo/upstat/serializers"
"github.com/chamanbravo/upstat/utils"
"github.com/gofiber/fiber/v2"
)

// @Tags StatusPages
// @Accept json
// @Produce json
// @Param body body serializers.CreateStatusPageIn true "Body"
// @Success 200 {object} serializers.SuccessResponse
// @Success 400 {object} serializers.ErrorResponse
// @Router /api/status-pages/create [post]
func CreateStatusPage(c *fiber.Ctx) error {
statusPage := new(serializers.CreateStatusPageIn)
if err := c.BodyParser(statusPage); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"message": err.Error(),
})
}

errors := utils.BodyValidator.Validate(statusPage)
if len(errors) > 0 {
return c.Status(400).JSON(errors)
}

err := queries.CreateStatusPage(statusPage)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"message": err.Error(),
})
}

return c.Status(200).JSON(fiber.Map{
"message": "Success",
})
}

// @Tags StatusPages
// @Accept json
// @Produce json
// @Success 200 {object} serializers.ListStatusPagesOut
// @Failure 400 {object} serializers.ErrorResponse
// @Router /api/status-pages/list [get]
func ListStatusPages(c *fiber.Ctx) error {
statusPages, err := queries.ListStatusPages()
if err != nil {
return c.Status(500).JSON(fiber.Map{
"errors": err.Error(),
})
}

return c.JSON(fiber.Map{
"message": "Status pages list",
"statusPages": statusPages,
})
}

// @Accept json
// @Produce json
// @Param id path string true "Status Page ID"
// @Success 200 {object} serializers.SuccessResponse
// @Failure 400 {object} serializers.ErrorResponse
// @Router /api/status-pages/delete/{id} [delete]
func DeleteStatusPage(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.DeleteStatusPageById(id)
if err != nil {
return c.JSON(fiber.Map{
"error": err.Error(),
"message": "Unable to delete a status page",
})
}

return c.JSON(fiber.Map{
"message": "Status Page channels deleted",
})
}

// @Tags Notifications
// @Accept json
// @Produce json
// @Param id path string true "Status Page ID"
// @Param body body serializers.CreateStatusPageIn true "Body"
// @Success 200 {object} serializers.SuccessResponse
// @Success 400 {object} serializers.ErrorResponse
// @Router /api/status-pages/update/{id} [put]
func UpdateStatusPage(c *fiber.Ctx) error {
idParam := c.Params("id")
if idParam == "" {
return c.Status(400).JSON(fiber.Map{
"message": "ID parameter is missing",
})
}

statusPage := new(serializers.CreateStatusPageIn)
if err := c.BodyParser(statusPage); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"message": err.Error(),
})
}

errors := utils.BodyValidator.Validate(statusPage)
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.UpdateStatusPage(id, statusPage)
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 "Status Page ID"
// @Success 200 {object} serializers.StatusPageInfo
// @Success 400 {object} serializers.ErrorResponse
// @Router /api/status-pages/info/{id} [get]
func StatusPageInfo(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",
})
}

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

return c.Status(200).JSON(fiber.Map{
"message": "success",
"statusPage": statusPage,
})
}
14 changes: 14 additions & 0 deletions database/migrations/20231229140812_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ CREATE TABLE notifications_monitors (
notification_id INTEGER REFERENCES notifications(id) ON DELETE CASCADE NOT NULL,
PRIMARY KEY (monitor_id, notification_id)
);

CREATE TABLE status_pages (
id SERIAL PRIMARY KEY,
name VARCHAR(32) NOT NULL,
slug VARCHAR(32) NOT NULL
);

CREATE TABLE status_pages_monitors (
monitor_id INTEGER REFERENCES monitors(id) ON DELETE CASCADE NOT NULL,
status_pages_id INTEGER REFERENCES status_pages(id) ON DELETE CASCADE NOT NULL,
PRIMARY KEY (monitor_id, status_pages_id)
);
-- +goose StatementEnd

-- +goose Down
Expand All @@ -52,4 +64,6 @@ DROP TABLE monitors;
DROP TABLE heartbeats;
DROP TABLE notifications;
DROP TABLE notifications_monitors;
DROP TABLE status_pages;
DROP TABLE status_pages_monitors;
-- +goose StatementEnd
Loading

0 comments on commit c707fe6

Please sign in to comment.