109 lines
2.3 KiB
Go
109 lines
2.3 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 CrashGroupHandler struct {
|
|
DB *gorm.DB
|
|
}
|
|
|
|
type CrashGroupListResponse struct {
|
|
CrashGroups []cairnapi.CrashGroup `json:"crash_groups"`
|
|
Total int64 `json:"total"`
|
|
Limit int `json:"limit"`
|
|
Offset int `json:"offset"`
|
|
}
|
|
|
|
func (h *CrashGroupHandler) 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
|
|
}
|
|
|
|
groups, total, err := models.ListCrashGroups(
|
|
c.Request.Context(), h.DB,
|
|
repoID, c.Query("status"),
|
|
limit, offset,
|
|
)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if groups == nil {
|
|
groups = []cairnapi.CrashGroup{}
|
|
}
|
|
|
|
c.JSON(http.StatusOK, CrashGroupListResponse{
|
|
CrashGroups: groups,
|
|
Total: total,
|
|
Limit: limit,
|
|
Offset: offset,
|
|
})
|
|
}
|
|
|
|
func (h *CrashGroupHandler) Detail(c *gin.Context) {
|
|
id, err := parseUintID(c.Param("id"), "crash group id")
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
group, err := models.GetCrashGroup(c.Request.Context(), h.DB, id)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "crash group not found"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, group)
|
|
}
|
|
|
|
type SearchHandler struct {
|
|
DB *gorm.DB
|
|
}
|
|
|
|
func (h *SearchHandler) Search(c *gin.Context) {
|
|
q := c.Query("q")
|
|
if q == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "missing 'q' parameter"})
|
|
return
|
|
}
|
|
|
|
limit, _ := strconv.Atoi(c.Query("limit"))
|
|
offset, _ := strconv.Atoi(c.Query("offset"))
|
|
if limit <= 0 {
|
|
limit = 50
|
|
}
|
|
|
|
artifacts, total, err := models.SearchArtifacts(c.Request.Context(), h.DB, q, limit, offset)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if artifacts == nil {
|
|
artifacts = []cairnapi.Artifact{}
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"artifacts": artifacts,
|
|
"total": total,
|
|
"limit": limit,
|
|
"offset": offset,
|
|
})
|
|
}
|