feat(api): add model delete, pagination, and cors config

This commit is contained in:
zenfun
2025-12-21 23:03:12 +08:00
parent 816ea93339
commit 73147fc55a
12 changed files with 304 additions and 6 deletions

View File

@@ -369,12 +369,19 @@ func (h *Handler) CreateModel(c *gin.Context) {
// @Tags admin
// @Produce json
// @Security AdminAuth
// @Param page query int false "page (1-based)"
// @Param limit query int false "limit (default 50, max 200)"
// @Param search query string false "search by name/kind"
// @Success 200 {array} model.Model
// @Failure 500 {object} gin.H
// @Router /admin/models [get]
func (h *Handler) ListModels(c *gin.Context) {
var models []model.Model
if err := h.db.Find(&models).Error; err != nil {
q := h.db.Model(&model.Model{}).Order("id desc")
query := parseListQuery(c)
q = applyListSearch(q, query.Search, "name", "kind")
q = applyListPagination(q, query)
if err := q.Find(&models).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list models", "details": err.Error()})
return
}
@@ -457,6 +464,45 @@ func (h *Handler) UpdateModel(c *gin.Context) {
c.JSON(http.StatusOK, existing)
}
// DeleteModel godoc
// @Summary Delete a model
// @Description Delete a model by id
// @Tags admin
// @Produce json
// @Security AdminAuth
// @Param id path int true "Model ID"
// @Success 200 {object} gin.H
// @Failure 400 {object} gin.H
// @Failure 404 {object} gin.H
// @Failure 500 {object} gin.H
// @Router /admin/models/{id} [delete]
func (h *Handler) DeleteModel(c *gin.Context) {
idParam := c.Param("id")
id, err := strconv.Atoi(idParam)
if err != nil || id <= 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
return
}
var existing model.Model
if err := h.db.First(&existing, id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "model not found"})
return
}
if err := h.db.Delete(&existing).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete model", "details": err.Error()})
return
}
if err := h.sync.SyncModelDelete(&existing); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync model delete", "details": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"status": "deleted"})
}
// SyncSnapshot godoc
// @Summary Force sync snapshot
// @Description Force full synchronization of DB state to Redis