Files
voidraft/internal/models/ent/document_create.go
landaiqing 8fce8bdca4
Some checks failed
CodeQL Advanced / Analyze (go) (push) Has been cancelled
CodeQL Advanced / Analyze (actions) (push) Has been cancelled
CodeQL Advanced / Analyze (c-cpp) (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
CodeQL Advanced / Analyze (rust) (push) Has been cancelled
♻️ Refactor backup service complete.
2025-12-16 23:20:40 +08:00

344 lines
9.4 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"voidraft/internal/models/ent/document"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
)
// DocumentCreate is the builder for creating a Document entity.
type DocumentCreate struct {
config
mutation *DocumentMutation
hooks []Hook
}
// SetUUID sets the "uuid" field.
func (_c *DocumentCreate) SetUUID(v string) *DocumentCreate {
_c.mutation.SetUUID(v)
return _c
}
// SetNillableUUID sets the "uuid" field if the given value is not nil.
func (_c *DocumentCreate) SetNillableUUID(v *string) *DocumentCreate {
if v != nil {
_c.SetUUID(*v)
}
return _c
}
// SetCreatedAt sets the "created_at" field.
func (_c *DocumentCreate) SetCreatedAt(v string) *DocumentCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *DocumentCreate) SetNillableCreatedAt(v *string) *DocumentCreate {
if v != nil {
_c.SetCreatedAt(*v)
}
return _c
}
// SetUpdatedAt sets the "updated_at" field.
func (_c *DocumentCreate) SetUpdatedAt(v string) *DocumentCreate {
_c.mutation.SetUpdatedAt(v)
return _c
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (_c *DocumentCreate) SetNillableUpdatedAt(v *string) *DocumentCreate {
if v != nil {
_c.SetUpdatedAt(*v)
}
return _c
}
// SetDeletedAt sets the "deleted_at" field.
func (_c *DocumentCreate) SetDeletedAt(v string) *DocumentCreate {
_c.mutation.SetDeletedAt(v)
return _c
}
// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_c *DocumentCreate) SetNillableDeletedAt(v *string) *DocumentCreate {
if v != nil {
_c.SetDeletedAt(*v)
}
return _c
}
// SetTitle sets the "title" field.
func (_c *DocumentCreate) SetTitle(v string) *DocumentCreate {
_c.mutation.SetTitle(v)
return _c
}
// SetContent sets the "content" field.
func (_c *DocumentCreate) SetContent(v string) *DocumentCreate {
_c.mutation.SetContent(v)
return _c
}
// SetNillableContent sets the "content" field if the given value is not nil.
func (_c *DocumentCreate) SetNillableContent(v *string) *DocumentCreate {
if v != nil {
_c.SetContent(*v)
}
return _c
}
// SetLocked sets the "locked" field.
func (_c *DocumentCreate) SetLocked(v bool) *DocumentCreate {
_c.mutation.SetLocked(v)
return _c
}
// SetNillableLocked sets the "locked" field if the given value is not nil.
func (_c *DocumentCreate) SetNillableLocked(v *bool) *DocumentCreate {
if v != nil {
_c.SetLocked(*v)
}
return _c
}
// Mutation returns the DocumentMutation object of the builder.
func (_c *DocumentCreate) Mutation() *DocumentMutation {
return _c.mutation
}
// Save creates the Document in the database.
func (_c *DocumentCreate) Save(ctx context.Context) (*Document, error) {
if err := _c.defaults(); err != nil {
return nil, err
}
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *DocumentCreate) SaveX(ctx context.Context) *Document {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *DocumentCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *DocumentCreate) ExecX(ctx context.Context) {
if err := _c.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (_c *DocumentCreate) defaults() error {
if _, ok := _c.mutation.UUID(); !ok {
if document.DefaultUUID == nil {
return fmt.Errorf("ent: uninitialized document.DefaultUUID (forgotten import ent/runtime?)")
}
v := document.DefaultUUID()
_c.mutation.SetUUID(v)
}
if _, ok := _c.mutation.CreatedAt(); !ok {
if document.DefaultCreatedAt == nil {
return fmt.Errorf("ent: uninitialized document.DefaultCreatedAt (forgotten import ent/runtime?)")
}
v := document.DefaultCreatedAt()
_c.mutation.SetCreatedAt(v)
}
if _, ok := _c.mutation.UpdatedAt(); !ok {
if document.DefaultUpdatedAt == nil {
return fmt.Errorf("ent: uninitialized document.DefaultUpdatedAt (forgotten import ent/runtime?)")
}
v := document.DefaultUpdatedAt()
_c.mutation.SetUpdatedAt(v)
}
if _, ok := _c.mutation.Content(); !ok {
v := document.DefaultContent
_c.mutation.SetContent(v)
}
if _, ok := _c.mutation.Locked(); !ok {
v := document.DefaultLocked
_c.mutation.SetLocked(v)
}
return nil
}
// check runs all checks and user-defined validators on the builder.
func (_c *DocumentCreate) check() error {
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Document.created_at"`)}
}
if _, ok := _c.mutation.UpdatedAt(); !ok {
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Document.updated_at"`)}
}
if _, ok := _c.mutation.Title(); !ok {
return &ValidationError{Name: "title", err: errors.New(`ent: missing required field "Document.title"`)}
}
if v, ok := _c.mutation.Title(); ok {
if err := document.TitleValidator(v); err != nil {
return &ValidationError{Name: "title", err: fmt.Errorf(`ent: validator failed for field "Document.title": %w`, err)}
}
}
if _, ok := _c.mutation.Locked(); !ok {
return &ValidationError{Name: "locked", err: errors.New(`ent: missing required field "Document.locked"`)}
}
return nil
}
func (_c *DocumentCreate) sqlSave(ctx context.Context) (*Document, error) {
if err := _c.check(); err != nil {
return nil, err
}
_node, _spec := _c.createSpec()
if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
id := _spec.ID.Value.(int64)
_node.ID = int(id)
_c.mutation.id = &_node.ID
_c.mutation.done = true
return _node, nil
}
func (_c *DocumentCreate) createSpec() (*Document, *sqlgraph.CreateSpec) {
var (
_node = &Document{config: _c.config}
_spec = sqlgraph.NewCreateSpec(document.Table, sqlgraph.NewFieldSpec(document.FieldID, field.TypeInt))
)
if value, ok := _c.mutation.UUID(); ok {
_spec.SetField(document.FieldUUID, field.TypeString, value)
_node.UUID = &value
}
if value, ok := _c.mutation.CreatedAt(); ok {
_spec.SetField(document.FieldCreatedAt, field.TypeString, value)
_node.CreatedAt = value
}
if value, ok := _c.mutation.UpdatedAt(); ok {
_spec.SetField(document.FieldUpdatedAt, field.TypeString, value)
_node.UpdatedAt = value
}
if value, ok := _c.mutation.DeletedAt(); ok {
_spec.SetField(document.FieldDeletedAt, field.TypeString, value)
_node.DeletedAt = &value
}
if value, ok := _c.mutation.Title(); ok {
_spec.SetField(document.FieldTitle, field.TypeString, value)
_node.Title = value
}
if value, ok := _c.mutation.Content(); ok {
_spec.SetField(document.FieldContent, field.TypeString, value)
_node.Content = value
}
if value, ok := _c.mutation.Locked(); ok {
_spec.SetField(document.FieldLocked, field.TypeBool, value)
_node.Locked = value
}
return _node, _spec
}
// DocumentCreateBulk is the builder for creating many Document entities in bulk.
type DocumentCreateBulk struct {
config
err error
builders []*DocumentCreate
}
// Save creates the Document entities in the database.
func (_c *DocumentCreateBulk) Save(ctx context.Context) ([]*Document, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*Document, len(_c.builders))
mutators := make([]Mutator, len(_c.builders))
for i := range _c.builders {
func(i int, root context.Context) {
builder := _c.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*DocumentMutation)
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, _c.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, _c.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
if specs[i].ID.Value != nil {
id := specs[i].ID.Value.(int64)
nodes[i].ID = int(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, _c.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (_c *DocumentCreateBulk) SaveX(ctx context.Context) []*Document {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *DocumentCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *DocumentCreateBulk) ExecX(ctx context.Context) {
if err := _c.Exec(ctx); err != nil {
panic(err)
}
}