test Animal Create Object

This commit is contained in:
Havoc412 2024-10-24 15:26:09 +08:00
parent 8be36697a7
commit 5bd3e4d6e7
5 changed files with 45 additions and 0 deletions

View File

@ -56,3 +56,8 @@ func (a *Animals) Detail(context *gin.Context) {
response.Fail(context, errcode.AnimalNoFind, errcode.ErrMsg[errcode.AnimalNoFind], "") response.Fail(context, errcode.AnimalNoFind, errcode.ErrMsg[errcode.AnimalNoFind], "")
} }
} }
func (a *Animals) Create(context *gin.Context) {
poi := context.GetStringMap(consts.ValidatorPrefix + "poi")
// TODO 感觉这里就是获取信息之后,然后解析后再存储,方便后续 Model 直接绑定到数据。
_ = poi
}

View File

@ -0,0 +1,6 @@
package location
type Poi struct {
Laitude float64 `form:"latitude" json:"latitude"`
Longitude float64 `form:"longitude" json:"longitude"`
}

View File

@ -54,6 +54,8 @@ func WebRegisterValidator() {
containers.Set(key, animal.List{}) containers.Set(key, animal.List{})
key = consts.ValidatorPrefix + "AnimalDetail" key = consts.ValidatorPrefix + "AnimalDetail"
containers.Set(key, animal.Detail{}) containers.Set(key, animal.Detail{})
key = consts.ValidatorPrefix + "AnimalCreate"
containers.Set(key, animal.Create{})
key = consts.ValidatorPrefix + "AnimalLikeCreate" key = consts.ValidatorPrefix + "AnimalLikeCreate"
containers.Set(key, animal_like.Create{}) containers.Set(key, animal_like.Create{})

View File

@ -0,0 +1,31 @@
package animal
import (
"catface/app/global/consts"
"catface/app/http/controller/web"
"catface/app/http/validator/common/location"
"catface/app/http/validator/core/data_transfer"
"catface/app/utils/response"
"github.com/gin-gonic/gin"
)
type Create struct {
Name string `form:"name" json:"name" binding:"required"`
Poi location.Poi `form:"poi" json:"poi"`
}
func (c Create) CheckParams(context *gin.Context) {
if err := context.ShouldBind(&c); err != nil {
response.ValidatorError(context, err)
return
}
extraAddBindDataContext := data_transfer.DataAddContext(c, consts.ValidatorPrefix, context)
if extraAddBindDataContext == nil {
response.ErrorSystem(context, "Animal Create 表单验证器json化失败", "")
} else {
// 验证完成,调用控制器,并将验证器成员(字段)递给控制器,保持上下文数据一致性
(&web.Animals{}).Create(extraAddBindDataContext)
}
}

View File

@ -121,6 +121,7 @@ func InitWebRouter() *gin.Engine {
{ {
animal.GET("", validatorFactory.Create(consts.ValidatorPrefix+"AnimalList")) animal.GET("", validatorFactory.Create(consts.ValidatorPrefix+"AnimalList"))
animal.GET(":anm_id", validatorFactory.Create(consts.ValidatorPrefix+"AnimalDetail")) animal.GET(":anm_id", validatorFactory.Create(consts.ValidatorPrefix+"AnimalDetail"))
animal.POST("", validatorFactory.Create(consts.ValidatorPrefix+"AnimalCreate"))
animal.POST("like", validatorFactory.Create(consts.ValidatorPrefix+"AnimalLikeCreate")) animal.POST("like", validatorFactory.Create(consts.ValidatorPrefix+"AnimalLikeCreate"))
animal.DELETE("like", validatorFactory.Create(consts.ValidatorPrefix+"AnimalLikeDelete")) animal.DELETE("like", validatorFactory.Create(consts.ValidatorPrefix+"AnimalLikeDelete"))