diff --git a/app/net.go b/app/net.go index 0a9674b..f4432fc 100644 --- a/app/net.go +++ b/app/net.go @@ -216,8 +216,7 @@ func (w *ResponseWriter) Send(status int, body any) { buf, err := json.Marshal(body) if err != nil { - LogError(err) - w.Fail(http.StatusInternalServerError, -1, err.Error()) + w.Error(err) return } @@ -229,27 +228,27 @@ func (w *ResponseWriter) Send(status int, body any) { } } -func (w *ResponseWriter) Fail(status int, code int, message ...string) { - info := map[string]any{"code": code, "message": ""} - if len(message) > 0 && len(message[0]) > 0 { - info["message"] = message[0] - } else { - info["message"] = http.StatusText(status) - } - w.Send(status, info) -} - func (w *ResponseWriter) Error(err error) { + var status int + var code int + var message string if ex, ok := err.(*Error); ok { - status := http.StatusBadRequest + status = http.StatusBadRequest + code = ex.Code + message = ex.Message if ex.Status > 0 { status = ex.Status } - w.Fail(status, ex.Code, ex.Message) } else { + status = http.StatusInternalServerError + code = -1 + message = err.Error() LogError(err) - w.Fail(http.StatusInternalServerError, -1, err.Error()) } + if len(message) == 0 { + message = http.StatusText(status) + } + w.Send(status, map[string]any{"code": code, "message": message}) } func (w *ResponseWriter) Ok(data any, message ...string) { diff --git a/app/rts.go b/app/rts.go index 42234a4..685f896 100644 --- a/app/rts.go +++ b/app/rts.go @@ -5,7 +5,6 @@ import ( "github.com/go-chi/chi/v5" "github.com/go-chi/jwtauth/v5" "gorm.io/gorm" - "net/http" "strings" ) @@ -178,7 +177,7 @@ func ListUser(w *ResponseWriter, r *Request) { err = DB.Scopes(search).Find(&userList).Error } if err != nil { - w.Fail(http.StatusInternalServerError, 1, err.Error()) + w.Error(err) } else { w.Ok(map[string]any{ "list": userList, @@ -252,11 +251,11 @@ func CreateGoods(w *ResponseWriter, r *Request) { name := r.Value("name") price := r.Float32("price", 0) if len(name) == 0 { - w.Fail(http.StatusBadRequest, 1, "商品名称错误") + w.Error(NewError(1, "商品名称错误")) return } if price <= 0 { - w.Fail(http.StatusBadRequest, 2, "商品价格错误") + w.Error(NewError(2, "商品价格错误")) return } var goods Goods @@ -268,15 +267,14 @@ func CreateGoods(w *ResponseWriter, r *Request) { } err = DB.Create(&goods).Error if err != nil { - w.Fail(http.StatusBadRequest, 3, "创建商品失败") + w.Error(NewError(3, "创建商品失败")) } else { w.Ok(goods) } } else if err != nil { - LogError(err) - w.Fail(http.StatusBadRequest, 4, "商品价格错误") + w.Error(err) } 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) id := uint(r.Uint64("id", 0)) if len(name) == 0 { - w.Fail(http.StatusBadRequest, 1, "商品名称错误") + w.Error(NewError(1, "商品名称错误")) return } if price <= 0 { - w.Fail(http.StatusBadRequest, 2, "商品价格错误") + w.Error(NewError(2, "商品价格错误")) return } var goods Goods err := DB.First(&goods, "id = ?", id).Error if errors.Is(err, gorm.ErrRecordNotFound) { - w.Fail(http.StatusBadRequest, 2, "商品不存在") + w.Error(NewError(2, "商品不存在")) + return } else if err != nil { - LogError(err) - w.Fail(http.StatusBadRequest, 3, err.Error()) - } else { - // 商品名称不能重复 - err = DB.Where("id <> ?", id).First(&Goods{}, "name = ?", name).Error - if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { - LogError(err) - w.Fail(http.StatusBadRequest, 4, err.Error()) + w.Error(err) + return + } + + // 商品名称不能重复 + err = DB.Where("id <> ?", id).First(&Goods{}, "name = ?", name).Error + if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { + 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 } - 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 查询商品列表 @@ -338,7 +335,7 @@ func GetGoodsList(w *ResponseWriter, r *Request) { err = DB.Scopes(search).Find(&goodsList).Error } if err != nil { - w.Fail(http.StatusInternalServerError, 1, err.Error()) + w.Error(err) } else { w.Ok(map[string]any{ "list": goodsList, @@ -367,7 +364,7 @@ func GetGoodsPrices(w *ResponseWriter, r *Request) { args = append([]any{strings.Join(queries, " AND ")}, args...) return db.Preload("Prices", args...) }).First(&goods, "id = ?", id).Error; err != nil { - w.Fail(http.StatusInternalServerError, 1, err.Error()) + w.Error(err) } else { w.Ok(goods) }