package api import ( "net/http" "strconv" "household-book/service" "github.com/gin-gonic/gin" ) type DashboardHandler struct { svc *service.DashboardService } func NewDashboardHandler(svc *service.DashboardService) *DashboardHandler { return &DashboardHandler{svc: svc} } func (h *DashboardHandler) Summary(c *gin.Context) { year := c.Query("year") month := c.Query("month") summary, err := h.svc.Summary(c.Request.Context(), year, month) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "获取摘要数据失败"}) return } c.JSON(http.StatusOK, summary) } func (h *DashboardHandler) Recent(c *gin.Context) { limit := 5 if l := c.Query("limit"); l != "" { if parsed, err := strconv.Atoi(l); err == nil && parsed > 0 && parsed <= 50 { limit = parsed } } items, err := h.svc.Recent(c.Request.Context(), limit) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "获取最近记录失败"}) return } c.JSON(http.StatusOK, items) }