ka/ent/profile_create.go

335 lines
8.6 KiB
Go
Raw Normal View History

2024-10-04 20:22:25 +02:00
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"code.icod.de/dalu/ka/ent/post"
"code.icod.de/dalu/ka/ent/profile"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/google/uuid"
)
// ProfileCreate is the builder for creating a Profile entity.
type ProfileCreate struct {
config
mutation *ProfileMutation
hooks []Hook
}
// SetCreatedAt sets the "created_at" field.
func (pc *ProfileCreate) SetCreatedAt(t time.Time) *ProfileCreate {
pc.mutation.SetCreatedAt(t)
return pc
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (pc *ProfileCreate) SetNillableCreatedAt(t *time.Time) *ProfileCreate {
if t != nil {
pc.SetCreatedAt(*t)
}
return pc
}
// SetUpdatedAt sets the "updated_at" field.
func (pc *ProfileCreate) SetUpdatedAt(t time.Time) *ProfileCreate {
pc.mutation.SetUpdatedAt(t)
return pc
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (pc *ProfileCreate) SetNillableUpdatedAt(t *time.Time) *ProfileCreate {
if t != nil {
pc.SetUpdatedAt(*t)
}
return pc
}
// SetName sets the "name" field.
func (pc *ProfileCreate) SetName(s string) *ProfileCreate {
pc.mutation.SetName(s)
return pc
}
// SetNillableName sets the "name" field if the given value is not nil.
func (pc *ProfileCreate) SetNillableName(s *string) *ProfileCreate {
if s != nil {
pc.SetName(*s)
}
return pc
}
// SetAddress sets the "address" field.
func (pc *ProfileCreate) SetAddress(s string) *ProfileCreate {
pc.mutation.SetAddress(s)
return pc
}
// SetNillableAddress sets the "address" field if the given value is not nil.
func (pc *ProfileCreate) SetNillableAddress(s *string) *ProfileCreate {
if s != nil {
pc.SetAddress(*s)
}
return pc
}
// SetPhone sets the "phone" field.
func (pc *ProfileCreate) SetPhone(s string) *ProfileCreate {
pc.mutation.SetPhone(s)
return pc
}
// SetNillablePhone sets the "phone" field if the given value is not nil.
func (pc *ProfileCreate) SetNillablePhone(s *string) *ProfileCreate {
if s != nil {
pc.SetPhone(*s)
}
return pc
}
// SetID sets the "id" field.
func (pc *ProfileCreate) SetID(u uuid.UUID) *ProfileCreate {
pc.mutation.SetID(u)
return pc
}
// SetNillableID sets the "id" field if the given value is not nil.
func (pc *ProfileCreate) SetNillableID(u *uuid.UUID) *ProfileCreate {
if u != nil {
pc.SetID(*u)
}
return pc
}
// AddPostIDs adds the "posts" edge to the Post entity by IDs.
func (pc *ProfileCreate) AddPostIDs(ids ...uuid.UUID) *ProfileCreate {
pc.mutation.AddPostIDs(ids...)
return pc
}
// AddPosts adds the "posts" edges to the Post entity.
func (pc *ProfileCreate) AddPosts(p ...*Post) *ProfileCreate {
ids := make([]uuid.UUID, len(p))
for i := range p {
ids[i] = p[i].ID
}
return pc.AddPostIDs(ids...)
}
// Mutation returns the ProfileMutation object of the builder.
func (pc *ProfileCreate) Mutation() *ProfileMutation {
return pc.mutation
}
// Save creates the Profile in the database.
func (pc *ProfileCreate) Save(ctx context.Context) (*Profile, error) {
pc.defaults()
return withHooks(ctx, pc.sqlSave, pc.mutation, pc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (pc *ProfileCreate) SaveX(ctx context.Context) *Profile {
v, err := pc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (pc *ProfileCreate) Exec(ctx context.Context) error {
_, err := pc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (pc *ProfileCreate) ExecX(ctx context.Context) {
if err := pc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (pc *ProfileCreate) defaults() {
if _, ok := pc.mutation.CreatedAt(); !ok {
v := profile.DefaultCreatedAt()
pc.mutation.SetCreatedAt(v)
}
if _, ok := pc.mutation.UpdatedAt(); !ok {
v := profile.DefaultUpdatedAt()
pc.mutation.SetUpdatedAt(v)
}
if _, ok := pc.mutation.ID(); !ok {
v := profile.DefaultID()
pc.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (pc *ProfileCreate) check() error {
if _, ok := pc.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Profile.created_at"`)}
}
return nil
}
func (pc *ProfileCreate) sqlSave(ctx context.Context) (*Profile, error) {
if err := pc.check(); err != nil {
return nil, err
}
_node, _spec := pc.createSpec()
if err := sqlgraph.CreateNode(ctx, pc.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
}
}
pc.mutation.id = &_node.ID
pc.mutation.done = true
return _node, nil
}
func (pc *ProfileCreate) createSpec() (*Profile, *sqlgraph.CreateSpec) {
var (
_node = &Profile{config: pc.config}
_spec = sqlgraph.NewCreateSpec(profile.Table, sqlgraph.NewFieldSpec(profile.FieldID, field.TypeUUID))
)
if id, ok := pc.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
if value, ok := pc.mutation.CreatedAt(); ok {
_spec.SetField(profile.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := pc.mutation.UpdatedAt(); ok {
_spec.SetField(profile.FieldUpdatedAt, field.TypeTime, value)
_node.UpdatedAt = value
}
if value, ok := pc.mutation.Name(); ok {
_spec.SetField(profile.FieldName, field.TypeString, value)
_node.Name = value
}
if value, ok := pc.mutation.Address(); ok {
_spec.SetField(profile.FieldAddress, field.TypeString, value)
_node.Address = value
}
if value, ok := pc.mutation.Phone(); ok {
_spec.SetField(profile.FieldPhone, field.TypeString, value)
_node.Phone = value
}
if nodes := pc.mutation.PostsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: profile.PostsTable,
Columns: []string{profile.PostsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(post.FieldID, field.TypeUUID),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// ProfileCreateBulk is the builder for creating many Profile entities in bulk.
type ProfileCreateBulk struct {
config
err error
builders []*ProfileCreate
}
// Save creates the Profile entities in the database.
func (pcb *ProfileCreateBulk) Save(ctx context.Context) ([]*Profile, error) {
if pcb.err != nil {
return nil, pcb.err
}
specs := make([]*sqlgraph.CreateSpec, len(pcb.builders))
nodes := make([]*Profile, len(pcb.builders))
mutators := make([]Mutator, len(pcb.builders))
for i := range pcb.builders {
func(i int, root context.Context) {
builder := pcb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*ProfileMutation)
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, pcb.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, pcb.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, pcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (pcb *ProfileCreateBulk) SaveX(ctx context.Context) []*Profile {
v, err := pcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (pcb *ProfileCreateBulk) Exec(ctx context.Context) error {
_, err := pcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (pcb *ProfileCreateBulk) ExecX(ctx context.Context) {
if err := pcb.Exec(ctx); err != nil {
panic(err)
}
}