This commit is contained in:
2024-11-09 05:55:44 +08:00
parent 5bbb3a8797
commit 85a71a64ff

View File

@@ -222,6 +222,21 @@ func NewSchema(config Config) (graphql.Schema, error) {
return fields return fields
} }
orderType := graphql.NewEnum(graphql.EnumConfig{
Name: "OrderType",
Description: "排序类型",
Values: graphql.EnumValueConfigMap{
"ASC": &graphql.EnumValueConfig{
Value: "ASC",
Description: "升序",
},
"DESC": &graphql.EnumValueConfig{
Value: "DESC",
Description: "降序",
},
},
})
schema, err := graphql.NewSchema(graphql.SchemaConfig{Query: graphql.NewObject(graphql.ObjectConfig{Name: "RootQuery", Fields: graphql.Fields{ schema, err := graphql.NewSchema(graphql.SchemaConfig{Query: graphql.NewObject(graphql.ObjectConfig{Name: "RootQuery", Fields: graphql.Fields{
"categorys": &graphql.Field{ "categorys": &graphql.Field{
Name: "categorys", Name: "categorys",
@@ -353,6 +368,8 @@ func NewSchema(config Config) (graphql.Schema, error) {
}, },
}), }),
Args: graphql.FieldConfigArgument{ Args: graphql.FieldConfigArgument{
"sort": &graphql.ArgumentConfig{Type: graphql.String, Description: "排序方法"},
"order": &graphql.ArgumentConfig{Type: orderType, Description: "排序方向", DefaultValue: "ASC"},
"orientation": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选横屏竖屏图像"}, "orientation": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选横屏竖屏图像"},
"follower": &graphql.ArgumentConfig{Type: graphql.Int, Description: "获取指定ID用户的关注列表发布的图像"}, "follower": &graphql.ArgumentConfig{Type: graphql.Int, Description: "获取指定ID用户的关注列表发布的图像"},
"interest": &graphql.ArgumentConfig{Type: graphql.Int, Description: "获取指定ID用户的兴趣推荐图像"}, "interest": &graphql.ArgumentConfig{Type: graphql.Int, Description: "获取指定ID用户的兴趣推荐图像"},
@@ -390,6 +407,8 @@ func NewSchema(config Config) (graphql.Schema, error) {
Similar int Similar int
Follower int Follower int
Orientation string Orientation string
Sort string
Order string
} }
mapstructure.Decode(p.Args, &args) mapstructure.Decode(p.Args, &args)
@@ -411,7 +430,7 @@ func NewSchema(config Config) (graphql.Schema, error) {
// 筛选条件 // 筛选条件
for _, format := range argFormat { for _, format := range argFormat {
if p.Args[format] != nil { if p.Args[format] != nil {
query = query.Where(fmt.Sprintf(format, " = ?"), p.Args[format]) query = query.Where(fmt.Sprintf("%s = ?", format), p.Args[format])
} }
} }
@@ -522,6 +541,11 @@ func NewSchema(config Config) (graphql.Schema, error) {
total = len(list) total = len(list)
var count int64
if len(list) > 0 {
query = query.Count(&count)
}
// 截取: 分页 // 截取: 分页
if args.After != 0 { if args.After != 0 {
index := -1 index := -1
@@ -575,12 +599,21 @@ func NewSchema(config Config) (graphql.Schema, error) {
query = query.Preload(item) query = query.Preload(item)
} }
// 排序规则
if args.Sort != "" && args.Order != "" {
query = query.Order(fmt.Sprintf("%s %s", args.Sort, args.Order))
}
// 输出(游戏截图分类ID为22) // 输出(游戏截图分类ID为22)
if err := query.Where("article_category_top_id = 22").Find(&images).Error; err != nil { if err := query.Where("article_category_top_id = 22").Find(&images).Error; err != nil {
fmt.Println("获取图像列表失败", err) fmt.Println("获取图像列表失败", err)
return nil, err return nil, err
} }
if total == 0 && count > 0 {
total = int(count)
}
return map[string]interface{}{ return map[string]interface{}{
"list": images, "list": images,
"total": total, "total": total,