排序条件

This commit is contained in:
2024-11-27 07:28:45 +08:00
parent b1b030acf3
commit a015fca246

View File

@@ -526,11 +526,20 @@ func NewSchema(config Config) (graphql.Schema, error) {
},
}),
Args: graphql.FieldConfigArgument{
"style": &graphql.ArgumentConfig{Type: graphql.String, Description: "按风格筛选游戏"},
"device": &graphql.ArgumentConfig{Type: graphql.String, Description: "按平台筛选游戏"},
"orientation": &graphql.ArgumentConfig{Type: graphql.String, Description: "按屏幕方向筛选游戏"},
"era": &graphql.ArgumentConfig{Type: graphql.String, Description: "按年份筛选游戏"},
"category_id": &graphql.ArgumentConfig{Type: graphql.Int, Description: "按分类ID筛选游戏"},
"tags": &graphql.ArgumentConfig{Type: graphql.String, Description: "按标签筛选游戏"},
"user_id": &graphql.ArgumentConfig{Type: graphql.Int, Description: "按用户ID筛选游戏"},
"id": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选游戏中指定ID的"},
"title": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选游戏中含有指定标题的"},
"era": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选游戏中含有指定年份的"},
"create_time": &graphql.ArgumentConfig{Type: graphql.DateTime, Description: "筛选游戏中创建时间等于指定值的"},
"update_time": &graphql.ArgumentConfig{Type: graphql.DateTime, Description: "筛选游戏中更新时间等于指定值的"},
"rank": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选游戏中含有指定排名的"},
"create_time": &graphql.ArgumentConfig{Type: graphql.DateTime, Description: "按创建时间筛选游戏"},
"update_time": &graphql.ArgumentConfig{Type: graphql.DateTime, Description: "按修改时间筛选游戏"},
"sort": &graphql.ArgumentConfig{Type: graphql.String, Description: "按指定字段排序游戏"},
"order": &graphql.ArgumentConfig{Type: orderType, Description: "排序类型(升序或降序)", DefaultValue: "ASC"},
"first": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中的前n個元素)"},
"last": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中的最後n個元素)"},
"after": &graphql.ArgumentConfig{Type: graphql.String, Description: "翻页参数(傳回清單中指定遊標之後的元素)"},
@@ -541,23 +550,30 @@ func NewSchema(config Config) (graphql.Schema, error) {
var total int
var err error
// 获取筛选条件
var arg struct {
ID int
Title string
Era string
First int
Last int
After string
Before string
var query = goqu.Dialect("mysql").From("web_article")
// 筛选条件
for _, format := range []string{"id", "style", "device", "orientation", "era", "category_id", "tags"} {
if p.Args[format] != nil {
query = query.Where(goqu.C(format).Eq(p.Args[format]))
}
}
// 排序条件
if p.Args["sort"] != nil {
if p.Args["order"].(string) == "ASC" {
query = query.Order(goqu.C(p.Args["sort"].(string)).Asc())
}
if p.Args["order"].(string) == "DESC" {
query = query.Order(goqu.C(p.Args["sort"].(string)).Desc())
}
}
mapstructure.Decode(p.Args, &arg)
var limit int = 10
if arg.First != 0 {
limit = arg.First
} else if arg.Last != 0 {
limit = arg.Last
if p.Args["first"] != nil {
limit = p.Args["first"].(int)
} else if p.Args["last"] != nil {
limit = p.Args["last"].(int)
}
if err := db.Limit(limit).Where("category_top_id = 22").Find(&games).Error; err != nil {
@@ -610,7 +626,7 @@ func NewSchema(config Config) (graphql.Schema, error) {
"content": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选图像中含有指定内容的"},
"remark": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选图像中含有指定备注的"},
"description": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选图像中含有指定描述的"},
"rank": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选图像中含有指定等级的"},
"rank": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选图像中含有指定排名的"},
"text": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选图像中含有指定文字的"},
"create_time": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选图像中创建时间等于指定值的"},
"update_time": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选图像中更新时间等于指定值的"},