184 lines
7.5 KiB
Go
184 lines
7.5 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/doug-martin/goqu/v9"
|
|
"github.com/graphql-go/graphql"
|
|
)
|
|
|
|
type Game struct {
|
|
ID int `json:"id" gorm:"primaryKey"`
|
|
Title string `json:"title"`
|
|
Orientation string `json:"orientation"`
|
|
Device string `json:"device"`
|
|
Era string `json:"era"`
|
|
Tags string `json:"tags"`
|
|
UserId int `json:"user_id"`
|
|
Content string `json:"content"`
|
|
Image string `json:"image"`
|
|
Images string `json:"images"`
|
|
User User `json:"user" gorm:"foreignKey:UserId"`
|
|
CollectCount int `json:"collect_count" gorm:"column:collect_num"`
|
|
PraiseCount int `json:"praise_count" gorm:"column:praise_num"`
|
|
CreateTime time.Time `json:"create_time"`
|
|
UpdateTime time.Time `json:"update_time"`
|
|
}
|
|
|
|
func (Game) TableName() string {
|
|
return "web_article"
|
|
}
|
|
|
|
var gameType = graphql.NewObject(graphql.ObjectConfig{
|
|
Name: "Game",
|
|
Description: "游戏",
|
|
Fields: graphql.Fields{
|
|
"id": &graphql.Field{Type: graphql.Int, Description: "游戏ID"},
|
|
"title": &graphql.Field{Type: graphql.String, Description: "游戏标题"},
|
|
"era": &graphql.Field{Type: graphql.String, Description: "游戏上线年份"},
|
|
"create_time": &graphql.Field{Type: graphql.DateTime, Description: "游戏创建时间"},
|
|
"update_time": &graphql.Field{Type: graphql.DateTime, Description: "游戏更新时间"},
|
|
"praise_count": &graphql.Field{Type: graphql.Int, Description: "点赞数"},
|
|
"collect_count": &graphql.Field{Type: graphql.Int, Description: "收藏数"},
|
|
"praise": &graphql.Field{Type: graphql.Boolean, Description: "当前用户是否点赞", Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
var user_id = p.Context.Value("user_id").(int)
|
|
if user_id != 0 {
|
|
var praise int64
|
|
if err := db.Table("web_praise").Where("user_id = ?", user_id).Where("praise_id = ?", p.Source.(Game).ID).Where("type = ?", 0).Count(&praise); err != nil {
|
|
return false, nil
|
|
}
|
|
if praise > 0 {
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, nil
|
|
}},
|
|
"collect": &graphql.Field{Type: graphql.Boolean, Description: "当前用户是否收藏", Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
var user_id = p.Context.Value("user_id").(int)
|
|
if user_id != 0 {
|
|
var collect int64
|
|
if err := db.Table("web_collect").Where("user_id = ?", user_id).Where("collect_id = ?", p.Source.(Game).ID).Where("type = ?", 0).Count(&collect); err != nil {
|
|
return false, nil
|
|
}
|
|
if collect > 0 {
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, nil
|
|
}},
|
|
},
|
|
})
|
|
|
|
var GameItems = &graphql.Field{
|
|
Name: "games",
|
|
Description: "游戏列表",
|
|
Type: graphql.NewObject(graphql.ObjectConfig{
|
|
Name: "GameConnection",
|
|
Description: "条件筛选游戏列表",
|
|
Fields: graphql.Fields{
|
|
"list": &graphql.Field{Type: graphql.NewList(gameType), Description: "游戏列表"},
|
|
"total": &graphql.Field{Type: graphql.Int, Description: "游戏总数"},
|
|
},
|
|
}),
|
|
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: "筛选游戏中含有指定标题的"},
|
|
"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: "按指定字段排序游戏", DefaultValue: "id"},
|
|
"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: "翻页参数(傳回清單中指定遊標之後的元素)"},
|
|
"before": &graphql.ArgumentConfig{Type: graphql.String, Description: "翻页参数(傳回清單中指定遊標之前的元素)"},
|
|
},
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
var games []Game
|
|
var total int
|
|
var err error
|
|
|
|
// 游戏的顶级ID为 22
|
|
var query = goqu.Dialect("mysql").From("web_article").Where(goqu.Ex{"category_top_id": 22})
|
|
|
|
// 筛选条件
|
|
for _, format := range []string{"id", "title", "style", "device", "orientation", "era", "category_id", "tags"} {
|
|
if p.Args[format] != nil {
|
|
query = query.Where(goqu.C(format).Eq(p.Args[format]))
|
|
}
|
|
}
|
|
|
|
// 如果查询了 total 字段
|
|
if existField(p.Info.FieldASTs[0].SelectionSet.Selections, "total") {
|
|
sql, _, _ := query.ToSQL()
|
|
sql = strings.Replace(sql, "SELECT *", "SELECT COUNT(*)", 1)
|
|
if err := db.Raw(sql).Scan(&total).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
// 如果没有外部排序则使用指定排序(正则sort只能是字母数字下划下)
|
|
if p.Args["text"] == nil && p.Args["similar"] == nil && p.Args["interest"] == nil {
|
|
sort := regexp.MustCompile(`[^a-zA-Z0-9_]`).ReplaceAllString(p.Args["sort"].(string), "")
|
|
query = query.Select("web_article.id", goqu.L(
|
|
fmt.Sprintf("ROW_NUMBER() OVER(ORDER BY web_article.%s %s)", sort, p.Args["order"]),
|
|
).As("row_num"))
|
|
} else {
|
|
// 排序条件
|
|
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())
|
|
}
|
|
}
|
|
}
|
|
|
|
// 取所有数据的前N条
|
|
sql, _, _ := query.ToSQL()
|
|
fmt.Println(sql)
|
|
|
|
// 遊標截取篩選結果集的前N条
|
|
var cursor string
|
|
if p.Args["after"] != nil {
|
|
cursor = fmt.Sprintf(`WHERE row_num > (SELECT row_num FROM RankedArticles WHERE RankedArticles.id = %d)`, p.Args["after"].(int))
|
|
}
|
|
|
|
var limit int = 10
|
|
if p.Args["first"] != nil {
|
|
limit = p.Args["first"].(int)
|
|
} else if p.Args["last"] != nil {
|
|
limit = p.Args["last"].(int)
|
|
}
|
|
|
|
sql = fmt.Sprintf(`
|
|
WITH RankedArticles AS (%s)
|
|
SELECT * FROM web_article INNER JOIN(
|
|
SELECT id, row_num FROM RankedArticles %s
|
|
) AS LimitedRanked ON LimitedRanked.id = web_article.id
|
|
ORDER BY LimitedRanked.row_num ASC LIMIT %d
|
|
`, sql, cursor, limit)
|
|
|
|
if err := db.Raw(sql).Scan(&games).Error; err != nil {
|
|
fmt.Println("获取游戏列表失败", err)
|
|
return nil, err
|
|
}
|
|
|
|
return map[string]interface{}{
|
|
"list": games,
|
|
"total": total,
|
|
}, err
|
|
},
|
|
}
|