// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "time" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" "gitserver.in/patialtech/rano/db/ent/user" "gitserver.in/patialtech/rano/db/ent/usersession" ) // UserSessionCreate is the builder for creating a UserSession entity. type UserSessionCreate struct { config mutation *UserSessionMutation hooks []Hook } // SetIssuedAt sets the "issued_at" field. func (usc *UserSessionCreate) SetIssuedAt(t time.Time) *UserSessionCreate { usc.mutation.SetIssuedAt(t) return usc } // SetExpiresAt sets the "expires_at" field. func (usc *UserSessionCreate) SetExpiresAt(t time.Time) *UserSessionCreate { usc.mutation.SetExpiresAt(t) return usc } // SetInvalidated sets the "invalidated" field. func (usc *UserSessionCreate) SetInvalidated(b bool) *UserSessionCreate { usc.mutation.SetInvalidated(b) return usc } // SetNillableInvalidated sets the "invalidated" field if the given value is not nil. func (usc *UserSessionCreate) SetNillableInvalidated(b *bool) *UserSessionCreate { if b != nil { usc.SetInvalidated(*b) } return usc } // SetUserAgent sets the "user_agent" field. func (usc *UserSessionCreate) SetUserAgent(s string) *UserSessionCreate { usc.mutation.SetUserAgent(s) return usc } // SetIP sets the "ip" field. func (usc *UserSessionCreate) SetIP(s string) *UserSessionCreate { usc.mutation.SetIP(s) return usc } // SetID sets the "id" field. func (usc *UserSessionCreate) SetID(i int64) *UserSessionCreate { usc.mutation.SetID(i) return usc } // SetUserID sets the "user" edge to the User entity by ID. func (usc *UserSessionCreate) SetUserID(id int64) *UserSessionCreate { usc.mutation.SetUserID(id) return usc } // SetUser sets the "user" edge to the User entity. func (usc *UserSessionCreate) SetUser(u *User) *UserSessionCreate { return usc.SetUserID(u.ID) } // Mutation returns the UserSessionMutation object of the builder. func (usc *UserSessionCreate) Mutation() *UserSessionMutation { return usc.mutation } // Save creates the UserSession in the database. func (usc *UserSessionCreate) Save(ctx context.Context) (*UserSession, error) { usc.defaults() return withHooks(ctx, usc.sqlSave, usc.mutation, usc.hooks) } // SaveX calls Save and panics if Save returns an error. func (usc *UserSessionCreate) SaveX(ctx context.Context) *UserSession { v, err := usc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (usc *UserSessionCreate) Exec(ctx context.Context) error { _, err := usc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (usc *UserSessionCreate) ExecX(ctx context.Context) { if err := usc.Exec(ctx); err != nil { panic(err) } } // defaults sets the default values of the builder before save. func (usc *UserSessionCreate) defaults() { if _, ok := usc.mutation.Invalidated(); !ok { v := usersession.DefaultInvalidated usc.mutation.SetInvalidated(v) } } // check runs all checks and user-defined validators on the builder. func (usc *UserSessionCreate) check() error { if _, ok := usc.mutation.IssuedAt(); !ok { return &ValidationError{Name: "issued_at", err: errors.New(`ent: missing required field "UserSession.issued_at"`)} } if _, ok := usc.mutation.ExpiresAt(); !ok { return &ValidationError{Name: "expires_at", err: errors.New(`ent: missing required field "UserSession.expires_at"`)} } if _, ok := usc.mutation.UserAgent(); !ok { return &ValidationError{Name: "user_agent", err: errors.New(`ent: missing required field "UserSession.user_agent"`)} } if v, ok := usc.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 _, ok := usc.mutation.IP(); !ok { return &ValidationError{Name: "ip", err: errors.New(`ent: missing required field "UserSession.ip"`)} } if v, ok := usc.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 len(usc.mutation.UserIDs()) == 0 { return &ValidationError{Name: "user", err: errors.New(`ent: missing required edge "UserSession.user"`)} } return nil } func (usc *UserSessionCreate) sqlSave(ctx context.Context) (*UserSession, error) { if err := usc.check(); err != nil { return nil, err } _node, _spec := usc.createSpec() if err := sqlgraph.CreateNode(ctx, usc.driver, _spec); err != nil { if sqlgraph.IsConstraintError(err) { err = &ConstraintError{msg: err.Error(), wrap: err} } return nil, err } if _spec.ID.Value != _node.ID { id := _spec.ID.Value.(int64) _node.ID = int64(id) } usc.mutation.id = &_node.ID usc.mutation.done = true return _node, nil } func (usc *UserSessionCreate) createSpec() (*UserSession, *sqlgraph.CreateSpec) { var ( _node = &UserSession{config: usc.config} _spec = sqlgraph.NewCreateSpec(usersession.Table, sqlgraph.NewFieldSpec(usersession.FieldID, field.TypeInt64)) ) if id, ok := usc.mutation.ID(); ok { _node.ID = id _spec.ID.Value = id } if value, ok := usc.mutation.IssuedAt(); ok { _spec.SetField(usersession.FieldIssuedAt, field.TypeTime, value) _node.IssuedAt = value } if value, ok := usc.mutation.ExpiresAt(); ok { _spec.SetField(usersession.FieldExpiresAt, field.TypeTime, value) _node.ExpiresAt = value } if value, ok := usc.mutation.Invalidated(); ok { _spec.SetField(usersession.FieldInvalidated, field.TypeBool, value) _node.Invalidated = value } if value, ok := usc.mutation.UserAgent(); ok { _spec.SetField(usersession.FieldUserAgent, field.TypeString, value) _node.UserAgent = value } if value, ok := usc.mutation.IP(); ok { _spec.SetField(usersession.FieldIP, field.TypeString, value) _node.IP = value } if nodes := usc.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) } _node.user_id = &nodes[0] _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // UserSessionCreateBulk is the builder for creating many UserSession entities in bulk. type UserSessionCreateBulk struct { config err error builders []*UserSessionCreate } // Save creates the UserSession entities in the database. func (uscb *UserSessionCreateBulk) Save(ctx context.Context) ([]*UserSession, error) { if uscb.err != nil { return nil, uscb.err } specs := make([]*sqlgraph.CreateSpec, len(uscb.builders)) nodes := make([]*UserSession, len(uscb.builders)) mutators := make([]Mutator, len(uscb.builders)) for i := range uscb.builders { func(i int, root context.Context) { builder := uscb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*UserSessionMutation) 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, uscb.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, uscb.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 && nodes[i].ID == 0 { id := specs[i].ID.Value.(int64) nodes[i].ID = int64(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, uscb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (uscb *UserSessionCreateBulk) SaveX(ctx context.Context) []*UserSession { v, err := uscb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (uscb *UserSessionCreateBulk) Exec(ctx context.Context) error { _, err := uscb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (uscb *UserSessionCreateBulk) ExecX(ctx context.Context) { if err := uscb.Exec(ctx); err != nil { panic(err) } }