// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "time" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" "git.dotya.ml/mirre-mt/pcmt/ent/agekey" "git.dotya.ml/mirre-mt/pcmt/ent/user" "github.com/google/uuid" ) // AgeKeyCreate is the builder for creating a AgeKey entity. type AgeKeyCreate struct { config mutation *AgeKeyMutation hooks []Hook } // SetKey sets the "key" field. func (akc *AgeKeyCreate) SetKey(b []byte) *AgeKeyCreate { akc.mutation.SetKey(b) return akc } // SetCreated sets the "created" field. func (akc *AgeKeyCreate) SetCreated(t time.Time) *AgeKeyCreate { akc.mutation.SetCreated(t) return akc } // SetNillableCreated sets the "created" field if the given value is not nil. func (akc *AgeKeyCreate) SetNillableCreated(t *time.Time) *AgeKeyCreate { if t != nil { akc.SetCreated(*t) } return akc } // SetUpdated sets the "updated" field. func (akc *AgeKeyCreate) SetUpdated(t time.Time) *AgeKeyCreate { akc.mutation.SetUpdated(t) return akc } // SetNillableUpdated sets the "updated" field if the given value is not nil. func (akc *AgeKeyCreate) SetNillableUpdated(t *time.Time) *AgeKeyCreate { if t != nil { akc.SetUpdated(*t) } return akc } // SetOwner sets the "owner" field. func (akc *AgeKeyCreate) SetOwner(u uuid.UUID) *AgeKeyCreate { akc.mutation.SetOwner(u) return akc } // SetID sets the "id" field. func (akc *AgeKeyCreate) SetID(u uuid.UUID) *AgeKeyCreate { akc.mutation.SetID(u) return akc } // SetNillableID sets the "id" field if the given value is not nil. func (akc *AgeKeyCreate) SetNillableID(u *uuid.UUID) *AgeKeyCreate { if u != nil { akc.SetID(*u) } return akc } // SetUserID sets the "user" edge to the User entity by ID. func (akc *AgeKeyCreate) SetUserID(id uuid.UUID) *AgeKeyCreate { akc.mutation.SetUserID(id) return akc } // SetUser sets the "user" edge to the User entity. func (akc *AgeKeyCreate) SetUser(u *User) *AgeKeyCreate { return akc.SetUserID(u.ID) } // Mutation returns the AgeKeyMutation object of the builder. func (akc *AgeKeyCreate) Mutation() *AgeKeyMutation { return akc.mutation } // Save creates the AgeKey in the database. func (akc *AgeKeyCreate) Save(ctx context.Context) (*AgeKey, error) { akc.defaults() return withHooks(ctx, akc.sqlSave, akc.mutation, akc.hooks) } // SaveX calls Save and panics if Save returns an error. func (akc *AgeKeyCreate) SaveX(ctx context.Context) *AgeKey { v, err := akc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (akc *AgeKeyCreate) Exec(ctx context.Context) error { _, err := akc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (akc *AgeKeyCreate) ExecX(ctx context.Context) { if err := akc.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (akc *AgeKeyCreate) defaults() { if _, ok := akc.mutation.Created(); !ok { v := agekey.DefaultCreated() akc.mutation.SetCreated(v) } if _, ok := akc.mutation.Updated(); !ok { v := agekey.DefaultUpdated() akc.mutation.SetUpdated(v) } if _, ok := akc.mutation.ID(); !ok { v := agekey.DefaultID() akc.mutation.SetID(v) } } // check runs all checks and user-defined validators on the builder. func (akc *AgeKeyCreate) check() error { if _, ok := akc.mutation.Key(); !ok { return &ValidationError{Name: "key", err: errors.New(`ent: missing required field "AgeKey.key"`)} } if v, ok := akc.mutation.Key(); ok { if err := agekey.KeyValidator(v); err != nil { return &ValidationError{Name: "key", err: fmt.Errorf(`ent: validator failed for field "AgeKey.key": %w`, err)} } } if _, ok := akc.mutation.Created(); !ok { return &ValidationError{Name: "created", err: errors.New(`ent: missing required field "AgeKey.created"`)} } if _, ok := akc.mutation.Updated(); !ok { return &ValidationError{Name: "updated", err: errors.New(`ent: missing required field "AgeKey.updated"`)} } if _, ok := akc.mutation.Owner(); !ok { return &ValidationError{Name: "owner", err: errors.New(`ent: missing required field "AgeKey.owner"`)} } if _, ok := akc.mutation.UserID(); !ok { return &ValidationError{Name: "user", err: errors.New(`ent: missing required edge "AgeKey.user"`)} } return nil } func (akc *AgeKeyCreate) sqlSave(ctx context.Context) (*AgeKey, error) { if err := akc.check(); err != nil { return nil, err } _node, _spec := akc.createSpec() if err := sqlgraph.CreateNode(ctx, akc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } if _spec.ID.Value != nil { if id, ok := _spec.ID.Value.(*uuid.UUID); ok { _node.ID = *id } else if err := _node.ID.Scan(_spec.ID.Value); err != nil { return nil, err } } akc.mutation.id = &_node.ID akc.mutation.done = true return _node, nil } func (akc *AgeKeyCreate) createSpec() (*AgeKey, *sqlgraph.CreateSpec) { var ( _node = &AgeKey{config: akc.config} _spec = sqlgraph.NewCreateSpec(agekey.Table, sqlgraph.NewFieldSpec(agekey.FieldID, field.TypeUUID)) ) if id, ok := akc.mutation.ID(); ok { _node.ID = id _spec.ID.Value = &id } if value, ok := akc.mutation.Key(); ok { _spec.SetField(agekey.FieldKey, field.TypeBytes, value) _node.Key = &value } if value, ok := akc.mutation.Created(); ok { _spec.SetField(agekey.FieldCreated, field.TypeTime, value) _node.Created = value } if value, ok := akc.mutation.Updated(); ok { _spec.SetField(agekey.FieldUpdated, field.TypeTime, value) _node.Updated = value } if nodes := akc.mutation.UserIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.O2O, Inverse: true, Table: agekey.UserTable, Columns: []string{agekey.UserColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.Owner = nodes[0] _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // AgeKeyCreateBulk is the builder for creating many AgeKey entities in bulk. type AgeKeyCreateBulk struct { config builders []*AgeKeyCreate } // Save creates the AgeKey entities in the database. func (akcb *AgeKeyCreateBulk) Save(ctx context.Context) ([]*AgeKey, error) { specs := make([]*sqlgraph.CreateSpec, len(akcb.builders)) nodes := make([]*AgeKey, len(akcb.builders)) mutators := make([]Mutator, len(akcb.builders)) for i := range akcb.builders { func(i int, root context.Context) { builder := akcb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*AgeKeyMutation) 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, akcb.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, akcb.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 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, akcb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (akcb *AgeKeyCreateBulk) SaveX(ctx context.Context) []*AgeKey { v, err := akcb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (akcb *AgeKeyCreateBulk) Exec(ctx context.Context) error { _, err := akcb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (akcb *AgeKeyCreateBulk) ExecX(ctx context.Context) { if err := akcb.Exec(ctx); err != nil { panic(err) } }