order
This commit is contained in:
@@ -6,23 +6,23 @@ import (
|
|||||||
|
|
||||||
"github.com/graphql-go/graphql"
|
"github.com/graphql-go/graphql"
|
||||||
"github.com/thoas/go-funk"
|
"github.com/thoas/go-funk"
|
||||||
|
"gorm.io/gorm/clause"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Collection struct {
|
type Collection struct {
|
||||||
ID int `json:"id" gorm:"primaryKey"`
|
ID int `json:"id" gorm:"primaryKey"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
Type int `json:"type"`
|
Type int `json:"type"`
|
||||||
Thumbnail string `json:"thumbnail"`
|
Thumbnail string `json:"thumbnail"`
|
||||||
Num int `json:"num"`
|
Num int `json:"num"`
|
||||||
Fans int `json:"fans"`
|
Fans int `json:"fans"`
|
||||||
UserId int `json:"user_id"`
|
UserId int `json:"user_id"`
|
||||||
ArticleId int `json:"article_id"`
|
ArticleId int `json:"article_id"`
|
||||||
CreateTime time.Time `json:"create_time"`
|
CreateTime time.Time `json:"create_time"`
|
||||||
UpdateTime time.Time `json:"update_time"`
|
UpdateTime time.Time `json:"update_time"`
|
||||||
User User `json:"user" gorm:"foreignKey:UserId;references:ID"`
|
User User `json:"user" gorm:"foreignKey:UserId;references:ID"`
|
||||||
PraiseCount int `json:"praise_count" gorm:"default:0"`
|
Fan bool `json:"praise" gorm:"-"`
|
||||||
Praise bool `json:"praise" gorm:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Collection) TableName() string {
|
func (Collection) TableName() string {
|
||||||
@@ -78,16 +78,19 @@ var CollectionItems = &graphql.Field{
|
|||||||
limit = p.Args["first"].(int)
|
limit = p.Args["first"].(int)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.Limit(limit).Preload("User").Find(&collects).Error; err != nil {
|
order := clause.OrderByColumn{
|
||||||
|
Column: clause.Column{Name: p.Args["sort"].(string)},
|
||||||
|
Desc: p.Args["order"].(string) == "DESC",
|
||||||
|
}
|
||||||
|
if err := db.Limit(limit).Preload("User").Order(order).Find(&collects).Error; err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var items = ListItem(p.Info.FieldASTs[0].SelectionSet.Selections)
|
var items = ListItem(p.Info.FieldASTs[0].SelectionSet.Selections)
|
||||||
if funk.Contains(items, "praise_count") {
|
if funk.Contains(items, "fan") {
|
||||||
for i := range collects {
|
for i := range collects {
|
||||||
collects[i].PraiseCount = collects[i].Fans
|
collects[i].Fan = false
|
||||||
collects[i].Praise = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user