Skip to content

Commit

Permalink
refactor: update validator for query
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Jun 5, 2020
1 parent 30b9683 commit 6243b3b
Show file tree
Hide file tree
Showing 23 changed files with 204 additions and 391 deletions.
14 changes: 8 additions & 6 deletions internal/app/admin_server/controller/admin/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (

type Query struct {
schema.Query
Status *model.AdminStatus `json:"status"` // 管理员状态
Status *model.AdminStatus `json:"status" url:"status" validate:"omitempty,number" comment:"管理员状态"` // 管理员状态
}

func GetList(c helper.Context, input Query) (res schema.Response) {
func GetList(c helper.Context, query Query) (res schema.Response) {
var (
err error
data = make([]schema.AdminProfile, 0)
Expand All @@ -40,15 +40,17 @@ func GetList(c helper.Context, input Query) (res schema.Response) {
helper.Response(&res, data, meta, err)
}()

query := input.Query

query.Normalize()

if err = query.Validate(); err != nil {
return
}

list := make([]model.Admin, 0)
filter := map[string]interface{}{}

if input.Status != nil {
filter["status"] = *input.Status
if query.Status != nil {
filter["status"] = *query.Status
}

if err = query.Order(database.Db.Limit(query.Limit).Offset(query.Limit * query.Page)).Where(filter).Find(&list).Error; err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/app/admin_server/controller/banner/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
type CreateParams struct {
Image string `json:"image" validate:"required,url,max=255" comment:"图片地址"` // 图片 URL
Href string `json:"href" validate:"required,url,max=255" comment:"图片跳转的地址"` // 图片跳转的 URL
Platform model.BannerPlatform `json:"platform" validate:"required,max=32,oneof=web app" comment:"平台"` // 用于哪个平台, web/app
Platform model.BannerPlatform `json:"platform" validate:"required,max=32,oneof=pc app" comment:"平台"` // 用于哪个平台, web/app
Description *string `json:"description" validate:"omitempty,max=255" comment:"描述"` // Banner 描述
Priority *int `json:"priority" validate:"omitempty,gt=0" comment:"优先级"` // 优先级,用于排序
Identifier *string `json:"identifier" validate:"omitempty,max=32" comment:"APP 标识符"` // APP 跳转标识符
Expand Down
24 changes: 15 additions & 9 deletions internal/app/admin_server/controller/banner/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (

type Query struct {
schema.Query
Platform *model.BannerPlatform `json:"platform" form:"platform"` // 根据平台筛选
Active *bool `json:"active" form:"active"` // 是否激活
Platform *model.BannerPlatform `json:"platform" url:"platform" validate:"omitempty,oneof=pc app" comment:"平台"` // 根据平台筛选
Active *bool `json:"active" url:"active" validate:"omitempty" comment:"是否激活"` // 是否激活
}

func GetBannerList(c helper.Context, q Query) (res schema.Response) {
func GetBannerList(c helper.Context, query Query) (res schema.Response) {
var (
err error
data = make([]schema.Banner, 0)
Expand All @@ -41,20 +41,26 @@ func GetBannerList(c helper.Context, q Query) (res schema.Response) {
helper.Response(&res, data, meta, err)
}()

query := q.Query

query.Normalize()

if err = query.Validate(); err != nil {
return
}

if err = query.Validate(); err != nil {
return
}

list := make([]model.Banner, 0)

filter := map[string]interface{}{}

if q.Platform != nil {
filter["platform"] = *q.Platform
if query.Platform != nil {
filter["platform"] = *query.Platform
}

if q.Active != nil {
filter["active"] = *q.Active
if query.Active != nil {
filter["active"] = *query.Active
} else {
filter["active"] = true
}
Expand Down
2 changes: 1 addition & 1 deletion internal/app/admin_server/controller/banner/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
type UpdateParams struct {
Image *string `json:"image" validate:"omitempty,url,max=255" comment:"图片地址"` // 图片 URL
Href *string `json:"href" validate:"omitempty,url,max=255" comment:"图片跳转的地址"` // 图片跳转的 URL
Platform *model.BannerPlatform `json:"platform" validate:"omitempty,max=32,oneof=web app" comment:"平台"` // 用于哪个平台, web/app
Platform *model.BannerPlatform `json:"platform" validate:"omitempty,max=32,oneof=pc app" comment:"平台"` // 用于哪个平台, web/app
Description *string `json:"description" validate:"omitempty,max=255" comment:"描述"` // Banner 描述
Priority *int `json:"priority" validate:"omitempty,gt=0" comment:"优先级"` // 优先级,用于排序
Identifier *string `json:"identifier" validate:"omitempty,max=32" comment:"APP 标识符"` // APP 跳转标识符
Expand Down
12 changes: 7 additions & 5 deletions internal/app/admin_server/controller/config/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Query struct {
schema.Query
}

func GetList(c helper.Context, q Query) (res schema.Response) {
func GetList(c helper.Context, query Query) (res schema.Response) {
var (
err error
data = make([]schema.Config, 0)
Expand Down Expand Up @@ -49,6 +49,12 @@ func GetList(c helper.Context, q Query) (res schema.Response) {
helper.Response(&res, data, meta, err)
}()

query.Normalize()

if err = query.Validate(); err != nil {
return
}

tx = database.Db.Begin()

adminInfo := model.Admin{
Expand All @@ -68,10 +74,6 @@ func GetList(c helper.Context, q Query) (res schema.Response) {
return
}

query := q.Query

query.Normalize()

list := make([]model.Config, 0)

filter := map[string]interface{}{}
Expand Down
20 changes: 11 additions & 9 deletions internal/app/admin_server/controller/help/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (

type Query struct {
schema.Query
Status *model.HelpStatus `json:"status" form:"status"` // 根据状态筛选
Type *model.HelpType `json:"type" form:"type"` // 根据类型筛选
Status *model.HelpStatus `json:"status" url:"status" validate:"omitempty,number" comment:"状态"` // 根据状态筛选
Type *model.HelpType `json:"type" url:"type" validate:"omitempty" comment:"类型"` // 根据类型筛选
}

func GetHelpList(c helper.Context, q Query) (res schema.Response) {
func GetHelpList(c helper.Context, query Query) (res schema.Response) {
var (
err error
data = make([]schema.Help, 0)
Expand All @@ -41,20 +41,22 @@ func GetHelpList(c helper.Context, q Query) (res schema.Response) {
helper.Response(&res, data, meta, err)
}()

query := q.Query

query.Normalize()

if err = query.Validate(); err != nil {
return
}

list := make([]model.Help, 0)

filter := map[string]interface{}{}

if q.Status != nil {
filter["status"] = *q.Status
if query.Status != nil {
filter["status"] = *query.Status
}

if q.Type != nil {
filter["type"] = *q.Type
if query.Type != nil {
filter["type"] = *query.Type
}

var total int64
Expand Down
32 changes: 17 additions & 15 deletions internal/app/admin_server/controller/logger/login/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (

type Query struct {
schema.Query
Uid *string `json:"uid" form:"uid"` // 根据用户 ID 筛选
Type *int `json:"type" form:"type"` // 根据类型筛选
Command *int `json:"command" form:"command"` // 根据登陆命令筛选
Ip *string `json:"ip"` // 根据 IP 筛选
Uid *string `json:"uid" url:"uid" validate:"omitempty" comment:"用户ID"` // 根据用户 ID 筛选
Type *int `json:"type" url:"type" validate:"omitempty,number" comment:"类型"` // 根据类型筛选
Command *int `json:"command" url:"command" validate:"omitempty,number" comment:"登陆命令"` // 根据登陆命令筛选
Ip *string `json:"ip" url:"ip" validate:"omitempty,ip" comment:"IP"` // 根据 IP 筛选
}

func GetLoginLogs(c helper.Context, q Query) (res schema.Response) {
func GetLoginLogs(c helper.Context, query Query) (res schema.Response) {
var (
err error
data = make([]schema.LogLogin, 0)
Expand All @@ -43,28 +43,30 @@ func GetLoginLogs(c helper.Context, q Query) (res schema.Response) {
helper.Response(&res, data, meta, err)
}()

query := q.Query

query.Normalize()

if err = query.Validate(); err != nil {
return
}

list := make([]model.LoginLog, 0)

filter := map[string]interface{}{}

if q.Uid != nil {
filter["uid"] = *q.Uid
if query.Uid != nil {
filter["uid"] = *query.Uid
}

if q.Type != nil {
filter["type"] = *q.Type
if query.Type != nil {
filter["type"] = *query.Type
}

if q.Command != nil {
filter["command"] = *q.Command
if query.Command != nil {
filter["command"] = *query.Command
}

if q.Ip != nil {
filter["last_ip"] = *q.Ip
if query.Ip != nil {
filter["last_ip"] = *query.Ip
}

var total int64
Expand Down
106 changes: 17 additions & 89 deletions internal/app/admin_server/controller/message/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,17 @@ import (

type Query struct {
schema.Query
Status *model.MessageStatus `json:"status" form:"status"`
Read *bool `json:"read" form:"read"`
}

type QueryAdmin struct {
Query
Uid *string `json:"uid" form:"uid"` // 指定某个用户ID
Status *model.MessageStatus `json:"status" url:"status" validate:"omitempty,number" comment:"状态"`
Read *bool `json:"read" url:"read" validate:"omitempty" comment:"是否已读"`
Uid *string `json:"uid" url:"uid" validate:"omitempty" comment:"用户ID"`
}

// 用户获取自己的消息列表
func GetMessageListByUser(c helper.Context, input Query) (res schema.Response) {
func GetMessageListByAdmin(c helper.Context, query Query) (res schema.Response) {
var (
err error
data = make([]schema.Message, 0) // 接口输出的数据
list = make([]model.Message, 0) // 数据库查询出的原始数据
data = make([]schema.MessageAdmin, 0) // 接口输出的数据
list = make([]model.Message, 0) // 数据库查询出的原始数据
meta = &schema.Meta{}
)

Expand All @@ -48,101 +44,33 @@ func GetMessageListByUser(c helper.Context, input Query) (res schema.Response) {
helper.Response(&res, data, meta, err)
}()

query := input.Query

query.Normalize()

var total int64

filter := map[string]interface{}{}

filter["uid"] = c.Uid

if input.Read != nil {
filter["read"] = *input.Read
}

if input.Status != nil {
filter["status"] = *input.Status
}

if err = query.Order(database.Db.Limit(query.Limit).Offset(query.Limit * query.Page)).Where(filter).Find(&list).Error; err != nil {
if err = query.Validate(); err != nil {
return
}

if err = database.Db.Model(model.Message{}).Where(filter).Count(&total).Error; err != nil {
return
}

for _, v := range list {
d := schema.Message{}
if er := mapstructure.Decode(v, &d.MessagePure); er != nil {
err = er
return
}
d.CreatedAt = v.CreatedAt.Format(time.RFC3339Nano)
d.UpdatedAt = v.UpdatedAt.Format(time.RFC3339Nano)
data = append(data, d)
}

meta.Total = total
meta.Num = len(data)
meta.Page = query.Page
meta.Limit = query.Limit
meta.Sort = query.Sort

return
}

// 用户获取自己的消息列表
func GetMessageListByAdmin(c helper.Context, input QueryAdmin) (res schema.Response) {
var (
err error
data = make([]schema.MessageAdmin, 0) // 接口输出的数据
list = make([]model.Message, 0) // 数据库查询出的原始数据
meta = &schema.Meta{}
)

defer func() {
if r := recover(); r != nil {
switch t := r.(type) {
case string:
err = errors.New(t)
case error:
err = t
default:
err = exception.Unknown
}
}

helper.Response(&res, data, meta, err)
}()

query := input.Query

query.Normalize()

filter := model.Message{}
filter := map[string]interface{}{}

if input.Uid != nil {
filter.Uid = *input.Uid
if query.Uid != nil {
filter["uid"] = *query.Uid
}

if input.Read != nil {
filter.Read = *input.Read
if query.Read != nil {
filter["read"] = *query.Read
}

if input.Status != nil {
filter.Status = *input.Status
if query.Status != nil {
filter["status"] = *query.Status
}

var total int64

if err = query.Order(database.Db.Limit(query.Limit).Offset(query.Limit * query.Page)).Where(&filter).Find(&list).Error; err != nil {
if err = query.Order(database.Db.Limit(query.Limit).Offset(query.Limit * query.Page)).Where(filter).Find(&list).Error; err != nil {
return
}

if err = database.Db.Model(&filter).Where(&filter).Count(&total).Error; err != nil {
if err = database.Db.Model(model.Message{}).Where(filter).Count(&total).Error; err != nil {
return
}

Expand All @@ -168,7 +96,7 @@ func GetMessageListByAdmin(c helper.Context, input QueryAdmin) (res schema.Respo

var GetMessageListByAdminRouter = router.Handler(func(c router.Context) {
var (
input QueryAdmin
input Query
)

c.ResponseFunc(c.ShouldBindQuery(&input), func() schema.Response {
Expand Down
Loading

0 comments on commit 6243b3b

Please sign in to comment.