rano/db/ent/audit_create.go
2024-11-10 14:52:33 +05:30

370 lines
10 KiB
Go

// 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/audit"
"gitserver.in/patialtech/rano/db/ent/user"
)
// AuditCreate is the builder for creating a Audit entity.
type AuditCreate struct {
config
mutation *AuditMutation
hooks []Hook
}
// SetCreatedAt sets the "created_at" field.
func (ac *AuditCreate) SetCreatedAt(t time.Time) *AuditCreate {
ac.mutation.SetCreatedAt(t)
return ac
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (ac *AuditCreate) SetNillableCreatedAt(t *time.Time) *AuditCreate {
if t != nil {
ac.SetCreatedAt(*t)
}
return ac
}
// SetEntName sets the "ent_name" field.
func (ac *AuditCreate) SetEntName(s string) *AuditCreate {
ac.mutation.SetEntName(s)
return ac
}
// SetEntID sets the "ent_id" field.
func (ac *AuditCreate) SetEntID(i int64) *AuditCreate {
ac.mutation.SetEntID(i)
return ac
}
// SetOperation sets the "operation" field.
func (ac *AuditCreate) SetOperation(a audit.Operation) *AuditCreate {
ac.mutation.SetOperation(a)
return ac
}
// SetDescription sets the "description" field.
func (ac *AuditCreate) SetDescription(s string) *AuditCreate {
ac.mutation.SetDescription(s)
return ac
}
// SetIP sets the "ip" field.
func (ac *AuditCreate) SetIP(s string) *AuditCreate {
ac.mutation.SetIP(s)
return ac
}
// SetNillableIP sets the "ip" field if the given value is not nil.
func (ac *AuditCreate) SetNillableIP(s *string) *AuditCreate {
if s != nil {
ac.SetIP(*s)
}
return ac
}
// SetUserName sets the "user_name" field.
func (ac *AuditCreate) SetUserName(s string) *AuditCreate {
ac.mutation.SetUserName(s)
return ac
}
// SetNillableUserName sets the "user_name" field if the given value is not nil.
func (ac *AuditCreate) SetNillableUserName(s *string) *AuditCreate {
if s != nil {
ac.SetUserName(*s)
}
return ac
}
// SetID sets the "id" field.
func (ac *AuditCreate) SetID(i int64) *AuditCreate {
ac.mutation.SetID(i)
return ac
}
// SetUserID sets the "user" edge to the User entity by ID.
func (ac *AuditCreate) SetUserID(id int64) *AuditCreate {
ac.mutation.SetUserID(id)
return ac
}
// SetNillableUserID sets the "user" edge to the User entity by ID if the given value is not nil.
func (ac *AuditCreate) SetNillableUserID(id *int64) *AuditCreate {
if id != nil {
ac = ac.SetUserID(*id)
}
return ac
}
// SetUser sets the "user" edge to the User entity.
func (ac *AuditCreate) SetUser(u *User) *AuditCreate {
return ac.SetUserID(u.ID)
}
// Mutation returns the AuditMutation object of the builder.
func (ac *AuditCreate) Mutation() *AuditMutation {
return ac.mutation
}
// Save creates the Audit in the database.
func (ac *AuditCreate) Save(ctx context.Context) (*Audit, error) {
ac.defaults()
return withHooks(ctx, ac.sqlSave, ac.mutation, ac.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (ac *AuditCreate) SaveX(ctx context.Context) *Audit {
v, err := ac.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (ac *AuditCreate) Exec(ctx context.Context) error {
_, err := ac.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (ac *AuditCreate) ExecX(ctx context.Context) {
if err := ac.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (ac *AuditCreate) defaults() {
if _, ok := ac.mutation.CreatedAt(); !ok {
v := audit.DefaultCreatedAt()
ac.mutation.SetCreatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (ac *AuditCreate) check() error {
if _, ok := ac.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Audit.created_at"`)}
}
if _, ok := ac.mutation.EntName(); !ok {
return &ValidationError{Name: "ent_name", err: errors.New(`ent: missing required field "Audit.ent_name"`)}
}
if v, ok := ac.mutation.EntName(); ok {
if err := audit.EntNameValidator(v); err != nil {
return &ValidationError{Name: "ent_name", err: fmt.Errorf(`ent: validator failed for field "Audit.ent_name": %w`, err)}
}
}
if _, ok := ac.mutation.EntID(); !ok {
return &ValidationError{Name: "ent_id", err: errors.New(`ent: missing required field "Audit.ent_id"`)}
}
if v, ok := ac.mutation.EntID(); ok {
if err := audit.EntIDValidator(v); err != nil {
return &ValidationError{Name: "ent_id", err: fmt.Errorf(`ent: validator failed for field "Audit.ent_id": %w`, err)}
}
}
if _, ok := ac.mutation.Operation(); !ok {
return &ValidationError{Name: "operation", err: errors.New(`ent: missing required field "Audit.operation"`)}
}
if v, ok := ac.mutation.Operation(); ok {
if err := audit.OperationValidator(v); err != nil {
return &ValidationError{Name: "operation", err: fmt.Errorf(`ent: validator failed for field "Audit.operation": %w`, err)}
}
}
if _, ok := ac.mutation.Description(); !ok {
return &ValidationError{Name: "description", err: errors.New(`ent: missing required field "Audit.description"`)}
}
if v, ok := ac.mutation.Description(); ok {
if err := audit.DescriptionValidator(v); err != nil {
return &ValidationError{Name: "description", err: fmt.Errorf(`ent: validator failed for field "Audit.description": %w`, err)}
}
}
if v, ok := ac.mutation.IP(); ok {
if err := audit.IPValidator(v); err != nil {
return &ValidationError{Name: "ip", err: fmt.Errorf(`ent: validator failed for field "Audit.ip": %w`, err)}
}
}
if v, ok := ac.mutation.UserName(); ok {
if err := audit.UserNameValidator(v); err != nil {
return &ValidationError{Name: "user_name", err: fmt.Errorf(`ent: validator failed for field "Audit.user_name": %w`, err)}
}
}
return nil
}
func (ac *AuditCreate) sqlSave(ctx context.Context) (*Audit, error) {
if err := ac.check(); err != nil {
return nil, err
}
_node, _spec := ac.createSpec()
if err := sqlgraph.CreateNode(ctx, ac.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)
}
ac.mutation.id = &_node.ID
ac.mutation.done = true
return _node, nil
}
func (ac *AuditCreate) createSpec() (*Audit, *sqlgraph.CreateSpec) {
var (
_node = &Audit{config: ac.config}
_spec = sqlgraph.NewCreateSpec(audit.Table, sqlgraph.NewFieldSpec(audit.FieldID, field.TypeInt64))
)
if id, ok := ac.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = id
}
if value, ok := ac.mutation.CreatedAt(); ok {
_spec.SetField(audit.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := ac.mutation.EntName(); ok {
_spec.SetField(audit.FieldEntName, field.TypeString, value)
_node.EntName = value
}
if value, ok := ac.mutation.EntID(); ok {
_spec.SetField(audit.FieldEntID, field.TypeInt64, value)
_node.EntID = value
}
if value, ok := ac.mutation.Operation(); ok {
_spec.SetField(audit.FieldOperation, field.TypeEnum, value)
_node.Operation = value
}
if value, ok := ac.mutation.Description(); ok {
_spec.SetField(audit.FieldDescription, field.TypeString, value)
_node.Description = value
}
if value, ok := ac.mutation.IP(); ok {
_spec.SetField(audit.FieldIP, field.TypeString, value)
_node.IP = value
}
if value, ok := ac.mutation.UserName(); ok {
_spec.SetField(audit.FieldUserName, field.TypeString, value)
_node.UserName = value
}
if nodes := ac.mutation.UserIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: audit.UserTable,
Columns: []string{audit.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
}
// AuditCreateBulk is the builder for creating many Audit entities in bulk.
type AuditCreateBulk struct {
config
err error
builders []*AuditCreate
}
// Save creates the Audit entities in the database.
func (acb *AuditCreateBulk) Save(ctx context.Context) ([]*Audit, error) {
if acb.err != nil {
return nil, acb.err
}
specs := make([]*sqlgraph.CreateSpec, len(acb.builders))
nodes := make([]*Audit, len(acb.builders))
mutators := make([]Mutator, len(acb.builders))
for i := range acb.builders {
func(i int, root context.Context) {
builder := acb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*AuditMutation)
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, acb.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, acb.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, acb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (acb *AuditCreateBulk) SaveX(ctx context.Context) []*Audit {
v, err := acb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (acb *AuditCreateBulk) Exec(ctx context.Context) error {
_, err := acb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (acb *AuditCreateBulk) ExecX(ctx context.Context) {
if err := acb.Exec(ctx); err != nil {
panic(err)
}
}