326 lines
8.3 KiB
Go
326 lines
8.3 KiB
Go
// Code generated by ent, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
"todolist/ent/task"
|
|
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/schema/field"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// TaskCreate is the builder for creating a Task entity.
|
|
type TaskCreate struct {
|
|
config
|
|
mutation *TaskMutation
|
|
hooks []Hook
|
|
}
|
|
|
|
// SetName sets the "name" field.
|
|
func (tc *TaskCreate) SetName(s string) *TaskCreate {
|
|
tc.mutation.SetName(s)
|
|
return tc
|
|
}
|
|
|
|
// SetNillableName sets the "name" field if the given value is not nil.
|
|
func (tc *TaskCreate) SetNillableName(s *string) *TaskCreate {
|
|
if s != nil {
|
|
tc.SetName(*s)
|
|
}
|
|
return tc
|
|
}
|
|
|
|
// SetCreatedAt sets the "created_at" field.
|
|
func (tc *TaskCreate) SetCreatedAt(t time.Time) *TaskCreate {
|
|
tc.mutation.SetCreatedAt(t)
|
|
return tc
|
|
}
|
|
|
|
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
|
|
func (tc *TaskCreate) SetNillableCreatedAt(t *time.Time) *TaskCreate {
|
|
if t != nil {
|
|
tc.SetCreatedAt(*t)
|
|
}
|
|
return tc
|
|
}
|
|
|
|
// SetUpdatedAt sets the "updated_at" field.
|
|
func (tc *TaskCreate) SetUpdatedAt(t time.Time) *TaskCreate {
|
|
tc.mutation.SetUpdatedAt(t)
|
|
return tc
|
|
}
|
|
|
|
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
|
|
func (tc *TaskCreate) SetNillableUpdatedAt(t *time.Time) *TaskCreate {
|
|
if t != nil {
|
|
tc.SetUpdatedAt(*t)
|
|
}
|
|
return tc
|
|
}
|
|
|
|
// SetCompletedAt sets the "completed_at" field.
|
|
func (tc *TaskCreate) SetCompletedAt(t time.Time) *TaskCreate {
|
|
tc.mutation.SetCompletedAt(t)
|
|
return tc
|
|
}
|
|
|
|
// SetNillableCompletedAt sets the "completed_at" field if the given value is not nil.
|
|
func (tc *TaskCreate) SetNillableCompletedAt(t *time.Time) *TaskCreate {
|
|
if t != nil {
|
|
tc.SetCompletedAt(*t)
|
|
}
|
|
return tc
|
|
}
|
|
|
|
// SetTags sets the "tags" field.
|
|
func (tc *TaskCreate) SetTags(s []string) *TaskCreate {
|
|
tc.mutation.SetTags(s)
|
|
return tc
|
|
}
|
|
|
|
// SetIPAddress sets the "ip_address" field.
|
|
func (tc *TaskCreate) SetIPAddress(s string) *TaskCreate {
|
|
tc.mutation.SetIPAddress(s)
|
|
return tc
|
|
}
|
|
|
|
// SetNillableIPAddress sets the "ip_address" field if the given value is not nil.
|
|
func (tc *TaskCreate) SetNillableIPAddress(s *string) *TaskCreate {
|
|
if s != nil {
|
|
tc.SetIPAddress(*s)
|
|
}
|
|
return tc
|
|
}
|
|
|
|
// SetID sets the "id" field.
|
|
func (tc *TaskCreate) SetID(u uuid.UUID) *TaskCreate {
|
|
tc.mutation.SetID(u)
|
|
return tc
|
|
}
|
|
|
|
// SetNillableID sets the "id" field if the given value is not nil.
|
|
func (tc *TaskCreate) SetNillableID(u *uuid.UUID) *TaskCreate {
|
|
if u != nil {
|
|
tc.SetID(*u)
|
|
}
|
|
return tc
|
|
}
|
|
|
|
// Mutation returns the TaskMutation object of the builder.
|
|
func (tc *TaskCreate) Mutation() *TaskMutation {
|
|
return tc.mutation
|
|
}
|
|
|
|
// Save creates the Task in the database.
|
|
func (tc *TaskCreate) Save(ctx context.Context) (*Task, error) {
|
|
tc.defaults()
|
|
return withHooks(ctx, tc.sqlSave, tc.mutation, tc.hooks)
|
|
}
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
func (tc *TaskCreate) SaveX(ctx context.Context) *Task {
|
|
v, err := tc.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (tc *TaskCreate) Exec(ctx context.Context) error {
|
|
_, err := tc.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (tc *TaskCreate) ExecX(ctx context.Context) {
|
|
if err := tc.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// defaults sets the default values of the builder before save.
|
|
func (tc *TaskCreate) defaults() {
|
|
if _, ok := tc.mutation.Name(); !ok {
|
|
v := task.DefaultName
|
|
tc.mutation.SetName(v)
|
|
}
|
|
if _, ok := tc.mutation.CreatedAt(); !ok {
|
|
v := task.DefaultCreatedAt()
|
|
tc.mutation.SetCreatedAt(v)
|
|
}
|
|
if _, ok := tc.mutation.UpdatedAt(); !ok {
|
|
v := task.DefaultUpdatedAt()
|
|
tc.mutation.SetUpdatedAt(v)
|
|
}
|
|
if _, ok := tc.mutation.IPAddress(); !ok {
|
|
v := task.DefaultIPAddress
|
|
tc.mutation.SetIPAddress(v)
|
|
}
|
|
if _, ok := tc.mutation.ID(); !ok {
|
|
v := task.DefaultID()
|
|
tc.mutation.SetID(v)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (tc *TaskCreate) check() error {
|
|
if _, ok := tc.mutation.Name(); !ok {
|
|
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Task.name"`)}
|
|
}
|
|
if _, ok := tc.mutation.CreatedAt(); !ok {
|
|
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Task.created_at"`)}
|
|
}
|
|
if _, ok := tc.mutation.UpdatedAt(); !ok {
|
|
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Task.updated_at"`)}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (tc *TaskCreate) sqlSave(ctx context.Context) (*Task, error) {
|
|
if err := tc.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
_node, _spec := tc.createSpec()
|
|
if err := sqlgraph.CreateNode(ctx, tc.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
|
|
}
|
|
}
|
|
tc.mutation.id = &_node.ID
|
|
tc.mutation.done = true
|
|
return _node, nil
|
|
}
|
|
|
|
func (tc *TaskCreate) createSpec() (*Task, *sqlgraph.CreateSpec) {
|
|
var (
|
|
_node = &Task{config: tc.config}
|
|
_spec = sqlgraph.NewCreateSpec(task.Table, sqlgraph.NewFieldSpec(task.FieldID, field.TypeUUID))
|
|
)
|
|
if id, ok := tc.mutation.ID(); ok {
|
|
_node.ID = id
|
|
_spec.ID.Value = &id
|
|
}
|
|
if value, ok := tc.mutation.Name(); ok {
|
|
_spec.SetField(task.FieldName, field.TypeString, value)
|
|
_node.Name = value
|
|
}
|
|
if value, ok := tc.mutation.CreatedAt(); ok {
|
|
_spec.SetField(task.FieldCreatedAt, field.TypeTime, value)
|
|
_node.CreatedAt = value
|
|
}
|
|
if value, ok := tc.mutation.UpdatedAt(); ok {
|
|
_spec.SetField(task.FieldUpdatedAt, field.TypeTime, value)
|
|
_node.UpdatedAt = value
|
|
}
|
|
if value, ok := tc.mutation.CompletedAt(); ok {
|
|
_spec.SetField(task.FieldCompletedAt, field.TypeTime, value)
|
|
_node.CompletedAt = &value
|
|
}
|
|
if value, ok := tc.mutation.Tags(); ok {
|
|
_spec.SetField(task.FieldTags, field.TypeJSON, value)
|
|
_node.Tags = value
|
|
}
|
|
if value, ok := tc.mutation.IPAddress(); ok {
|
|
_spec.SetField(task.FieldIPAddress, field.TypeString, value)
|
|
_node.IPAddress = value
|
|
}
|
|
return _node, _spec
|
|
}
|
|
|
|
// TaskCreateBulk is the builder for creating many Task entities in bulk.
|
|
type TaskCreateBulk struct {
|
|
config
|
|
err error
|
|
builders []*TaskCreate
|
|
}
|
|
|
|
// Save creates the Task entities in the database.
|
|
func (tcb *TaskCreateBulk) Save(ctx context.Context) ([]*Task, error) {
|
|
if tcb.err != nil {
|
|
return nil, tcb.err
|
|
}
|
|
specs := make([]*sqlgraph.CreateSpec, len(tcb.builders))
|
|
nodes := make([]*Task, len(tcb.builders))
|
|
mutators := make([]Mutator, len(tcb.builders))
|
|
for i := range tcb.builders {
|
|
func(i int, root context.Context) {
|
|
builder := tcb.builders[i]
|
|
builder.defaults()
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*TaskMutation)
|
|
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, tcb.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, tcb.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, tcb.builders[0].mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (tcb *TaskCreateBulk) SaveX(ctx context.Context) []*Task {
|
|
v, err := tcb.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (tcb *TaskCreateBulk) Exec(ctx context.Context) error {
|
|
_, err := tcb.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (tcb *TaskCreateBulk) ExecX(ctx context.Context) {
|
|
if err := tcb.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|