2024/09/15 13:30:55 D:/workpace/博客/front/admin/controller/goodCategory/good_category.go:81 record not found
代码:
func Edit(c *gin.Context) {
var updateReq models.GoodCategory
if err := c.ShouldBindJSON(&updateReq); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"err": err})
return
}
res := database.DB.Where("id=?", updateReq.ID).First(&models.GoodCategory{})
if res.Error != nil {
//这里未找到数据也会报错,将错误判断 使得返回200状态码
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
c.JSON(http.StatusOK, gin.H{"mgs": "未找到相关数据"})
return
}
c.JSON(http.StatusOK, gin.H{"errs": fmt.Sprintf("database err %v", res.Error)})
return
}
err := database.DB.Save(&updateReq).Error
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"err": err,
})
return
}
c.JSON(http.StatusOK, gin.H{"msg": "更新成功过"})
}