// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "tss-rocks-be/ent/contributor" "tss-rocks-be/ent/contributorsociallink" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" ) // ContributorSocialLinkCreate is the builder for creating a ContributorSocialLink entity. type ContributorSocialLinkCreate struct { config mutation *ContributorSocialLinkMutation hooks []Hook } // SetType sets the "type" field. func (cslc *ContributorSocialLinkCreate) SetType(c contributorsociallink.Type) *ContributorSocialLinkCreate { cslc.mutation.SetType(c) return cslc } // SetName sets the "name" field. func (cslc *ContributorSocialLinkCreate) SetName(s string) *ContributorSocialLinkCreate { cslc.mutation.SetName(s) return cslc } // SetNillableName sets the "name" field if the given value is not nil. func (cslc *ContributorSocialLinkCreate) SetNillableName(s *string) *ContributorSocialLinkCreate { if s != nil { cslc.SetName(*s) } return cslc } // SetValue sets the "value" field. func (cslc *ContributorSocialLinkCreate) SetValue(s string) *ContributorSocialLinkCreate { cslc.mutation.SetValue(s) return cslc } // SetContributorID sets the "contributor" edge to the Contributor entity by ID. func (cslc *ContributorSocialLinkCreate) SetContributorID(id int) *ContributorSocialLinkCreate { cslc.mutation.SetContributorID(id) return cslc } // SetNillableContributorID sets the "contributor" edge to the Contributor entity by ID if the given value is not nil. func (cslc *ContributorSocialLinkCreate) SetNillableContributorID(id *int) *ContributorSocialLinkCreate { if id != nil { cslc = cslc.SetContributorID(*id) } return cslc } // SetContributor sets the "contributor" edge to the Contributor entity. func (cslc *ContributorSocialLinkCreate) SetContributor(c *Contributor) *ContributorSocialLinkCreate { return cslc.SetContributorID(c.ID) } // Mutation returns the ContributorSocialLinkMutation object of the builder. func (cslc *ContributorSocialLinkCreate) Mutation() *ContributorSocialLinkMutation { return cslc.mutation } // Save creates the ContributorSocialLink in the database. func (cslc *ContributorSocialLinkCreate) Save(ctx context.Context) (*ContributorSocialLink, error) { return withHooks(ctx, cslc.sqlSave, cslc.mutation, cslc.hooks) } // SaveX calls Save and panics if Save returns an error. func (cslc *ContributorSocialLinkCreate) SaveX(ctx context.Context) *ContributorSocialLink { v, err := cslc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (cslc *ContributorSocialLinkCreate) Exec(ctx context.Context) error { _, err := cslc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (cslc *ContributorSocialLinkCreate) ExecX(ctx context.Context) { if err := cslc.Exec(ctx); err != nil { panic(err) } } // check runs all checks and user-defined validators on the builder. func (cslc *ContributorSocialLinkCreate) check() error { if _, ok := cslc.mutation.GetType(); !ok { return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "ContributorSocialLink.type"`)} } if v, ok := cslc.mutation.GetType(); ok { if err := contributorsociallink.TypeValidator(v); err != nil { return &ValidationError{Name: "type", err: fmt.Errorf(`ent: validator failed for field "ContributorSocialLink.type": %w`, err)} } } if _, ok := cslc.mutation.Value(); !ok { return &ValidationError{Name: "value", err: errors.New(`ent: missing required field "ContributorSocialLink.value"`)} } if v, ok := cslc.mutation.Value(); ok { if err := contributorsociallink.ValueValidator(v); err != nil { return &ValidationError{Name: "value", err: fmt.Errorf(`ent: validator failed for field "ContributorSocialLink.value": %w`, err)} } } return nil } func (cslc *ContributorSocialLinkCreate) sqlSave(ctx context.Context) (*ContributorSocialLink, error) { if err := cslc.check(); err != nil { return nil, err } _node, _spec := cslc.createSpec() if err := sqlgraph.CreateNode(ctx, cslc.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) cslc.mutation.id = &_node.ID cslc.mutation.done = true return _node, nil } func (cslc *ContributorSocialLinkCreate) createSpec() (*ContributorSocialLink, *sqlgraph.CreateSpec) { var ( _node = &ContributorSocialLink{config: cslc.config} _spec = sqlgraph.NewCreateSpec(contributorsociallink.Table, sqlgraph.NewFieldSpec(contributorsociallink.FieldID, field.TypeInt)) ) if value, ok := cslc.mutation.GetType(); ok { _spec.SetField(contributorsociallink.FieldType, field.TypeEnum, value) _node.Type = value } if value, ok := cslc.mutation.Name(); ok { _spec.SetField(contributorsociallink.FieldName, field.TypeString, value) _node.Name = value } if value, ok := cslc.mutation.Value(); ok { _spec.SetField(contributorsociallink.FieldValue, field.TypeString, value) _node.Value = value } if nodes := cslc.mutation.ContributorIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: contributorsociallink.ContributorTable, Columns: []string{contributorsociallink.ContributorColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(contributor.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.contributor_social_links = &nodes[0] _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // ContributorSocialLinkCreateBulk is the builder for creating many ContributorSocialLink entities in bulk. type ContributorSocialLinkCreateBulk struct { config err error builders []*ContributorSocialLinkCreate } // Save creates the ContributorSocialLink entities in the database. func (cslcb *ContributorSocialLinkCreateBulk) Save(ctx context.Context) ([]*ContributorSocialLink, error) { if cslcb.err != nil { return nil, cslcb.err } specs := make([]*sqlgraph.CreateSpec, len(cslcb.builders)) nodes := make([]*ContributorSocialLink, len(cslcb.builders)) mutators := make([]Mutator, len(cslcb.builders)) for i := range cslcb.builders { func(i int, root context.Context) { builder := cslcb.builders[i] var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*ContributorSocialLinkMutation) 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, cslcb.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, cslcb.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, cslcb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (cslcb *ContributorSocialLinkCreateBulk) SaveX(ctx context.Context) []*ContributorSocialLink { v, err := cslcb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (cslcb *ContributorSocialLinkCreateBulk) Exec(ctx context.Context) error { _, err := cslcb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (cslcb *ContributorSocialLinkCreateBulk) ExecX(ctx context.Context) { if err := cslcb.Exec(ctx); err != nil { panic(err) } }