300 lines
8.1 KiB
Go
300 lines
8.1 KiB
Go
// Code generated by ent, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/schema/field"
|
|
"github.com/xmdhs/authlib-skin/db/ent/skin"
|
|
"github.com/xmdhs/authlib-skin/db/ent/user"
|
|
"github.com/xmdhs/authlib-skin/db/ent/userprofile"
|
|
)
|
|
|
|
// UserCreate is the builder for creating a User entity.
|
|
type UserCreate struct {
|
|
config
|
|
mutation *UserMutation
|
|
hooks []Hook
|
|
}
|
|
|
|
// SetEmail sets the "email" field.
|
|
func (uc *UserCreate) SetEmail(s string) *UserCreate {
|
|
uc.mutation.SetEmail(s)
|
|
return uc
|
|
}
|
|
|
|
// SetPassword sets the "password" field.
|
|
func (uc *UserCreate) SetPassword(s string) *UserCreate {
|
|
uc.mutation.SetPassword(s)
|
|
return uc
|
|
}
|
|
|
|
// SetSalt sets the "salt" field.
|
|
func (uc *UserCreate) SetSalt(s string) *UserCreate {
|
|
uc.mutation.SetSalt(s)
|
|
return uc
|
|
}
|
|
|
|
// SetState sets the "state" field.
|
|
func (uc *UserCreate) SetState(i int) *UserCreate {
|
|
uc.mutation.SetState(i)
|
|
return uc
|
|
}
|
|
|
|
// SetRegTime sets the "reg_time" field.
|
|
func (uc *UserCreate) SetRegTime(i int64) *UserCreate {
|
|
uc.mutation.SetRegTime(i)
|
|
return uc
|
|
}
|
|
|
|
// AddSkinIDs adds the "skin" edge to the Skin entity by IDs.
|
|
func (uc *UserCreate) AddSkinIDs(ids ...int) *UserCreate {
|
|
uc.mutation.AddSkinIDs(ids...)
|
|
return uc
|
|
}
|
|
|
|
// AddSkin adds the "skin" edges to the Skin entity.
|
|
func (uc *UserCreate) AddSkin(s ...*Skin) *UserCreate {
|
|
ids := make([]int, len(s))
|
|
for i := range s {
|
|
ids[i] = s[i].ID
|
|
}
|
|
return uc.AddSkinIDs(ids...)
|
|
}
|
|
|
|
// SetProfileID sets the "profile" edge to the UserProfile entity by ID.
|
|
func (uc *UserCreate) SetProfileID(id int) *UserCreate {
|
|
uc.mutation.SetProfileID(id)
|
|
return uc
|
|
}
|
|
|
|
// SetNillableProfileID sets the "profile" edge to the UserProfile entity by ID if the given value is not nil.
|
|
func (uc *UserCreate) SetNillableProfileID(id *int) *UserCreate {
|
|
if id != nil {
|
|
uc = uc.SetProfileID(*id)
|
|
}
|
|
return uc
|
|
}
|
|
|
|
// SetProfile sets the "profile" edge to the UserProfile entity.
|
|
func (uc *UserCreate) SetProfile(u *UserProfile) *UserCreate {
|
|
return uc.SetProfileID(u.ID)
|
|
}
|
|
|
|
// Mutation returns the UserMutation object of the builder.
|
|
func (uc *UserCreate) Mutation() *UserMutation {
|
|
return uc.mutation
|
|
}
|
|
|
|
// Save creates the User in the database.
|
|
func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
|
|
return withHooks(ctx, uc.sqlSave, uc.mutation, uc.hooks)
|
|
}
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
func (uc *UserCreate) SaveX(ctx context.Context) *User {
|
|
v, err := uc.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (uc *UserCreate) Exec(ctx context.Context) error {
|
|
_, err := uc.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (uc *UserCreate) ExecX(ctx context.Context) {
|
|
if err := uc.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (uc *UserCreate) check() error {
|
|
if _, ok := uc.mutation.Email(); !ok {
|
|
return &ValidationError{Name: "email", err: errors.New(`ent: missing required field "User.email"`)}
|
|
}
|
|
if _, ok := uc.mutation.Password(); !ok {
|
|
return &ValidationError{Name: "password", err: errors.New(`ent: missing required field "User.password"`)}
|
|
}
|
|
if _, ok := uc.mutation.Salt(); !ok {
|
|
return &ValidationError{Name: "salt", err: errors.New(`ent: missing required field "User.salt"`)}
|
|
}
|
|
if _, ok := uc.mutation.State(); !ok {
|
|
return &ValidationError{Name: "state", err: errors.New(`ent: missing required field "User.state"`)}
|
|
}
|
|
if _, ok := uc.mutation.RegTime(); !ok {
|
|
return &ValidationError{Name: "reg_time", err: errors.New(`ent: missing required field "User.reg_time"`)}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
|
|
if err := uc.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
_node, _spec := uc.createSpec()
|
|
if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil {
|
|
if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return nil, err
|
|
}
|
|
id := _spec.ID.Value.(int64)
|
|
_node.ID = int(id)
|
|
uc.mutation.id = &_node.ID
|
|
uc.mutation.done = true
|
|
return _node, nil
|
|
}
|
|
|
|
func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) {
|
|
var (
|
|
_node = &User{config: uc.config}
|
|
_spec = sqlgraph.NewCreateSpec(user.Table, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt))
|
|
)
|
|
if value, ok := uc.mutation.Email(); ok {
|
|
_spec.SetField(user.FieldEmail, field.TypeString, value)
|
|
_node.Email = value
|
|
}
|
|
if value, ok := uc.mutation.Password(); ok {
|
|
_spec.SetField(user.FieldPassword, field.TypeString, value)
|
|
_node.Password = value
|
|
}
|
|
if value, ok := uc.mutation.Salt(); ok {
|
|
_spec.SetField(user.FieldSalt, field.TypeString, value)
|
|
_node.Salt = value
|
|
}
|
|
if value, ok := uc.mutation.State(); ok {
|
|
_spec.SetField(user.FieldState, field.TypeInt, value)
|
|
_node.State = value
|
|
}
|
|
if value, ok := uc.mutation.RegTime(); ok {
|
|
_spec.SetField(user.FieldRegTime, field.TypeInt64, value)
|
|
_node.RegTime = value
|
|
}
|
|
if nodes := uc.mutation.SkinIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: true,
|
|
Table: user.SkinTable,
|
|
Columns: []string{user.SkinColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(skin.FieldID, field.TypeInt),
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges = append(_spec.Edges, edge)
|
|
}
|
|
if nodes := uc.mutation.ProfileIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2O,
|
|
Inverse: false,
|
|
Table: user.ProfileTable,
|
|
Columns: []string{user.ProfileColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(userprofile.FieldID, field.TypeInt),
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges = append(_spec.Edges, edge)
|
|
}
|
|
return _node, _spec
|
|
}
|
|
|
|
// UserCreateBulk is the builder for creating many User entities in bulk.
|
|
type UserCreateBulk struct {
|
|
config
|
|
builders []*UserCreate
|
|
}
|
|
|
|
// Save creates the User entities in the database.
|
|
func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) {
|
|
specs := make([]*sqlgraph.CreateSpec, len(ucb.builders))
|
|
nodes := make([]*User, len(ucb.builders))
|
|
mutators := make([]Mutator, len(ucb.builders))
|
|
for i := range ucb.builders {
|
|
func(i int, root context.Context) {
|
|
builder := ucb.builders[i]
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*UserMutation)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
}
|
|
if err := builder.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
builder.mutation = mutation
|
|
var err error
|
|
nodes[i], specs[i] = builder.createSpec()
|
|
if i < len(mutators)-1 {
|
|
_, err = mutators[i+1].Mutate(root, ucb.builders[i+1].mutation)
|
|
} else {
|
|
spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
|
|
// Invoke the actual operation on the latest mutation in the chain.
|
|
if err = sqlgraph.BatchCreate(ctx, ucb.driver, spec); err != nil {
|
|
if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
}
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
mutation.id = &nodes[i].ID
|
|
if specs[i].ID.Value != nil {
|
|
id := specs[i].ID.Value.(int64)
|
|
nodes[i].ID = int(id)
|
|
}
|
|
mutation.done = true
|
|
return nodes[i], nil
|
|
})
|
|
for i := len(builder.hooks) - 1; i >= 0; i-- {
|
|
mut = builder.hooks[i](mut)
|
|
}
|
|
mutators[i] = mut
|
|
}(i, ctx)
|
|
}
|
|
if len(mutators) > 0 {
|
|
if _, err := mutators[0].Mutate(ctx, ucb.builders[0].mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (ucb *UserCreateBulk) SaveX(ctx context.Context) []*User {
|
|
v, err := ucb.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (ucb *UserCreateBulk) Exec(ctx context.Context) error {
|
|
_, err := ucb.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (ucb *UserCreateBulk) ExecX(ctx context.Context) {
|
|
if err := ucb.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|