// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "time" "tss-rocks-be/ent/permission" "tss-rocks-be/ent/role" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" ) // PermissionCreate is the builder for creating a Permission entity. type PermissionCreate struct { config mutation *PermissionMutation hooks []Hook } // SetResource sets the "resource" field. func (pc *PermissionCreate) SetResource(s string) *PermissionCreate { pc.mutation.SetResource(s) return pc } // SetAction sets the "action" field. func (pc *PermissionCreate) SetAction(s string) *PermissionCreate { pc.mutation.SetAction(s) return pc } // SetDescription sets the "description" field. func (pc *PermissionCreate) SetDescription(s string) *PermissionCreate { pc.mutation.SetDescription(s) return pc } // SetNillableDescription sets the "description" field if the given value is not nil. func (pc *PermissionCreate) SetNillableDescription(s *string) *PermissionCreate { if s != nil { pc.SetDescription(*s) } return pc } // SetCreatedAt sets the "created_at" field. func (pc *PermissionCreate) SetCreatedAt(t time.Time) *PermissionCreate { pc.mutation.SetCreatedAt(t) return pc } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (pc *PermissionCreate) SetNillableCreatedAt(t *time.Time) *PermissionCreate { if t != nil { pc.SetCreatedAt(*t) } return pc } // SetUpdatedAt sets the "updated_at" field. func (pc *PermissionCreate) SetUpdatedAt(t time.Time) *PermissionCreate { pc.mutation.SetUpdatedAt(t) return pc } // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. func (pc *PermissionCreate) SetNillableUpdatedAt(t *time.Time) *PermissionCreate { if t != nil { pc.SetUpdatedAt(*t) } return pc } // AddRoleIDs adds the "roles" edge to the Role entity by IDs. func (pc *PermissionCreate) AddRoleIDs(ids ...int) *PermissionCreate { pc.mutation.AddRoleIDs(ids...) return pc } // AddRoles adds the "roles" edges to the Role entity. func (pc *PermissionCreate) AddRoles(r ...*Role) *PermissionCreate { ids := make([]int, len(r)) for i := range r { ids[i] = r[i].ID } return pc.AddRoleIDs(ids...) } // Mutation returns the PermissionMutation object of the builder. func (pc *PermissionCreate) Mutation() *PermissionMutation { return pc.mutation } // Save creates the Permission in the database. func (pc *PermissionCreate) Save(ctx context.Context) (*Permission, error) { pc.defaults() return withHooks(ctx, pc.sqlSave, pc.mutation, pc.hooks) } // SaveX calls Save and panics if Save returns an error. func (pc *PermissionCreate) SaveX(ctx context.Context) *Permission { v, err := pc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (pc *PermissionCreate) Exec(ctx context.Context) error { _, err := pc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (pc *PermissionCreate) ExecX(ctx context.Context) { if err := pc.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (pc *PermissionCreate) defaults() { if _, ok := pc.mutation.CreatedAt(); !ok { v := permission.DefaultCreatedAt() pc.mutation.SetCreatedAt(v) } if _, ok := pc.mutation.UpdatedAt(); !ok { v := permission.DefaultUpdatedAt() pc.mutation.SetUpdatedAt(v) } } // check runs all checks and user-defined validators on the builder. func (pc *PermissionCreate) check() error { if _, ok := pc.mutation.Resource(); !ok { return &ValidationError{Name: "resource", err: errors.New(`ent: missing required field "Permission.resource"`)} } if v, ok := pc.mutation.Resource(); ok { if err := permission.ResourceValidator(v); err != nil { return &ValidationError{Name: "resource", err: fmt.Errorf(`ent: validator failed for field "Permission.resource": %w`, err)} } } if _, ok := pc.mutation.Action(); !ok { return &ValidationError{Name: "action", err: errors.New(`ent: missing required field "Permission.action"`)} } if v, ok := pc.mutation.Action(); ok { if err := permission.ActionValidator(v); err != nil { return &ValidationError{Name: "action", err: fmt.Errorf(`ent: validator failed for field "Permission.action": %w`, err)} } } if _, ok := pc.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Permission.created_at"`)} } if _, ok := pc.mutation.UpdatedAt(); !ok { return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Permission.updated_at"`)} } return nil } func (pc *PermissionCreate) sqlSave(ctx context.Context) (*Permission, error) { if err := pc.check(); err != nil { return nil, err } _node, _spec := pc.createSpec() if err := sqlgraph.CreateNode(ctx, pc.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) pc.mutation.id = &_node.ID pc.mutation.done = true return _node, nil } func (pc *PermissionCreate) createSpec() (*Permission, *sqlgraph.CreateSpec) { var ( _node = &Permission{config: pc.config} _spec = sqlgraph.NewCreateSpec(permission.Table, sqlgraph.NewFieldSpec(permission.FieldID, field.TypeInt)) ) if value, ok := pc.mutation.Resource(); ok { _spec.SetField(permission.FieldResource, field.TypeString, value) _node.Resource = value } if value, ok := pc.mutation.Action(); ok { _spec.SetField(permission.FieldAction, field.TypeString, value) _node.Action = value } if value, ok := pc.mutation.Description(); ok { _spec.SetField(permission.FieldDescription, field.TypeString, value) _node.Description = value } if value, ok := pc.mutation.CreatedAt(); ok { _spec.SetField(permission.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value } if value, ok := pc.mutation.UpdatedAt(); ok { _spec.SetField(permission.FieldUpdatedAt, field.TypeTime, value) _node.UpdatedAt = value } if nodes := pc.mutation.RolesIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2M, Inverse: true, Table: permission.RolesTable, Columns: permission.RolesPrimaryKey, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(role.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // PermissionCreateBulk is the builder for creating many Permission entities in bulk. type PermissionCreateBulk struct { config err error builders []*PermissionCreate } // Save creates the Permission entities in the database. func (pcb *PermissionCreateBulk) Save(ctx context.Context) ([]*Permission, error) { if pcb.err != nil { return nil, pcb.err } specs := make([]*sqlgraph.CreateSpec, len(pcb.builders)) nodes := make([]*Permission, len(pcb.builders)) mutators := make([]Mutator, len(pcb.builders)) for i := range pcb.builders { func(i int, root context.Context) { builder := pcb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*PermissionMutation) 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, pcb.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, pcb.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, pcb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (pcb *PermissionCreateBulk) SaveX(ctx context.Context) []*Permission { v, err := pcb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (pcb *PermissionCreateBulk) Exec(ctx context.Context) error { _, err := pcb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (pcb *PermissionCreateBulk) ExecX(ctx context.Context) { if err := pcb.Exec(ctx); err != nil { panic(err) } }