Added deployments overview
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"log"
|
||||
"quay/app/models"
|
||||
"quay/app/repository"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
type DeploymentHandler struct {
|
||||
Repo repository.DeploymentRepository
|
||||
}
|
||||
|
||||
func NewDeploymentHandler(repo repository.DeploymentRepository) *DeploymentHandler {
|
||||
return &DeploymentHandler{Repo: repo}
|
||||
}
|
||||
|
||||
// GetDeployment godoc
|
||||
// @Summary Get deployment by ID
|
||||
// @Description Get a single deployment by its ID
|
||||
// @Tags Deployments
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path string true "Deployment ID"
|
||||
// @Success 200 {object} models.Deployment
|
||||
// @Failure 404 {object} models.APIError
|
||||
// @Failure 500 {object} models.APIError
|
||||
// @Router /deployments/{id} [get]
|
||||
func (h *DeploymentHandler) GetDeployment(c fiber.Ctx) error {
|
||||
id := c.Params("id")
|
||||
|
||||
deployment, err := h.Repo.GetDeploymentByID(id)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return c.Status(fiber.StatusNotFound).JSON(&models.APIError{
|
||||
Message: "Deployment not found",
|
||||
})
|
||||
}
|
||||
log.Println("Error getting deployment by id: ", err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(&models.APIError{
|
||||
Message: "Unexpected error while getting deployment by id",
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(&deployment)
|
||||
}
|
||||
|
||||
// GetDeploymentsBySite godoc
|
||||
// @Summary Get deployments for a site
|
||||
// @Description Get a list of deployments for a specific site
|
||||
// @Tags Deployments
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param siteId path string true "Site ID"
|
||||
// @Param limit query int false "Maximum number of deployments to return" default(100)
|
||||
// @Success 200 {object} models.GetDeploymentsBySiteResponse
|
||||
// @Failure 500 {object} models.APIError
|
||||
// @Router /sites/{siteId}/deployments [get]
|
||||
func (h *DeploymentHandler) GetDeploymentsBySite(c fiber.Ctx) error {
|
||||
siteId := c.Params("id")
|
||||
limit, err := strconv.Atoi(c.Query("limit", "100"))
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(&models.APIError{
|
||||
Message: "Invalid limit",
|
||||
})
|
||||
}
|
||||
|
||||
deployments, err := h.Repo.GetDeploymentsForSite(siteId, limit)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return c.JSON(models.GetDeploymentsBySiteResponse{
|
||||
Deployments: []models.Deployment{},
|
||||
Total: 0,
|
||||
})
|
||||
}
|
||||
|
||||
log.Println("Error getting deployments: ", err)
|
||||
return c.JSON(models.APIError{
|
||||
Message: "Unexpected error while getting deployments: ",
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(models.GetDeploymentsBySiteResponse{
|
||||
Deployments: deployments,
|
||||
Total: len(deployments),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user