76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
cairnapi "github.com/mattnite/cairn/internal/api"
|
|
"github.com/mattnite/cairn/internal/models"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type ArtifactHandler struct {
|
|
DB *gorm.DB
|
|
}
|
|
|
|
type ArtifactListResponse struct {
|
|
Artifacts []cairnapi.Artifact `json:"artifacts"`
|
|
Total int64 `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
|
|
}
|
|
|
|
repoID, err := parseOptionalUintID(c.Query("repository_id"), "repository_id")
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
artifacts, total, err := models.ListArtifacts(c.Request.Context(), h.DB, models.ListArtifactsParams{
|
|
RepositoryID: repoID,
|
|
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 = []cairnapi.Artifact{}
|
|
}
|
|
|
|
c.JSON(http.StatusOK, ArtifactListResponse{
|
|
Artifacts: artifacts,
|
|
Total: total,
|
|
Limit: limit,
|
|
Offset: offset,
|
|
})
|
|
}
|
|
|
|
func (h *ArtifactHandler) Detail(c *gin.Context) {
|
|
id, err := parseUintID(c.Param("id"), "artifact id")
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
artifact, err := models.GetArtifact(c.Request.Context(), h.DB, id)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "artifact not found"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, artifact)
|
|
}
|