// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "home-vue-go/internal/ent/site" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" ) // SiteCreate is the builder for creating a Site entity. type SiteCreate struct { config mutation *SiteMutation hooks []Hook } // SetName sets the "name" field. func (_c *SiteCreate) SetName(v string) *SiteCreate { _c.mutation.SetName(v) return _c } // SetURL sets the "url" field. func (_c *SiteCreate) SetURL(v string) *SiteCreate { _c.mutation.SetURL(v) return _c } // SetIcon sets the "icon" field. func (_c *SiteCreate) SetIcon(v string) *SiteCreate { _c.mutation.SetIcon(v) return _c } // SetSortOrder sets the "sort_order" field. func (_c *SiteCreate) SetSortOrder(v int) *SiteCreate { _c.mutation.SetSortOrder(v) return _c } // SetNillableSortOrder sets the "sort_order" field if the given value is not nil. func (_c *SiteCreate) SetNillableSortOrder(v *int) *SiteCreate { if v != nil { _c.SetSortOrder(*v) } return _c } // SetID sets the "id" field. func (_c *SiteCreate) SetID(v int) *SiteCreate { _c.mutation.SetID(v) return _c } // Mutation returns the SiteMutation object of the builder. func (_c *SiteCreate) Mutation() *SiteMutation { return _c.mutation } // Save creates the Site in the database. func (_c *SiteCreate) Save(ctx context.Context) (*Site, error) { _c.defaults() return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) } // SaveX calls Save and panics if Save returns an error. func (_c *SiteCreate) SaveX(ctx context.Context) *Site { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *SiteCreate) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *SiteCreate) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (_c *SiteCreate) defaults() { if _, ok := _c.mutation.SortOrder(); !ok { v := site.DefaultSortOrder _c.mutation.SetSortOrder(v) } } // check runs all checks and user-defined validators on the builder. func (_c *SiteCreate) check() error { if _, ok := _c.mutation.Name(); !ok { return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Site.name"`)} } if _, ok := _c.mutation.URL(); !ok { return &ValidationError{Name: "url", err: errors.New(`ent: missing required field "Site.url"`)} } if _, ok := _c.mutation.Icon(); !ok { return &ValidationError{Name: "icon", err: errors.New(`ent: missing required field "Site.icon"`)} } if _, ok := _c.mutation.SortOrder(); !ok { return &ValidationError{Name: "sort_order", err: errors.New(`ent: missing required field "Site.sort_order"`)} } return nil } func (_c *SiteCreate) sqlSave(ctx context.Context) (*Site, error) { if err := _c.check(); err != nil { return nil, err } _node, _spec := _c.createSpec() if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } if _spec.ID.Value != _node.ID { id := _spec.ID.Value.(int64) _node.ID = int(id) } _c.mutation.id = &_node.ID _c.mutation.done = true return _node, nil } func (_c *SiteCreate) createSpec() (*Site, *sqlgraph.CreateSpec) { var ( _node = &Site{config: _c.config} _spec = sqlgraph.NewCreateSpec(site.Table, sqlgraph.NewFieldSpec(site.FieldID, field.TypeInt)) ) if id, ok := _c.mutation.ID(); ok { _node.ID = id _spec.ID.Value = id } if value, ok := _c.mutation.Name(); ok { _spec.SetField(site.FieldName, field.TypeString, value) _node.Name = value } if value, ok := _c.mutation.URL(); ok { _spec.SetField(site.FieldURL, field.TypeString, value) _node.URL = value } if value, ok := _c.mutation.Icon(); ok { _spec.SetField(site.FieldIcon, field.TypeString, value) _node.Icon = value } if value, ok := _c.mutation.SortOrder(); ok { _spec.SetField(site.FieldSortOrder, field.TypeInt, value) _node.SortOrder = value } return _node, _spec } // SiteCreateBulk is the builder for creating many Site entities in bulk. type SiteCreateBulk struct { config err error builders []*SiteCreate } // Save creates the Site entities in the database. func (_c *SiteCreateBulk) Save(ctx context.Context) ([]*Site, error) { if _c.err != nil { return nil, _c.err } specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) nodes := make([]*Site, len(_c.builders)) mutators := make([]Mutator, len(_c.builders)) for i := range _c.builders { func(i int, root context.Context) { builder := _c.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*SiteMutation) 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, _c.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, _c.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 && nodes[i].ID == 0 { 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, _c.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (_c *SiteCreateBulk) SaveX(ctx context.Context) []*Site { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *SiteCreateBulk) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *SiteCreateBulk) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } }