🎏finish name selece
This commit is contained in:
parent
1b5ef757b7
commit
c018a63cd3
@ -21,16 +21,6 @@ import (
|
|||||||
type Animals struct { // INFO 起到一个标记的作用,这样 web.xxx 的时候不同模块就不会命名冲突了。
|
type Animals struct { // INFO 起到一个标记的作用,这样 web.xxx 的时候不同模块就不会命名冲突了。
|
||||||
}
|
}
|
||||||
|
|
||||||
// contains 检查 id 是否在 ids 切片中
|
|
||||||
func contains(ids []int, id int) bool {
|
|
||||||
for _, v := range ids {
|
|
||||||
if v == id {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *Animals) List(context *gin.Context) {
|
func (a *Animals) List(context *gin.Context) {
|
||||||
// 1. Get Params
|
// 1. Get Params
|
||||||
attrs := context.GetString(consts.ValidatorPrefix + "attrs")
|
attrs := context.GetString(consts.ValidatorPrefix + "attrs")
|
||||||
@ -238,3 +228,15 @@ func (a *Animals) Create(context *gin.Context) {
|
|||||||
response.Fail(context, consts.CurdCreatFailCode, consts.CurdCreatFailMsg+",新增错误", "")
|
response.Fail(context, consts.CurdCreatFailCode, consts.CurdCreatFailMsg+",新增错误", "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Animals) Name(context *gin.Context) {
|
||||||
|
attrs := context.GetString(consts.ValidatorPrefix + "attrs")
|
||||||
|
name := context.GetString(consts.ValidatorPrefix + "name")
|
||||||
|
|
||||||
|
animals := curd.CreateAnimalsCurdFactory().ShowByName(attrs, name)
|
||||||
|
if animals != nil {
|
||||||
|
response.Success(context, consts.CurdStatusOkMsg, animals)
|
||||||
|
} else {
|
||||||
|
response.Fail(context, errcode.AnimalNoFind, errcode.ErrMsg[errcode.AnimalNoFind], "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -57,6 +57,8 @@ func WebRegisterValidator() {
|
|||||||
containers.Set(key, animal.Detail{})
|
containers.Set(key, animal.Detail{})
|
||||||
key = consts.ValidatorPrefix + "AnimalCreate"
|
key = consts.ValidatorPrefix + "AnimalCreate"
|
||||||
containers.Set(key, animal.Create{})
|
containers.Set(key, animal.Create{})
|
||||||
|
key = consts.ValidatorPrefix + "AnimalName"
|
||||||
|
containers.Set(key, animal.Name{})
|
||||||
|
|
||||||
key = consts.ValidatorPrefix + "AnimalLikeCreate"
|
key = consts.ValidatorPrefix + "AnimalLikeCreate"
|
||||||
containers.Set(key, animal_like.Create{})
|
containers.Set(key, animal_like.Create{})
|
||||||
|
30
app/http/validator/web/animal/name.go
Normal file
30
app/http/validator/web/animal/name.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package animal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"catface/app/global/consts"
|
||||||
|
"catface/app/http/controller/web"
|
||||||
|
"catface/app/http/validator/core/data_transfer"
|
||||||
|
"catface/app/utils/response"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Name struct {
|
||||||
|
Attrs string `form:"attrs" json:"attrs"`
|
||||||
|
Name string `form:"name" json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n Name) CheckParams(context *gin.Context) {
|
||||||
|
if err := context.ShouldBind(&n); err != nil {
|
||||||
|
response.ValidatorError(context, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
extraAddBindDataContext := data_transfer.DataAddContext(n, consts.ValidatorPrefix, context)
|
||||||
|
if extraAddBindDataContext == nil {
|
||||||
|
response.ErrorSystem(context, "animialList表单验证器json化失败", "")
|
||||||
|
} else {
|
||||||
|
// 验证完成,调用控制器,并将验证器成员(字段)递给控制器,保持上下文数据一致性
|
||||||
|
(&web.Animals{}).Name(extraAddBindDataContext)
|
||||||
|
}
|
||||||
|
}
|
@ -99,6 +99,26 @@ func (a *Animal) ShowByIDs(ids []int64, attrs ...string) (temp []Animal) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 采取前后通配符的方式,同时查询 name & nick_names 字段
|
||||||
|
* @param {string} name
|
||||||
|
* @param {...string} attrs
|
||||||
|
* @return {*}
|
||||||
|
*/
|
||||||
|
func (a *Animal) ShowByName(name string, attrs ...string) (temp []Animal) {
|
||||||
|
db := a.DB.Table(a.TableName())
|
||||||
|
|
||||||
|
if len(attrs) > 0 {
|
||||||
|
db = db.Select(attrs)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := db.Where("name LIKE ? OR nick_names LIKE ?", "%"+name+"%", "%"+name+"%").Find(&temp).Error
|
||||||
|
if err != nil {
|
||||||
|
variable.ZapLog.Error("Animal ShowByName Error", zap.Error(err))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (a *Animal) InsertDate(c *gin.Context) (int64, bool) {
|
func (a *Animal) InsertDate(c *gin.Context) (int64, bool) {
|
||||||
var tmp Animal
|
var tmp Animal
|
||||||
if err := data_bind.ShouldBindFormDataToModel(c, &tmp); err == nil {
|
if err := data_bind.ShouldBindFormDataToModel(c, &tmp); err == nil {
|
||||||
|
@ -4,3 +4,8 @@ type AnimalWithLikeList struct {
|
|||||||
Animal Animal `json:"animal"`
|
Animal Animal `json:"animal"`
|
||||||
Like bool `json:"like,omitempty"`
|
Like bool `json:"like,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AnimalWithNickNameHit struct {
|
||||||
|
Animal Animal `json:"animal"`
|
||||||
|
NickNameHit bool `json:"nick_name_hit,omitempty"`
|
||||||
|
}
|
||||||
|
@ -2,6 +2,7 @@ package curd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"catface/app/model"
|
"catface/app/model"
|
||||||
|
"catface/app/utils/gorm_v2"
|
||||||
"catface/app/utils/model_handler"
|
"catface/app/utils/model_handler"
|
||||||
"catface/app/utils/query_handler"
|
"catface/app/utils/query_handler"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -95,6 +96,22 @@ func (a *AnimalsCurd) List(attrs string, gender string, breed string, sterilizat
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *AnimalsCurd) ShowByName(attrs string, name string) (temp []model.AnimalWithNickNameHit) {
|
||||||
|
validSelectedFields := getSelectAttrs(attrs)
|
||||||
|
|
||||||
|
animals := model.CreateAnimalFactory("").ShowByName(name, validSelectedFields...)
|
||||||
|
|
||||||
|
for _, animal := range animals {
|
||||||
|
animalWithNameHit := model.AnimalWithNickNameHit{
|
||||||
|
Animal: animal,
|
||||||
|
NickNameHit: !gorm_v2.IsLikePatternMatch(animal.Name, name), // 通过对比 name,然后取反。
|
||||||
|
}
|
||||||
|
temp = append(temp, animalWithNameHit)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (a *AnimalsCurd) Detail(id string) *model.Animal {
|
func (a *AnimalsCurd) Detail(id string) *model.Animal {
|
||||||
idInt, err := strconv.Atoi(id)
|
idInt, err := strconv.Atoi(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package gorm_v2
|
package gorm_v2
|
||||||
|
|
||||||
import "gorm.io/gorm"
|
import (
|
||||||
|
"regexp"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: 通过检查字段的方式构建 Where 函数。
|
* @description: 通过检查字段的方式构建 Where 函数。
|
||||||
@ -20,3 +24,15 @@ func BuildWhere(db *gorm.DB, conditions map[string][]uint8) *gorm.DB {
|
|||||||
}
|
}
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isLikePatternMatch 检查字符串是否匹配 LIKE '%name%' 模式
|
||||||
|
func IsLikePatternMatch(input, pattern string) bool {
|
||||||
|
// 构建正则表达式
|
||||||
|
regexPattern := ".*" + regexp.QuoteMeta(pattern) + ".*"
|
||||||
|
|
||||||
|
// 编译正则表达式
|
||||||
|
re := regexp.MustCompile(regexPattern)
|
||||||
|
|
||||||
|
// 检查输入字符串是否匹配正则表达式
|
||||||
|
return re.MatchString(input)
|
||||||
|
}
|
||||||
|
@ -3,10 +3,12 @@ package routers
|
|||||||
import (
|
import (
|
||||||
"catface/app/global/consts"
|
"catface/app/global/consts"
|
||||||
"catface/app/global/variable"
|
"catface/app/global/variable"
|
||||||
|
|
||||||
// "catface/app/http/controller/captcha" // 验证码组件
|
// "catface/app/http/controller/captcha" // 验证码组件
|
||||||
// "catface/app/http/middleware/authorization"
|
// "catface/app/http/middleware/authorization"
|
||||||
"catface/app/http/middleware/cors"
|
"catface/app/http/middleware/cors"
|
||||||
validatorFactory "catface/app/http/validator/core/factory"
|
validatorFactory "catface/app/http/validator/core/factory"
|
||||||
|
|
||||||
// TODO validatorFactory "catface/app/http/validator/core/factory"
|
// TODO validatorFactory "catface/app/http/validator/core/factory"
|
||||||
"catface/app/utils/gin_release"
|
"catface/app/utils/gin_release"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -122,6 +124,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("", validatorFactory.Create(consts.ValidatorPrefix+"AnimalCreate"))
|
||||||
|
animal.GET("name", validatorFactory.Create(consts.ValidatorPrefix+"AnimalName"))
|
||||||
|
|
||||||
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"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user