This commit is contained in:
2024-12-02 12:43:59 +08:00
parent 1d78500109
commit 3d1a8d3884
3 changed files with 39 additions and 69 deletions

View File

@@ -1,25 +1,33 @@
package api
import (
"fmt"
"time"
"github.com/graphql-go/graphql"
)
type Collect struct {
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"`
}
func (Collect) TableName() string {
return "web_collect"
func (Collection) TableName() string {
return "web_member_explorer"
}
var CollectItems = &graphql.Field{
Name: "collects",
var CollectionItems = &graphql.Field{
Name: "collections",
Description: "收藏列表",
Type: graphql.NewObject(graphql.ObjectConfig{
Name: "CollectConnection",
@@ -30,14 +38,19 @@ var CollectItems = &graphql.Field{
Description: "收藏",
Fields: graphql.Fields{
"id": &graphql.Field{Type: graphql.Int, Description: "收藏ID"},
"title": &graphql.Field{Type: graphql.String, Description: "收藏标题"},
"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: "点赞数"},
"collect_count": &graphql.Field{Type: graphql.Int, Description: "收藏数"},
"praise": &graphql.Field{Type: graphql.Boolean, Description: "当前用户是否点赞"},
"collect": &graphql.Field{Type: graphql.Boolean, Description: "当前用户是否收藏"},
"user": &graphql.Field{Type: userType, Description: "收藏用户"},
},
})), Description: "收藏列表"},
"total": &graphql.Field{Type: graphql.Int, Description: "收藏总数"},
@@ -55,8 +68,19 @@ var CollectItems = &graphql.Field{
"before": &graphql.ArgumentConfig{Type: graphql.String, Description: "翻页参数(傳回清單中指定遊標之前的元素)"},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
var collects []Collect
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
}
return map[string]interface{}{
"list": collects,
"total": total,

View File

@@ -1,54 +0,0 @@
package api
import (
"fmt"
"strconv"
"strings"
)
type IDS []int
// 合并为以逗号分隔的字符串
func (ids IDS) ToString() (str string) {
return strings.Trim(strings.Join(strings.Fields(fmt.Sprint(ids)), ","), "[]")
}
type ImageList []Image
// 按照ID排序
func (image *ImageList) SortByIDList(id_list []string) {
var sortedImageList ImageList
for _, id := range id_list {
id_number, _ := strconv.Atoi(id)
for _, image := range *image {
if image.ID == id_number {
sortedImageList = append(sortedImageList, image)
}
}
}
*image = sortedImageList
}
// 取到所有的文章ID, 去除重复
func (images *ImageList) ToAllArticleID() (uniqueIds IDS) {
article_ids := make(map[int]bool)
for _, image := range *images {
article_ids[image.ArticleID] = true
}
for id := range article_ids {
uniqueIds = append(uniqueIds, id)
}
return uniqueIds
}
// 取到所有的用户ID, 去除重复
func (images *ImageList) ToAllUserID() (uniqueIds IDS) {
user_ids := make(map[int]bool)
for _, image := range *images {
user_ids[image.UserID] = true
}
for id := range user_ids {
uniqueIds = append(uniqueIds, id)
}
return uniqueIds
}

View File

@@ -239,9 +239,9 @@ func main() {
schema, err := graphql.NewSchema(graphql.SchemaConfig{Query: graphql.NewObject(graphql.ObjectConfig{Name: "RootQuery", Fields: graphql.Fields{
"users": api.UserItems,
"collects": api.CollectItems,
"games": api.GameItems,
"works": api.WorkItems,
"collections": api.CollectionItems,
"articles": api.ArticleItems,
"article": api.ArticleItem,
"images": api.ImageItems,