// Code generated by entc, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "time" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" "github.com/go-kratos/kratos/examples/blog/internal/data/ent/article" "github.com/go-kratos/kratos/examples/blog/internal/data/ent/comment" ) // CommentCreate is the builder for creating a Comment entity. type CommentCreate struct { config mutation *CommentMutation hooks []Hook } // SetName sets the "name" field. func (cc *CommentCreate) SetName(s string) *CommentCreate { cc.mutation.SetName(s) return cc } // SetContent sets the "content" field. func (cc *CommentCreate) SetContent(s string) *CommentCreate { cc.mutation.SetContent(s) return cc } // SetCreatedAt sets the "created_at" field. func (cc *CommentCreate) SetCreatedAt(t time.Time) *CommentCreate { cc.mutation.SetCreatedAt(t) return cc } // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. func (cc *CommentCreate) SetNillableCreatedAt(t *time.Time) *CommentCreate { if t != nil { cc.SetCreatedAt(*t) } return cc } // SetUpdatedAt sets the "updated_at" field. func (cc *CommentCreate) SetUpdatedAt(t time.Time) *CommentCreate { cc.mutation.SetUpdatedAt(t) return cc } // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. func (cc *CommentCreate) SetNillableUpdatedAt(t *time.Time) *CommentCreate { if t != nil { cc.SetUpdatedAt(*t) } return cc } // SetID sets the "id" field. func (cc *CommentCreate) SetID(i int64) *CommentCreate { cc.mutation.SetID(i) return cc } // SetPostID sets the "post" edge to the Article entity by ID. func (cc *CommentCreate) SetPostID(id int64) *CommentCreate { cc.mutation.SetPostID(id) return cc } // SetNillablePostID sets the "post" edge to the Article entity by ID if the given value is not nil. func (cc *CommentCreate) SetNillablePostID(id *int64) *CommentCreate { if id != nil { cc = cc.SetPostID(*id) } return cc } // SetPost sets the "post" edge to the Article entity. func (cc *CommentCreate) SetPost(a *Article) *CommentCreate { return cc.SetPostID(a.ID) } // Mutation returns the CommentMutation object of the builder. func (cc *CommentCreate) Mutation() *CommentMutation { return cc.mutation } // Save creates the Comment in the database. func (cc *CommentCreate) Save(ctx context.Context) (*Comment, error) { var ( err error node *Comment ) cc.defaults() if len(cc.hooks) == 0 { if err = cc.check(); err != nil { return nil, err } node, err = cc.sqlSave(ctx) } else { var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*CommentMutation) if !ok { return nil, fmt.Errorf("unexpected mutation type %T", m) } if err = cc.check(); err != nil { return nil, err } cc.mutation = mutation node, err = cc.sqlSave(ctx) mutation.done = true return node, err }) for i := len(cc.hooks) - 1; i >= 0; i-- { mut = cc.hooks[i](mut) } if _, err := mut.Mutate(ctx, cc.mutation); err != nil { return nil, err } } return node, err } // SaveX calls Save and panics if Save returns an error. func (cc *CommentCreate) SaveX(ctx context.Context) *Comment { v, err := cc.Save(ctx) if err != nil { panic(err) } return v } // defaults sets the default values of the builder before save. func (cc *CommentCreate) defaults() { if _, ok := cc.mutation.CreatedAt(); !ok { v := comment.DefaultCreatedAt() cc.mutation.SetCreatedAt(v) } if _, ok := cc.mutation.UpdatedAt(); !ok { v := comment.DefaultUpdatedAt() cc.mutation.SetUpdatedAt(v) } } // check runs all checks and user-defined validators on the builder. func (cc *CommentCreate) check() error { if _, ok := cc.mutation.Name(); !ok { return &ValidationError{Name: "name", err: errors.New("ent: missing required field \"name\"")} } if _, ok := cc.mutation.Content(); !ok { return &ValidationError{Name: "content", err: errors.New("ent: missing required field \"content\"")} } if _, ok := cc.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New("ent: missing required field \"created_at\"")} } if _, ok := cc.mutation.UpdatedAt(); !ok { return &ValidationError{Name: "updated_at", err: errors.New("ent: missing required field \"updated_at\"")} } return nil } func (cc *CommentCreate) sqlSave(ctx context.Context) (*Comment, error) { _node, _spec := cc.createSpec() if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil { if cerr, ok := isSQLConstraintError(err); ok { err = cerr } return nil, err } if _node.ID == 0 { id := _spec.ID.Value.(int64) _node.ID = int64(id) } return _node, nil } func (cc *CommentCreate) createSpec() (*Comment, *sqlgraph.CreateSpec) { var ( _node = &Comment{config: cc.config} _spec = &sqlgraph.CreateSpec{ Table: comment.Table, ID: &sqlgraph.FieldSpec{ Type: field.TypeInt64, Column: comment.FieldID, }, } ) if id, ok := cc.mutation.ID(); ok { _node.ID = id _spec.ID.Value = id } if value, ok := cc.mutation.Name(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeString, Value: value, Column: comment.FieldName, }) _node.Name = value } if value, ok := cc.mutation.Content(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeString, Value: value, Column: comment.FieldContent, }) _node.Content = value } if value, ok := cc.mutation.CreatedAt(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeTime, Value: value, Column: comment.FieldCreatedAt, }) _node.CreatedAt = value } if value, ok := cc.mutation.UpdatedAt(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeTime, Value: value, Column: comment.FieldUpdatedAt, }) _node.UpdatedAt = value } if nodes := cc.mutation.PostIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: true, Table: comment.PostTable, Columns: []string{comment.PostColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ Type: field.TypeInt64, Column: article.FieldID, }, }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // CommentCreateBulk is the builder for creating many Comment entities in bulk. type CommentCreateBulk struct { config builders []*CommentCreate } // Save creates the Comment entities in the database. func (ccb *CommentCreateBulk) Save(ctx context.Context) ([]*Comment, error) { specs := make([]*sqlgraph.CreateSpec, len(ccb.builders)) nodes := make([]*Comment, len(ccb.builders)) mutators := make([]Mutator, len(ccb.builders)) for i := range ccb.builders { func(i int, root context.Context) { builder := ccb.builders[i] builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*CommentMutation) if !ok { return nil, fmt.Errorf("unexpected mutation type %T", m) } if err := builder.check(); err != nil { return nil, err } builder.mutation = mutation nodes[i], specs[i] = builder.createSpec() var err error if i < len(mutators)-1 { _, err = mutators[i+1].Mutate(root, ccb.builders[i+1].mutation) } else { // Invoke the actual operation on the latest mutation in the chain. if err = sqlgraph.BatchCreate(ctx, ccb.driver, &sqlgraph.BatchCreateSpec{Nodes: specs}); err != nil { if cerr, ok := isSQLConstraintError(err); ok { err = cerr } } } mutation.done = true if err != nil { return nil, err } if nodes[i].ID == 0 { id := specs[i].ID.Value.(int64) nodes[i].ID = int64(id) } 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, ccb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (ccb *CommentCreateBulk) SaveX(ctx context.Context) []*Comment { v, err := ccb.Save(ctx) if err != nil { panic(err) } return v }