pcmt/ent/settings_create.go

258 lines
6.8 KiB
Go
Raw Normal View History

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"git.dotya.ml/mirre-mt/pcmt/ent/settings"
"github.com/google/uuid"
)
// SettingsCreate is the builder for creating a Settings entity.
type SettingsCreate struct {
config
mutation *SettingsMutation
hooks []Hook
}
// SetHibpAPIKey sets the "hibp_api_key" field.
func (sc *SettingsCreate) SetHibpAPIKey(s string) *SettingsCreate {
sc.mutation.SetHibpAPIKey(s)
return sc
}
// SetNillableHibpAPIKey sets the "hibp_api_key" field if the given value is not nil.
func (sc *SettingsCreate) SetNillableHibpAPIKey(s *string) *SettingsCreate {
if s != nil {
sc.SetHibpAPIKey(*s)
}
return sc
}
// SetDehashedAPIKey sets the "dehashed_api_key" field.
func (sc *SettingsCreate) SetDehashedAPIKey(s string) *SettingsCreate {
sc.mutation.SetDehashedAPIKey(s)
return sc
}
// SetNillableDehashedAPIKey sets the "dehashed_api_key" field if the given value is not nil.
func (sc *SettingsCreate) SetNillableDehashedAPIKey(s *string) *SettingsCreate {
if s != nil {
sc.SetDehashedAPIKey(*s)
}
return sc
}
// SetSearches sets the "searches" field.
func (sc *SettingsCreate) SetSearches(u uint64) *SettingsCreate {
sc.mutation.SetSearches(u)
return sc
}
// SetNillableSearches sets the "searches" field if the given value is not nil.
func (sc *SettingsCreate) SetNillableSearches(u *uint64) *SettingsCreate {
if u != nil {
sc.SetSearches(*u)
}
return sc
}
// SetID sets the "id" field.
func (sc *SettingsCreate) SetID(u uuid.UUID) *SettingsCreate {
sc.mutation.SetID(u)
return sc
}
// SetNillableID sets the "id" field if the given value is not nil.
func (sc *SettingsCreate) SetNillableID(u *uuid.UUID) *SettingsCreate {
if u != nil {
sc.SetID(*u)
}
return sc
}
// Mutation returns the SettingsMutation object of the builder.
func (sc *SettingsCreate) Mutation() *SettingsMutation {
return sc.mutation
}
// Save creates the Settings in the database.
func (sc *SettingsCreate) Save(ctx context.Context) (*Settings, error) {
sc.defaults()
return withHooks(ctx, sc.sqlSave, sc.mutation, sc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (sc *SettingsCreate) SaveX(ctx context.Context) *Settings {
v, err := sc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (sc *SettingsCreate) Exec(ctx context.Context) error {
_, err := sc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (sc *SettingsCreate) ExecX(ctx context.Context) {
if err := sc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (sc *SettingsCreate) defaults() {
if _, ok := sc.mutation.Searches(); !ok {
v := settings.DefaultSearches
sc.mutation.SetSearches(v)
}
if _, ok := sc.mutation.ID(); !ok {
v := settings.DefaultID()
sc.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (sc *SettingsCreate) check() error {
if _, ok := sc.mutation.Searches(); !ok {
return &ValidationError{Name: "searches", err: errors.New(`ent: missing required field "Settings.searches"`)}
}
return nil
}
func (sc *SettingsCreate) sqlSave(ctx context.Context) (*Settings, error) {
if err := sc.check(); err != nil {
return nil, err
}
_node, _spec := sc.createSpec()
if err := sqlgraph.CreateNode(ctx, sc.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
if _spec.ID.Value != nil {
if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
_node.ID = *id
} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
return nil, err
}
}
sc.mutation.id = &_node.ID
sc.mutation.done = true
return _node, nil
}
func (sc *SettingsCreate) createSpec() (*Settings, *sqlgraph.CreateSpec) {
var (
_node = &Settings{config: sc.config}
_spec = sqlgraph.NewCreateSpec(settings.Table, sqlgraph.NewFieldSpec(settings.FieldID, field.TypeUUID))
)
if id, ok := sc.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
if value, ok := sc.mutation.HibpAPIKey(); ok {
_spec.SetField(settings.FieldHibpAPIKey, field.TypeString, value)
_node.HibpAPIKey = value
}
if value, ok := sc.mutation.DehashedAPIKey(); ok {
_spec.SetField(settings.FieldDehashedAPIKey, field.TypeString, value)
_node.DehashedAPIKey = value
}
if value, ok := sc.mutation.Searches(); ok {
_spec.SetField(settings.FieldSearches, field.TypeUint64, value)
_node.Searches = value
}
return _node, _spec
}
// SettingsCreateBulk is the builder for creating many Settings entities in bulk.
type SettingsCreateBulk struct {
config
builders []*SettingsCreate
}
// Save creates the Settings entities in the database.
func (scb *SettingsCreateBulk) Save(ctx context.Context) ([]*Settings, error) {
specs := make([]*sqlgraph.CreateSpec, len(scb.builders))
nodes := make([]*Settings, len(scb.builders))
mutators := make([]Mutator, len(scb.builders))
for i := range scb.builders {
func(i int, root context.Context) {
builder := scb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*SettingsMutation)
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, scb.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, scb.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
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, scb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (scb *SettingsCreateBulk) SaveX(ctx context.Context) []*Settings {
v, err := scb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (scb *SettingsCreateBulk) Exec(ctx context.Context) error {
_, err := scb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (scb *SettingsCreateBulk) ExecX(ctx context.Context) {
if err := scb.Exec(ctx); err != nil {
panic(err)
}
}