|
|
@ -423,8 +423,8 @@ func UpdateGoods(w *ResponseWriter, r *Request) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetGoodsList 查询商品列表
|
|
|
|
// ListGoods 查询商品列表
|
|
|
|
func GetGoodsList(w *ResponseWriter, r *Request) { |
|
|
|
func ListGoods(w *ResponseWriter, r *Request) { |
|
|
|
search := func(db *gorm.DB) *gorm.DB { |
|
|
|
search := func(db *gorm.DB) *gorm.DB { |
|
|
|
return db. |
|
|
|
return db. |
|
|
|
Model(&Goods{}). |
|
|
|
Model(&Goods{}). |
|
|
@ -436,7 +436,7 @@ func GetGoodsList(w *ResponseWriter, r *Request) { |
|
|
|
var total int64 |
|
|
|
var total int64 |
|
|
|
var err error |
|
|
|
var err error |
|
|
|
if err = DB.Scopes(search).Count(&total).Error; err == nil { |
|
|
|
if err = DB.Scopes(search).Count(&total).Error; err == nil { |
|
|
|
err = DB.Scopes(search).Find(&goodsList).Error |
|
|
|
err = DB.Scopes(search).Preload("Creator").Preload("Updater").Find(&goodsList).Error |
|
|
|
} |
|
|
|
} |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
w.Error(err) |
|
|
|
w.Error(err) |
|
|
@ -448,7 +448,8 @@ func GetGoodsList(w *ResponseWriter, r *Request) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func GetGoodsPrices(w *ResponseWriter, r *Request) { |
|
|
|
// 查询价格列表
|
|
|
|
|
|
|
|
func ListPrices(w *ResponseWriter, r *Request) { |
|
|
|
id := uint(r.Uint64("id", 0)) |
|
|
|
id := uint(r.Uint64("id", 0)) |
|
|
|
var goods Goods |
|
|
|
var goods Goods |
|
|
|
if err := DB.Scopes(func(db *gorm.DB) *gorm.DB { |
|
|
|
if err := DB.Scopes(func(db *gorm.DB) *gorm.DB { |
|
|
@ -463,7 +464,7 @@ func GetGoodsPrices(w *ResponseWriter, r *Request) { |
|
|
|
args = append(args, val) |
|
|
|
args = append(args, val) |
|
|
|
} |
|
|
|
} |
|
|
|
if len(queries) == 0 { |
|
|
|
if len(queries) == 0 { |
|
|
|
return db.Preload("Prices") |
|
|
|
return db.Preload("Prices.Creator") |
|
|
|
} |
|
|
|
} |
|
|
|
args = append([]any{strings.Join(queries, " AND ")}, args...) |
|
|
|
args = append([]any{strings.Join(queries, " AND ")}, args...) |
|
|
|
return db.Preload("Prices", args...) |
|
|
|
return db.Preload("Prices", args...) |
|
|
@ -516,8 +517,8 @@ func RegisterRoutes(r chi.Router) { |
|
|
|
r.Group(func(r chi.Router) { |
|
|
|
r.Group(func(r chi.Router) { |
|
|
|
r.Post("/goods", Handler(CreateGoods)) |
|
|
|
r.Post("/goods", Handler(CreateGoods)) |
|
|
|
r.Patch("/goods/:id", Handler(UpdateGoods)) |
|
|
|
r.Patch("/goods/:id", Handler(UpdateGoods)) |
|
|
|
r.Get("/goods", Handler(GetGoodsList)) |
|
|
|
r.Get("/goods", Handler(ListGoods)) |
|
|
|
r.Get("/goods/:id/prices", Handler(GetGoodsPrices)) |
|
|
|
r.Get("/goods/:id/prices", Handler(ListPrices)) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|