补充用户信息
This commit is contained in:
27
api/work.go
27
api/work.go
@@ -41,6 +41,8 @@ var workType = graphql.NewObject(graphql.ObjectConfig{
|
||||
Fields: graphql.Fields{
|
||||
"id": &graphql.Field{Type: graphql.Int, Description: "作品ID"},
|
||||
"title": &graphql.Field{Type: graphql.String, Description: "作品标题"},
|
||||
"tags": &graphql.Field{Type: graphql.String, Description: "作品标签"},
|
||||
"user": &graphql.Field{Type: userType, Description: "所属用户"},
|
||||
"create_time": &graphql.Field{Type: graphql.DateTime, Description: "作品创建时间"},
|
||||
"update_time": &graphql.Field{Type: graphql.DateTime, Description: "作品更新时间"},
|
||||
"praise": &graphql.Field{Type: graphql.Boolean, Description: "当前用户是否点赞"},
|
||||
@@ -173,10 +175,33 @@ var WorkItems = &graphql.Field{
|
||||
fmt.Println(sql)
|
||||
|
||||
if err := db.Raw(sql).Scan(&works).Error; err != nil {
|
||||
fmt.Println("获取游戏列表失败", err)
|
||||
fmt.Println("获取作品列表失败", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if funk.Contains(fields, "user") {
|
||||
var ids []int
|
||||
for _, game := range works {
|
||||
ids = append(ids, game.UserId)
|
||||
}
|
||||
ids = funk.UniqInt(ids)
|
||||
|
||||
var users []User
|
||||
if err := db.Table("web_member").Where("id in (?)", ids).Find(&users).Error; err != nil {
|
||||
fmt.Println("获取用户信息失败", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for index, work := range works {
|
||||
for _, user := range users {
|
||||
if work.UserId == user.ID {
|
||||
works[index].User = user
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"list": works,
|
||||
"total": total,
|
||||
|
Reference in New Issue
Block a user