update
This commit is contained in:
111
internal/handler/history_handler.go
Normal file
111
internal/handler/history_handler.go
Normal file
@ -0,0 +1,111 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"fashionapi/internal/dto"
|
||||
"fashionapi/internal/middleware"
|
||||
"fashionapi/internal/pkg/response"
|
||||
"fashionapi/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// HistoryHandler 浏览历史接口(需 Bearer 鉴权,挂在 /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/auth/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
|
||||
}
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
perPage, _ := strconv.Atoi(c.DefaultQuery("per_page", "24"))
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if perPage < 1 || perPage > 100 {
|
||||
perPage = 24
|
||||
}
|
||||
items, total, err := h.hist.ListPaged(c.Request.Context(), userID, page, perPage)
|
||||
if err != nil {
|
||||
fail(c, err)
|
||||
return
|
||||
}
|
||||
response.Data(c, http.StatusOK, gin.H{
|
||||
"items": items,
|
||||
"total": total,
|
||||
"last_page": response.LastPage(total, perPage),
|
||||
})
|
||||
}
|
||||
|
||||
// Record 记录一次浏览(点开文章详情;upsert:同文章刷新时间,不新增行)。
|
||||
//
|
||||
// POST /api/v1/auth/history
|
||||
// body: { target_uid } // 带 r=/s= 前缀的对外编码串
|
||||
// 成功: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/auth/history/:target_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("target_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/auth/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})
|
||||
}
|
||||
Reference in New Issue
Block a user