Also accept flexible card-order form values, move mock seed under db/mock, and regenerate Swagger. Co-authored-by: Cursor <cursoragent@cursor.com>
178 lines
4.5 KiB
Go
178 lines
4.5 KiB
Go
package activitylog
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"online-back/internal/appresult"
|
|
"online-back/internal/handlers"
|
|
"online-back/pkg/logging"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jackc/pgx/v4"
|
|
)
|
|
|
|
const baseURL = "/activity-logs"
|
|
|
|
type handler struct {
|
|
logger *logging.Logger
|
|
repository Repository
|
|
}
|
|
|
|
func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler {
|
|
return &handler{logger: logger, repository: repository}
|
|
}
|
|
|
|
// Register only wires read endpoints: activity log entries are written by
|
|
// the Record middleware, never through the API.
|
|
func (h *handler) Register(router *gin.RouterGroup) {
|
|
group := router.Group(baseURL, appresult.JwtTokenCheck())
|
|
group.GET("", h.list)
|
|
group.GET("/:id", h.getByID)
|
|
}
|
|
|
|
// list godoc
|
|
// @Summary List activity logs
|
|
// @Description Returns a paginated audit trail of user actions (create/update/delete). Each log includes the actor's username and role from role_user.
|
|
// @Tags activity-logs
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param user_id query int false "Filter by user ID"
|
|
// @Param module query string false "Filter by module (e.g. cards, branch-users)"
|
|
// @Param action query string false "Filter by action (create, update, delete)"
|
|
// @Param page query int false "Page number" default(1)
|
|
// @Param limit query int false "Items per page" default(10)
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /activity-logs [get]
|
|
func (h *handler) list(c *gin.Context) {
|
|
filter, ok := parseListFilter(c)
|
|
if !ok {
|
|
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
|
return
|
|
}
|
|
|
|
result, err := h.repository.List(c.Request.Context(), filter)
|
|
if err != nil {
|
|
h.logger.Errorf("list activity logs failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
|
|
}
|
|
|
|
// getByID godoc
|
|
// @Summary Get activity log by ID
|
|
// @Description Returns a single activity log entry
|
|
// @Tags activity-logs
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Activity log ID"
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 404 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /activity-logs/{id} [get]
|
|
func (h *handler) getByID(c *gin.Context) {
|
|
id, ok := parseID(c.Param("id"))
|
|
if !ok {
|
|
// GET /activity-logs/card-orders → list that module instead of 400.
|
|
q := c.Request.URL.Query()
|
|
if q.Get("module") == "" {
|
|
q.Set("module", c.Param("id"))
|
|
c.Request.URL.RawQuery = q.Encode()
|
|
}
|
|
h.list(c)
|
|
return
|
|
}
|
|
|
|
item, err := h.repository.GetByID(c.Request.Context(), id)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
|
return
|
|
}
|
|
if err != nil {
|
|
h.logger.Errorf("get activity log failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
func parseID(raw string) (int64, bool) {
|
|
id, err := strconv.ParseInt(raw, 10, 64)
|
|
if err != nil || id <= 0 {
|
|
return 0, false
|
|
}
|
|
return id, true
|
|
}
|
|
|
|
const (
|
|
defaultPage = 1
|
|
defaultLimit = 10
|
|
maxLimit = 100
|
|
)
|
|
|
|
func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
|
filter := ListFilter{
|
|
Page: defaultPage,
|
|
Limit: defaultLimit,
|
|
}
|
|
|
|
if id, ok := optionalQueryID(c, "user_id", "userId"); !ok {
|
|
return ListFilter{}, false
|
|
} else if id != nil {
|
|
filter.UserId = id
|
|
}
|
|
|
|
filter.Module = queryValue(c, "module")
|
|
filter.Action = queryValue(c, "action")
|
|
|
|
if raw := queryValue(c, "page"); raw != "" {
|
|
page, err := strconv.Atoi(raw)
|
|
if err != nil || page < 1 {
|
|
return ListFilter{}, false
|
|
}
|
|
filter.Page = page
|
|
}
|
|
|
|
if raw := queryValue(c, "limit"); raw != "" {
|
|
limit, err := strconv.Atoi(raw)
|
|
if err != nil || limit < 1 {
|
|
return ListFilter{}, false
|
|
}
|
|
if limit > maxLimit {
|
|
limit = maxLimit
|
|
}
|
|
filter.Limit = limit
|
|
}
|
|
|
|
return filter, true
|
|
}
|
|
|
|
func queryValue(c *gin.Context, keys ...string) string {
|
|
for _, key := range keys {
|
|
if raw := strings.TrimSpace(c.Query(key)); raw != "" && raw != "undefined" && raw != "null" {
|
|
return raw
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func optionalQueryID(c *gin.Context, keys ...string) (*int64, bool) {
|
|
raw := queryValue(c, keys...)
|
|
if raw == "" {
|
|
return nil, true
|
|
}
|
|
id, ok := parseID(raw)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
return &id, true
|
|
}
|