diff --git a/api/collect.go b/api/collect.go index ada4a91..d558b3c 100644 --- a/api/collect.go +++ b/api/collect.go @@ -5,21 +5,24 @@ import ( "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"` + 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 { @@ -79,6 +82,14 @@ var CollectionItems = &graphql.Field{ return nil, err } + items := LoadItem(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, diff --git a/api/graphql.go b/api/graphql.go index 5f7803c..577a2d4 100644 --- a/api/graphql.go +++ b/api/graphql.go @@ -124,6 +124,19 @@ func CheckColorNullRows(offset int) { } } +// 获取 List 中的所有字段名 +func ListItem(requestedFields []ast.Selection) (data []string) { + for _, field := range requestedFields { + fieldAST, _ := field.(*ast.Field) + if fieldAST.Name.Value == "list" { + for _, str := range LoadItem(fieldAST.SelectionSet.Selections) { + data = append(data, str) + } + } + } + return data +} + // 获取所有字段名 func LoadItem(requestedFields []ast.Selection) (data []string) { var items = []string{"user", "article"}