// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "time" "code.icod.de/dalu/ka/ent/category" "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" ) // PostCreate is the builder for creating a Post entity. type PostCreate struct { config mutation *PostMutation hooks []Hook } // SetCreatedAt sets the "created_at" field. func (pc *PostCreate) SetCreatedAt(t time.Time) *PostCreate { pc.mutation.SetCreatedAt(t) return pc } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (pc *PostCreate) SetNillableCreatedAt(t *time.Time) *PostCreate { if t != nil { pc.SetCreatedAt(*t) } return pc } // SetUpdatedAt sets the "updated_at" field. func (pc *PostCreate) SetUpdatedAt(t time.Time) *PostCreate { pc.mutation.SetUpdatedAt(t) return pc } // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. func (pc *PostCreate) SetNillableUpdatedAt(t *time.Time) *PostCreate { if t != nil { pc.SetUpdatedAt(*t) } return pc } // SetExpires sets the "expires" field. func (pc *PostCreate) SetExpires(b bool) *PostCreate { pc.mutation.SetExpires(b) return pc } // SetNillableExpires sets the "expires" field if the given value is not nil. func (pc *PostCreate) SetNillableExpires(b *bool) *PostCreate { if b != nil { pc.SetExpires(*b) } return pc } // SetExpireTime sets the "expire_time" field. func (pc *PostCreate) SetExpireTime(t time.Time) *PostCreate { pc.mutation.SetExpireTime(t) return pc } // SetNillableExpireTime sets the "expire_time" field if the given value is not nil. func (pc *PostCreate) SetNillableExpireTime(t *time.Time) *PostCreate { if t != nil { pc.SetExpireTime(*t) } return pc } // SetTitle sets the "title" field. func (pc *PostCreate) SetTitle(s string) *PostCreate { pc.mutation.SetTitle(s) return pc } // SetBody sets the "body" field. func (pc *PostCreate) SetBody(s string) *PostCreate { pc.mutation.SetBody(s) return pc } // SetID sets the "id" field. func (pc *PostCreate) SetID(u uuid.UUID) *PostCreate { pc.mutation.SetID(u) return pc } // SetNillableID sets the "id" field if the given value is not nil. func (pc *PostCreate) SetNillableID(u *uuid.UUID) *PostCreate { if u != nil { pc.SetID(*u) } return pc } // SetCategoryID sets the "category" edge to the Category entity by ID. func (pc *PostCreate) SetCategoryID(id uuid.UUID) *PostCreate { pc.mutation.SetCategoryID(id) return pc } // SetNillableCategoryID sets the "category" edge to the Category entity by ID if the given value is not nil. func (pc *PostCreate) SetNillableCategoryID(id *uuid.UUID) *PostCreate { if id != nil { pc = pc.SetCategoryID(*id) } return pc } // SetCategory sets the "category" edge to the Category entity. func (pc *PostCreate) SetCategory(c *Category) *PostCreate { return pc.SetCategoryID(c.ID) } // SetProfileID sets the "profile" edge to the Profile entity by ID. func (pc *PostCreate) SetProfileID(id uuid.UUID) *PostCreate { pc.mutation.SetProfileID(id) return pc } // SetNillableProfileID sets the "profile" edge to the Profile entity by ID if the given value is not nil. func (pc *PostCreate) SetNillableProfileID(id *uuid.UUID) *PostCreate { if id != nil { pc = pc.SetProfileID(*id) } return pc } // SetProfile sets the "profile" edge to the Profile entity. func (pc *PostCreate) SetProfile(p *Profile) *PostCreate { return pc.SetProfileID(p.ID) } // Mutation returns the PostMutation object of the builder. func (pc *PostCreate) Mutation() *PostMutation { return pc.mutation } // Save creates the Post in the database. func (pc *PostCreate) Save(ctx context.Context) (*Post, error) { pc.defaults() return withHooks(ctx, pc.sqlSave, pc.mutation, pc.hooks) } // SaveX calls Save and panics if Save returns an error. func (pc *PostCreate) SaveX(ctx context.Context) *Post { v, err := pc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (pc *PostCreate) Exec(ctx context.Context) error { _, err := pc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (pc *PostCreate) 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 *PostCreate) defaults() { if _, ok := pc.mutation.CreatedAt(); !ok { v := post.DefaultCreatedAt() pc.mutation.SetCreatedAt(v) } if _, ok := pc.mutation.UpdatedAt(); !ok { v := post.DefaultUpdatedAt() pc.mutation.SetUpdatedAt(v) } if _, ok := pc.mutation.Expires(); !ok { v := post.DefaultExpires pc.mutation.SetExpires(v) } if _, ok := pc.mutation.ID(); !ok { v := post.DefaultID() pc.mutation.SetID(v) } } // check runs all checks and user-defined validators on the builder. func (pc *PostCreate) check() error { if _, ok := pc.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Post.created_at"`)} } if _, ok := pc.mutation.Expires(); !ok { return &ValidationError{Name: "expires", err: errors.New(`ent: missing required field "Post.expires"`)} } if _, ok := pc.mutation.Title(); !ok { return &ValidationError{Name: "title", err: errors.New(`ent: missing required field "Post.title"`)} } if v, ok := pc.mutation.Title(); ok { if err := post.TitleValidator(v); err != nil { return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "Post.title": %w`, err)} } } if _, ok := pc.mutation.Body(); !ok { return &ValidationError{Name: "body", err: errors.New(`ent: missing required field "Post.body"`)} } if v, ok := pc.mutation.Body(); ok { if err := post.BodyValidator(v); err != nil { return &ValidationError{Name: "body", err: fmt.Errorf(`ent: validator failed for field "Post.body": %w`, err)} } } return nil } func (pc *PostCreate) sqlSave(ctx context.Context) (*Post, 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 *PostCreate) createSpec() (*Post, *sqlgraph.CreateSpec) { var ( _node = &Post{config: pc.config} _spec = sqlgraph.NewCreateSpec(post.Table, sqlgraph.NewFieldSpec(post.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(post.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value } if value, ok := pc.mutation.UpdatedAt(); ok { _spec.SetField(post.FieldUpdatedAt, field.TypeTime, value) _node.UpdatedAt = value } if value, ok := pc.mutation.Expires(); ok { _spec.SetField(post.FieldExpires, field.TypeBool, value) _node.Expires = value } if value, ok := pc.mutation.ExpireTime(); ok { _spec.SetField(post.FieldExpireTime, field.TypeTime, value) _node.ExpireTime = &value } if value, ok := pc.mutation.Title(); ok { _spec.SetField(post.FieldTitle, field.TypeString, value) _node.Title = value } if value, ok := pc.mutation.Body(); ok { _spec.SetField(post.FieldBody, field.TypeString, value) _node.Body = value } if nodes := pc.mutation.CategoryIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: post.CategoryTable, Columns: []string{post.CategoryColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(category.FieldID, field.TypeUUID), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.category_posts = &nodes[0] _spec.Edges = append(_spec.Edges, edge) } if nodes := pc.mutation.ProfileIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: post.ProfileTable, Columns: []string{post.ProfileColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(profile.FieldID, field.TypeUUID), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.profile_posts = &nodes[0] _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // PostCreateBulk is the builder for creating many Post entities in bulk. type PostCreateBulk struct { config err error builders []*PostCreate } // Save creates the Post entities in the database. func (pcb *PostCreateBulk) Save(ctx context.Context) ([]*Post, error) { if pcb.err != nil { return nil, pcb.err } specs := make([]*sqlgraph.CreateSpec, len(pcb.builders)) nodes := make([]*Post, 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.(*PostMutation) 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 *PostCreateBulk) SaveX(ctx context.Context) []*Post { v, err := pcb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (pcb *PostCreateBulk) Exec(ctx context.Context) error { _, err := pcb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (pcb *PostCreateBulk) ExecX(ctx context.Context) { if err := pcb.Exec(ctx); err != nil { panic(err) } }