Handle user creation upon registration.
This commit is contained in:
parent
79d2db3c1f
commit
8657380530
13 changed files with 201 additions and 46 deletions
|
|
@ -14,6 +14,14 @@ import (
|
|||
func init() {
|
||||
userFields := schema.User{}.Fields()
|
||||
_ = userFields
|
||||
// userDescUsername is the schema descriptor for username field.
|
||||
userDescUsername := userFields[0].Descriptor()
|
||||
// user.UsernameValidator is a validator for the "username" field. It is called by the builders before save.
|
||||
user.UsernameValidator = userDescUsername.Validators[0].(func(string) error)
|
||||
// userDescPassword is the schema descriptor for password field.
|
||||
userDescPassword := userFields[1].Descriptor()
|
||||
// user.PasswordValidator is a validator for the "password" field. It is called by the builders before save.
|
||||
user.PasswordValidator = userDescPassword.Validators[0].(func(string) error)
|
||||
// userDescCreatedAt is the schema descriptor for created_at field.
|
||||
userDescCreatedAt := userFields[2].Descriptor()
|
||||
// user.DefaultCreatedAt holds the default value on creation for the created_at field.
|
||||
|
|
|
|||
|
|
@ -16,9 +16,11 @@ type User struct {
|
|||
func (User) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("username").
|
||||
Unique(),
|
||||
Unique().
|
||||
NotEmpty(),
|
||||
field.String("password").
|
||||
Sensitive(),
|
||||
Sensitive().
|
||||
NotEmpty(),
|
||||
field.Time("created_at").
|
||||
Default(time.Now).
|
||||
Immutable(),
|
||||
|
|
|
|||
|
|
@ -40,6 +40,10 @@ func ValidColumn(column string) bool {
|
|||
}
|
||||
|
||||
var (
|
||||
// UsernameValidator is a validator for the "username" field. It is called by the builders before save.
|
||||
UsernameValidator func(string) error
|
||||
// PasswordValidator is a validator for the "password" field. It is called by the builders before save.
|
||||
PasswordValidator func(string) error
|
||||
// DefaultCreatedAt holds the default value on creation for the "created_at" field.
|
||||
DefaultCreatedAt func() time.Time
|
||||
)
|
||||
|
|
|
|||
|
|
@ -128,9 +128,19 @@ func (uc *UserCreate) check() error {
|
|||
if _, ok := uc.mutation.Username(); !ok {
|
||||
return &ValidationError{Name: "username", err: errors.New(`ent: missing required field "username"`)}
|
||||
}
|
||||
if v, ok := uc.mutation.Username(); ok {
|
||||
if err := user.UsernameValidator(v); err != nil {
|
||||
return &ValidationError{Name: "username", err: fmt.Errorf(`ent: validator failed for field "username": %w`, err)}
|
||||
}
|
||||
}
|
||||
if _, ok := uc.mutation.Password(); !ok {
|
||||
return &ValidationError{Name: "password", err: errors.New(`ent: missing required field "password"`)}
|
||||
}
|
||||
if v, ok := uc.mutation.Password(); ok {
|
||||
if err := user.PasswordValidator(v); err != nil {
|
||||
return &ValidationError{Name: "password", err: fmt.Errorf(`ent: validator failed for field "password": %w`, err)}
|
||||
}
|
||||
}
|
||||
if _, ok := uc.mutation.CreatedAt(); !ok {
|
||||
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "created_at"`)}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,6 +50,9 @@ func (uu *UserUpdate) Save(ctx context.Context) (int, error) {
|
|||
affected int
|
||||
)
|
||||
if len(uu.hooks) == 0 {
|
||||
if err = uu.check(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
affected, err = uu.sqlSave(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
|
|
@ -57,6 +60,9 @@ func (uu *UserUpdate) Save(ctx context.Context) (int, error) {
|
|||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
if err = uu.check(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
uu.mutation = mutation
|
||||
affected, err = uu.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
|
|
@ -97,6 +103,21 @@ func (uu *UserUpdate) ExecX(ctx context.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
// check runs all checks and user-defined validators on the builder.
|
||||
func (uu *UserUpdate) check() error {
|
||||
if v, ok := uu.mutation.Username(); ok {
|
||||
if err := user.UsernameValidator(v); err != nil {
|
||||
return &ValidationError{Name: "username", err: fmt.Errorf("ent: validator failed for field \"username\": %w", err)}
|
||||
}
|
||||
}
|
||||
if v, ok := uu.mutation.Password(); ok {
|
||||
if err := user.PasswordValidator(v); err != nil {
|
||||
return &ValidationError{Name: "password", err: fmt.Errorf("ent: validator failed for field \"password\": %w", err)}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
||||
_spec := &sqlgraph.UpdateSpec{
|
||||
Node: &sqlgraph.NodeSpec{
|
||||
|
|
@ -179,6 +200,9 @@ func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) {
|
|||
node *User
|
||||
)
|
||||
if len(uuo.hooks) == 0 {
|
||||
if err = uuo.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node, err = uuo.sqlSave(ctx)
|
||||
} else {
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
|
|
@ -186,6 +210,9 @@ func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) {
|
|||
if !ok {
|
||||
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
||||
}
|
||||
if err = uuo.check(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
uuo.mutation = mutation
|
||||
node, err = uuo.sqlSave(ctx)
|
||||
mutation.done = true
|
||||
|
|
@ -226,6 +253,21 @@ func (uuo *UserUpdateOne) ExecX(ctx context.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
// check runs all checks and user-defined validators on the builder.
|
||||
func (uuo *UserUpdateOne) check() error {
|
||||
if v, ok := uuo.mutation.Username(); ok {
|
||||
if err := user.UsernameValidator(v); err != nil {
|
||||
return &ValidationError{Name: "username", err: fmt.Errorf("ent: validator failed for field \"username\": %w", err)}
|
||||
}
|
||||
}
|
||||
if v, ok := uuo.mutation.Password(); ok {
|
||||
if err := user.PasswordValidator(v); err != nil {
|
||||
return &ValidationError{Name: "password", err: fmt.Errorf("ent: validator failed for field \"password\": %w", err)}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) {
|
||||
_spec := &sqlgraph.UpdateSpec{
|
||||
Node: &sqlgraph.NodeSpec{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue