package handler import ( "net/http" "fashionapi/internal/dto" "fashionapi/internal/middleware" "fashionapi/internal/pkg/pagination" "fashionapi/internal/pkg/response" "fashionapi/internal/service" "github.com/gin-gonic/gin" ) // HistoryHandler 浏览历史接口(需 Bearer 鉴权,主挂载于 /api/v1/me,legacy /api/v1/auth 仅兼容旧前端)。 type HistoryHandler struct { hist service.HistoryService } // NewHistoryHandler 创建浏览历史 handler。 func NewHistoryHandler(hist service.HistoryService) *HistoryHandler { return &HistoryHandler{hist: hist} } // List 当前用户浏览历史(服务端分页)。 // // GET /api/v1/me/history?page=1&per_page=24 // 成功:200 { data: { items:[...], total, last_page } } func (h *HistoryHandler) List(c *gin.Context) { userID, ok := middleware.UserIDFrom(c) if !ok { response.Error(c, http.StatusUnauthorized, "unauthorized") return } p := pagination.Parse(c, "page", "per_page", 1, 24, 100) items, total, err := h.hist.ListPaged(c.Request.Context(), userID, p.Page, p.PageSize) if err != nil { fail(c, err) return } response.Data(c, http.StatusOK, gin.H{ "items": items, "total": total, "last_page": response.LastPage(total, p.PageSize), }) } // Record 记录一次浏览(点开文章详情;upsert:同文章刷新时间,不新增行)。 // // POST /api/v1/me/history // body: { target_uid } // 类型进密码、无前缀的对外编码串 // 成功:200 { data: { ok: true } } func (h *HistoryHandler) Record(c *gin.Context) { userID, ok := middleware.UserIDFrom(c) if !ok { response.Error(c, http.StatusUnauthorized, "unauthorized") return } var req dto.HistoryAdd if err := c.ShouldBindJSON(&req); err != nil { response.Error(c, http.StatusBadRequest, "invalid request") return } if err := h.hist.Record(c.Request.Context(), userID, req); err != nil { fail(c, err) return } response.Data(c, http.StatusOK, gin.H{"ok": true}) } // Remove 删除一条历史。 // // DELETE /api/v1/me/history/:uid // 成功:200 { data: { ok: true } } func (h *HistoryHandler) Remove(c *gin.Context) { userID, ok := middleware.UserIDFrom(c) if !ok { response.Error(c, http.StatusUnauthorized, "unauthorized") return } uid := c.Param("uid") if err := h.hist.Remove(c.Request.Context(), userID, uid); err != nil { fail(c, err) return } response.Data(c, http.StatusOK, gin.H{"ok": true}) } // Clear 清空当前用户全部历史。 // // DELETE /api/v1/me/history // 成功:200 { data: { ok: true } } func (h *HistoryHandler) Clear(c *gin.Context) { userID, ok := middleware.UserIDFrom(c) if !ok { response.Error(c, http.StatusUnauthorized, "unauthorized") return } if err := h.hist.Clear(c.Request.Context(), userID); err != nil { fail(c, err) return } response.Data(c, http.StatusOK, gin.H{"ok": true}) }