From c8a8da2e25850d9ed1fc0e693eb63c2fa764a6d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=89?= Date: Mon, 20 Nov 2023 23:56:49 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/graphql.go | 90 +++++++++++++++++++++++++------------------------- api/struct.go | 34 +++++++++++-------- 2 files changed, 65 insertions(+), 59 deletions(-) diff --git a/api/graphql.go b/api/graphql.go index a48c509..929a71e 100644 --- a/api/graphql.go +++ b/api/graphql.go @@ -120,32 +120,41 @@ func NewSchema() (graphql.Schema, error) { }, }, Resolve: func(p graphql.ResolveParams) (interface{}, error) { + // 返回字段 var fields []string requestedFields := p.Info.FieldASTs[0].SelectionSet.Selections for _, field := range requestedFields { fieldAST, ok := field.(*ast.Field) if ok { - fields = append(fields, fieldAST.Name.Value) + if fieldAST.Name.Value == "user" { + fields = append(fields, "user_id") + } else if fieldAST.Name.Value == "article" { + fields = append(fields, "article_category_top_id") + } else { + fields = append(fields, fieldAST.Name.Value) + } } } fields_str := strings.Join(fields, ",") + + // 筛选条件 var where []string if p.Args["id"] != nil { where = append(where, fmt.Sprintf("id=%d", p.Args["id"])) } if p.Args["width"] != nil { - where = append(where, fmt.Sprintf("width='%s'", p.Args["width"])) + where = append(where, fmt.Sprintf("width=%d", p.Args["width"])) } if p.Args["height"] != nil { - where = append(where, fmt.Sprintf("height='%s'", p.Args["height"])) + where = append(where, fmt.Sprintf("height=%d", p.Args["height"])) } if p.Args["content"] != nil { where = append(where, fmt.Sprintf("content='%s'", p.Args["content"])) } - // 筛选条件 where_str := strings.Join(where, " AND ") fmt.Println(where_str) + // 执行查询 var query strings.Builder query.WriteString(fmt.Sprintf("SELECT %s FROM web_images WHERE %s LIMIT %s", fields_str, where_str, "10")) fmt.Println(query.String()) @@ -155,48 +164,39 @@ func NewSchema() (graphql.Schema, error) { fmt.Println("获取图像列表失败", err) return nil, err } - fmt.Println(images) + + // 获取用户信息(如果图像列表不为空且请求字段中包含user) + if len(images) > 0 && strings.Contains(fields_str, "user_id") { + // 取到所有的用户ID, 去除重复 + user_ids := make(map[int]bool) + for _, image := range images { + user_ids[image.UserID] = true + } + // map 转换为数组 + uniqueIds := make([]int, 0, len(user_ids)) + for id := range user_ids { + uniqueIds = append(uniqueIds, id) + } + // 合并为以逗号分隔的字符串 + user_ids_str := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(uniqueIds)), ","), "[]") + fmt.Println(user_ids_str) + // 查询用户信息 + var users []User + if err := connection.Select(&users, fmt.Sprintf("SELECT id,user_name,avatar,rank,create_time,update_time FROM web_member WHERE id IN (%s)", user_ids_str)); err != nil { + fmt.Println("获取用户列表失败", err) + return nil, err + } + // 将用户信息与图像信息关联 + for i, image := range images { + for _, user := range users { + if image.UserID == user.ID { + images[i].User = user + } + } + } + } + return images, nil - //return []interface{}{ - // map[string]interface{}{ - // "id": 1, - // "width": 100, - // "height": 100, - // "content": "content", - // "article_category_top_id": 1, - // "praise_count": 1, - // "collect_count": 1, - // "create_time": "2018-01-01 00:00:00", - // "update_time": "2018-01-01 00:00:00", - // "user": map[string]interface{}{ - // "id": 1, - // "name": "user1", - // "age": 10, - // "info": "info", - // "price": 1.1, - // "avatar": "", - // }, - // }, - // map[string]interface{}{ - // "id": 2, - // "width": 100, - // "height": 100, - // "content": "content", - // "article_category_top_id": 1, - // "praise_count": 1, - // "collect_count": 1, - // "create_time": "2018-01-01 00:00:00", - // "update_time": "2018-01-01 00:00:00", - // "user": map[string]interface{}{ - // "id": 2, - // "name": "user2", - // "age": 20, - // "info": "info", - // "price": 2.2, - // "avatar": "", - // }, - // }, - //}, nil }, } diff --git a/api/struct.go b/api/struct.go index 2461459..e9322a2 100644 --- a/api/struct.go +++ b/api/struct.go @@ -2,23 +2,21 @@ package api import ( "time" - - "git.satori.love/gameui/webp/models" ) type Image struct { - Id int `json:"id" db:"id"` - Width int `json:"width" db:"width"` - Height int `json:"height" db:"height"` - Content string `json:"content" db:"content"` - ArticleCategoryTopId int `json:"article_category_top_id" db:"article_category_top_id"` - PraiseCount int `json:"praise_count" db:"praise_count"` - CollectCount int `json:"collect_count" db:"collect_count"` - UserID int `json:"user_id" db:"user_id"` - User models.User `json:"user" db:"user"` - Article models.Article `json:"article" db:"article"` - CreateTime time.Time `json:"createTime" db:"createTime"` - UpdateTime time.Time `json:"updateTime" db:"updateTime"` + Id int `json:"id" db:"id"` + Width int `json:"width" db:"width"` + Height int `json:"height" db:"height"` + Content string `json:"content" db:"content"` + ArticleCategoryTopId int `json:"article_category_top_id" db:"article_category_top_id"` + PraiseCount int `json:"praise_count" db:"praise_count"` + CollectCount int `json:"collect_count" db:"collect_count"` + UserID int `json:"user_id" db:"user_id"` + User User `json:"user" db:"user"` + Article Article `json:"article" db:"article"` + CreateTime time.Time `json:"create_time" db:"create_time"` + UpdateTime time.Time `json:"update_time" db:"update_time"` } type User struct { @@ -29,3 +27,11 @@ type User struct { CreateTime time.Time `json:"create_time" db:"create_time"` UpdateTime time.Time `json:"update_time" db:"update_time"` } + +type Article struct { + Id int `json:"id" db:"id"` + Title string `json:"title" db:"title"` + Tags string `json:"tags" db:"tags"` + CreateTime time.Time `json:"create_time" db:"create_time"` + UpdateTime time.Time `json:"update_time" db:"update_time"` +}