diff --git a/app/http/controller/web/animal_controller.go b/app/http/controller/web/animal_controller.go index 334de6e..86266f8 100644 --- a/app/http/controller/web/animal_controller.go +++ b/app/http/controller/web/animal_controller.go @@ -56,3 +56,8 @@ func (a *Animals) Detail(context *gin.Context) { 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 +} diff --git a/app/http/validator/common/location/poi.go b/app/http/validator/common/location/poi.go new file mode 100644 index 0000000..f4506ba --- /dev/null +++ b/app/http/validator/common/location/poi.go @@ -0,0 +1,6 @@ +package location + +type Poi struct { + Laitude float64 `form:"latitude" json:"latitude"` + Longitude float64 `form:"longitude" json:"longitude"` +} diff --git a/app/http/validator/common/register_validator/web_register_validator.go b/app/http/validator/common/register_validator/web_register_validator.go index e032a29..f4d5456 100644 --- a/app/http/validator/common/register_validator/web_register_validator.go +++ b/app/http/validator/common/register_validator/web_register_validator.go @@ -54,6 +54,8 @@ func WebRegisterValidator() { containers.Set(key, animal.List{}) key = consts.ValidatorPrefix + "AnimalDetail" containers.Set(key, animal.Detail{}) + key = consts.ValidatorPrefix + "AnimalCreate" + containers.Set(key, animal.Create{}) key = consts.ValidatorPrefix + "AnimalLikeCreate" containers.Set(key, animal_like.Create{}) diff --git a/app/http/validator/web/animal/create.go b/app/http/validator/web/animal/create.go new file mode 100644 index 0000000..82c86a8 --- /dev/null +++ b/app/http/validator/web/animal/create.go @@ -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) + } +} diff --git a/routers/web.go b/routers/web.go index f82f1a3..0fe827b 100644 --- a/routers/web.go +++ b/routers/web.go @@ -121,6 +121,7 @@ func InitWebRouter() *gin.Engine { { animal.GET("", validatorFactory.Create(consts.ValidatorPrefix+"AnimalList")) 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.DELETE("like", validatorFactory.Create(consts.ValidatorPrefix+"AnimalLikeDelete"))