1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
// internal/handler/product_handler.go
package handler
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/yourusername/my-web-app/internal/middleware"
"github.com/yourusername/my-web-app/internal/model"
"github.com/yourusername/my-web-app/internal/service"
)
type ProductHandler struct {
productService service.ProductService
}
// NewProductHandler 创建产品处理器
func NewProductHandler(productService service.ProductService) *ProductHandler {
return &ProductHandler{productService: productService}
}
// CreateProductRequest 创建产品请求结构
type CreateProductRequest struct {
Name string `json:"name" binding:"required"`
Description string `json:"description"`
Price float64 `json:"price" binding:"required,gt=0"`
SKU string `json:"sku"`
Stock int `json:"stock" binding:"gte=0"`
Category string `json:"category"`
ImageURL string `json:"image_url"`
}
// Create 创建产品
func (h *ProductHandler) Create(c *gin.Context) {
// 检查权限
role, _ := c.Get("role")
if role != "admin" {
c.JSON(http.StatusForbidden, middleware.ErrorResponse{
Error: "权限不足",
})
return
}
var req CreateProductRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, middleware.ErrorResponse{
Error: "请求数据无效",
})
return
}
product := &model.Product{
Name: req.Name,
Description: req.Description,
Price: req.Price,
SKU: req.SKU,
Stock: req.Stock,
Category: req.Category,
ImageURL: req.ImageURL,
}
if err := h.productService.Create(product); err != nil {
c.JSON(http.StatusBadRequest, middleware.ErrorResponse{
Error: err.Error(),
})
return
}
c.JSON(http.StatusCreated, gin.H{
"message": "产品创建成功",
"product": product,
})
}
// Get 获取单个产品
func (h *ProductHandler) Get(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
c.JSON(http.StatusBadRequest, middleware.ErrorResponse{
Error: "无效的产品ID",
})
return
}
product, err := h.productService.GetProductByID(uint(id))
if err != nil {
c.JSON(http.StatusNotFound, middleware.ErrorResponse{
Error: err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"product": product,
})
}
// UpdateProductRequest 更新产品请求结构
type UpdateProductRequest struct {
Name string `json:"name" binding:"omitempty"`
Description string `json:"description"`
Price float64 `json:"price" binding:"omitempty,gt=0"`
SKU string `json:"sku"`
Stock int `json:"stock" binding:"omitempty,gte=0"`
Category string `json:"category"`
ImageURL string `json:"image_url"`
}
// Update 更新产品
func (h *ProductHandler) Update(c *gin.Context) {
// 检查权限
role, _ := c.Get("role")
if role != "admin" {
c.JSON(http.StatusForbidden, middleware.ErrorResponse{
Error: "权限不足",
})
return
}
idStr := c.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
c.JSON(http.StatusBadRequest, middleware.ErrorResponse{
Error: "无效的产品ID",
})
return
}
var req UpdateProductRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, middleware.ErrorResponse{
Error: "请求数据无效",
})
return
}
// 获取现有产品
product, err := h.productService.GetProductByID(uint(id))
if err != nil {
c.JSON(http.StatusNotFound, middleware.ErrorResponse{
Error: err.Error(),
})
return
}
// 更新字段
if req.Name != "" {
product.Name = req.Name
}
product.Description = req.Description
if req.Price > 0 {
product.Price = req.Price
}
if req.SKU != "" {
product.SKU = req.SKU
}
if req.Stock >= 0 {
product.Stock = req.Stock
}
product.Category = req.Category
product.ImageURL = req.ImageURL
if err := h.productService.UpdateProduct(product); err != nil {
c.JSON(http.StatusBadRequest, middleware.ErrorResponse{
Error: err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"message": "产品更新成功",
"product": product,
})
}
// Delete 删除产品
func (h *ProductHandler) Delete(c *gin.Context) {
// 检查权限
role, _ := c.Get("role")
if role != "admin" {
c.JSON(http.StatusForbidden, middleware.ErrorResponse{
Error: "权限不足",
})
return
}
idStr := c.Param("id")
id, err := strconv.ParseUint(idStr, 10, 32)
if err != nil {
c.JSON(http.StatusBadRequest, middleware.ErrorResponse{
Error: "无效的产品ID",
})
return
}
if err := h.productService.DeleteProduct(uint(id)); err != nil {
c.JSON(http.StatusNotFound, middleware.ErrorResponse{
Error: err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"message": "产品删除成功",
})
}
// List 列出产品
func (h *ProductHandler) List(c *gin.Context) {
// 获取分页参数
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
category := c.Query("category")
products, total, err := h.productService.ListProducts(page, pageSize, category)
if err != nil {
c.JSON(http.StatusInternalServerError, middleware.ErrorResponse{
Error: "获取产品列表失败",
})
return
}
c.JSON(http.StatusOK, gin.H{
"products": products,
"pagination": gin.H{
"total": total,
"page": page,
"page_size": pageSize,
"pages": (total + int64(pageSize) - 1) / int64(pageSize),
},
})
}
// Search 搜索产品
func (h *ProductHandler) Search(c *gin.Context) {
// 获取查询参数
query := c.Query("q")
if query == "" {
c.JSON(http.StatusBadRequest, middleware.ErrorResponse{
Error: "搜索关键词不能为空",
})
return
}
// 获取分页参数
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
products, total, err := h.productService.SearchProducts(query, page, pageSize)
if err != nil {
c.JSON(http.StatusInternalServerError, middleware.ErrorResponse{
Error: "搜索产品失败",
})
return
}
c.JSON(http.StatusOK, gin.H{
"products": products,
"query": query,
"pagination": gin.H{
"total": total,
"page": page,
"page_size": pageSize,
"pages": (total + int64(pageSize) - 1) / int64(pageSize),
},
})
}
|