From 6bc7acba47ce11abef0d3ecd85eb2b05eb428dd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=89?= Date: Wed, 31 Jul 2024 15:03:10 +0800 Subject: [PATCH] articles --- api/graphql.go | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ bin/main.go | 9 +++-- 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/api/graphql.go b/api/graphql.go index 878982d..db9c6b5 100644 --- a/api/graphql.go +++ b/api/graphql.go @@ -343,6 +343,97 @@ func NewSchema(config Config) (graphql.Schema, error) { }, nil }, }, + "articles": &graphql.Field{ + Type: graphql.NewObject(graphql.ObjectConfig{ + Name: "ArticleConnection", + Fields: graphql.Fields{ + "list": &graphql.Field{Type: graphql.NewList(article)}, + "total": &graphql.Field{Type: graphql.Int}, + }, + }), + Args: graphql.FieldConfigArgument{ + "id": &graphql.ArgumentConfig{Type: graphql.Int}, + "title": &graphql.ArgumentConfig{Type: graphql.String}, + "tags": &graphql.ArgumentConfig{Type: graphql.String}, + "create_time": &graphql.ArgumentConfig{Type: graphql.DateTime}, + "update_time": &graphql.ArgumentConfig{Type: graphql.DateTime}, + }, + 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 { + switch fieldAST.Name.Value { + case "list": + for _, field := range fieldAST.SelectionSet.Selections { + fieldAST, ok := field.(*ast.Field) + if ok { + fields = append(fields, fieldAST.Name.Value) + } + } + case "total": + fmt.Println("total") + default: + fmt.Println(fieldAST.Name.Value) + } + } + } + first := p.Args["first"] + after := p.Args["after"] + 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["title"] != nil { + where = append(where, fmt.Sprintf("title='%s'", p.Args["title"])) + } + // 筛选条件 + where_str := strings.Join(where, " AND ") + if where_str != "" { + where_str = "WHERE " + where_str + } + + var query strings.Builder + query.WriteString(fmt.Sprintf("SELECT %s FROM web_article %s LIMIT %d OFFSET %s", fields_str, where_str, first, after)) + // 返回翻页信息 + var articles []Article + if err := connection.Select(&articles, query.String()); err != nil { + fmt.Println("获取文章列表失败", err) + return nil, err + } + return map[string]interface{}{ + "list": articles, + "total": 0, + }, nil + }, + }, + "tags": &graphql.Field{ + Type: graphql.NewObject(graphql.ObjectConfig{ + Name: "TagConnection", + Fields: graphql.Fields{ + "list": &graphql.Field{Type: graphql.NewList(graphql.String)}, + "total": &graphql.Field{Type: graphql.Int}, + }, + }), + Args: graphql.FieldConfigArgument{ + "first": &graphql.ArgumentConfig{Type: graphql.Int, DefaultValue: 10}, + "after": &graphql.ArgumentConfig{Type: graphql.String, DefaultValue: "0"}, + "before": &graphql.ArgumentConfig{Type: graphql.String}, + }, + Resolve: func(p graphql.ResolveParams) (interface{}, error) { + var tags []string + if err := connection.Select(&tags, "SELECT DISTINCT tags FROM web_images LIMIT 10"); err != nil { + fmt.Println("获取标签列表失败", err) + return nil, err + } + return map[string]interface{}{ + "list": tags, + "total": 0, + }, nil + }, + }, }})}) if err != nil { return schema, err diff --git a/bin/main.go b/bin/main.go index 5ea9970..919ee25 100644 --- a/bin/main.go +++ b/bin/main.go @@ -316,6 +316,13 @@ func main() { // 文字搜索支持翻页 // 文字搜索支持与按颜色筛选混合 + // TODO 查找含有指定文字的图像 + // TODO 查找含有指定标签的图像 + // TODO 查找含有指定特征的图像 + // TODO 查找含有指定颜色的图像(倒排索引) + // TODO 查找含有指定分类的图像 + // 1,000,000 + // 获取查询条件(忽略空值) QueryConditions := func(key string) (list []string) { for _, item := range strings.Split(r.URL.Query().Get(key), ",") { @@ -350,8 +357,6 @@ func main() { images.Total = 0 images.Next = false images.List = make([]interface{}, 0) - - // 将对象转换为有缩进的JSON输出 data, _ := json.MarshalIndent(images, "", " ") w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.Write(data)