cairn/internal/handler/crashgroups.go

98 lines
2.0 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 CrashGroupHandler struct {
Pool *pgxpool.Pool
}
type CrashGroupListResponse struct {
CrashGroups []models.CrashGroup `json:"crash_groups"`
Total int `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
}
groups, total, err := models.ListCrashGroups(
c.Request.Context(), h.Pool,
c.Query("repository_id"), c.Query("status"),
limit, offset,
)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if groups == nil {
groups = []models.CrashGroup{}
}
c.JSON(http.StatusOK, CrashGroupListResponse{
CrashGroups: groups,
Total: total,
Limit: limit,
Offset: offset,
})
}
func (h *CrashGroupHandler) Detail(c *gin.Context) {
id := c.Param("id")
group, err := models.GetCrashGroup(c.Request.Context(), h.Pool, id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "crash group not found"})
return
}
c.JSON(http.StatusOK, group)
}
type SearchHandler struct {
Pool *pgxpool.Pool
}
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.Pool, q, limit, offset)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if artifacts == nil {
artifacts = []models.Artifact{}
}
c.JSON(http.StatusOK, gin.H{
"artifacts": artifacts,
"total": total,
"limit": limit,
"offset": offset,
})
}