31ea370cea
- 修复 JWTConfig 缺少 mapstructure tag 导致 access_expire_min 解析为 0, token 签发即过期,所有 API 请求返回 401 - 全部 config struct 补齐 mapstructure tag(secret/dsn/hmac_secret 等) - 模型层从 hotel/HotelID 统一重命名为 shop/ShopID - 删除旧 migrations(001-004),新增 001_init 综合迁移文件 - 更新 schema.sql、testutil、handler/service/model 相关引用 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
115 lines
2.9 KiB
Go
115 lines
2.9 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/wangjia/jiu/backend/internal/middleware"
|
|
"github.com/wangjia/jiu/backend/internal/model"
|
|
)
|
|
|
|
type ProductHandler struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewProductHandler(db *gorm.DB) *ProductHandler {
|
|
return &ProductHandler{db: db}
|
|
}
|
|
|
|
// List GET /api/v1/products
|
|
func (h *ProductHandler) List(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
keyword := c.Query("keyword")
|
|
categoryID := c.Query("category_id")
|
|
|
|
query := h.db.Model(&model.Product{}).
|
|
Where("shop_id = ? AND deleted_at IS NULL", shopID)
|
|
|
|
if keyword != "" {
|
|
query = query.Where("name LIKE ? OR code LIKE ? OR barcode LIKE ?",
|
|
"%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%")
|
|
}
|
|
if categoryID != "" {
|
|
query = query.Where("category_id = ?", categoryID)
|
|
}
|
|
|
|
var total int64
|
|
query.Count(&total)
|
|
|
|
var products []model.Product
|
|
offset := (page - 1) * pageSize
|
|
query.Preload("Category").Offset(offset).Limit(pageSize).
|
|
Order("id DESC").Find(&products)
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"data": products,
|
|
"total": total,
|
|
"page": page,
|
|
"page_size": pageSize,
|
|
})
|
|
}
|
|
|
|
// Create POST /api/v1/products
|
|
func (h *ProductHandler) Create(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
var product model.Product
|
|
if err := c.ShouldBindJSON(&product); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
product.ShopID = shopID
|
|
|
|
if err := h.db.Create(&product).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, gin.H{"data": product})
|
|
}
|
|
|
|
// Update PUT /api/v1/products/:id
|
|
func (h *ProductHandler) Update(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
id := c.Param("id")
|
|
|
|
var product model.Product
|
|
if err := h.db.Where("id = ? AND shop_id = ? AND deleted_at IS NULL", id, shopID).
|
|
First(&product).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
|
return
|
|
}
|
|
|
|
if err := c.ShouldBindJSON(&product); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
product.ShopID = shopID // 防止篡改
|
|
|
|
if err := h.db.Save(&product).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": product})
|
|
}
|
|
|
|
// Delete DELETE /api/v1/products/:id (软删除)
|
|
func (h *ProductHandler) Delete(c *gin.Context) {
|
|
shopID := middleware.GetShopID(c)
|
|
id := c.Param("id")
|
|
|
|
now := timeNow()
|
|
result := h.db.Model(&model.Product{}).
|
|
Where("id = ? AND shop_id = ? AND deleted_at IS NULL", id, shopID).
|
|
Update("deleted_at", now)
|
|
|
|
if result.RowsAffected == 0 {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "deleted"})
|
|
}
|