// 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/localbreach" "git.dotya.ml/mirre-mt/pcmt/ent/schema" "git.dotya.ml/mirre-mt/pcmt/ent/trackedbreaches" "github.com/google/uuid" ) // LocalBreachCreate is the builder for creating a LocalBreach entity. type LocalBreachCreate struct { config mutation *LocalBreachMutation hooks []Hook } // SetName sets the "name" field. func (lbc *LocalBreachCreate) SetName(s string) *LocalBreachCreate { lbc.mutation.SetName(s) return lbc } // SetDate sets the "date" field. func (lbc *LocalBreachCreate) SetDate(t time.Time) *LocalBreachCreate { lbc.mutation.SetDate(t) return lbc } // SetNillableDate sets the "date" field if the given value is not nil. func (lbc *LocalBreachCreate) SetNillableDate(t *time.Time) *LocalBreachCreate { if t != nil { lbc.SetDate(*t) } return lbc } // SetDescription sets the "description" field. func (lbc *LocalBreachCreate) SetDescription(s string) *LocalBreachCreate { lbc.mutation.SetDescription(s) return lbc } // SetNillableDescription sets the "description" field if the given value is not nil. func (lbc *LocalBreachCreate) SetNillableDescription(s *string) *LocalBreachCreate { if s != nil { lbc.SetDescription(*s) } return lbc } // SetIsVerified sets the "is_verified" field. func (lbc *LocalBreachCreate) SetIsVerified(b bool) *LocalBreachCreate { lbc.mutation.SetIsVerified(b) return lbc } // SetContainsPasswords sets the "contains_passwords" field. func (lbc *LocalBreachCreate) SetContainsPasswords(b bool) *LocalBreachCreate { lbc.mutation.SetContainsPasswords(b) return lbc } // SetContainsHashes sets the "contains_hashes" field. func (lbc *LocalBreachCreate) SetContainsHashes(b bool) *LocalBreachCreate { lbc.mutation.SetContainsHashes(b) return lbc } // SetContainsEmails sets the "contains_emails" field. func (lbc *LocalBreachCreate) SetContainsEmails(b bool) *LocalBreachCreate { lbc.mutation.SetContainsEmails(b) return lbc } // SetContainsUsernames sets the "contains_usernames" field. func (lbc *LocalBreachCreate) SetContainsUsernames(b bool) *LocalBreachCreate { lbc.mutation.SetContainsUsernames(b) return lbc } // SetHashType sets the "hash_type" field. func (lbc *LocalBreachCreate) SetHashType(s string) *LocalBreachCreate { lbc.mutation.SetHashType(s) return lbc } // SetNillableHashType sets the "hash_type" field if the given value is not nil. func (lbc *LocalBreachCreate) SetNillableHashType(s *string) *LocalBreachCreate { if s != nil { lbc.SetHashType(*s) } return lbc } // SetHashSalted sets the "hash_salted" field. func (lbc *LocalBreachCreate) SetHashSalted(b bool) *LocalBreachCreate { lbc.mutation.SetHashSalted(b) return lbc } // SetNillableHashSalted sets the "hash_salted" field if the given value is not nil. func (lbc *LocalBreachCreate) SetNillableHashSalted(b *bool) *LocalBreachCreate { if b != nil { lbc.SetHashSalted(*b) } return lbc } // SetHashPeppered sets the "hash_peppered" field. func (lbc *LocalBreachCreate) SetHashPeppered(b bool) *LocalBreachCreate { lbc.mutation.SetHashPeppered(b) return lbc } // SetNillableHashPeppered sets the "hash_peppered" field if the given value is not nil. func (lbc *LocalBreachCreate) SetNillableHashPeppered(b *bool) *LocalBreachCreate { if b != nil { lbc.SetHashPeppered(*b) } return lbc } // SetData sets the "data" field. func (lbc *LocalBreachCreate) SetData(s *schema.Data) *LocalBreachCreate { lbc.mutation.SetData(s) return lbc } // SetCreatedAt sets the "created_at" field. func (lbc *LocalBreachCreate) SetCreatedAt(t time.Time) *LocalBreachCreate { lbc.mutation.SetCreatedAt(t) return lbc } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (lbc *LocalBreachCreate) SetNillableCreatedAt(t *time.Time) *LocalBreachCreate { if t != nil { lbc.SetCreatedAt(*t) } return lbc } // SetID sets the "id" field. func (lbc *LocalBreachCreate) SetID(u uuid.UUID) *LocalBreachCreate { lbc.mutation.SetID(u) return lbc } // SetNillableID sets the "id" field if the given value is not nil. func (lbc *LocalBreachCreate) SetNillableID(u *uuid.UUID) *LocalBreachCreate { if u != nil { lbc.SetID(*u) } return lbc } // AddTrackedBreachIDs adds the "tracked_breaches" edge to the TrackedBreaches entity by IDs. func (lbc *LocalBreachCreate) AddTrackedBreachIDs(ids ...uuid.UUID) *LocalBreachCreate { lbc.mutation.AddTrackedBreachIDs(ids...) return lbc } // AddTrackedBreaches adds the "tracked_breaches" edges to the TrackedBreaches entity. func (lbc *LocalBreachCreate) AddTrackedBreaches(t ...*TrackedBreaches) *LocalBreachCreate { ids := make([]uuid.UUID, len(t)) for i := range t { ids[i] = t[i].ID } return lbc.AddTrackedBreachIDs(ids...) } // Mutation returns the LocalBreachMutation object of the builder. func (lbc *LocalBreachCreate) Mutation() *LocalBreachMutation { return lbc.mutation } // Save creates the LocalBreach in the database. func (lbc *LocalBreachCreate) Save(ctx context.Context) (*LocalBreach, error) { lbc.defaults() return withHooks(ctx, lbc.sqlSave, lbc.mutation, lbc.hooks) } // SaveX calls Save and panics if Save returns an error. func (lbc *LocalBreachCreate) SaveX(ctx context.Context) *LocalBreach { v, err := lbc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (lbc *LocalBreachCreate) Exec(ctx context.Context) error { _, err := lbc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (lbc *LocalBreachCreate) ExecX(ctx context.Context) { if err := lbc.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (lbc *LocalBreachCreate) defaults() { if _, ok := lbc.mutation.Date(); !ok { v := localbreach.DefaultDate lbc.mutation.SetDate(v) } if _, ok := lbc.mutation.CreatedAt(); !ok { v := localbreach.DefaultCreatedAt lbc.mutation.SetCreatedAt(v) } if _, ok := lbc.mutation.ID(); !ok { v := localbreach.DefaultID() lbc.mutation.SetID(v) } } // check runs all checks and user-defined validators on the builder. func (lbc *LocalBreachCreate) check() error { if _, ok := lbc.mutation.Name(); !ok { return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "LocalBreach.name"`)} } if v, ok := lbc.mutation.Name(); ok { if err := localbreach.NameValidator(v); err != nil { return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "LocalBreach.name": %w`, err)} } } if _, ok := lbc.mutation.Date(); !ok { return &ValidationError{Name: "date", err: errors.New(`ent: missing required field "LocalBreach.date"`)} } if _, ok := lbc.mutation.IsVerified(); !ok { return &ValidationError{Name: "is_verified", err: errors.New(`ent: missing required field "LocalBreach.is_verified"`)} } if _, ok := lbc.mutation.ContainsPasswords(); !ok { return &ValidationError{Name: "contains_passwords", err: errors.New(`ent: missing required field "LocalBreach.contains_passwords"`)} } if _, ok := lbc.mutation.ContainsHashes(); !ok { return &ValidationError{Name: "contains_hashes", err: errors.New(`ent: missing required field "LocalBreach.contains_hashes"`)} } if _, ok := lbc.mutation.ContainsEmails(); !ok { return &ValidationError{Name: "contains_emails", err: errors.New(`ent: missing required field "LocalBreach.contains_emails"`)} } if _, ok := lbc.mutation.ContainsUsernames(); !ok { return &ValidationError{Name: "contains_usernames", err: errors.New(`ent: missing required field "LocalBreach.contains_usernames"`)} } if _, ok := lbc.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "LocalBreach.created_at"`)} } return nil } func (lbc *LocalBreachCreate) sqlSave(ctx context.Context) (*LocalBreach, error) { if err := lbc.check(); err != nil { return nil, err } _node, _spec := lbc.createSpec() if err := sqlgraph.CreateNode(ctx, lbc.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 } } lbc.mutation.id = &_node.ID lbc.mutation.done = true return _node, nil } func (lbc *LocalBreachCreate) createSpec() (*LocalBreach, *sqlgraph.CreateSpec) { var ( _node = &LocalBreach{config: lbc.config} _spec = sqlgraph.NewCreateSpec(localbreach.Table, sqlgraph.NewFieldSpec(localbreach.FieldID, field.TypeUUID)) ) if id, ok := lbc.mutation.ID(); ok { _node.ID = id _spec.ID.Value = &id } if value, ok := lbc.mutation.Name(); ok { _spec.SetField(localbreach.FieldName, field.TypeString, value) _node.Name = value } if value, ok := lbc.mutation.Date(); ok { _spec.SetField(localbreach.FieldDate, field.TypeTime, value) _node.Date = value } if value, ok := lbc.mutation.Description(); ok { _spec.SetField(localbreach.FieldDescription, field.TypeString, value) _node.Description = &value } if value, ok := lbc.mutation.IsVerified(); ok { _spec.SetField(localbreach.FieldIsVerified, field.TypeBool, value) _node.IsVerified = value } if value, ok := lbc.mutation.ContainsPasswords(); ok { _spec.SetField(localbreach.FieldContainsPasswords, field.TypeBool, value) _node.ContainsPasswords = value } if value, ok := lbc.mutation.ContainsHashes(); ok { _spec.SetField(localbreach.FieldContainsHashes, field.TypeBool, value) _node.ContainsHashes = value } if value, ok := lbc.mutation.ContainsEmails(); ok { _spec.SetField(localbreach.FieldContainsEmails, field.TypeBool, value) _node.ContainsEmails = value } if value, ok := lbc.mutation.ContainsUsernames(); ok { _spec.SetField(localbreach.FieldContainsUsernames, field.TypeBool, value) _node.ContainsUsernames = value } if value, ok := lbc.mutation.HashType(); ok { _spec.SetField(localbreach.FieldHashType, field.TypeString, value) _node.HashType = &value } if value, ok := lbc.mutation.HashSalted(); ok { _spec.SetField(localbreach.FieldHashSalted, field.TypeBool, value) _node.HashSalted = &value } if value, ok := lbc.mutation.HashPeppered(); ok { _spec.SetField(localbreach.FieldHashPeppered, field.TypeBool, value) _node.HashPeppered = &value } if value, ok := lbc.mutation.Data(); ok { _spec.SetField(localbreach.FieldData, field.TypeJSON, value) _node.Data = value } if value, ok := lbc.mutation.CreatedAt(); ok { _spec.SetField(localbreach.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value } if nodes := lbc.mutation.TrackedBreachesIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: true, Table: localbreach.TrackedBreachesTable, Columns: localbreach.TrackedBreachesPrimaryKey, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(trackedbreaches.FieldID, field.TypeUUID), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // LocalBreachCreateBulk is the builder for creating many LocalBreach entities in bulk. type LocalBreachCreateBulk struct { config builders []*LocalBreachCreate } // Save creates the LocalBreach entities in the database. func (lbcb *LocalBreachCreateBulk) Save(ctx context.Context) ([]*LocalBreach, error) { specs := make([]*sqlgraph.CreateSpec, len(lbcb.builders)) nodes := make([]*LocalBreach, len(lbcb.builders)) mutators := make([]Mutator, len(lbcb.builders)) for i := range lbcb.builders { func(i int, root context.Context) { builder := lbcb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*LocalBreachMutation) 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, lbcb.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, lbcb.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, lbcb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (lbcb *LocalBreachCreateBulk) SaveX(ctx context.Context) []*LocalBreach { v, err := lbcb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (lbcb *LocalBreachCreateBulk) Exec(ctx context.Context) error { _, err := lbcb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (lbcb *LocalBreachCreateBulk) ExecX(ctx context.Context) { if err := lbcb.Exec(ctx); err != nil { panic(err) } }