|
|
|
// 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"
|
|
|
|
"github.com/go-kratos/kratos/examples/blog/internal/data/ent/tag"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ArticleCreate is the builder for creating a Article entity.
|
|
|
|
type ArticleCreate struct {
|
|
|
|
config
|
|
|
|
mutation *ArticleMutation
|
|
|
|
hooks []Hook
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTitle sets the "title" field.
|
|
|
|
func (ac *ArticleCreate) SetTitle(s string) *ArticleCreate {
|
|
|
|
ac.mutation.SetTitle(s)
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetContent sets the "content" field.
|
|
|
|
func (ac *ArticleCreate) SetContent(s string) *ArticleCreate {
|
|
|
|
ac.mutation.SetContent(s)
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCreatedAt sets the "created_at" field.
|
|
|
|
func (ac *ArticleCreate) SetCreatedAt(t time.Time) *ArticleCreate {
|
|
|
|
ac.mutation.SetCreatedAt(t)
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
|
|
|
|
func (ac *ArticleCreate) SetNillableCreatedAt(t *time.Time) *ArticleCreate {
|
|
|
|
if t != nil {
|
|
|
|
ac.SetCreatedAt(*t)
|
|
|
|
}
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetUpdatedAt sets the "updated_at" field.
|
|
|
|
func (ac *ArticleCreate) SetUpdatedAt(t time.Time) *ArticleCreate {
|
|
|
|
ac.mutation.SetUpdatedAt(t)
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
|
|
|
|
func (ac *ArticleCreate) SetNillableUpdatedAt(t *time.Time) *ArticleCreate {
|
|
|
|
if t != nil {
|
|
|
|
ac.SetUpdatedAt(*t)
|
|
|
|
}
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetID sets the "id" field.
|
|
|
|
func (ac *ArticleCreate) SetID(i int64) *ArticleCreate {
|
|
|
|
ac.mutation.SetID(i)
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddCommentIDs adds the "comments" edge to the Comment entity by IDs.
|
|
|
|
func (ac *ArticleCreate) AddCommentIDs(ids ...int64) *ArticleCreate {
|
|
|
|
ac.mutation.AddCommentIDs(ids...)
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddComments adds the "comments" edges to the Comment entity.
|
|
|
|
func (ac *ArticleCreate) AddComments(c ...*Comment) *ArticleCreate {
|
|
|
|
ids := make([]int64, len(c))
|
|
|
|
for i := range c {
|
|
|
|
ids[i] = c[i].ID
|
|
|
|
}
|
|
|
|
return ac.AddCommentIDs(ids...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTagIDs adds the "tags" edge to the Tag entity by IDs.
|
|
|
|
func (ac *ArticleCreate) AddTagIDs(ids ...int64) *ArticleCreate {
|
|
|
|
ac.mutation.AddTagIDs(ids...)
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTags adds the "tags" edges to the Tag entity.
|
|
|
|
func (ac *ArticleCreate) AddTags(t ...*Tag) *ArticleCreate {
|
|
|
|
ids := make([]int64, len(t))
|
|
|
|
for i := range t {
|
|
|
|
ids[i] = t[i].ID
|
|
|
|
}
|
|
|
|
return ac.AddTagIDs(ids...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mutation returns the ArticleMutation object of the builder.
|
|
|
|
func (ac *ArticleCreate) Mutation() *ArticleMutation {
|
|
|
|
return ac.mutation
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save creates the Article in the database.
|
|
|
|
func (ac *ArticleCreate) Save(ctx context.Context) (*Article, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
node *Article
|
|
|
|
)
|
|
|
|
ac.defaults()
|
|
|
|
if len(ac.hooks) == 0 {
|
|
|
|
if err = ac.check(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
node, err = ac.sqlSave(ctx)
|
|
|
|
} else {
|
|
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
|
|
mutation, ok := m.(*ArticleMutation)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
|
|
}
|
|
|
|
if err = ac.check(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ac.mutation = mutation
|
|
|
|
node, err = ac.sqlSave(ctx)
|
|
|
|
mutation.done = true
|
|
|
|
return node, err
|
|
|
|
})
|
|
|
|
for i := len(ac.hooks) - 1; i >= 0; i-- {
|
|
|
|
mut = ac.hooks[i](mut)
|
|
|
|
}
|
|
|
|
if _, err := mut.Mutate(ctx, ac.mutation); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
|
|
func (ac *ArticleCreate) SaveX(ctx context.Context) *Article {
|
|
|
|
v, err := ac.Save(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// defaults sets the default values of the builder before save.
|
|
|
|
func (ac *ArticleCreate) defaults() {
|
|
|
|
if _, ok := ac.mutation.CreatedAt(); !ok {
|
|
|
|
v := article.DefaultCreatedAt()
|
|
|
|
ac.mutation.SetCreatedAt(v)
|
|
|
|
}
|
|
|
|
if _, ok := ac.mutation.UpdatedAt(); !ok {
|
|
|
|
v := article.DefaultUpdatedAt()
|
|
|
|
ac.mutation.SetUpdatedAt(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
|
|
func (ac *ArticleCreate) check() error {
|
|
|
|
if _, ok := ac.mutation.Title(); !ok {
|
|
|
|
return &ValidationError{Name: "title", err: errors.New("ent: missing required field \"title\"")}
|
|
|
|
}
|
|
|
|
if _, ok := ac.mutation.Content(); !ok {
|
|
|
|
return &ValidationError{Name: "content", err: errors.New("ent: missing required field \"content\"")}
|
|
|
|
}
|
|
|
|
if _, ok := ac.mutation.CreatedAt(); !ok {
|
|
|
|
return &ValidationError{Name: "created_at", err: errors.New("ent: missing required field \"created_at\"")}
|
|
|
|
}
|
|
|
|
if _, ok := ac.mutation.UpdatedAt(); !ok {
|
|
|
|
return &ValidationError{Name: "updated_at", err: errors.New("ent: missing required field \"updated_at\"")}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *ArticleCreate) sqlSave(ctx context.Context) (*Article, error) {
|
|
|
|
_node, _spec := ac.createSpec()
|
|
|
|
if err := sqlgraph.CreateNode(ctx, ac.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 (ac *ArticleCreate) createSpec() (*Article, *sqlgraph.CreateSpec) {
|
|
|
|
var (
|
|
|
|
_node = &Article{config: ac.config}
|
|
|
|
_spec = &sqlgraph.CreateSpec{
|
|
|
|
Table: article.Table,
|
|
|
|
ID: &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeInt64,
|
|
|
|
Column: article.FieldID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if id, ok := ac.mutation.ID(); ok {
|
|
|
|
_node.ID = id
|
|
|
|
_spec.ID.Value = id
|
|
|
|
}
|
|
|
|
if value, ok := ac.mutation.Title(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeString,
|
|
|
|
Value: value,
|
|
|
|
Column: article.FieldTitle,
|
|
|
|
})
|
|
|
|
_node.Title = value
|
|
|
|
}
|
|
|
|
if value, ok := ac.mutation.Content(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeString,
|
|
|
|
Value: value,
|
|
|
|
Column: article.FieldContent,
|
|
|
|
})
|
|
|
|
_node.Content = value
|
|
|
|
}
|
|
|
|
if value, ok := ac.mutation.CreatedAt(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeTime,
|
|
|
|
Value: value,
|
|
|
|
Column: article.FieldCreatedAt,
|
|
|
|
})
|
|
|
|
_node.CreatedAt = value
|
|
|
|
}
|
|
|
|
if value, ok := ac.mutation.UpdatedAt(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeTime,
|
|
|
|
Value: value,
|
|
|
|
Column: article.FieldUpdatedAt,
|
|
|
|
})
|
|
|
|
_node.UpdatedAt = value
|
|
|
|
}
|
|
|
|
if nodes := ac.mutation.CommentsIDs(); len(nodes) > 0 {
|
|
|
|
edge := &sqlgraph.EdgeSpec{
|
|
|
|
Rel: sqlgraph.O2M,
|
|
|
|
Inverse: false,
|
|
|
|
Table: article.CommentsTable,
|
|
|
|
Columns: []string{article.CommentsColumn},
|
|
|
|
Bidi: false,
|
|
|
|
Target: &sqlgraph.EdgeTarget{
|
|
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeInt64,
|
|
|
|
Column: comment.FieldID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, k := range nodes {
|
|
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
|
|
}
|
|
|
|
_spec.Edges = append(_spec.Edges, edge)
|
|
|
|
}
|
|
|
|
if nodes := ac.mutation.TagsIDs(); len(nodes) > 0 {
|
|
|
|
edge := &sqlgraph.EdgeSpec{
|
|
|
|
Rel: sqlgraph.M2M,
|
|
|
|
Inverse: true,
|
|
|
|
Table: article.TagsTable,
|
|
|
|
Columns: article.TagsPrimaryKey,
|
|
|
|
Bidi: false,
|
|
|
|
Target: &sqlgraph.EdgeTarget{
|
|
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeInt64,
|
|
|
|
Column: tag.FieldID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, k := range nodes {
|
|
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
|
|
}
|
|
|
|
_spec.Edges = append(_spec.Edges, edge)
|
|
|
|
}
|
|
|
|
return _node, _spec
|
|
|
|
}
|
|
|
|
|
|
|
|
// ArticleCreateBulk is the builder for creating many Article entities in bulk.
|
|
|
|
type ArticleCreateBulk struct {
|
|
|
|
config
|
|
|
|
builders []*ArticleCreate
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save creates the Article entities in the database.
|
|
|
|
func (acb *ArticleCreateBulk) Save(ctx context.Context) ([]*Article, error) {
|
|
|
|
specs := make([]*sqlgraph.CreateSpec, len(acb.builders))
|
|
|
|
nodes := make([]*Article, len(acb.builders))
|
|
|
|
mutators := make([]Mutator, len(acb.builders))
|
|
|
|
for i := range acb.builders {
|
|
|
|
func(i int, root context.Context) {
|
|
|
|
builder := acb.builders[i]
|
|
|
|
builder.defaults()
|
|
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
|
|
mutation, ok := m.(*ArticleMutation)
|
|
|
|
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, acb.builders[i+1].mutation)
|
|
|
|
} else {
|
|
|
|
// Invoke the actual operation on the latest mutation in the chain.
|
|
|
|
if err = sqlgraph.BatchCreate(ctx, acb.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, acb.builders[0].mutation); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
|
|
func (acb *ArticleCreateBulk) SaveX(ctx context.Context) []*Article {
|
|
|
|
v, err := acb.Save(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|