Files
webp/api/user.go
2024-12-03 05:39:30 +08:00

94 lines
3.9 KiB
Go

package api
import (
"time"
"github.com/graphql-go/graphql"
)
type User struct {
ID int `json:"id" db:"id" gorm:"primaryKey"`
UserName string `json:"user_name" db:"user_name"`
Avatar string `json:"avatar" db:"avatar"`
Rank string `json:"rank" db:"rank"`
Price int `json:"price" db:"price"`
CreateTime time.Time `json:"create_time" db:"create_time"`
UpdateTime time.Time `json:"update_time" db:"update_time"`
}
func (User) TableName() string {
return "web_member"
}
var userType = graphql.NewObject(graphql.ObjectConfig{
Name: "user",
Description: "用户信息",
Fields: graphql.Fields{
"id": &graphql.Field{Type: graphql.Int, Description: "用户ID"},
"user_name": &graphql.Field{Type: graphql.String, Description: "用户名"},
"avatar": &graphql.Field{Type: graphql.String, Description: "用户头像"},
"rank": &graphql.Field{Type: graphql.String, Description: "用户等级"},
"price": &graphql.Field{Type: graphql.Int, Description: "用户金币"},
"create_time": &graphql.Field{Type: graphql.DateTime, Description: "用户创建时间"},
"update_time": &graphql.Field{Type: graphql.DateTime, Description: "用户更新时间"},
},
})
var UserItems = &graphql.Field{
Name: "users",
Description: "用户列表",
Type: graphql.NewObject(graphql.ObjectConfig{
Name: "UserConnection",
Description: "条件筛选用户列表",
Fields: graphql.Fields{
"list": &graphql.Field{Type: graphql.NewList(userType), Description: "用户列表"},
"total": &graphql.Field{Type: graphql.Int, Description: "用户总数"},
},
}),
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{Type: graphql.Int, Description: "筛选用户中指定ID的"},
"user_name": &graphql.ArgumentConfig{Type: graphql.String, Description: "筛选用户中含有指定用户名的"},
"create_time": &graphql.ArgumentConfig{Type: graphql.DateTime, Description: "筛选用户中创建时间等于指定值的"},
"update_time": &graphql.ArgumentConfig{Type: graphql.DateTime, Description: "筛选用户中更新时间等于指定值的"},
"first": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中的前n個元素)"},
"last": &graphql.ArgumentConfig{Type: graphql.Int, Description: "翻页参数(傳回清單中的最後n個元素)"},
"after": &graphql.ArgumentConfig{Type: graphql.String, Description: "翻页参数(傳回清單中指定遊標之後的元素)"},
"before": &graphql.ArgumentConfig{Type: graphql.String, Description: "翻页参数(傳回清單中指定遊標之前的元素)"},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
//var where []string
//if p.Args["id"] != nil {
// where = append(where, fmt.Sprintf("id=%d", p.Args["id"]))
//}
//if p.Args["user_name"] != nil {
// where = append(where, fmt.Sprintf("user_name='%s'", p.Args["user_name"]))
//}
//// 筛选条件
//where_str := strings.Join(where, " AND ")
//if where_str != "" {
// where_str = "WHERE " + where_str
//}
//var query strings.Builder
var users []User
var total int
//fields := strings.Join(get_fields(p.Info.FieldASTs[0].SelectionSet.Selections), ",")
//query.WriteString(fmt.Sprintf("SELECT %s FROM web_member %s LIMIT %d OFFSET %d", fields, where_str, 10, 0))
//if err := connection.Select(&users, query.String()); err != nil {
// fmt.Println("获取用户列表失败", err)
// return nil, err
//}
//if len(users) > 0 {
// query.Reset()
// query.WriteString(fmt.Sprintf("SELECT COUNT(*) FROM web_member %s", where_str))
// if err := connection.Get(&total, query.String()); err != nil {
// fmt.Println("获取用户总数失败", err)
// return nil, err
// }
//}
return map[string]interface{}{
"list": users,
"total": total,
}, nil
},
}