cairn/internal/handler/search.go

65 lines
1.4 KiB
Go

package handler
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/mattnite/cairn/internal/models"
)
type ArtifactHandler struct {
Pool *pgxpool.Pool
}
type ArtifactListResponse struct {
Artifacts []models.Artifact `json:"artifacts"`
Total int `json:"total"`
Limit int `json:"limit"`
Offset int `json:"offset"`
}
func (h *ArtifactHandler) List(c *gin.Context) {
limit, _ := strconv.Atoi(c.Query("limit"))
offset, _ := strconv.Atoi(c.Query("offset"))
if limit <= 0 {
limit = 50
}
artifacts, total, err := models.ListArtifacts(c.Request.Context(), h.Pool, models.ListArtifactsParams{
RepositoryID: c.Query("repository_id"),
CommitSHA: c.Query("commit_sha"),
Type: c.Query("type"),
Limit: limit,
Offset: offset,
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if artifacts == nil {
artifacts = []models.Artifact{}
}
c.JSON(http.StatusOK, ArtifactListResponse{
Artifacts: artifacts,
Total: total,
Limit: limit,
Offset: offset,
})
}
func (h *ArtifactHandler) Detail(c *gin.Context) {
id := c.Param("id")
artifact, err := models.GetArtifact(c.Request.Context(), h.Pool, id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "artifact not found"})
return
}
c.JSON(http.StatusOK, artifact)
}