531 lines
16 KiB
Go
531 lines
16 KiB
Go
// Code generated by ent, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
"tss-rocks-be/ent/permission"
|
|
"tss-rocks-be/ent/predicate"
|
|
"tss-rocks-be/ent/role"
|
|
|
|
"entgo.io/ent/dialect/sql"
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/schema/field"
|
|
)
|
|
|
|
// PermissionUpdate is the builder for updating Permission entities.
|
|
type PermissionUpdate struct {
|
|
config
|
|
hooks []Hook
|
|
mutation *PermissionMutation
|
|
}
|
|
|
|
// Where appends a list predicates to the PermissionUpdate builder.
|
|
func (pu *PermissionUpdate) Where(ps ...predicate.Permission) *PermissionUpdate {
|
|
pu.mutation.Where(ps...)
|
|
return pu
|
|
}
|
|
|
|
// SetResource sets the "resource" field.
|
|
func (pu *PermissionUpdate) SetResource(s string) *PermissionUpdate {
|
|
pu.mutation.SetResource(s)
|
|
return pu
|
|
}
|
|
|
|
// SetNillableResource sets the "resource" field if the given value is not nil.
|
|
func (pu *PermissionUpdate) SetNillableResource(s *string) *PermissionUpdate {
|
|
if s != nil {
|
|
pu.SetResource(*s)
|
|
}
|
|
return pu
|
|
}
|
|
|
|
// SetAction sets the "action" field.
|
|
func (pu *PermissionUpdate) SetAction(s string) *PermissionUpdate {
|
|
pu.mutation.SetAction(s)
|
|
return pu
|
|
}
|
|
|
|
// SetNillableAction sets the "action" field if the given value is not nil.
|
|
func (pu *PermissionUpdate) SetNillableAction(s *string) *PermissionUpdate {
|
|
if s != nil {
|
|
pu.SetAction(*s)
|
|
}
|
|
return pu
|
|
}
|
|
|
|
// SetDescription sets the "description" field.
|
|
func (pu *PermissionUpdate) SetDescription(s string) *PermissionUpdate {
|
|
pu.mutation.SetDescription(s)
|
|
return pu
|
|
}
|
|
|
|
// SetNillableDescription sets the "description" field if the given value is not nil.
|
|
func (pu *PermissionUpdate) SetNillableDescription(s *string) *PermissionUpdate {
|
|
if s != nil {
|
|
pu.SetDescription(*s)
|
|
}
|
|
return pu
|
|
}
|
|
|
|
// ClearDescription clears the value of the "description" field.
|
|
func (pu *PermissionUpdate) ClearDescription() *PermissionUpdate {
|
|
pu.mutation.ClearDescription()
|
|
return pu
|
|
}
|
|
|
|
// SetUpdatedAt sets the "updated_at" field.
|
|
func (pu *PermissionUpdate) SetUpdatedAt(t time.Time) *PermissionUpdate {
|
|
pu.mutation.SetUpdatedAt(t)
|
|
return pu
|
|
}
|
|
|
|
// AddRoleIDs adds the "roles" edge to the Role entity by IDs.
|
|
func (pu *PermissionUpdate) AddRoleIDs(ids ...int) *PermissionUpdate {
|
|
pu.mutation.AddRoleIDs(ids...)
|
|
return pu
|
|
}
|
|
|
|
// AddRoles adds the "roles" edges to the Role entity.
|
|
func (pu *PermissionUpdate) AddRoles(r ...*Role) *PermissionUpdate {
|
|
ids := make([]int, len(r))
|
|
for i := range r {
|
|
ids[i] = r[i].ID
|
|
}
|
|
return pu.AddRoleIDs(ids...)
|
|
}
|
|
|
|
// Mutation returns the PermissionMutation object of the builder.
|
|
func (pu *PermissionUpdate) Mutation() *PermissionMutation {
|
|
return pu.mutation
|
|
}
|
|
|
|
// ClearRoles clears all "roles" edges to the Role entity.
|
|
func (pu *PermissionUpdate) ClearRoles() *PermissionUpdate {
|
|
pu.mutation.ClearRoles()
|
|
return pu
|
|
}
|
|
|
|
// RemoveRoleIDs removes the "roles" edge to Role entities by IDs.
|
|
func (pu *PermissionUpdate) RemoveRoleIDs(ids ...int) *PermissionUpdate {
|
|
pu.mutation.RemoveRoleIDs(ids...)
|
|
return pu
|
|
}
|
|
|
|
// RemoveRoles removes "roles" edges to Role entities.
|
|
func (pu *PermissionUpdate) RemoveRoles(r ...*Role) *PermissionUpdate {
|
|
ids := make([]int, len(r))
|
|
for i := range r {
|
|
ids[i] = r[i].ID
|
|
}
|
|
return pu.RemoveRoleIDs(ids...)
|
|
}
|
|
|
|
// Save executes the query and returns the number of nodes affected by the update operation.
|
|
func (pu *PermissionUpdate) Save(ctx context.Context) (int, error) {
|
|
pu.defaults()
|
|
return withHooks(ctx, pu.sqlSave, pu.mutation, pu.hooks)
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (pu *PermissionUpdate) SaveX(ctx context.Context) int {
|
|
affected, err := pu.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return affected
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (pu *PermissionUpdate) Exec(ctx context.Context) error {
|
|
_, err := pu.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (pu *PermissionUpdate) ExecX(ctx context.Context) {
|
|
if err := pu.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// defaults sets the default values of the builder before save.
|
|
func (pu *PermissionUpdate) defaults() {
|
|
if _, ok := pu.mutation.UpdatedAt(); !ok {
|
|
v := permission.UpdateDefaultUpdatedAt()
|
|
pu.mutation.SetUpdatedAt(v)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (pu *PermissionUpdate) check() error {
|
|
if v, ok := pu.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 v, ok := pu.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)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (pu *PermissionUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
|
if err := pu.check(); err != nil {
|
|
return n, err
|
|
}
|
|
_spec := sqlgraph.NewUpdateSpec(permission.Table, permission.Columns, sqlgraph.NewFieldSpec(permission.FieldID, field.TypeInt))
|
|
if ps := pu.mutation.predicates; len(ps) > 0 {
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
if value, ok := pu.mutation.Resource(); ok {
|
|
_spec.SetField(permission.FieldResource, field.TypeString, value)
|
|
}
|
|
if value, ok := pu.mutation.Action(); ok {
|
|
_spec.SetField(permission.FieldAction, field.TypeString, value)
|
|
}
|
|
if value, ok := pu.mutation.Description(); ok {
|
|
_spec.SetField(permission.FieldDescription, field.TypeString, value)
|
|
}
|
|
if pu.mutation.DescriptionCleared() {
|
|
_spec.ClearField(permission.FieldDescription, field.TypeString)
|
|
}
|
|
if value, ok := pu.mutation.UpdatedAt(); ok {
|
|
_spec.SetField(permission.FieldUpdatedAt, field.TypeTime, value)
|
|
}
|
|
if pu.mutation.RolesCleared() {
|
|
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),
|
|
},
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := pu.mutation.RemovedRolesIDs(); len(nodes) > 0 && !pu.mutation.RolesCleared() {
|
|
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.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := pu.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.Add = append(_spec.Edges.Add, edge)
|
|
}
|
|
if n, err = sqlgraph.UpdateNodes(ctx, pu.driver, _spec); err != nil {
|
|
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
|
err = &NotFoundError{permission.Label}
|
|
} else if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return 0, err
|
|
}
|
|
pu.mutation.done = true
|
|
return n, nil
|
|
}
|
|
|
|
// PermissionUpdateOne is the builder for updating a single Permission entity.
|
|
type PermissionUpdateOne struct {
|
|
config
|
|
fields []string
|
|
hooks []Hook
|
|
mutation *PermissionMutation
|
|
}
|
|
|
|
// SetResource sets the "resource" field.
|
|
func (puo *PermissionUpdateOne) SetResource(s string) *PermissionUpdateOne {
|
|
puo.mutation.SetResource(s)
|
|
return puo
|
|
}
|
|
|
|
// SetNillableResource sets the "resource" field if the given value is not nil.
|
|
func (puo *PermissionUpdateOne) SetNillableResource(s *string) *PermissionUpdateOne {
|
|
if s != nil {
|
|
puo.SetResource(*s)
|
|
}
|
|
return puo
|
|
}
|
|
|
|
// SetAction sets the "action" field.
|
|
func (puo *PermissionUpdateOne) SetAction(s string) *PermissionUpdateOne {
|
|
puo.mutation.SetAction(s)
|
|
return puo
|
|
}
|
|
|
|
// SetNillableAction sets the "action" field if the given value is not nil.
|
|
func (puo *PermissionUpdateOne) SetNillableAction(s *string) *PermissionUpdateOne {
|
|
if s != nil {
|
|
puo.SetAction(*s)
|
|
}
|
|
return puo
|
|
}
|
|
|
|
// SetDescription sets the "description" field.
|
|
func (puo *PermissionUpdateOne) SetDescription(s string) *PermissionUpdateOne {
|
|
puo.mutation.SetDescription(s)
|
|
return puo
|
|
}
|
|
|
|
// SetNillableDescription sets the "description" field if the given value is not nil.
|
|
func (puo *PermissionUpdateOne) SetNillableDescription(s *string) *PermissionUpdateOne {
|
|
if s != nil {
|
|
puo.SetDescription(*s)
|
|
}
|
|
return puo
|
|
}
|
|
|
|
// ClearDescription clears the value of the "description" field.
|
|
func (puo *PermissionUpdateOne) ClearDescription() *PermissionUpdateOne {
|
|
puo.mutation.ClearDescription()
|
|
return puo
|
|
}
|
|
|
|
// SetUpdatedAt sets the "updated_at" field.
|
|
func (puo *PermissionUpdateOne) SetUpdatedAt(t time.Time) *PermissionUpdateOne {
|
|
puo.mutation.SetUpdatedAt(t)
|
|
return puo
|
|
}
|
|
|
|
// AddRoleIDs adds the "roles" edge to the Role entity by IDs.
|
|
func (puo *PermissionUpdateOne) AddRoleIDs(ids ...int) *PermissionUpdateOne {
|
|
puo.mutation.AddRoleIDs(ids...)
|
|
return puo
|
|
}
|
|
|
|
// AddRoles adds the "roles" edges to the Role entity.
|
|
func (puo *PermissionUpdateOne) AddRoles(r ...*Role) *PermissionUpdateOne {
|
|
ids := make([]int, len(r))
|
|
for i := range r {
|
|
ids[i] = r[i].ID
|
|
}
|
|
return puo.AddRoleIDs(ids...)
|
|
}
|
|
|
|
// Mutation returns the PermissionMutation object of the builder.
|
|
func (puo *PermissionUpdateOne) Mutation() *PermissionMutation {
|
|
return puo.mutation
|
|
}
|
|
|
|
// ClearRoles clears all "roles" edges to the Role entity.
|
|
func (puo *PermissionUpdateOne) ClearRoles() *PermissionUpdateOne {
|
|
puo.mutation.ClearRoles()
|
|
return puo
|
|
}
|
|
|
|
// RemoveRoleIDs removes the "roles" edge to Role entities by IDs.
|
|
func (puo *PermissionUpdateOne) RemoveRoleIDs(ids ...int) *PermissionUpdateOne {
|
|
puo.mutation.RemoveRoleIDs(ids...)
|
|
return puo
|
|
}
|
|
|
|
// RemoveRoles removes "roles" edges to Role entities.
|
|
func (puo *PermissionUpdateOne) RemoveRoles(r ...*Role) *PermissionUpdateOne {
|
|
ids := make([]int, len(r))
|
|
for i := range r {
|
|
ids[i] = r[i].ID
|
|
}
|
|
return puo.RemoveRoleIDs(ids...)
|
|
}
|
|
|
|
// Where appends a list predicates to the PermissionUpdate builder.
|
|
func (puo *PermissionUpdateOne) Where(ps ...predicate.Permission) *PermissionUpdateOne {
|
|
puo.mutation.Where(ps...)
|
|
return puo
|
|
}
|
|
|
|
// Select allows selecting one or more fields (columns) of the returned entity.
|
|
// The default is selecting all fields defined in the entity schema.
|
|
func (puo *PermissionUpdateOne) Select(field string, fields ...string) *PermissionUpdateOne {
|
|
puo.fields = append([]string{field}, fields...)
|
|
return puo
|
|
}
|
|
|
|
// Save executes the query and returns the updated Permission entity.
|
|
func (puo *PermissionUpdateOne) Save(ctx context.Context) (*Permission, error) {
|
|
puo.defaults()
|
|
return withHooks(ctx, puo.sqlSave, puo.mutation, puo.hooks)
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (puo *PermissionUpdateOne) SaveX(ctx context.Context) *Permission {
|
|
node, err := puo.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return node
|
|
}
|
|
|
|
// Exec executes the query on the entity.
|
|
func (puo *PermissionUpdateOne) Exec(ctx context.Context) error {
|
|
_, err := puo.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (puo *PermissionUpdateOne) ExecX(ctx context.Context) {
|
|
if err := puo.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// defaults sets the default values of the builder before save.
|
|
func (puo *PermissionUpdateOne) defaults() {
|
|
if _, ok := puo.mutation.UpdatedAt(); !ok {
|
|
v := permission.UpdateDefaultUpdatedAt()
|
|
puo.mutation.SetUpdatedAt(v)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (puo *PermissionUpdateOne) check() error {
|
|
if v, ok := puo.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 v, ok := puo.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)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (puo *PermissionUpdateOne) sqlSave(ctx context.Context) (_node *Permission, err error) {
|
|
if err := puo.check(); err != nil {
|
|
return _node, err
|
|
}
|
|
_spec := sqlgraph.NewUpdateSpec(permission.Table, permission.Columns, sqlgraph.NewFieldSpec(permission.FieldID, field.TypeInt))
|
|
id, ok := puo.mutation.ID()
|
|
if !ok {
|
|
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Permission.id" for update`)}
|
|
}
|
|
_spec.Node.ID.Value = id
|
|
if fields := puo.fields; len(fields) > 0 {
|
|
_spec.Node.Columns = make([]string, 0, len(fields))
|
|
_spec.Node.Columns = append(_spec.Node.Columns, permission.FieldID)
|
|
for _, f := range fields {
|
|
if !permission.ValidColumn(f) {
|
|
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
|
|
}
|
|
if f != permission.FieldID {
|
|
_spec.Node.Columns = append(_spec.Node.Columns, f)
|
|
}
|
|
}
|
|
}
|
|
if ps := puo.mutation.predicates; len(ps) > 0 {
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
if value, ok := puo.mutation.Resource(); ok {
|
|
_spec.SetField(permission.FieldResource, field.TypeString, value)
|
|
}
|
|
if value, ok := puo.mutation.Action(); ok {
|
|
_spec.SetField(permission.FieldAction, field.TypeString, value)
|
|
}
|
|
if value, ok := puo.mutation.Description(); ok {
|
|
_spec.SetField(permission.FieldDescription, field.TypeString, value)
|
|
}
|
|
if puo.mutation.DescriptionCleared() {
|
|
_spec.ClearField(permission.FieldDescription, field.TypeString)
|
|
}
|
|
if value, ok := puo.mutation.UpdatedAt(); ok {
|
|
_spec.SetField(permission.FieldUpdatedAt, field.TypeTime, value)
|
|
}
|
|
if puo.mutation.RolesCleared() {
|
|
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),
|
|
},
|
|
}
|
|
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := puo.mutation.RemovedRolesIDs(); len(nodes) > 0 && !puo.mutation.RolesCleared() {
|
|
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.Clear = append(_spec.Edges.Clear, edge)
|
|
}
|
|
if nodes := puo.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.Add = append(_spec.Edges.Add, edge)
|
|
}
|
|
_node = &Permission{config: puo.config}
|
|
_spec.Assign = _node.assignValues
|
|
_spec.ScanValues = _node.scanValues
|
|
if err = sqlgraph.UpdateNode(ctx, puo.driver, _spec); err != nil {
|
|
if _, ok := err.(*sqlgraph.NotFoundError); ok {
|
|
err = &NotFoundError{permission.Label}
|
|
} else if sqlgraph.IsConstraintError(err) {
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
|
}
|
|
return nil, err
|
|
}
|
|
puo.mutation.done = true
|
|
return _node, nil
|
|
}
|