270 lines
7.1 KiB
Go
270 lines
7.1 KiB
Go
|
// Code generated by ent, DO NOT EDIT.
|
||
|
|
||
|
package ent
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"thesis/ent/key"
|
||
|
"thesis/ent/transactions"
|
||
|
|
||
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
||
|
"entgo.io/ent/schema/field"
|
||
|
)
|
||
|
|
||
|
// KeyCreate is the builder for creating a Key entity.
|
||
|
type KeyCreate struct {
|
||
|
config
|
||
|
mutation *KeyMutation
|
||
|
hooks []Hook
|
||
|
}
|
||
|
|
||
|
// SetPublicKey sets the "publicKey" field.
|
||
|
func (kc *KeyCreate) SetPublicKey(s string) *KeyCreate {
|
||
|
kc.mutation.SetPublicKey(s)
|
||
|
return kc
|
||
|
}
|
||
|
|
||
|
// SetOwner sets the "Owner" field.
|
||
|
func (kc *KeyCreate) SetOwner(s string) *KeyCreate {
|
||
|
kc.mutation.SetOwner(s)
|
||
|
return kc
|
||
|
}
|
||
|
|
||
|
// SetTrustScore sets the "trustScore" field.
|
||
|
func (kc *KeyCreate) SetTrustScore(f float64) *KeyCreate {
|
||
|
kc.mutation.SetTrustScore(f)
|
||
|
return kc
|
||
|
}
|
||
|
|
||
|
// SetNillableTrustScore sets the "trustScore" field if the given value is not nil.
|
||
|
func (kc *KeyCreate) SetNillableTrustScore(f *float64) *KeyCreate {
|
||
|
if f != nil {
|
||
|
kc.SetTrustScore(*f)
|
||
|
}
|
||
|
return kc
|
||
|
}
|
||
|
|
||
|
// AddSignedIDs adds the "Signed" edge to the Transactions entity by IDs.
|
||
|
func (kc *KeyCreate) AddSignedIDs(ids ...int) *KeyCreate {
|
||
|
kc.mutation.AddSignedIDs(ids...)
|
||
|
return kc
|
||
|
}
|
||
|
|
||
|
// AddSigned adds the "Signed" edges to the Transactions entity.
|
||
|
func (kc *KeyCreate) AddSigned(t ...*Transactions) *KeyCreate {
|
||
|
ids := make([]int, len(t))
|
||
|
for i := range t {
|
||
|
ids[i] = t[i].ID
|
||
|
}
|
||
|
return kc.AddSignedIDs(ids...)
|
||
|
}
|
||
|
|
||
|
// Mutation returns the KeyMutation object of the builder.
|
||
|
func (kc *KeyCreate) Mutation() *KeyMutation {
|
||
|
return kc.mutation
|
||
|
}
|
||
|
|
||
|
// Save creates the Key in the database.
|
||
|
func (kc *KeyCreate) Save(ctx context.Context) (*Key, error) {
|
||
|
kc.defaults()
|
||
|
return withHooks(ctx, kc.sqlSave, kc.mutation, kc.hooks)
|
||
|
}
|
||
|
|
||
|
// SaveX calls Save and panics if Save returns an error.
|
||
|
func (kc *KeyCreate) SaveX(ctx context.Context) *Key {
|
||
|
v, err := kc.Save(ctx)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return v
|
||
|
}
|
||
|
|
||
|
// Exec executes the query.
|
||
|
func (kc *KeyCreate) Exec(ctx context.Context) error {
|
||
|
_, err := kc.Save(ctx)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// ExecX is like Exec, but panics if an error occurs.
|
||
|
func (kc *KeyCreate) ExecX(ctx context.Context) {
|
||
|
if err := kc.Exec(ctx); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// defaults sets the default values of the builder before save.
|
||
|
func (kc *KeyCreate) defaults() {
|
||
|
if _, ok := kc.mutation.TrustScore(); !ok {
|
||
|
v := key.DefaultTrustScore
|
||
|
kc.mutation.SetTrustScore(v)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// check runs all checks and user-defined validators on the builder.
|
||
|
func (kc *KeyCreate) check() error {
|
||
|
if _, ok := kc.mutation.PublicKey(); !ok {
|
||
|
return &ValidationError{Name: "publicKey", err: errors.New(`ent: missing required field "Key.publicKey"`)}
|
||
|
}
|
||
|
if v, ok := kc.mutation.PublicKey(); ok {
|
||
|
if err := key.PublicKeyValidator(v); err != nil {
|
||
|
return &ValidationError{Name: "publicKey", err: fmt.Errorf(`ent: validator failed for field "Key.publicKey": %w`, err)}
|
||
|
}
|
||
|
}
|
||
|
if _, ok := kc.mutation.Owner(); !ok {
|
||
|
return &ValidationError{Name: "Owner", err: errors.New(`ent: missing required field "Key.Owner"`)}
|
||
|
}
|
||
|
if v, ok := kc.mutation.Owner(); ok {
|
||
|
if err := key.OwnerValidator(v); err != nil {
|
||
|
return &ValidationError{Name: "Owner", err: fmt.Errorf(`ent: validator failed for field "Key.Owner": %w`, err)}
|
||
|
}
|
||
|
}
|
||
|
if _, ok := kc.mutation.TrustScore(); !ok {
|
||
|
return &ValidationError{Name: "trustScore", err: errors.New(`ent: missing required field "Key.trustScore"`)}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (kc *KeyCreate) sqlSave(ctx context.Context) (*Key, error) {
|
||
|
if err := kc.check(); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
_node, _spec := kc.createSpec()
|
||
|
if err := sqlgraph.CreateNode(ctx, kc.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)
|
||
|
kc.mutation.id = &_node.ID
|
||
|
kc.mutation.done = true
|
||
|
return _node, nil
|
||
|
}
|
||
|
|
||
|
func (kc *KeyCreate) createSpec() (*Key, *sqlgraph.CreateSpec) {
|
||
|
var (
|
||
|
_node = &Key{config: kc.config}
|
||
|
_spec = sqlgraph.NewCreateSpec(key.Table, sqlgraph.NewFieldSpec(key.FieldID, field.TypeInt))
|
||
|
)
|
||
|
if value, ok := kc.mutation.PublicKey(); ok {
|
||
|
_spec.SetField(key.FieldPublicKey, field.TypeString, value)
|
||
|
_node.PublicKey = value
|
||
|
}
|
||
|
if value, ok := kc.mutation.Owner(); ok {
|
||
|
_spec.SetField(key.FieldOwner, field.TypeString, value)
|
||
|
_node.Owner = value
|
||
|
}
|
||
|
if value, ok := kc.mutation.TrustScore(); ok {
|
||
|
_spec.SetField(key.FieldTrustScore, field.TypeFloat64, value)
|
||
|
_node.TrustScore = value
|
||
|
}
|
||
|
if nodes := kc.mutation.SignedIDs(); len(nodes) > 0 {
|
||
|
edge := &sqlgraph.EdgeSpec{
|
||
|
Rel: sqlgraph.M2M,
|
||
|
Inverse: false,
|
||
|
Table: key.SignedTable,
|
||
|
Columns: key.SignedPrimaryKey,
|
||
|
Bidi: false,
|
||
|
Target: &sqlgraph.EdgeTarget{
|
||
|
IDSpec: sqlgraph.NewFieldSpec(transactions.FieldID, field.TypeInt),
|
||
|
},
|
||
|
}
|
||
|
for _, k := range nodes {
|
||
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
||
|
}
|
||
|
_spec.Edges = append(_spec.Edges, edge)
|
||
|
}
|
||
|
return _node, _spec
|
||
|
}
|
||
|
|
||
|
// KeyCreateBulk is the builder for creating many Key entities in bulk.
|
||
|
type KeyCreateBulk struct {
|
||
|
config
|
||
|
err error
|
||
|
builders []*KeyCreate
|
||
|
}
|
||
|
|
||
|
// Save creates the Key entities in the database.
|
||
|
func (kcb *KeyCreateBulk) Save(ctx context.Context) ([]*Key, error) {
|
||
|
if kcb.err != nil {
|
||
|
return nil, kcb.err
|
||
|
}
|
||
|
specs := make([]*sqlgraph.CreateSpec, len(kcb.builders))
|
||
|
nodes := make([]*Key, len(kcb.builders))
|
||
|
mutators := make([]Mutator, len(kcb.builders))
|
||
|
for i := range kcb.builders {
|
||
|
func(i int, root context.Context) {
|
||
|
builder := kcb.builders[i]
|
||
|
builder.defaults()
|
||
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||
|
mutation, ok := m.(*KeyMutation)
|
||
|
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, kcb.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, kcb.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, kcb.builders[0].mutation); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
}
|
||
|
return nodes, nil
|
||
|
}
|
||
|
|
||
|
// SaveX is like Save, but panics if an error occurs.
|
||
|
func (kcb *KeyCreateBulk) SaveX(ctx context.Context) []*Key {
|
||
|
v, err := kcb.Save(ctx)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return v
|
||
|
}
|
||
|
|
||
|
// Exec executes the query.
|
||
|
func (kcb *KeyCreateBulk) Exec(ctx context.Context) error {
|
||
|
_, err := kcb.Save(ctx)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// ExecX is like Exec, but panics if an error occurs.
|
||
|
func (kcb *KeyCreateBulk) ExecX(ctx context.Context) {
|
||
|
if err := kcb.Exec(ctx); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|