From 3d1a8d3884c50fa88a2c77a95a1107e5c77061c8 Mon Sep 17 00:00:00 2001 From: satori Date: Mon, 2 Dec 2024 12:43:59 +0800 Subject: [PATCH] first --- api/collect.go | 38 ++++++++++++++++++++++++++++------- api/struct.go | 54 -------------------------------------------------- bin/main.go | 16 +++++++-------- 3 files changed, 39 insertions(+), 69 deletions(-) delete mode 100644 api/struct.go diff --git a/api/collect.go b/api/collect.go index 1d5c346..fe4ebfa 100644 --- a/api/collect.go +++ b/api/collect.go @@ -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, diff --git a/api/struct.go b/api/struct.go deleted file mode 100644 index 8925211..0000000 --- a/api/struct.go +++ /dev/null @@ -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 -} diff --git a/bin/main.go b/bin/main.go index f3c4c97..7583b3a 100644 --- a/bin/main.go +++ b/bin/main.go @@ -238,14 +238,14 @@ 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, - "articles": api.ArticleItems, - "article": api.ArticleItem, - "images": api.ImageItems, - "image": api.ImageItem, + "users": api.UserItems, + "games": api.GameItems, + "works": api.WorkItems, + "collections": api.CollectionItems, + "articles": api.ArticleItems, + "article": api.ArticleItem, + "images": api.ImageItems, + "image": api.ImageItem, }})}) if err != nil {