作者 yangfu

更新: godevp user repository query

... ... @@ -8,6 +8,7 @@ import (
"github.com/tiptok/godevp/pkg/application/clientVersion/query"
"github.com/tiptok/godevp/pkg/application/factory"
"github.com/tiptok/godevp/pkg/domain"
"time"
)
// 客户端版本服务
... ... @@ -36,6 +37,7 @@ func (clientVersionService *ClientVersionService) CreateClientVersion(createClie
Title: createClientVersionCommand.Title,
Remark: createClientVersionCommand.Remark,
ClientPackageInfo: createClientVersionCommand.ClientPackageInfo,
CreateTime: time.Now(),
}
var clientVersionRepository domain.ClientVersionRepository
if value, err := factory.CreateClientVersionRepository(map[string]interface{}{
... ...
... ... @@ -12,7 +12,7 @@ type UpdateUsersCommand struct {
}
func (updateUsersCommand *UpdateUsersCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (updateUsersCommand *UpdateUsersCommand) ValidateCommand() error {
... ...
... ... @@ -2,6 +2,8 @@ package repository
import (
"fmt"
"github.com/linmadan/egglib-go/persistent/pg/ormx"
"strings"
"github.com/go-pg/pg/v10"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
... ... @@ -10,6 +12,15 @@ import (
"github.com/tiptok/godevp/pkg/infrastructure/pg/models"
)
var (
usersFields = ormx.FieldNames((*models.Users)(nil))
usersRowString = strings.Join(usersFields, ",")
usersRowInsertFields = ormx.Remove(usersFields, "id")
usersRowInsertString = strings.Join(usersRowInsertFields, ",")
usersRowsInsertPlaceHolder = ormx.ParamPlaceHolder(len(usersRowInsertFields))
usersRowsUpdate = strings.Join(ormx.Remove(usersFields, "id"), "=?,") + "=?"
)
type UsersRepository struct {
transactionContext *pgTransaction.TransactionContext
}
... ... @@ -26,14 +37,14 @@ func (repository *UsersRepository) Save(users *users.Users) (*users.Users, error
}
if _, err := tx.QueryOne(
pg.Scan(&users.Id, &users.Name, &users.Phone, &users.Passwd, pg.Array(&users.Roles), &users.Status, &users.AdminType, &users.CreateTime, &users.UpdateTime),
"INSERT INTO users (name, phone, passwd, roles, status, admin_type, create_time, update_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING id, name, phone, passwd, roles, status, admin_type, create_time, update_time",
"INSERT INTO users ("+usersRowInsertString+") VALUES ("+usersRowsInsertPlaceHolder+") RETURNING "+usersRowString,
users.Name, users.Phone, users.Passwd, pg.Array(users.Roles), users.Status, users.AdminType, users.CreateTime, users.UpdateTime); err != nil {
return users, err
}
} else {
if _, err := tx.QueryOne(
pg.Scan(&users.Id, &users.Name, &users.Phone, &users.Passwd, pg.Array(&users.Roles), &users.Status, &users.AdminType, &users.CreateTime, &users.UpdateTime),
"UPDATE users SET name=?, phone=?, passwd=?, roles=?, status=?, admin_type=?, create_time=?, update_time=? WHERE id=? RETURNING id, name, phone, passwd, roles, status, admin_type, create_time, update_time",
"UPDATE users SET "+usersRowsUpdate+" WHERE id=? RETURNING "+usersRowString,
users.Name, users.Phone, users.Passwd, pg.Array(users.Roles), users.Status, users.AdminType, users.CreateTime, users.UpdateTime, users.Identify()); err != nil {
return users, err
}
... ... @@ -52,12 +63,12 @@ func (repository *UsersRepository) Remove(users *users.Users) (*users.Users, err
func (repository *UsersRepository) FindOne(queryOptions map[string]interface{}) (*users.Users, error) {
tx := repository.transactionContext.PgTx
usersModel := new(models.Users)
query := tx.Model(usersModel)
query := ormx.NewQuery(tx.Model(usersModel), queryOptions)
if usersId, ok := queryOptions["usersId"]; ok {
query = query.Where("users.id = ?", usersId)
query.Where("users.id = ?", usersId)
}
if phone, ok := queryOptions["phone"]; ok {
query = query.Where("users.phone = ?", phone)
query.Where("users.phone = ?", phone)
}
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
... ... @@ -76,23 +87,8 @@ func (repository *UsersRepository) Find(queryOptions map[string]interface{}) (in
tx := repository.transactionContext.PgTx
var usersModels []*models.Users
userss := make([]*users.Users, 0)
query := tx.Model(&usersModels)
if offset, ok := queryOptions["offset"]; ok {
offset := offset.(int)
if offset > -1 {
query = query.Offset(offset)
}
} else {
query = query.Offset(0)
}
if limit, ok := queryOptions["limit"]; ok {
limit := limit.(int)
if limit > -1 {
query = query.Limit(limit)
}
} else {
query = query.Limit(20)
}
query := ormx.NewQuery(tx.Model(&usersModels), queryOptions)
query.SetLimit()
if count, err := query.Order("id DESC").SelectAndCount(); err != nil {
return 0, userss, err
} else {
... ...
... ... @@ -2,6 +2,7 @@
schema:
- graph/*.graphqls
- graph/access/*.graphqls
- graph/schema/*.graphqls
# Where should the generated server code go?
exec:
... ... @@ -19,13 +20,14 @@ model:
package: model
# Where should the resolver implementations go?
#resolver:
# layout: follow-schema
# dir: graph
# package: graph
resolver:
layout: follow-schema
dir: graph
package: graph
#resolver:
# filename: resolver.go
# type: Resolver
dir: graph/resolvers
package: resolvers
# Optional: turn on use `gqlgen:"fieldName"` tags in your models
# struct_tag: json
... ...
package resolvers
// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
import (
"context"
"fmt"
"github.com/tiptok/godevp/pkg/port/graphql/graph/generated"
"github.com/tiptok/godevp/pkg/port/graphql/graph/model"
)
func (r *accessResolver) User(ctx context.Context, obj *model.Access) (*model.Users, error) {
panic(fmt.Errorf("not implemented"))
}
func (r *mutationResolver) CreateMenu(ctx context.Context, input *model.MenuAccessInput) ([]*model.Access, error) {
panic(fmt.Errorf("not implemented"))
}
func (r *queryResolver) Menu(ctx context.Context, input *model.MenuAccessInput) ([]*model.Access, error) {
panic(fmt.Errorf("not implemented"))
}
// Access returns generated.AccessResolver implementation.
func (r *Resolver) Access() generated.AccessResolver { return &accessResolver{r} }
type accessResolver struct{ *Resolver }
... ...
package resolvers
// This file will not be regenerated automatically.
//
// It serves as dependency injection for your app, add any dependencies you require here.
type Resolver struct{}
... ...
package resolvers
// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
import (
"context"
"fmt"
"github.com/tiptok/godevp/pkg/domain"
"github.com/tiptok/godevp/pkg/port/graphql/graph/generated"
"github.com/tiptok/godevp/pkg/port/graphql/graph/libs"
"github.com/tiptok/godevp/pkg/port/graphql/graph/model"
)
func (r *clientVersionResolver) CreateTime(ctx context.Context, obj *domain.ClientVersion) (*libs.Datetime, error) {
panic(fmt.Errorf("not implemented"))
}
func (r *mutationResolver) CreateUsers(ctx context.Context, input model.CreateUsersInput) (*model.Users, error) {
panic(fmt.Errorf("not implemented"))
}
func (r *mutationResolver) RemoveUsers(ctx context.Context, input model.RemoveUsersInput) (*model.Users, error) {
panic(fmt.Errorf("not implemented"))
}
func (r *mutationResolver) UpdateUsers(ctx context.Context, input model.UpdateUsersInput) (*model.Users, error) {
panic(fmt.Errorf("not implemented"))
}
func (r *queryResolver) User(ctx context.Context, input model.GetUsersInput) (*model.Users, error) {
panic(fmt.Errorf("not implemented"))
}
func (r *queryResolver) Users(ctx context.Context, input model.ListUsersInput) ([]*model.Users, error) {
panic(fmt.Errorf("not implemented"))
}
func (r *queryResolver) Menus(ctx context.Context, input *model.MenuAccessInput) ([]*model.Access, error) {
panic(fmt.Errorf("not implemented"))
}
func (r *queryResolver) ClientVersion(ctx context.Context, id *int) (*domain.ClientVersion, error) {
panic(fmt.Errorf("not implemented"))
}
func (r *roleResolver) Access(ctx context.Context, obj *model.Role) ([]*model.Access, error) {
panic(fmt.Errorf("not implemented"))
}
func (r *usersResolver) Roles(ctx context.Context, obj *model.Users) ([]*model.Role, error) {
panic(fmt.Errorf("not implemented"))
}
// ClientVersion returns generated.ClientVersionResolver implementation.
func (r *Resolver) ClientVersion() generated.ClientVersionResolver { return &clientVersionResolver{r} }
// Mutation returns generated.MutationResolver implementation.
func (r *Resolver) Mutation() generated.MutationResolver { return &mutationResolver{r} }
// Query returns generated.QueryResolver implementation.
func (r *Resolver) Query() generated.QueryResolver { return &queryResolver{r} }
// Role returns generated.RoleResolver implementation.
func (r *Resolver) Role() generated.RoleResolver { return &roleResolver{r} }
// Users returns generated.UsersResolver implementation.
func (r *Resolver) Users() generated.UsersResolver { return &usersResolver{r} }
type clientVersionResolver struct{ *Resolver }
type mutationResolver struct{ *Resolver }
type queryResolver struct{ *Resolver }
type roleResolver struct{ *Resolver }
type usersResolver struct{ *Resolver }
... ...