diff --git a/api/article.go b/api/article.go index c21efa4..c77b5ee 100644 --- a/api/article.go +++ b/api/article.go @@ -1,8 +1,11 @@ package api import ( + "encoding/json" "fmt" + "io" "log" + "net/http" "regexp" "strings" "time" @@ -32,6 +35,7 @@ type Article struct { Emoji3 int `json:"emoji3"` Emoji4 int `json:"emoji4"` Emoji5 int `json:"emoji5"` + Views int `json:"views"` } func (Article) TableName() string { @@ -60,6 +64,7 @@ var articleType = graphql.NewObject(graphql.ObjectConfig{ "emoji3": &graphql.Field{Type: graphql.Int, Description: "表情3数量"}, "emoji4": &graphql.Field{Type: graphql.Int, Description: "表情4数量"}, "emoji5": &graphql.Field{Type: graphql.Int, Description: "表情5数量"}, + "views": &graphql.Field{Type: graphql.Int, Description: "浏览量"}, }, }) @@ -211,6 +216,60 @@ var ArticleItems = &graphql.Field{ return nil, err } + var items = ListItem(p.Info.FieldASTs[0].SelectionSet.Selections) + if funk.Contains(items, "views") { + type ApiResponse struct { + ID int `json:"id"` + Count int `json:"count"` + } + + // 0. 收集要查询的 ID + var ids []int + for x := range articles { + ids = append(ids, articles[x].ID) + } + idx := strings.Trim(strings.Replace(fmt.Sprint(ids), " ", ",", -1), "[]") + + // 1. 发送 GET 请求 + resp, err := http.Get("http://localhost:6005/api/get_views/文章?ids=" + idx) + if err != nil { + fmt.Println("Error making GET request:", err) + return nil, err + } + defer resp.Body.Close() + + // 2. 检查 HTTP 状态码 + if resp.StatusCode != http.StatusOK { + fmt.Printf("Request failed with status code: %d\n", resp.StatusCode) + return nil, err + } + + // 3. 读取响应体 + body, err := io.ReadAll(resp.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + return nil, err + } + + // 4. 解析 JSON 数据到结构体 + var data []ApiResponse + err = json.Unmarshal(body, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + return nil, err + } + + // 5. 赋值到数据集 + for _, item := range data { + for i := range articles { + if articles[i].ID == item.ID { + articles[i].Views = item.Count + continue + } + } + } + } + return map[string]interface{}{ "list": articles, "total": total, diff --git a/api/game.go b/api/game.go index 33a2e05..c53393e 100644 --- a/api/game.go +++ b/api/game.go @@ -1,7 +1,10 @@ package api import ( + "encoding/json" "fmt" + "io" + "net/http" "regexp" "strings" "time" @@ -38,6 +41,7 @@ type Game struct { Emoji3 int `json:"emoji3"` Emoji4 int `json:"emoji4"` Emoji5 int `json:"emoji5"` + Views int `json:"views"` } func (Game) TableName() string { @@ -70,6 +74,7 @@ var gameType = graphql.NewObject(graphql.ObjectConfig{ "emoji3": &graphql.Field{Type: graphql.Int, Description: "表情3数量"}, "emoji4": &graphql.Field{Type: graphql.Int, Description: "表情4数量"}, "emoji5": &graphql.Field{Type: graphql.Int, Description: "表情5数量"}, + "views": &graphql.Field{Type: graphql.Int, Description: "浏览量"}, }, }) @@ -251,6 +256,60 @@ var GameItems = &graphql.Field{ } } + var items = ListItem(p.Info.FieldASTs[0].SelectionSet.Selections) + if funk.Contains(items, "views") { + type ApiResponse struct { + ID int `json:"id"` + Count int `json:"count"` + } + + // 0. 收集要查询的 ID + var ids []int + for x := range games { + ids = append(ids, games[x].ID) + } + idx := strings.Trim(strings.Replace(fmt.Sprint(ids), " ", ",", -1), "[]") + + // 1. 发送 GET 请求 + resp, err := http.Get("http://localhost:6005/api/get_views/文章?ids=" + idx) + if err != nil { + fmt.Println("Error making GET request:", err) + return nil, err + } + defer resp.Body.Close() + + // 2. 检查 HTTP 状态码 + if resp.StatusCode != http.StatusOK { + fmt.Printf("Request failed with status code: %d\n", resp.StatusCode) + return nil, err + } + + // 3. 读取响应体 + body, err := io.ReadAll(resp.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + return nil, err + } + + // 4. 解析 JSON 数据到结构体 + var data []ApiResponse + err = json.Unmarshal(body, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + return nil, err + } + + // 5. 赋值到数据集 + for _, item := range data { + for i := range games { + if games[i].ID == item.ID { + games[i].Views = item.Count + continue + } + } + } + } + return map[string]interface{}{ "list": games, "total": total, diff --git a/api/image.go b/api/image.go index c4197f2..0c66916 100644 --- a/api/image.go +++ b/api/image.go @@ -5,7 +5,9 @@ import ( "database/sql/driver" "encoding/json" "fmt" + "io" "log" + "net/http" "regexp" "strconv" "strings" @@ -14,6 +16,7 @@ import ( "git.satori.love/gameui/webp/models" "github.com/doug-martin/goqu/v9" "github.com/graphql-go/graphql" + "github.com/thoas/go-funk" "github.com/zhenghaoz/gorse/client" ) @@ -42,6 +45,7 @@ type Image struct { Emoji3 int `json:"emoji3"` Emoji4 int `json:"emoji4"` Emoji5 int `json:"emoji5"` + Views int `json:"views"` } func (Image) TableName() string { @@ -161,6 +165,7 @@ var imageType = graphql.NewObject(graphql.ObjectConfig{ "emoji3": &graphql.Field{Type: graphql.Int, Description: "表情3数量"}, "emoji4": &graphql.Field{Type: graphql.Int, Description: "表情4数量"}, "emoji5": &graphql.Field{Type: graphql.Int, Description: "表情5数量"}, + "views": &graphql.Field{Type: graphql.Int, Description: "浏览量"}, }, }) @@ -616,6 +621,60 @@ var ImageItems = &graphql.Field{ return nil, err } + var items = ListItem(p.Info.FieldASTs[0].SelectionSet.Selections) + if funk.Contains(items, "views") { + type ApiResponse struct { + ID int `json:"id"` + Count int `json:"count"` + } + + // 0. 收集要查询的 ID + var ids []int + for x := range images { + ids = append(ids, images[x].ID) + } + idx := strings.Trim(strings.Replace(fmt.Sprint(ids), " ", ",", -1), "[]") + + // 1. 发送 GET 请求 + resp, err := http.Get("http://localhost:6005/api/get_views/截图?ids=" + idx) + if err != nil { + fmt.Println("Error making GET request:", err) + return nil, err + } + defer resp.Body.Close() + + // 2. 检查 HTTP 状态码 + if resp.StatusCode != http.StatusOK { + fmt.Printf("Request failed with status code: %d\n", resp.StatusCode) + return nil, err + } + + // 3. 读取响应体 + body, err := io.ReadAll(resp.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + return nil, err + } + + // 4. 解析 JSON 数据到结构体 + var data []ApiResponse + err = json.Unmarshal(body, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + return nil, err + } + + // 5. 赋值到数据集 + for _, item := range data { + for i := range images { + if images[i].ID == item.ID { + images[i].Views = item.Count + continue + } + } + } + } + return map[string]interface{}{ "list": images, "total": total, diff --git a/api/work.go b/api/work.go index d68cdbf..72f9de4 100644 --- a/api/work.go +++ b/api/work.go @@ -1,7 +1,10 @@ package api import ( + "encoding/json" "fmt" + "io" + "net/http" "regexp" "strings" "time" @@ -34,6 +37,7 @@ type Work struct { Emoji3 int `json:"emoji3"` Emoji4 int `json:"emoji4"` Emoji5 int `json:"emoji5"` + Views int `json:"views"` } func (Work) TableName() string { @@ -59,6 +63,7 @@ var workType = graphql.NewObject(graphql.ObjectConfig{ "emoji3": &graphql.Field{Type: graphql.Int, Description: "表情3数量"}, "emoji4": &graphql.Field{Type: graphql.Int, Description: "表情4数量"}, "emoji5": &graphql.Field{Type: graphql.Int, Description: "表情5数量"}, + "views": &graphql.Field{Type: graphql.Int, Description: "浏览量"}, }, }) @@ -212,6 +217,60 @@ var WorkItems = &graphql.Field{ } } + var items = ListItem(p.Info.FieldASTs[0].SelectionSet.Selections) + if funk.Contains(items, "views") { + type ApiResponse struct { + ID int `json:"id"` + Count int `json:"count"` + } + + // 0. 收集要查询的 ID + var ids []int + for x := range works { + ids = append(ids, works[x].ID) + } + idx := strings.Trim(strings.Replace(fmt.Sprint(ids), " ", ",", -1), "[]") + + // 1. 发送 GET 请求 + resp, err := http.Get("http://localhost:6005/api/get_views/文章?ids=" + idx) + if err != nil { + fmt.Println("Error making GET request:", err) + return nil, err + } + defer resp.Body.Close() + + // 2. 检查 HTTP 状态码 + if resp.StatusCode != http.StatusOK { + fmt.Printf("Request failed with status code: %d\n", resp.StatusCode) + return nil, err + } + + // 3. 读取响应体 + body, err := io.ReadAll(resp.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + return nil, err + } + + // 4. 解析 JSON 数据到结构体 + var data []ApiResponse + err = json.Unmarshal(body, &data) + if err != nil { + fmt.Println("Error unmarshalling JSON:", err) + return nil, err + } + + // 5. 赋值到数据集 + for _, item := range data { + for i := range works { + if works[i].ID == item.ID { + works[i].Views = item.Count + continue + } + } + } + } + return map[string]interface{}{ "list": works, "total": total,