处理字段
This commit is contained in:
@@ -93,6 +93,7 @@ func NewSchema() (graphql.Schema, error) {
|
|||||||
"rank": &graphql.ArgumentConfig{Type: graphql.String},
|
"rank": &graphql.ArgumentConfig{Type: graphql.String},
|
||||||
"create_time": &graphql.ArgumentConfig{Type: graphql.DateTime},
|
"create_time": &graphql.ArgumentConfig{Type: graphql.DateTime},
|
||||||
"update_time": &graphql.ArgumentConfig{Type: graphql.DateTime},
|
"update_time": &graphql.ArgumentConfig{Type: graphql.DateTime},
|
||||||
|
"text": &graphql.ArgumentConfig{Type: graphql.String}, // 查找图像中的文字
|
||||||
"first": &graphql.ArgumentConfig{Type: graphql.Int, DefaultValue: 10}, // 翻页参数
|
"first": &graphql.ArgumentConfig{Type: graphql.Int, DefaultValue: 10}, // 翻页参数
|
||||||
"after": &graphql.ArgumentConfig{Type: graphql.String, DefaultValue: "0"}, // 翻页参数
|
"after": &graphql.ArgumentConfig{Type: graphql.String, DefaultValue: "0"}, // 翻页参数
|
||||||
},
|
},
|
||||||
@@ -102,17 +103,20 @@ func NewSchema() (graphql.Schema, error) {
|
|||||||
for _, field := range requestedFields {
|
for _, field := range requestedFields {
|
||||||
fieldAST, ok := field.(*ast.Field)
|
fieldAST, ok := field.(*ast.Field)
|
||||||
if ok {
|
if ok {
|
||||||
if fieldAST.Name.Value == "list" {
|
switch fieldAST.Name.Value {
|
||||||
|
case "list":
|
||||||
for _, field := range fieldAST.SelectionSet.Selections {
|
for _, field := range fieldAST.SelectionSet.Selections {
|
||||||
fieldAST, ok := field.(*ast.Field)
|
fieldAST, ok := field.(*ast.Field)
|
||||||
if ok {
|
if ok {
|
||||||
fields = append(fields, fieldAST.Name.Value)
|
fields = append(fields, fieldAST.Name.Value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if fieldAST.Name.Value == "next" {
|
case "next":
|
||||||
fmt.Println("next")
|
fmt.Println("next")
|
||||||
} else {
|
case "text":
|
||||||
fields = append(fields, fieldAST.Name.Value)
|
fmt.Println("text")
|
||||||
|
default:
|
||||||
|
fmt.Println(fieldAST.Name.Value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -194,23 +198,25 @@ func NewSchema() (graphql.Schema, error) {
|
|||||||
for _, field := range requestedFields {
|
for _, field := range requestedFields {
|
||||||
fieldAST, ok := field.(*ast.Field)
|
fieldAST, ok := field.(*ast.Field)
|
||||||
if ok {
|
if ok {
|
||||||
if fieldAST.Name.Value == "list" {
|
switch fieldAST.Name.Value {
|
||||||
|
case "list":
|
||||||
for _, field := range fieldAST.SelectionSet.Selections {
|
for _, field := range fieldAST.SelectionSet.Selections {
|
||||||
fieldAST, ok := field.(*ast.Field)
|
fieldAST, ok := field.(*ast.Field)
|
||||||
if ok {
|
if ok {
|
||||||
if fieldAST.Name.Value == "user" {
|
switch fieldAST.Name.Value {
|
||||||
|
case "user":
|
||||||
fields = append(fields, "user_id")
|
fields = append(fields, "user_id")
|
||||||
} else if fieldAST.Name.Value == "article" {
|
case "article":
|
||||||
fields = append(fields, "article_id")
|
fields = append(fields, "article_id")
|
||||||
} else {
|
default:
|
||||||
fields = append(fields, fieldAST.Name.Value)
|
fields = append(fields, fieldAST.Name.Value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if fieldAST.Name.Value == "next" {
|
case "next":
|
||||||
fmt.Println("next")
|
fmt.Println("next")
|
||||||
} else {
|
default:
|
||||||
fields = append(fields, fieldAST.Name.Value)
|
fmt.Println(fieldAST.Name.Value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,8 +20,8 @@ type Image struct {
|
|||||||
CollectCount int `json:"collect_count" db:"collect_count"`
|
CollectCount int `json:"collect_count" db:"collect_count"`
|
||||||
ArticleID int `json:"article_id" db:"article_id"`
|
ArticleID int `json:"article_id" db:"article_id"`
|
||||||
UserID int `json:"user_id" db:"user_id"`
|
UserID int `json:"user_id" db:"user_id"`
|
||||||
User User `json:"user" db:"user"`
|
User User `json:"user" db:"-"`
|
||||||
Article Article `json:"article" db:"article"`
|
Article Article `json:"article" db:"-"`
|
||||||
CreateTime time.Time `json:"create_time" db:"create_time"`
|
CreateTime time.Time `json:"create_time" db:"create_time"`
|
||||||
UpdateTime time.Time `json:"update_time" db:"update_time"`
|
UpdateTime time.Time `json:"update_time" db:"update_time"`
|
||||||
}
|
}
|
||||||
@@ -41,3 +42,40 @@ type Article struct {
|
|||||||
CreateTime time.Time `json:"create_time" db:"create_time"`
|
CreateTime time.Time `json:"create_time" db:"create_time"`
|
||||||
UpdateTime time.Time `json:"update_time" db:"update_time"`
|
UpdateTime time.Time `json:"update_time" db:"update_time"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 利用反射获取每个结构体所有字段
|
||||||
|
func getFields(s interface{}) (fields []string) {
|
||||||
|
t := reflect.TypeOf(s)
|
||||||
|
for i := 0; i < t.NumField(); i++ {
|
||||||
|
fields = append(fields, t.Field(i).Name)
|
||||||
|
}
|
||||||
|
return fields
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查字段是否存在
|
||||||
|
func checkFields(fields []string, fields_list []string) bool {
|
||||||
|
for _, field := range fields {
|
||||||
|
if !contains(fields_list, field) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断字符串是否存在于字符串数组中
|
||||||
|
func contains(s []string, e string) bool {
|
||||||
|
for _, v := range s {
|
||||||
|
if v == e {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func QueryUserList(fields []string) (user_list []User) {
|
||||||
|
if !checkFields(fields, getFields(User{})) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
@@ -368,7 +368,6 @@ func main() {
|
|||||||
conditions.WriteString(fmt.Sprintf(" id IN (%s)", strings.Join(idsStr, ","))) // 拼接查询条件
|
conditions.WriteString(fmt.Sprintf(" id IN (%s)", strings.Join(idsStr, ","))) // 拼接查询条件
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//conditions.WriteString(fmt.Sprintf(" LIMIT %d, %d", (images.Page-1)*images.PageSize, images.PageSize)) // 拼接分页条件(已经从ids分页)
|
|
||||||
sql := fmt.Sprintf("SELECT id, width, height, content, update_time, create_time, user_id, article_id, article_category_top_id, praise_count, collect_count FROM web_images %s", conditions.String())
|
sql := fmt.Sprintf("SELECT id, width, height, content, update_time, create_time, user_id, article_id, article_category_top_id, praise_count, collect_count FROM web_images %s", conditions.String())
|
||||||
rows, err := mysqlConnection.Database.Query(sql)
|
rows, err := mysqlConnection.Database.Query(sql)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -56,6 +56,7 @@ type SearchData struct {
|
|||||||
took int
|
took int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取搜索结果的 ID 列表
|
||||||
func (sd *SearchData) GetIDList() (id_list []string) {
|
func (sd *SearchData) GetIDList() (id_list []string) {
|
||||||
for _, hit := range sd.Hits.Hits {
|
for _, hit := range sd.Hits.Hits {
|
||||||
id_list = append(id_list, hit.ID)
|
id_list = append(id_list, hit.ID)
|
||||||
@@ -63,6 +64,7 @@ func (sd *SearchData) GetIDList() (id_list []string) {
|
|||||||
return id_list
|
return id_list
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取搜索结果的内容列表
|
||||||
func ElasticsearchSearch(text string) (r *SearchData) {
|
func ElasticsearchSearch(text string) (r *SearchData) {
|
||||||
// 通过字符串构建查询
|
// 通过字符串构建查询
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
Reference in New Issue
Block a user