rano/db/ent/usersession_update.go

500 lines
15 KiB
Go
Raw Permalink Normal View History

2024-11-10 09:22:33 +00:00
// 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"
"gitserver.in/patialtech/rano/db/ent/predicate"
"gitserver.in/patialtech/rano/db/ent/user"
"gitserver.in/patialtech/rano/db/ent/usersession"
)
// UserSessionUpdate is the builder for updating UserSession entities.
type UserSessionUpdate struct {
config
hooks []Hook
mutation *UserSessionMutation
}
// Where appends a list predicates to the UserSessionUpdate builder.
func (usu *UserSessionUpdate) Where(ps ...predicate.UserSession) *UserSessionUpdate {
usu.mutation.Where(ps...)
return usu
}
// SetIssuedAt sets the "issued_at" field.
func (usu *UserSessionUpdate) SetIssuedAt(t time.Time) *UserSessionUpdate {
usu.mutation.SetIssuedAt(t)
return usu
}
// SetNillableIssuedAt sets the "issued_at" field if the given value is not nil.
func (usu *UserSessionUpdate) SetNillableIssuedAt(t *time.Time) *UserSessionUpdate {
if t != nil {
usu.SetIssuedAt(*t)
}
return usu
}
// SetExpiresAt sets the "expires_at" field.
func (usu *UserSessionUpdate) SetExpiresAt(t time.Time) *UserSessionUpdate {
usu.mutation.SetExpiresAt(t)
return usu
}
// SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
func (usu *UserSessionUpdate) SetNillableExpiresAt(t *time.Time) *UserSessionUpdate {
if t != nil {
usu.SetExpiresAt(*t)
}
return usu
}
// SetInvalidated sets the "invalidated" field.
func (usu *UserSessionUpdate) SetInvalidated(b bool) *UserSessionUpdate {
usu.mutation.SetInvalidated(b)
return usu
}
// SetNillableInvalidated sets the "invalidated" field if the given value is not nil.
func (usu *UserSessionUpdate) SetNillableInvalidated(b *bool) *UserSessionUpdate {
if b != nil {
usu.SetInvalidated(*b)
}
return usu
}
// ClearInvalidated clears the value of the "invalidated" field.
func (usu *UserSessionUpdate) ClearInvalidated() *UserSessionUpdate {
usu.mutation.ClearInvalidated()
return usu
}
// SetUserAgent sets the "user_agent" field.
func (usu *UserSessionUpdate) SetUserAgent(s string) *UserSessionUpdate {
usu.mutation.SetUserAgent(s)
return usu
}
// SetNillableUserAgent sets the "user_agent" field if the given value is not nil.
func (usu *UserSessionUpdate) SetNillableUserAgent(s *string) *UserSessionUpdate {
if s != nil {
usu.SetUserAgent(*s)
}
return usu
}
// SetIP sets the "ip" field.
func (usu *UserSessionUpdate) SetIP(s string) *UserSessionUpdate {
usu.mutation.SetIP(s)
return usu
}
// SetNillableIP sets the "ip" field if the given value is not nil.
func (usu *UserSessionUpdate) SetNillableIP(s *string) *UserSessionUpdate {
if s != nil {
usu.SetIP(*s)
}
return usu
}
// SetUserID sets the "user" edge to the User entity by ID.
func (usu *UserSessionUpdate) SetUserID(id int64) *UserSessionUpdate {
usu.mutation.SetUserID(id)
return usu
}
// SetUser sets the "user" edge to the User entity.
func (usu *UserSessionUpdate) SetUser(u *User) *UserSessionUpdate {
return usu.SetUserID(u.ID)
}
// Mutation returns the UserSessionMutation object of the builder.
func (usu *UserSessionUpdate) Mutation() *UserSessionMutation {
return usu.mutation
}
// ClearUser clears the "user" edge to the User entity.
func (usu *UserSessionUpdate) ClearUser() *UserSessionUpdate {
usu.mutation.ClearUser()
return usu
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (usu *UserSessionUpdate) Save(ctx context.Context) (int, error) {
return withHooks(ctx, usu.sqlSave, usu.mutation, usu.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (usu *UserSessionUpdate) SaveX(ctx context.Context) int {
affected, err := usu.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (usu *UserSessionUpdate) Exec(ctx context.Context) error {
_, err := usu.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (usu *UserSessionUpdate) ExecX(ctx context.Context) {
if err := usu.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (usu *UserSessionUpdate) check() error {
if v, ok := usu.mutation.UserAgent(); ok {
if err := usersession.UserAgentValidator(v); err != nil {
return &ValidationError{Name: "user_agent", err: fmt.Errorf(`ent: validator failed for field "UserSession.user_agent": %w`, err)}
}
}
if v, ok := usu.mutation.IP(); ok {
if err := usersession.IPValidator(v); err != nil {
return &ValidationError{Name: "ip", err: fmt.Errorf(`ent: validator failed for field "UserSession.ip": %w`, err)}
}
}
if usu.mutation.UserCleared() && len(usu.mutation.UserIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "UserSession.user"`)
}
return nil
}
func (usu *UserSessionUpdate) sqlSave(ctx context.Context) (n int, err error) {
if err := usu.check(); err != nil {
return n, err
}
_spec := sqlgraph.NewUpdateSpec(usersession.Table, usersession.Columns, sqlgraph.NewFieldSpec(usersession.FieldID, field.TypeInt64))
if ps := usu.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := usu.mutation.IssuedAt(); ok {
_spec.SetField(usersession.FieldIssuedAt, field.TypeTime, value)
}
if value, ok := usu.mutation.ExpiresAt(); ok {
_spec.SetField(usersession.FieldExpiresAt, field.TypeTime, value)
}
if value, ok := usu.mutation.Invalidated(); ok {
_spec.SetField(usersession.FieldInvalidated, field.TypeBool, value)
}
if usu.mutation.InvalidatedCleared() {
_spec.ClearField(usersession.FieldInvalidated, field.TypeBool)
}
if value, ok := usu.mutation.UserAgent(); ok {
_spec.SetField(usersession.FieldUserAgent, field.TypeString, value)
}
if value, ok := usu.mutation.IP(); ok {
_spec.SetField(usersession.FieldIP, field.TypeString, value)
}
if usu.mutation.UserCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usersession.UserTable,
Columns: []string{usersession.UserColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := usu.mutation.UserIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usersession.UserTable,
Columns: []string{usersession.UserColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
},
}
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, usu.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{usersession.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
usu.mutation.done = true
return n, nil
}
// UserSessionUpdateOne is the builder for updating a single UserSession entity.
type UserSessionUpdateOne struct {
config
fields []string
hooks []Hook
mutation *UserSessionMutation
}
// SetIssuedAt sets the "issued_at" field.
func (usuo *UserSessionUpdateOne) SetIssuedAt(t time.Time) *UserSessionUpdateOne {
usuo.mutation.SetIssuedAt(t)
return usuo
}
// SetNillableIssuedAt sets the "issued_at" field if the given value is not nil.
func (usuo *UserSessionUpdateOne) SetNillableIssuedAt(t *time.Time) *UserSessionUpdateOne {
if t != nil {
usuo.SetIssuedAt(*t)
}
return usuo
}
// SetExpiresAt sets the "expires_at" field.
func (usuo *UserSessionUpdateOne) SetExpiresAt(t time.Time) *UserSessionUpdateOne {
usuo.mutation.SetExpiresAt(t)
return usuo
}
// SetNillableExpiresAt sets the "expires_at" field if the given value is not nil.
func (usuo *UserSessionUpdateOne) SetNillableExpiresAt(t *time.Time) *UserSessionUpdateOne {
if t != nil {
usuo.SetExpiresAt(*t)
}
return usuo
}
// SetInvalidated sets the "invalidated" field.
func (usuo *UserSessionUpdateOne) SetInvalidated(b bool) *UserSessionUpdateOne {
usuo.mutation.SetInvalidated(b)
return usuo
}
// SetNillableInvalidated sets the "invalidated" field if the given value is not nil.
func (usuo *UserSessionUpdateOne) SetNillableInvalidated(b *bool) *UserSessionUpdateOne {
if b != nil {
usuo.SetInvalidated(*b)
}
return usuo
}
// ClearInvalidated clears the value of the "invalidated" field.
func (usuo *UserSessionUpdateOne) ClearInvalidated() *UserSessionUpdateOne {
usuo.mutation.ClearInvalidated()
return usuo
}
// SetUserAgent sets the "user_agent" field.
func (usuo *UserSessionUpdateOne) SetUserAgent(s string) *UserSessionUpdateOne {
usuo.mutation.SetUserAgent(s)
return usuo
}
// SetNillableUserAgent sets the "user_agent" field if the given value is not nil.
func (usuo *UserSessionUpdateOne) SetNillableUserAgent(s *string) *UserSessionUpdateOne {
if s != nil {
usuo.SetUserAgent(*s)
}
return usuo
}
// SetIP sets the "ip" field.
func (usuo *UserSessionUpdateOne) SetIP(s string) *UserSessionUpdateOne {
usuo.mutation.SetIP(s)
return usuo
}
// SetNillableIP sets the "ip" field if the given value is not nil.
func (usuo *UserSessionUpdateOne) SetNillableIP(s *string) *UserSessionUpdateOne {
if s != nil {
usuo.SetIP(*s)
}
return usuo
}
// SetUserID sets the "user" edge to the User entity by ID.
func (usuo *UserSessionUpdateOne) SetUserID(id int64) *UserSessionUpdateOne {
usuo.mutation.SetUserID(id)
return usuo
}
// SetUser sets the "user" edge to the User entity.
func (usuo *UserSessionUpdateOne) SetUser(u *User) *UserSessionUpdateOne {
return usuo.SetUserID(u.ID)
}
// Mutation returns the UserSessionMutation object of the builder.
func (usuo *UserSessionUpdateOne) Mutation() *UserSessionMutation {
return usuo.mutation
}
// ClearUser clears the "user" edge to the User entity.
func (usuo *UserSessionUpdateOne) ClearUser() *UserSessionUpdateOne {
usuo.mutation.ClearUser()
return usuo
}
// Where appends a list predicates to the UserSessionUpdate builder.
func (usuo *UserSessionUpdateOne) Where(ps ...predicate.UserSession) *UserSessionUpdateOne {
usuo.mutation.Where(ps...)
return usuo
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (usuo *UserSessionUpdateOne) Select(field string, fields ...string) *UserSessionUpdateOne {
usuo.fields = append([]string{field}, fields...)
return usuo
}
// Save executes the query and returns the updated UserSession entity.
func (usuo *UserSessionUpdateOne) Save(ctx context.Context) (*UserSession, error) {
return withHooks(ctx, usuo.sqlSave, usuo.mutation, usuo.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (usuo *UserSessionUpdateOne) SaveX(ctx context.Context) *UserSession {
node, err := usuo.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (usuo *UserSessionUpdateOne) Exec(ctx context.Context) error {
_, err := usuo.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (usuo *UserSessionUpdateOne) ExecX(ctx context.Context) {
if err := usuo.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (usuo *UserSessionUpdateOne) check() error {
if v, ok := usuo.mutation.UserAgent(); ok {
if err := usersession.UserAgentValidator(v); err != nil {
return &ValidationError{Name: "user_agent", err: fmt.Errorf(`ent: validator failed for field "UserSession.user_agent": %w`, err)}
}
}
if v, ok := usuo.mutation.IP(); ok {
if err := usersession.IPValidator(v); err != nil {
return &ValidationError{Name: "ip", err: fmt.Errorf(`ent: validator failed for field "UserSession.ip": %w`, err)}
}
}
if usuo.mutation.UserCleared() && len(usuo.mutation.UserIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "UserSession.user"`)
}
return nil
}
func (usuo *UserSessionUpdateOne) sqlSave(ctx context.Context) (_node *UserSession, err error) {
if err := usuo.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(usersession.Table, usersession.Columns, sqlgraph.NewFieldSpec(usersession.FieldID, field.TypeInt64))
id, ok := usuo.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "UserSession.id" for update`)}
}
_spec.Node.ID.Value = id
if fields := usuo.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, usersession.FieldID)
for _, f := range fields {
if !usersession.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != usersession.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := usuo.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := usuo.mutation.IssuedAt(); ok {
_spec.SetField(usersession.FieldIssuedAt, field.TypeTime, value)
}
if value, ok := usuo.mutation.ExpiresAt(); ok {
_spec.SetField(usersession.FieldExpiresAt, field.TypeTime, value)
}
if value, ok := usuo.mutation.Invalidated(); ok {
_spec.SetField(usersession.FieldInvalidated, field.TypeBool, value)
}
if usuo.mutation.InvalidatedCleared() {
_spec.ClearField(usersession.FieldInvalidated, field.TypeBool)
}
if value, ok := usuo.mutation.UserAgent(); ok {
_spec.SetField(usersession.FieldUserAgent, field.TypeString, value)
}
if value, ok := usuo.mutation.IP(); ok {
_spec.SetField(usersession.FieldIP, field.TypeString, value)
}
if usuo.mutation.UserCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usersession.UserTable,
Columns: []string{usersession.UserColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := usuo.mutation.UserIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: usersession.UserTable,
Columns: []string{usersession.UserColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
_node = &UserSession{config: usuo.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues
if err = sqlgraph.UpdateNode(ctx, usuo.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{usersession.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
usuo.mutation.done = true
return _node, nil
}