authentication_update.go•21.5 kB
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/Southclaws/storyden/internal/ent/account"
"github.com/Southclaws/storyden/internal/ent/authentication"
"github.com/Southclaws/storyden/internal/ent/predicate"
"github.com/rs/xid"
)
// AuthenticationUpdate is the builder for updating Authentication entities.
type AuthenticationUpdate struct {
config
hooks []Hook
mutation *AuthenticationMutation
modifiers []func(*sql.UpdateBuilder)
}
// Where appends a list predicates to the AuthenticationUpdate builder.
func (_u *AuthenticationUpdate) Where(ps ...predicate.Authentication) *AuthenticationUpdate {
_u.mutation.Where(ps...)
return _u
}
// SetExpiresAt sets the "expires_at" field.
func (_u *AuthenticationUpdate) SetExpiresAt(v time.Time) *AuthenticationUpdate {
_u.mutation.SetExpiresAt(v)
return _u
}
// SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
func (_u *AuthenticationUpdate) SetNillableExpiresAt(v *time.Time) *AuthenticationUpdate {
if v != nil {
_u.SetExpiresAt(*v)
}
return _u
}
// ClearExpiresAt clears the value of the "expires_at" field.
func (_u *AuthenticationUpdate) ClearExpiresAt() *AuthenticationUpdate {
_u.mutation.ClearExpiresAt()
return _u
}
// SetService sets the "service" field.
func (_u *AuthenticationUpdate) SetService(v string) *AuthenticationUpdate {
_u.mutation.SetService(v)
return _u
}
// SetNillableService sets the "service" field if the given value is not nil.
func (_u *AuthenticationUpdate) SetNillableService(v *string) *AuthenticationUpdate {
if v != nil {
_u.SetService(*v)
}
return _u
}
// SetTokenType sets the "token_type" field.
func (_u *AuthenticationUpdate) SetTokenType(v string) *AuthenticationUpdate {
_u.mutation.SetTokenType(v)
return _u
}
// SetNillableTokenType sets the "token_type" field if the given value is not nil.
func (_u *AuthenticationUpdate) SetNillableTokenType(v *string) *AuthenticationUpdate {
if v != nil {
_u.SetTokenType(*v)
}
return _u
}
// SetIdentifier sets the "identifier" field.
func (_u *AuthenticationUpdate) SetIdentifier(v string) *AuthenticationUpdate {
_u.mutation.SetIdentifier(v)
return _u
}
// SetNillableIdentifier sets the "identifier" field if the given value is not nil.
func (_u *AuthenticationUpdate) SetNillableIdentifier(v *string) *AuthenticationUpdate {
if v != nil {
_u.SetIdentifier(*v)
}
return _u
}
// SetToken sets the "token" field.
func (_u *AuthenticationUpdate) SetToken(v string) *AuthenticationUpdate {
_u.mutation.SetToken(v)
return _u
}
// SetNillableToken sets the "token" field if the given value is not nil.
func (_u *AuthenticationUpdate) SetNillableToken(v *string) *AuthenticationUpdate {
if v != nil {
_u.SetToken(*v)
}
return _u
}
// SetName sets the "name" field.
func (_u *AuthenticationUpdate) SetName(v string) *AuthenticationUpdate {
_u.mutation.SetName(v)
return _u
}
// SetNillableName sets the "name" field if the given value is not nil.
func (_u *AuthenticationUpdate) SetNillableName(v *string) *AuthenticationUpdate {
if v != nil {
_u.SetName(*v)
}
return _u
}
// ClearName clears the value of the "name" field.
func (_u *AuthenticationUpdate) ClearName() *AuthenticationUpdate {
_u.mutation.ClearName()
return _u
}
// SetDisabled sets the "disabled" field.
func (_u *AuthenticationUpdate) SetDisabled(v bool) *AuthenticationUpdate {
_u.mutation.SetDisabled(v)
return _u
}
// SetNillableDisabled sets the "disabled" field if the given value is not nil.
func (_u *AuthenticationUpdate) SetNillableDisabled(v *bool) *AuthenticationUpdate {
if v != nil {
_u.SetDisabled(*v)
}
return _u
}
// SetMetadata sets the "metadata" field.
func (_u *AuthenticationUpdate) SetMetadata(v map[string]interface{}) *AuthenticationUpdate {
_u.mutation.SetMetadata(v)
return _u
}
// ClearMetadata clears the value of the "metadata" field.
func (_u *AuthenticationUpdate) ClearMetadata() *AuthenticationUpdate {
_u.mutation.ClearMetadata()
return _u
}
// SetAccountAuthentication sets the "account_authentication" field.
func (_u *AuthenticationUpdate) SetAccountAuthentication(v xid.ID) *AuthenticationUpdate {
_u.mutation.SetAccountAuthentication(v)
return _u
}
// SetNillableAccountAuthentication sets the "account_authentication" field if the given value is not nil.
func (_u *AuthenticationUpdate) SetNillableAccountAuthentication(v *xid.ID) *AuthenticationUpdate {
if v != nil {
_u.SetAccountAuthentication(*v)
}
return _u
}
// SetAccountID sets the "account" edge to the Account entity by ID.
func (_u *AuthenticationUpdate) SetAccountID(id xid.ID) *AuthenticationUpdate {
_u.mutation.SetAccountID(id)
return _u
}
// SetAccount sets the "account" edge to the Account entity.
func (_u *AuthenticationUpdate) SetAccount(v *Account) *AuthenticationUpdate {
return _u.SetAccountID(v.ID)
}
// Mutation returns the AuthenticationMutation object of the builder.
func (_u *AuthenticationUpdate) Mutation() *AuthenticationMutation {
return _u.mutation
}
// ClearAccount clears the "account" edge to the Account entity.
func (_u *AuthenticationUpdate) ClearAccount() *AuthenticationUpdate {
_u.mutation.ClearAccount()
return _u
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *AuthenticationUpdate) Save(ctx context.Context) (int, error) {
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *AuthenticationUpdate) SaveX(ctx context.Context) int {
affected, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (_u *AuthenticationUpdate) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *AuthenticationUpdate) ExecX(ctx context.Context) {
if err := _u.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (_u *AuthenticationUpdate) check() error {
if v, ok := _u.mutation.Service(); ok {
if err := authentication.ServiceValidator(v); err != nil {
return &ValidationError{Name: "service", err: fmt.Errorf(`ent: validator failed for field "Authentication.service": %w`, err)}
}
}
if v, ok := _u.mutation.TokenType(); ok {
if err := authentication.TokenTypeValidator(v); err != nil {
return &ValidationError{Name: "token_type", err: fmt.Errorf(`ent: validator failed for field "Authentication.token_type": %w`, err)}
}
}
if v, ok := _u.mutation.Token(); ok {
if err := authentication.TokenValidator(v); err != nil {
return &ValidationError{Name: "token", err: fmt.Errorf(`ent: validator failed for field "Authentication.token": %w`, err)}
}
}
if _u.mutation.AccountCleared() && len(_u.mutation.AccountIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "Authentication.account"`)
}
return nil
}
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *AuthenticationUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *AuthenticationUpdate {
_u.modifiers = append(_u.modifiers, modifiers...)
return _u
}
func (_u *AuthenticationUpdate) sqlSave(ctx context.Context) (_node int, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(authentication.Table, authentication.Columns, sqlgraph.NewFieldSpec(authentication.FieldID, field.TypeString))
if ps := _u.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := _u.mutation.ExpiresAt(); ok {
_spec.SetField(authentication.FieldExpiresAt, field.TypeTime, value)
}
if _u.mutation.ExpiresAtCleared() {
_spec.ClearField(authentication.FieldExpiresAt, field.TypeTime)
}
if value, ok := _u.mutation.Service(); ok {
_spec.SetField(authentication.FieldService, field.TypeString, value)
}
if value, ok := _u.mutation.TokenType(); ok {
_spec.SetField(authentication.FieldTokenType, field.TypeString, value)
}
if value, ok := _u.mutation.Identifier(); ok {
_spec.SetField(authentication.FieldIdentifier, field.TypeString, value)
}
if value, ok := _u.mutation.Token(); ok {
_spec.SetField(authentication.FieldToken, field.TypeString, value)
}
if value, ok := _u.mutation.Name(); ok {
_spec.SetField(authentication.FieldName, field.TypeString, value)
}
if _u.mutation.NameCleared() {
_spec.ClearField(authentication.FieldName, field.TypeString)
}
if value, ok := _u.mutation.Disabled(); ok {
_spec.SetField(authentication.FieldDisabled, field.TypeBool, value)
}
if value, ok := _u.mutation.Metadata(); ok {
_spec.SetField(authentication.FieldMetadata, field.TypeJSON, value)
}
if _u.mutation.MetadataCleared() {
_spec.ClearField(authentication.FieldMetadata, field.TypeJSON)
}
if _u.mutation.AccountCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: authentication.AccountTable,
Columns: []string{authentication.AccountColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.AccountIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: authentication.AccountTable,
Columns: []string{authentication.AccountColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
_spec.AddModifiers(_u.modifiers...)
if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{authentication.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
_u.mutation.done = true
return _node, nil
}
// AuthenticationUpdateOne is the builder for updating a single Authentication entity.
type AuthenticationUpdateOne struct {
config
fields []string
hooks []Hook
mutation *AuthenticationMutation
modifiers []func(*sql.UpdateBuilder)
}
// SetExpiresAt sets the "expires_at" field.
func (_u *AuthenticationUpdateOne) SetExpiresAt(v time.Time) *AuthenticationUpdateOne {
_u.mutation.SetExpiresAt(v)
return _u
}
// SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
func (_u *AuthenticationUpdateOne) SetNillableExpiresAt(v *time.Time) *AuthenticationUpdateOne {
if v != nil {
_u.SetExpiresAt(*v)
}
return _u
}
// ClearExpiresAt clears the value of the "expires_at" field.
func (_u *AuthenticationUpdateOne) ClearExpiresAt() *AuthenticationUpdateOne {
_u.mutation.ClearExpiresAt()
return _u
}
// SetService sets the "service" field.
func (_u *AuthenticationUpdateOne) SetService(v string) *AuthenticationUpdateOne {
_u.mutation.SetService(v)
return _u
}
// SetNillableService sets the "service" field if the given value is not nil.
func (_u *AuthenticationUpdateOne) SetNillableService(v *string) *AuthenticationUpdateOne {
if v != nil {
_u.SetService(*v)
}
return _u
}
// SetTokenType sets the "token_type" field.
func (_u *AuthenticationUpdateOne) SetTokenType(v string) *AuthenticationUpdateOne {
_u.mutation.SetTokenType(v)
return _u
}
// SetNillableTokenType sets the "token_type" field if the given value is not nil.
func (_u *AuthenticationUpdateOne) SetNillableTokenType(v *string) *AuthenticationUpdateOne {
if v != nil {
_u.SetTokenType(*v)
}
return _u
}
// SetIdentifier sets the "identifier" field.
func (_u *AuthenticationUpdateOne) SetIdentifier(v string) *AuthenticationUpdateOne {
_u.mutation.SetIdentifier(v)
return _u
}
// SetNillableIdentifier sets the "identifier" field if the given value is not nil.
func (_u *AuthenticationUpdateOne) SetNillableIdentifier(v *string) *AuthenticationUpdateOne {
if v != nil {
_u.SetIdentifier(*v)
}
return _u
}
// SetToken sets the "token" field.
func (_u *AuthenticationUpdateOne) SetToken(v string) *AuthenticationUpdateOne {
_u.mutation.SetToken(v)
return _u
}
// SetNillableToken sets the "token" field if the given value is not nil.
func (_u *AuthenticationUpdateOne) SetNillableToken(v *string) *AuthenticationUpdateOne {
if v != nil {
_u.SetToken(*v)
}
return _u
}
// SetName sets the "name" field.
func (_u *AuthenticationUpdateOne) SetName(v string) *AuthenticationUpdateOne {
_u.mutation.SetName(v)
return _u
}
// SetNillableName sets the "name" field if the given value is not nil.
func (_u *AuthenticationUpdateOne) SetNillableName(v *string) *AuthenticationUpdateOne {
if v != nil {
_u.SetName(*v)
}
return _u
}
// ClearName clears the value of the "name" field.
func (_u *AuthenticationUpdateOne) ClearName() *AuthenticationUpdateOne {
_u.mutation.ClearName()
return _u
}
// SetDisabled sets the "disabled" field.
func (_u *AuthenticationUpdateOne) SetDisabled(v bool) *AuthenticationUpdateOne {
_u.mutation.SetDisabled(v)
return _u
}
// SetNillableDisabled sets the "disabled" field if the given value is not nil.
func (_u *AuthenticationUpdateOne) SetNillableDisabled(v *bool) *AuthenticationUpdateOne {
if v != nil {
_u.SetDisabled(*v)
}
return _u
}
// SetMetadata sets the "metadata" field.
func (_u *AuthenticationUpdateOne) SetMetadata(v map[string]interface{}) *AuthenticationUpdateOne {
_u.mutation.SetMetadata(v)
return _u
}
// ClearMetadata clears the value of the "metadata" field.
func (_u *AuthenticationUpdateOne) ClearMetadata() *AuthenticationUpdateOne {
_u.mutation.ClearMetadata()
return _u
}
// SetAccountAuthentication sets the "account_authentication" field.
func (_u *AuthenticationUpdateOne) SetAccountAuthentication(v xid.ID) *AuthenticationUpdateOne {
_u.mutation.SetAccountAuthentication(v)
return _u
}
// SetNillableAccountAuthentication sets the "account_authentication" field if the given value is not nil.
func (_u *AuthenticationUpdateOne) SetNillableAccountAuthentication(v *xid.ID) *AuthenticationUpdateOne {
if v != nil {
_u.SetAccountAuthentication(*v)
}
return _u
}
// SetAccountID sets the "account" edge to the Account entity by ID.
func (_u *AuthenticationUpdateOne) SetAccountID(id xid.ID) *AuthenticationUpdateOne {
_u.mutation.SetAccountID(id)
return _u
}
// SetAccount sets the "account" edge to the Account entity.
func (_u *AuthenticationUpdateOne) SetAccount(v *Account) *AuthenticationUpdateOne {
return _u.SetAccountID(v.ID)
}
// Mutation returns the AuthenticationMutation object of the builder.
func (_u *AuthenticationUpdateOne) Mutation() *AuthenticationMutation {
return _u.mutation
}
// ClearAccount clears the "account" edge to the Account entity.
func (_u *AuthenticationUpdateOne) ClearAccount() *AuthenticationUpdateOne {
_u.mutation.ClearAccount()
return _u
}
// Where appends a list predicates to the AuthenticationUpdate builder.
func (_u *AuthenticationUpdateOne) Where(ps ...predicate.Authentication) *AuthenticationUpdateOne {
_u.mutation.Where(ps...)
return _u
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (_u *AuthenticationUpdateOne) Select(field string, fields ...string) *AuthenticationUpdateOne {
_u.fields = append([]string{field}, fields...)
return _u
}
// Save executes the query and returns the updated Authentication entity.
func (_u *AuthenticationUpdateOne) Save(ctx context.Context) (*Authentication, error) {
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *AuthenticationUpdateOne) SaveX(ctx context.Context) *Authentication {
node, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (_u *AuthenticationUpdateOne) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *AuthenticationUpdateOne) ExecX(ctx context.Context) {
if err := _u.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (_u *AuthenticationUpdateOne) check() error {
if v, ok := _u.mutation.Service(); ok {
if err := authentication.ServiceValidator(v); err != nil {
return &ValidationError{Name: "service", err: fmt.Errorf(`ent: validator failed for field "Authentication.service": %w`, err)}
}
}
if v, ok := _u.mutation.TokenType(); ok {
if err := authentication.TokenTypeValidator(v); err != nil {
return &ValidationError{Name: "token_type", err: fmt.Errorf(`ent: validator failed for field "Authentication.token_type": %w`, err)}
}
}
if v, ok := _u.mutation.Token(); ok {
if err := authentication.TokenValidator(v); err != nil {
return &ValidationError{Name: "token", err: fmt.Errorf(`ent: validator failed for field "Authentication.token": %w`, err)}
}
}
if _u.mutation.AccountCleared() && len(_u.mutation.AccountIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "Authentication.account"`)
}
return nil
}
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *AuthenticationUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *AuthenticationUpdateOne {
_u.modifiers = append(_u.modifiers, modifiers...)
return _u
}
func (_u *AuthenticationUpdateOne) sqlSave(ctx context.Context) (_node *Authentication, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(authentication.Table, authentication.Columns, sqlgraph.NewFieldSpec(authentication.FieldID, field.TypeString))
id, ok := _u.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Authentication.id" for update`)}
}
_spec.Node.ID.Value = id
if fields := _u.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, authentication.FieldID)
for _, f := range fields {
if !authentication.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != authentication.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := _u.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := _u.mutation.ExpiresAt(); ok {
_spec.SetField(authentication.FieldExpiresAt, field.TypeTime, value)
}
if _u.mutation.ExpiresAtCleared() {
_spec.ClearField(authentication.FieldExpiresAt, field.TypeTime)
}
if value, ok := _u.mutation.Service(); ok {
_spec.SetField(authentication.FieldService, field.TypeString, value)
}
if value, ok := _u.mutation.TokenType(); ok {
_spec.SetField(authentication.FieldTokenType, field.TypeString, value)
}
if value, ok := _u.mutation.Identifier(); ok {
_spec.SetField(authentication.FieldIdentifier, field.TypeString, value)
}
if value, ok := _u.mutation.Token(); ok {
_spec.SetField(authentication.FieldToken, field.TypeString, value)
}
if value, ok := _u.mutation.Name(); ok {
_spec.SetField(authentication.FieldName, field.TypeString, value)
}
if _u.mutation.NameCleared() {
_spec.ClearField(authentication.FieldName, field.TypeString)
}
if value, ok := _u.mutation.Disabled(); ok {
_spec.SetField(authentication.FieldDisabled, field.TypeBool, value)
}
if value, ok := _u.mutation.Metadata(); ok {
_spec.SetField(authentication.FieldMetadata, field.TypeJSON, value)
}
if _u.mutation.MetadataCleared() {
_spec.ClearField(authentication.FieldMetadata, field.TypeJSON)
}
if _u.mutation.AccountCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: authentication.AccountTable,
Columns: []string{authentication.AccountColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.AccountIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: authentication.AccountTable,
Columns: []string{authentication.AccountColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
_spec.AddModifiers(_u.modifiers...)
_node = &Authentication{config: _u.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues
if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{authentication.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
_u.mutation.done = true
return _node, nil
}