// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "polaris/ent/storage" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" ) // StorageCreate is the builder for creating a Storage entity. type StorageCreate struct { config mutation *StorageMutation hooks []Hook } // SetName sets the "name" field. func (sc *StorageCreate) SetName(s string) *StorageCreate { sc.mutation.SetName(s) return sc } // SetImplementation sets the "implementation" field. func (sc *StorageCreate) SetImplementation(s storage.Implementation) *StorageCreate { sc.mutation.SetImplementation(s) return sc } // SetTvPath sets the "tv_path" field. func (sc *StorageCreate) SetTvPath(s string) *StorageCreate { sc.mutation.SetTvPath(s) return sc } // SetNillableTvPath sets the "tv_path" field if the given value is not nil. func (sc *StorageCreate) SetNillableTvPath(s *string) *StorageCreate { if s != nil { sc.SetTvPath(*s) } return sc } // SetMoviePath sets the "movie_path" field. func (sc *StorageCreate) SetMoviePath(s string) *StorageCreate { sc.mutation.SetMoviePath(s) return sc } // SetNillableMoviePath sets the "movie_path" field if the given value is not nil. func (sc *StorageCreate) SetNillableMoviePath(s *string) *StorageCreate { if s != nil { sc.SetMoviePath(*s) } return sc } // SetSettings sets the "settings" field. func (sc *StorageCreate) SetSettings(s string) *StorageCreate { sc.mutation.SetSettings(s) return sc } // SetNillableSettings sets the "settings" field if the given value is not nil. func (sc *StorageCreate) SetNillableSettings(s *string) *StorageCreate { if s != nil { sc.SetSettings(*s) } return sc } // SetDeleted sets the "deleted" field. func (sc *StorageCreate) SetDeleted(b bool) *StorageCreate { sc.mutation.SetDeleted(b) return sc } // SetNillableDeleted sets the "deleted" field if the given value is not nil. func (sc *StorageCreate) SetNillableDeleted(b *bool) *StorageCreate { if b != nil { sc.SetDeleted(*b) } return sc } // SetDefault sets the "default" field. func (sc *StorageCreate) SetDefault(b bool) *StorageCreate { sc.mutation.SetDefault(b) return sc } // SetNillableDefault sets the "default" field if the given value is not nil. func (sc *StorageCreate) SetNillableDefault(b *bool) *StorageCreate { if b != nil { sc.SetDefault(*b) } return sc } // Mutation returns the StorageMutation object of the builder. func (sc *StorageCreate) Mutation() *StorageMutation { return sc.mutation } // Save creates the Storage in the database. func (sc *StorageCreate) Save(ctx context.Context) (*Storage, error) { sc.defaults() return withHooks(ctx, sc.sqlSave, sc.mutation, sc.hooks) } // SaveX calls Save and panics if Save returns an error. func (sc *StorageCreate) SaveX(ctx context.Context) *Storage { v, err := sc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (sc *StorageCreate) Exec(ctx context.Context) error { _, err := sc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (sc *StorageCreate) ExecX(ctx context.Context) { if err := sc.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (sc *StorageCreate) defaults() { if _, ok := sc.mutation.Deleted(); !ok { v := storage.DefaultDeleted sc.mutation.SetDeleted(v) } if _, ok := sc.mutation.Default(); !ok { v := storage.DefaultDefault sc.mutation.SetDefault(v) } } // check runs all checks and user-defined validators on the builder. func (sc *StorageCreate) check() error { if _, ok := sc.mutation.Name(); !ok { return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Storage.name"`)} } if _, ok := sc.mutation.Implementation(); !ok { return &ValidationError{Name: "implementation", err: errors.New(`ent: missing required field "Storage.implementation"`)} } if v, ok := sc.mutation.Implementation(); ok { if err := storage.ImplementationValidator(v); err != nil { return &ValidationError{Name: "implementation", err: fmt.Errorf(`ent: validator failed for field "Storage.implementation": %w`, err)} } } if _, ok := sc.mutation.Deleted(); !ok { return &ValidationError{Name: "deleted", err: errors.New(`ent: missing required field "Storage.deleted"`)} } if _, ok := sc.mutation.Default(); !ok { return &ValidationError{Name: "default", err: errors.New(`ent: missing required field "Storage.default"`)} } return nil } func (sc *StorageCreate) sqlSave(ctx context.Context) (*Storage, error) { if err := sc.check(); err != nil { return nil, err } _node, _spec := sc.createSpec() if err := sqlgraph.CreateNode(ctx, sc.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) sc.mutation.id = &_node.ID sc.mutation.done = true return _node, nil } func (sc *StorageCreate) createSpec() (*Storage, *sqlgraph.CreateSpec) { var ( _node = &Storage{config: sc.config} _spec = sqlgraph.NewCreateSpec(storage.Table, sqlgraph.NewFieldSpec(storage.FieldID, field.TypeInt)) ) if value, ok := sc.mutation.Name(); ok { _spec.SetField(storage.FieldName, field.TypeString, value) _node.Name = value } if value, ok := sc.mutation.Implementation(); ok { _spec.SetField(storage.FieldImplementation, field.TypeEnum, value) _node.Implementation = value } if value, ok := sc.mutation.TvPath(); ok { _spec.SetField(storage.FieldTvPath, field.TypeString, value) _node.TvPath = value } if value, ok := sc.mutation.MoviePath(); ok { _spec.SetField(storage.FieldMoviePath, field.TypeString, value) _node.MoviePath = value } if value, ok := sc.mutation.Settings(); ok { _spec.SetField(storage.FieldSettings, field.TypeString, value) _node.Settings = value } if value, ok := sc.mutation.Deleted(); ok { _spec.SetField(storage.FieldDeleted, field.TypeBool, value) _node.Deleted = value } if value, ok := sc.mutation.Default(); ok { _spec.SetField(storage.FieldDefault, field.TypeBool, value) _node.Default = value } return _node, _spec } // StorageCreateBulk is the builder for creating many Storage entities in bulk. type StorageCreateBulk struct { config err error builders []*StorageCreate } // Save creates the Storage entities in the database. func (scb *StorageCreateBulk) Save(ctx context.Context) ([]*Storage, error) { if scb.err != nil { return nil, scb.err } specs := make([]*sqlgraph.CreateSpec, len(scb.builders)) nodes := make([]*Storage, len(scb.builders)) mutators := make([]Mutator, len(scb.builders)) for i := range scb.builders { func(i int, root context.Context) { builder := scb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*StorageMutation) 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, scb.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, scb.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, scb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (scb *StorageCreateBulk) SaveX(ctx context.Context) []*Storage { v, err := scb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (scb *StorageCreateBulk) Exec(ctx context.Context) error { _, err := scb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (scb *StorageCreateBulk) ExecX(ctx context.Context) { if err := scb.Exec(ctx); err != nil { panic(err) } }