游戏 作品 文章的收藏点赞
This commit is contained in:
@@ -25,6 +25,8 @@
|
||||
- [ ] 從caddy日誌收集用戶訪問目標數據時間
|
||||
- [ ] 計算指標作爲API提供輸出
|
||||
- [ ] 數據與指標統覽後臺
|
||||
- [ ] 游戏 作品 文章 收藏夹 分别添加喜欢数点赞数是否喜欢是否点赞字段
|
||||
|
||||
|
||||
|
||||
```sql
|
||||
|
@@ -11,16 +11,18 @@ import (
|
||||
)
|
||||
|
||||
type Article struct {
|
||||
ID int `json:"id" db:"id" gorm:"primaryKey"`
|
||||
Title string `json:"title" db:"title"`
|
||||
Orientation string `json:"orientation" db:"orientation"`
|
||||
Device string `json:"device" db:"device"`
|
||||
Era string `json:"era" db:"era"`
|
||||
Tags string `json:"tags" db:"tags"`
|
||||
UserId int `json:"user_id" db:"user_id"`
|
||||
User User `json:"user" gorm:"foreignKey:UserId"`
|
||||
CreateTime time.Time `json:"create_time" db:"create_time"`
|
||||
UpdateTime time.Time `json:"update_time" db:"update_time"`
|
||||
ID int `json:"id" db:"id" gorm:"primaryKey"`
|
||||
Title string `json:"title" db:"title"`
|
||||
Orientation string `json:"orientation" db:"orientation"`
|
||||
Device string `json:"device" db:"device"`
|
||||
Era string `json:"era" db:"era"`
|
||||
Tags string `json:"tags" db:"tags"`
|
||||
UserId int `json:"user_id" db:"user_id"`
|
||||
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" db:"create_time"`
|
||||
UpdateTime time.Time `json:"update_time" db:"update_time"`
|
||||
}
|
||||
|
||||
func (Article) TableName() string {
|
||||
@@ -31,15 +33,43 @@ var articleType = graphql.NewObject(graphql.ObjectConfig{
|
||||
Name: "Article",
|
||||
Description: "文章",
|
||||
Fields: graphql.Fields{
|
||||
"id": &graphql.Field{Type: graphql.Int, Description: "ID"},
|
||||
"title": &graphql.Field{Type: graphql.String, Description: "标题"},
|
||||
"orientation": &graphql.Field{Type: graphql.String, Description: "方向"},
|
||||
"device": &graphql.Field{Type: graphql.String, Description: "设备"},
|
||||
"era": &graphql.Field{Type: graphql.String, Description: "游戏上线年份"},
|
||||
"tags": &graphql.Field{Type: graphql.String, Description: "标签"},
|
||||
"user": &graphql.Field{Type: userType, Description: "所属用户"},
|
||||
"create_time": &graphql.Field{Type: graphql.DateTime, Description: "创建时间"},
|
||||
"update_time": &graphql.Field{Type: graphql.DateTime, Description: "更新时间"},
|
||||
"id": &graphql.Field{Type: graphql.Int, Description: "ID"},
|
||||
"title": &graphql.Field{Type: graphql.String, Description: "标题"},
|
||||
"orientation": &graphql.Field{Type: graphql.String, Description: "方向"},
|
||||
"device": &graphql.Field{Type: graphql.String, Description: "设备"},
|
||||
"era": &graphql.Field{Type: graphql.String, Description: "游戏上线年份"},
|
||||
"tags": &graphql.Field{Type: graphql.String, Description: "标签"},
|
||||
"user": &graphql.Field{Type: userType, 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.(Article).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.(Article).ID).Where("type = ?", 0).Count(&collect); err != nil {
|
||||
return false, nil
|
||||
}
|
||||
if collect > 0 {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}},
|
||||
"text_count": &graphql.Field{Type: graphql.Int, Description: "文字数量", Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
||||
var count int64
|
||||
err := db.Table("web_images").Where("article_id = ?", p.Source.(Article).ID).Where("text != ''").Count(&count).Error
|
||||
@@ -94,7 +124,8 @@ var ArticleItems = &graphql.Field{
|
||||
var total int
|
||||
var err error
|
||||
|
||||
var query = goqu.Dialect("mysql").From("web_article")
|
||||
// 文章的顶级ID为9
|
||||
var query = goqu.Dialect("mysql").From("web_article").Where(goqu.Ex{"category_id": 10})
|
||||
|
||||
// 筛选条件
|
||||
for _, format := range []string{"id", "style", "device", "orientation", "era", "category_id", "tags"} {
|
||||
@@ -116,13 +147,13 @@ var ArticleItems = &graphql.Field{
|
||||
// 如果没有外部排序则使用指定排序(正则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_images.id", goqu.L(
|
||||
fmt.Sprintf("ROW_NUMBER() OVER(ORDER BY web_images.%s %s)", sort, p.Args["order"]),
|
||||
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"))
|
||||
}
|
||||
|
||||
// 取所有数据的前N条
|
||||
sql, _, _ := query.Where(goqu.Ex{"article_category_top_id": 9}).ToSQL()
|
||||
sql, _, _ := query.ToSQL()
|
||||
fmt.Println(sql)
|
||||
|
||||
// 遊標截取篩選結果集的前N条
|
||||
@@ -140,13 +171,14 @@ var ArticleItems = &graphql.Field{
|
||||
|
||||
sql = fmt.Sprintf(`
|
||||
WITH RankedArticles AS (%s)
|
||||
SELECT * FROM web_images INNER JOIN(
|
||||
SELECT * FROM web_article INNER JOIN(
|
||||
SELECT id, row_num FROM RankedArticles %s
|
||||
) AS LimitedRanked ON LimitedRanked.id = web_images.id
|
||||
) AS LimitedRanked ON LimitedRanked.id = web_article.id
|
||||
ORDER BY LimitedRanked.row_num ASC LIMIT %d
|
||||
`, sql, cursor, limit)
|
||||
|
||||
if err := db.Limit(limit).Where("category_top_id = 9").Find(&articles).Error; err != nil {
|
||||
if err := db.Raw(sql).Scan(&articles).Error; err != nil {
|
||||
fmt.Println("获取游戏列表失败", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
29
api/game.go
29
api/game.go
@@ -11,19 +11,21 @@ import (
|
||||
)
|
||||
|
||||
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"`
|
||||
CreateTime time.Time `json:"create_time"`
|
||||
UpdateTime time.Time `json:"update_time"`
|
||||
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 {
|
||||
@@ -106,6 +108,7 @@ var GameItems = &graphql.Field{
|
||||
var total int
|
||||
var err error
|
||||
|
||||
// 游戏的顶级ID为 22
|
||||
var query = goqu.Dialect("mysql").From("web_article").Where(goqu.Ex{"category_top_id": 22})
|
||||
|
||||
// 筛选条件
|
||||
|
73
api/work.go
73
api/work.go
@@ -2,7 +2,6 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
@@ -11,19 +10,21 @@ import (
|
||||
)
|
||||
|
||||
type Work 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"`
|
||||
CreateTime time.Time `json:"create_time"`
|
||||
UpdateTime time.Time `json:"update_time"`
|
||||
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 (Work) TableName() string {
|
||||
@@ -40,8 +41,32 @@ var workType = graphql.NewObject(graphql.ObjectConfig{
|
||||
"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: "当前用户是否点赞"},
|
||||
"collect": &graphql.Field{Type: graphql.Boolean, 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.(Work).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.(Work).ID).Where("type = ?", 0).Count(&collect); err != nil {
|
||||
return false, nil
|
||||
}
|
||||
if collect > 0 {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}},
|
||||
},
|
||||
})
|
||||
|
||||
@@ -74,7 +99,7 @@ var WorkItems = &graphql.Field{
|
||||
var total int
|
||||
var err error
|
||||
|
||||
var query = goqu.Dialect("mysql").From("web_article")
|
||||
var query = goqu.Dialect("mysql").From("web_article").Where(goqu.Ex{"category_top_id": 1})
|
||||
|
||||
// 筛选条件
|
||||
for _, format := range []string{"id", "tags"} {
|
||||
@@ -96,13 +121,13 @@ var WorkItems = &graphql.Field{
|
||||
// 如果没有外部排序则使用指定排序(正则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_images.id", goqu.L(
|
||||
fmt.Sprintf("ROW_NUMBER() OVER(ORDER BY web_images.%s %s)", sort, p.Args["order"]),
|
||||
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"))
|
||||
}
|
||||
|
||||
// 取所有数据的前N条
|
||||
sql, _, _ := query.Where(goqu.Ex{"article_category_top_id": 1}).ToSQL()
|
||||
sql, _, _ := query.ToSQL()
|
||||
fmt.Println(sql)
|
||||
|
||||
// 遊標截取篩選結果集的前N条
|
||||
@@ -120,14 +145,14 @@ var WorkItems = &graphql.Field{
|
||||
|
||||
sql = fmt.Sprintf(`
|
||||
WITH RankedArticles AS (%s)
|
||||
SELECT * FROM web_images INNER JOIN(
|
||||
SELECT * FROM web_article INNER JOIN(
|
||||
SELECT id, row_num FROM RankedArticles %s
|
||||
) AS LimitedRanked ON LimitedRanked.id = web_images.id
|
||||
) AS LimitedRanked ON LimitedRanked.id = web_article.id
|
||||
ORDER BY LimitedRanked.row_num ASC LIMIT %d
|
||||
`, sql, cursor, limit)
|
||||
|
||||
if err := db.Limit(limit).Where("category_top_id = 1").Find(&works).Error; err != nil {
|
||||
log.Println("获取作品列表失败", err)
|
||||
if err := db.Raw(sql).Scan(&works).Error; err != nil {
|
||||
fmt.Println("获取游戏列表失败", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user