- Remove BOM from all Go source files - Standardize import ordering and whitespace across handlers and services - Replace PostgreSQL full-text search with ILIKE pattern matching in post and user repositories - Enhance JPush client with TLS transport, rate limit monitoring, and simplified API - Refactor PushService to include userID in device management methods - Add MaxDevicesPerUser limit and extract helper functions for push payload construction
221 lines
5.1 KiB
Go
221 lines
5.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"with_you/internal/dto"
|
|
"with_you/internal/model"
|
|
"with_you/internal/pkg/response"
|
|
"with_you/internal/service"
|
|
)
|
|
|
|
type TradeHandler struct {
|
|
tradeService service.TradeService
|
|
}
|
|
|
|
func NewTradeHandler(tradeService service.TradeService) *TradeHandler {
|
|
return &TradeHandler{tradeService: tradeService}
|
|
}
|
|
|
|
func (h *TradeHandler) List(c *gin.Context) {
|
|
currentUserID := c.GetString("user_id")
|
|
if currentUserID == "" {
|
|
currentUserID = ""
|
|
}
|
|
|
|
var tradeType *model.TradeType
|
|
if v := c.Query("trade_type"); v != "" {
|
|
t := model.TradeType(v)
|
|
tradeType = &t
|
|
}
|
|
|
|
var category *model.TradeCategory
|
|
if v := c.Query("category"); v != "" {
|
|
cat := model.TradeCategory(v)
|
|
category = &cat
|
|
}
|
|
|
|
cursorStr := c.Query("cursor")
|
|
pageSize := 20
|
|
if v := c.Query("page_size"); v != "" {
|
|
if ps, err := strconv.Atoi(v); err == nil && ps > 0 && ps <= 100 {
|
|
pageSize = ps
|
|
}
|
|
}
|
|
|
|
keyword := c.Query("keyword")
|
|
|
|
var currentUserIDPtr *string
|
|
if currentUserID != "" {
|
|
currentUserIDPtr = ¤tUserID
|
|
}
|
|
|
|
var resp *dto.TradeCursorPageResponse
|
|
var err error
|
|
if keyword != "" {
|
|
resp, err = h.tradeService.Search(c.Request.Context(), keyword, tradeType, category, cursorStr, pageSize, currentUserIDPtr)
|
|
} else {
|
|
resp, err = h.tradeService.List(c.Request.Context(), tradeType, category, cursorStr, pageSize, currentUserIDPtr)
|
|
}
|
|
if err != nil {
|
|
response.HandleError(c, err, "获取交易列表失败")
|
|
return
|
|
}
|
|
response.Success(c, resp)
|
|
}
|
|
|
|
func (h *TradeHandler) GetByID(c *gin.Context) {
|
|
id := c.Param("id")
|
|
currentUserID := c.GetString("user_id")
|
|
|
|
var currentUserIDPtr *string
|
|
if currentUserID != "" {
|
|
currentUserIDPtr = ¤tUserID
|
|
}
|
|
|
|
resp, err := h.tradeService.GetByID(c.Request.Context(), id, currentUserIDPtr)
|
|
if err != nil {
|
|
response.HandleError(c, err, "获取交易详情失败")
|
|
return
|
|
}
|
|
response.Success(c, resp)
|
|
}
|
|
|
|
func (h *TradeHandler) Create(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "请先登录")
|
|
return
|
|
}
|
|
|
|
var req dto.CreateTradeItemRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
|
|
item, err := h.tradeService.Create(c.Request.Context(), userID, req)
|
|
if err != nil {
|
|
response.HandleError(c, err, "创建交易失败")
|
|
return
|
|
}
|
|
|
|
var currentUserIDPtr *string
|
|
if userID != "" {
|
|
currentUserIDPtr = &userID
|
|
}
|
|
detail, err := h.tradeService.GetByID(c.Request.Context(), item.ID, currentUserIDPtr)
|
|
if err != nil {
|
|
response.Success(c, item)
|
|
return
|
|
}
|
|
response.Success(c, detail)
|
|
}
|
|
|
|
func (h *TradeHandler) Update(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "请先登录")
|
|
return
|
|
}
|
|
|
|
id := c.Param("id")
|
|
|
|
var req dto.UpdateTradeItemRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
|
|
if err := h.tradeService.Update(c.Request.Context(), userID, id, req); err != nil {
|
|
response.HandleError(c, err, "更新交易失败")
|
|
return
|
|
}
|
|
|
|
var currentUserIDPtr *string
|
|
if userID != "" {
|
|
currentUserIDPtr = &userID
|
|
}
|
|
detail, err := h.tradeService.GetByID(c.Request.Context(), id, currentUserIDPtr)
|
|
if err != nil {
|
|
response.Success(c, nil)
|
|
return
|
|
}
|
|
response.Success(c, detail)
|
|
}
|
|
|
|
func (h *TradeHandler) Delete(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "请先登录")
|
|
return
|
|
}
|
|
|
|
id := c.Param("id")
|
|
if err := h.tradeService.Delete(c.Request.Context(), userID, id); err != nil {
|
|
response.HandleError(c, err, "删除交易失败")
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
func (h *TradeHandler) UpdateStatus(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "请先登录")
|
|
return
|
|
}
|
|
|
|
id := c.Param("id")
|
|
|
|
var req dto.UpdateTradeItemStatusRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, err.Error())
|
|
return
|
|
}
|
|
|
|
if err := h.tradeService.UpdateStatus(c.Request.Context(), userID, id, model.TradeItemStatus(req.Status)); err != nil {
|
|
response.HandleError(c, err, "更新交易状态失败")
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
func (h *TradeHandler) RecordView(c *gin.Context) {
|
|
id := c.Param("id")
|
|
_ = h.tradeService.RecordView(c.Request.Context(), id)
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
func (h *TradeHandler) Favorite(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "请先登录")
|
|
return
|
|
}
|
|
|
|
id := c.Param("id")
|
|
if err := h.tradeService.Favorite(c.Request.Context(), userID, id); err != nil {
|
|
response.HandleError(c, err, "收藏失败")
|
|
return
|
|
}
|
|
response.Success(c, dto.TradeFavoriteResponse{Favorited: true})
|
|
}
|
|
|
|
func (h *TradeHandler) Unfavorite(c *gin.Context) {
|
|
userID := c.GetString("user_id")
|
|
if userID == "" {
|
|
response.Unauthorized(c, "请先登录")
|
|
return
|
|
}
|
|
|
|
id := c.Param("id")
|
|
if err := h.tradeService.Unfavorite(c.Request.Context(), userID, id); err != nil {
|
|
response.HandleError(c, err, "取消收藏失败")
|
|
return
|
|
}
|
|
response.Success(c, dto.TradeFavoriteResponse{Favorited: false})
|
|
}
|