feat(animal): 优化动物列表查询并添加用户友好错误信息

- 在动物模型中添加排序条件,按更新时间降序排列
- 在错误代码中添加针对用户的错误信息
- 在动物控制器中使用用户友好的错误信息
This commit is contained in:
Havoc412 2024-11-09 05:51:43 +08:00
parent 8186f41347
commit eeeaa2a86f
4 changed files with 12 additions and 2 deletions

View File

@ -9,3 +9,7 @@ func AnimalMsgInit(m msg) {
m[ErrAnimalSqlFind] = "Animals 表单查询失败" m[ErrAnimalSqlFind] = "Animals 表单查询失败"
m[AnimalNoFind] = "Animals 没有查询到符合条件的目标" m[AnimalNoFind] = "Animals 没有查询到符合条件的目标"
} }
func AnimalMsgUserInit(m msg) {
m[AnimalNoFind] = "没有更多符合此条件的毛茸茸啦,试着更换查询条件或者新增吧~"
}

View File

@ -4,6 +4,8 @@ type msg map[int]string
var ErrMsg msg var ErrMsg msg
var ErrMsgForUser msg
func init() { func init() {
ErrMsg = make(msg) ErrMsg = make(msg)
GeneralMsgInit(ErrMsg) GeneralMsgInit(ErrMsg)
@ -11,6 +13,10 @@ func init() {
UserMsgInit(ErrMsg) UserMsgInit(ErrMsg)
EnocunterMsgInit(ErrMsg) EnocunterMsgInit(ErrMsg)
NlpMsgInit(ErrMsg) NlpMsgInit(ErrMsg)
// INGO
ErrMsgForUser = make(msg)
AnimalMsgUserInit(ErrMsgForUser)
} }
func GeneralMsgInit(m msg) { func GeneralMsgInit(m msg) {

View File

@ -94,7 +94,7 @@ func (a *Animals) List(context *gin.Context) {
"key": key, "key": key,
}) })
} else { } else {
response.Fail(context, errcode.AnimalNoFind, errcode.ErrMsg[errcode.AnimalNoFind], "") response.Fail(context, errcode.AnimalNoFind, errcode.ErrMsg[errcode.AnimalNoFind], errcode.ErrMsgForUser[errcode.AnimalNoFind])
} }
} }

View File

@ -69,7 +69,7 @@ func (a *Animal) Show(attrs []string, gender []uint8, breed []uint8, sterilizati
db = db.Where("id not in (?)", notInIds) db = db.Where("id not in (?)", notInIds)
} }
err := db.Find(&temp).Error err := db.Order("updated_at DESC").Find(&temp).Error
if err != nil { if err != nil {
variable.ZapLog.Error("Animal Show Error", zap.Error(err)) variable.ZapLog.Error("Animal Show Error", zap.Error(err))
} }