zkt24/z2/backend/ent/transactions_create.go
2024-04-25 13:51:47 +02:00

313 lines
8.9 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"thesis/ent/blocks"
"thesis/ent/key"
"thesis/ent/transactions"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
)
// TransactionsCreate is the builder for creating a Transactions entity.
type TransactionsCreate struct {
config
mutation *TransactionsMutation
hooks []Hook
}
// SetType sets the "type" field.
func (tc *TransactionsCreate) SetType(i int) *TransactionsCreate {
tc.mutation.SetType(i)
return tc
}
// SetTimestamp sets the "timestamp" field.
func (tc *TransactionsCreate) SetTimestamp(i int) *TransactionsCreate {
tc.mutation.SetTimestamp(i)
return tc
}
// SetComment sets the "comment" field.
func (tc *TransactionsCreate) SetComment(s string) *TransactionsCreate {
tc.mutation.SetComment(s)
return tc
}
// SetContent sets the "content" field.
func (tc *TransactionsCreate) SetContent(b []byte) *TransactionsCreate {
tc.mutation.SetContent(b)
return tc
}
// SetHash sets the "hash" field.
func (tc *TransactionsCreate) SetHash(s string) *TransactionsCreate {
tc.mutation.SetHash(s)
return tc
}
// SetSignature sets the "signature" field.
func (tc *TransactionsCreate) SetSignature(s string) *TransactionsCreate {
tc.mutation.SetSignature(s)
return tc
}
// AddSignerIDs adds the "Signer" edge to the Key entity by IDs.
func (tc *TransactionsCreate) AddSignerIDs(ids ...int) *TransactionsCreate {
tc.mutation.AddSignerIDs(ids...)
return tc
}
// AddSigner adds the "Signer" edges to the Key entity.
func (tc *TransactionsCreate) AddSigner(k ...*Key) *TransactionsCreate {
ids := make([]int, len(k))
for i := range k {
ids[i] = k[i].ID
}
return tc.AddSignerIDs(ids...)
}
// AddBlockIDs adds the "Block" edge to the Blocks entity by IDs.
func (tc *TransactionsCreate) AddBlockIDs(ids ...int) *TransactionsCreate {
tc.mutation.AddBlockIDs(ids...)
return tc
}
// AddBlock adds the "Block" edges to the Blocks entity.
func (tc *TransactionsCreate) AddBlock(b ...*Blocks) *TransactionsCreate {
ids := make([]int, len(b))
for i := range b {
ids[i] = b[i].ID
}
return tc.AddBlockIDs(ids...)
}
// Mutation returns the TransactionsMutation object of the builder.
func (tc *TransactionsCreate) Mutation() *TransactionsMutation {
return tc.mutation
}
// Save creates the Transactions in the database.
func (tc *TransactionsCreate) Save(ctx context.Context) (*Transactions, error) {
return withHooks(ctx, tc.sqlSave, tc.mutation, tc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (tc *TransactionsCreate) SaveX(ctx context.Context) *Transactions {
v, err := tc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (tc *TransactionsCreate) Exec(ctx context.Context) error {
_, err := tc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (tc *TransactionsCreate) ExecX(ctx context.Context) {
if err := tc.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (tc *TransactionsCreate) check() error {
if _, ok := tc.mutation.GetType(); !ok {
return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "Transactions.type"`)}
}
if _, ok := tc.mutation.Timestamp(); !ok {
return &ValidationError{Name: "timestamp", err: errors.New(`ent: missing required field "Transactions.timestamp"`)}
}
if _, ok := tc.mutation.Comment(); !ok {
return &ValidationError{Name: "comment", err: errors.New(`ent: missing required field "Transactions.comment"`)}
}
if _, ok := tc.mutation.Content(); !ok {
return &ValidationError{Name: "content", err: errors.New(`ent: missing required field "Transactions.content"`)}
}
if _, ok := tc.mutation.Hash(); !ok {
return &ValidationError{Name: "hash", err: errors.New(`ent: missing required field "Transactions.hash"`)}
}
if _, ok := tc.mutation.Signature(); !ok {
return &ValidationError{Name: "signature", err: errors.New(`ent: missing required field "Transactions.signature"`)}
}
return nil
}
func (tc *TransactionsCreate) sqlSave(ctx context.Context) (*Transactions, error) {
if err := tc.check(); err != nil {
return nil, err
}
_node, _spec := tc.createSpec()
if err := sqlgraph.CreateNode(ctx, tc.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
id := _spec.ID.Value.(int64)
_node.ID = int(id)
tc.mutation.id = &_node.ID
tc.mutation.done = true
return _node, nil
}
func (tc *TransactionsCreate) createSpec() (*Transactions, *sqlgraph.CreateSpec) {
var (
_node = &Transactions{config: tc.config}
_spec = sqlgraph.NewCreateSpec(transactions.Table, sqlgraph.NewFieldSpec(transactions.FieldID, field.TypeInt))
)
if value, ok := tc.mutation.GetType(); ok {
_spec.SetField(transactions.FieldType, field.TypeInt, value)
_node.Type = value
}
if value, ok := tc.mutation.Timestamp(); ok {
_spec.SetField(transactions.FieldTimestamp, field.TypeInt, value)
_node.Timestamp = value
}
if value, ok := tc.mutation.Comment(); ok {
_spec.SetField(transactions.FieldComment, field.TypeString, value)
_node.Comment = value
}
if value, ok := tc.mutation.Content(); ok {
_spec.SetField(transactions.FieldContent, field.TypeBytes, value)
_node.Content = value
}
if value, ok := tc.mutation.Hash(); ok {
_spec.SetField(transactions.FieldHash, field.TypeString, value)
_node.Hash = value
}
if value, ok := tc.mutation.Signature(); ok {
_spec.SetField(transactions.FieldSignature, field.TypeString, value)
_node.Signature = value
}
if nodes := tc.mutation.SignerIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: true,
Table: transactions.SignerTable,
Columns: transactions.SignerPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(key.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := tc.mutation.BlockIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2M,
Inverse: true,
Table: transactions.BlockTable,
Columns: transactions.BlockPrimaryKey,
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(blocks.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// TransactionsCreateBulk is the builder for creating many Transactions entities in bulk.
type TransactionsCreateBulk struct {
config
err error
builders []*TransactionsCreate
}
// Save creates the Transactions entities in the database.
func (tcb *TransactionsCreateBulk) Save(ctx context.Context) ([]*Transactions, error) {
if tcb.err != nil {
return nil, tcb.err
}
specs := make([]*sqlgraph.CreateSpec, len(tcb.builders))
nodes := make([]*Transactions, len(tcb.builders))
mutators := make([]Mutator, len(tcb.builders))
for i := range tcb.builders {
func(i int, root context.Context) {
builder := tcb.builders[i]
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*TransactionsMutation)
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, tcb.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, tcb.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 {
id := specs[i].ID.Value.(int64)
nodes[i].ID = int(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, tcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (tcb *TransactionsCreateBulk) SaveX(ctx context.Context) []*Transactions {
v, err := tcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (tcb *TransactionsCreateBulk) Exec(ctx context.Context) error {
_, err := tcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (tcb *TransactionsCreateBulk) ExecX(ctx context.Context) {
if err := tcb.Exec(ctx); err != nil {
panic(err)
}
}