101 lines
4.4 KiB
Go
101 lines
4.4 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/graphql-go/graphql"
|
|
"github.com/thoas/go-funk"
|
|
)
|
|
|
|
type Collection struct {
|
|
ID int `json:"id" gorm:"primaryKey"`
|
|
Title string `json:"title"`
|
|
Content string `json:"content"`
|
|
Type int `json:"type"`
|
|
Thumbnail string `json:"thumbnail"`
|
|
Num int `json:"num"`
|
|
Fans int `json:"fans"`
|
|
UserId int `json:"user_id"`
|
|
ArticleId int `json:"article_id"`
|
|
CreateTime time.Time `json:"create_time"`
|
|
UpdateTime time.Time `json:"update_time"`
|
|
User User `json:"user" gorm:"foreignKey:UserId;references:ID"`
|
|
PraiseCount int `json:"praise_count" gorm:"default:0"`
|
|
Praise bool `json:"praise" gorm:"-"`
|
|
}
|
|
|
|
func (Collection) TableName() string {
|
|
return "web_member_explorer"
|
|
}
|
|
|
|
var CollectionItems = &graphql.Field{
|
|
Name: "collections",
|
|
Description: "收藏列表",
|
|
Type: graphql.NewObject(graphql.ObjectConfig{
|
|
Name: "CollectConnection",
|
|
Description: "条件筛选收藏列表",
|
|
Fields: graphql.Fields{
|
|
"list": &graphql.Field{Type: graphql.NewList(graphql.NewObject(graphql.ObjectConfig{
|
|
Name: "Collect",
|
|
Description: "收藏",
|
|
Fields: graphql.Fields{
|
|
"id": &graphql.Field{Type: graphql.Int, Description: "收藏ID"},
|
|
"type": &graphql.Field{Type: graphql.Int, Description: "收藏类型"},
|
|
"title": &graphql.Field{Type: graphql.String, Description: "收藏标题"},
|
|
"content": &graphql.Field{Type: graphql.String, Description: "收藏内容"},
|
|
"thumbnail": &graphql.Field{Type: graphql.String, Description: "收藏缩略图"},
|
|
"num": &graphql.Field{Type: graphql.Int, Description: "收藏数量"},
|
|
"fans": &graphql.Field{Type: graphql.Int, 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: "点赞数"},
|
|
"praise": &graphql.Field{Type: graphql.Boolean, Description: "当前用户是否点赞"},
|
|
"user": &graphql.Field{Type: userType, Description: "收藏用户"},
|
|
},
|
|
})), Description: "收藏列表"},
|
|
"total": &graphql.Field{Type: graphql.Int, Description: "收藏总数"},
|
|
},
|
|
}),
|
|
Args: graphql.FieldConfigArgument{
|
|
"id": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选收藏中指定ID的"},
|
|
"title": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选收藏中含有指定标题的"},
|
|
"type": &graphql.ArgumentConfig{Type: graphql.Int, 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 collects []Collection
|
|
var total int
|
|
var limit int = 10
|
|
|
|
if p.Args["first"] != nil {
|
|
limit = p.Args["first"].(int)
|
|
}
|
|
|
|
if err := db.Limit(limit).Preload("User").Find(&collects).Error; err != nil {
|
|
fmt.Println(err.Error())
|
|
return nil, err
|
|
}
|
|
|
|
var items = ListItem(p.Info.FieldASTs[0].SelectionSet.Selections)
|
|
if funk.Contains(items, "praise_count") {
|
|
for i := range collects {
|
|
collects[i].PraiseCount = collects[i].Fans
|
|
collects[i].Praise = false
|
|
}
|
|
}
|
|
|
|
return map[string]interface{}{
|
|
"list": collects,
|
|
"total": total,
|
|
}, nil
|
|
},
|
|
}
|