|
|
@ -5,7 +5,6 @@ import ( |
|
|
|
"github.com/go-chi/chi/v5" |
|
|
|
"github.com/go-chi/chi/v5" |
|
|
|
"github.com/go-chi/jwtauth/v5" |
|
|
|
"github.com/go-chi/jwtauth/v5" |
|
|
|
"gorm.io/gorm" |
|
|
|
"gorm.io/gorm" |
|
|
|
"net/http" |
|
|
|
|
|
|
|
"strings" |
|
|
|
"strings" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
@ -178,7 +177,7 @@ func ListUser(w *ResponseWriter, r *Request) { |
|
|
|
err = DB.Scopes(search).Find(&userList).Error |
|
|
|
err = DB.Scopes(search).Find(&userList).Error |
|
|
|
} |
|
|
|
} |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
w.Fail(http.StatusInternalServerError, 1, err.Error()) |
|
|
|
w.Error(err) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
w.Ok(map[string]any{ |
|
|
|
w.Ok(map[string]any{ |
|
|
|
"list": userList, |
|
|
|
"list": userList, |
|
|
@ -252,11 +251,11 @@ func CreateGoods(w *ResponseWriter, r *Request) { |
|
|
|
name := r.Value("name") |
|
|
|
name := r.Value("name") |
|
|
|
price := r.Float32("price", 0) |
|
|
|
price := r.Float32("price", 0) |
|
|
|
if len(name) == 0 { |
|
|
|
if len(name) == 0 { |
|
|
|
w.Fail(http.StatusBadRequest, 1, "商品名称错误") |
|
|
|
w.Error(NewError(1, "商品名称错误")) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
if price <= 0 { |
|
|
|
if price <= 0 { |
|
|
|
w.Fail(http.StatusBadRequest, 2, "商品价格错误") |
|
|
|
w.Error(NewError(2, "商品价格错误")) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
var goods Goods |
|
|
|
var goods Goods |
|
|
@ -268,15 +267,14 @@ func CreateGoods(w *ResponseWriter, r *Request) { |
|
|
|
} |
|
|
|
} |
|
|
|
err = DB.Create(&goods).Error |
|
|
|
err = DB.Create(&goods).Error |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
w.Fail(http.StatusBadRequest, 3, "创建商品失败") |
|
|
|
w.Error(NewError(3, "创建商品失败")) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
w.Ok(goods) |
|
|
|
w.Ok(goods) |
|
|
|
} |
|
|
|
} |
|
|
|
} else if err != nil { |
|
|
|
} else if err != nil { |
|
|
|
LogError(err) |
|
|
|
w.Error(err) |
|
|
|
w.Fail(http.StatusBadRequest, 4, "商品价格错误") |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
w.Fail(http.StatusBadRequest, 5, "商品已经存在") |
|
|
|
w.Error(NewError(5, "商品已经存在")) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -286,40 +284,39 @@ func UpdateGoods(w *ResponseWriter, r *Request) { |
|
|
|
price := r.Float32("price", 0) |
|
|
|
price := r.Float32("price", 0) |
|
|
|
id := uint(r.Uint64("id", 0)) |
|
|
|
id := uint(r.Uint64("id", 0)) |
|
|
|
if len(name) == 0 { |
|
|
|
if len(name) == 0 { |
|
|
|
w.Fail(http.StatusBadRequest, 1, "商品名称错误") |
|
|
|
w.Error(NewError(1, "商品名称错误")) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
if price <= 0 { |
|
|
|
if price <= 0 { |
|
|
|
w.Fail(http.StatusBadRequest, 2, "商品价格错误") |
|
|
|
w.Error(NewError(2, "商品价格错误")) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
var goods Goods |
|
|
|
var goods Goods |
|
|
|
err := DB.First(&goods, "id = ?", id).Error |
|
|
|
err := DB.First(&goods, "id = ?", id).Error |
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) { |
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) { |
|
|
|
w.Fail(http.StatusBadRequest, 2, "商品不存在") |
|
|
|
w.Error(NewError(2, "商品不存在")) |
|
|
|
|
|
|
|
return |
|
|
|
} else if err != nil { |
|
|
|
} else if err != nil { |
|
|
|
LogError(err) |
|
|
|
w.Error(err) |
|
|
|
w.Fail(http.StatusBadRequest, 3, err.Error()) |
|
|
|
return |
|
|
|
} else { |
|
|
|
} |
|
|
|
// 商品名称不能重复
|
|
|
|
|
|
|
|
err = DB.Where("id <> ?", id).First(&Goods{}, "name = ?", name).Error |
|
|
|
// 商品名称不能重复
|
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { |
|
|
|
err = DB.Where("id <> ?", id).First(&Goods{}, "name = ?", name).Error |
|
|
|
LogError(err) |
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { |
|
|
|
w.Fail(http.StatusBadRequest, 4, err.Error()) |
|
|
|
w.Error(err) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if goods.Name != name || goods.Price != price { |
|
|
|
|
|
|
|
goods.Name = name |
|
|
|
|
|
|
|
goods.Price = price |
|
|
|
|
|
|
|
err = DB.Save(&goods).Error |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
w.Error(err) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
if goods.Name != name || goods.Price != price { |
|
|
|
|
|
|
|
goods.Name = name |
|
|
|
|
|
|
|
goods.Price = price |
|
|
|
|
|
|
|
err = DB.Save(&goods).Error |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
LogError(err) |
|
|
|
|
|
|
|
w.Fail(http.StatusBadRequest, 5, err.Error()) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
w.Ok(goods, "修改成功") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
w.Ok(goods, "修改成功") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetGoodsList 查询商品列表
|
|
|
|
// GetGoodsList 查询商品列表
|
|
|
@ -338,7 +335,7 @@ func GetGoodsList(w *ResponseWriter, r *Request) { |
|
|
|
err = DB.Scopes(search).Find(&goodsList).Error |
|
|
|
err = DB.Scopes(search).Find(&goodsList).Error |
|
|
|
} |
|
|
|
} |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
w.Fail(http.StatusInternalServerError, 1, err.Error()) |
|
|
|
w.Error(err) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
w.Ok(map[string]any{ |
|
|
|
w.Ok(map[string]any{ |
|
|
|
"list": goodsList, |
|
|
|
"list": goodsList, |
|
|
@ -367,7 +364,7 @@ func GetGoodsPrices(w *ResponseWriter, r *Request) { |
|
|
|
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...) |
|
|
|
}).First(&goods, "id = ?", id).Error; err != nil { |
|
|
|
}).First(&goods, "id = ?", id).Error; err != nil { |
|
|
|
w.Fail(http.StatusInternalServerError, 1, err.Error()) |
|
|
|
w.Error(err) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
w.Ok(goods) |
|
|
|
w.Ok(goods) |
|
|
|
} |
|
|
|
} |
|
|
|