Files
jiu/backend/internal/handler/warehouse.go
T
wangjia 31ea370cea fix(backend): JWT config mapstructure tag 修复 + 模型从 hotel 重构为 shop
- 修复 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>
2026-04-07 22:20:12 +08:00

62 lines
1.6 KiB
Go

package handler
import (
"net/http"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"github.com/wangjia/jiu/backend/internal/middleware"
"github.com/wangjia/jiu/backend/internal/model"
)
type WarehouseHandler struct {
db *gorm.DB
}
func NewWarehouseHandler(db *gorm.DB) *WarehouseHandler {
return &WarehouseHandler{db: db}
}
func (h *WarehouseHandler) List(c *gin.Context) {
shopID := middleware.GetShopID(c)
var warehouses []model.Warehouse
h.db.Where("shop_id = ? AND deleted_at IS NULL", shopID).Find(&warehouses)
c.JSON(http.StatusOK, gin.H{"data": warehouses})
}
func (h *WarehouseHandler) Create(c *gin.Context) {
shopID := middleware.GetShopID(c)
var w model.Warehouse
if err := c.ShouldBindJSON(&w); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
w.ShopID = shopID
h.db.Create(&w)
c.JSON(http.StatusCreated, gin.H{"data": w})
}
func (h *WarehouseHandler) Update(c *gin.Context) {
shopID := middleware.GetShopID(c)
var w model.Warehouse
if err := h.db.Where("id = ? AND shop_id = ? AND deleted_at IS NULL", c.Param("id"), shopID).
First(&w).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
return
}
c.ShouldBindJSON(&w)
w.ShopID = shopID
h.db.Save(&w)
c.JSON(http.StatusOK, gin.H{"data": w})
}
func (h *WarehouseHandler) Delete(c *gin.Context) {
shopID := middleware.GetShopID(c)
now := timeNow()
h.db.Model(&model.Warehouse{}).
Where("id = ? AND shop_id = ? AND deleted_at IS NULL", c.Param("id"), shopID).
Update("deleted_at", now)
c.JSON(http.StatusOK, gin.H{"message": "deleted"})
}