mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
feat: add admin log deletion
This commit is contained in:
@@ -161,6 +161,62 @@ type LogStatsResponse struct {
|
||||
ByStatus map[string]int64 `json:"by_status"`
|
||||
}
|
||||
|
||||
type DeleteLogsRequest struct {
|
||||
Before string `json:"before"`
|
||||
KeyID uint `json:"key_id"`
|
||||
Model string `json:"model"`
|
||||
}
|
||||
|
||||
type DeleteLogsResponse struct {
|
||||
DeletedCount int64 `json:"deleted_count"`
|
||||
}
|
||||
|
||||
// DeleteLogs godoc
|
||||
// @Summary Delete logs (admin)
|
||||
// @Description Delete logs before a given timestamp with optional filters
|
||||
// @Tags admin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param request body DeleteLogsRequest true "Delete filters"
|
||||
// @Success 200 {object} DeleteLogsResponse
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/logs [delete]
|
||||
func (h *Handler) DeleteLogs(c *gin.Context) {
|
||||
var req DeleteLogsRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
before := strings.TrimSpace(req.Before)
|
||||
if before == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "before is required"})
|
||||
return
|
||||
}
|
||||
ts, err := time.Parse(time.RFC3339, before)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid before time"})
|
||||
return
|
||||
}
|
||||
|
||||
q := h.db.Unscoped().Where("created_at < ?", ts.UTC())
|
||||
if req.KeyID > 0 {
|
||||
q = q.Where("key_id = ?", req.KeyID)
|
||||
}
|
||||
if model := strings.TrimSpace(req.Model); model != "" {
|
||||
q = q.Where("model_name = ?", model)
|
||||
}
|
||||
|
||||
res := q.Delete(&model.LogRecord{})
|
||||
if res.Error != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to delete logs", "details": res.Error.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, DeleteLogsResponse{DeletedCount: res.RowsAffected})
|
||||
}
|
||||
|
||||
// LogStats godoc
|
||||
// @Summary Log stats (admin)
|
||||
// @Description Aggregate log stats with basic filtering
|
||||
|
||||
Reference in New Issue
Block a user