Files
backend/internal/server/handlers_audit.go

30 lines
697 B
Go
Raw Normal View History

package server
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"code.littlelan.cn/CarrotAssistant/backend/internal/store"
)
// GET /admin/audit-logs — optional ?app_id= and ?action= filters, ?limit=
// capped at 500 for safety.
func (d Deps) handleListAuditLogs(c *gin.Context) {
q := d.DB.Model(&store.AuditLog{}).Order("id DESC")
if a := c.Query("app_id"); a != "" {
q = q.Where("app_id = ?", a)
}
if act := c.Query("action"); act != "" {
q = q.Where("action = ?", act)
}
limit := 200
if n, err := strconv.Atoi(c.Query("limit")); err == nil && n > 0 && n <= 500 {
limit = n
}
var logs []store.AuditLog
q.Limit(limit).Find(&logs)
c.JSON(http.StatusOK, logs)
}