mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
330 lines
8.5 KiB
Go
330 lines
8.5 KiB
Go
// Copyright 2019-present Facebook Inc. All rights reserved.
|
|
// This source code is licensed under the Apache 2.0 license found
|
|
// in the LICENSE file in the root directory of this source tree.
|
|
|
|
// Code generated by entc, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/facebook/ent/dialect/sql/sqlgraph"
|
|
"github.com/facebook/ent/entc/integration/privacy/ent/task"
|
|
"github.com/facebook/ent/entc/integration/privacy/ent/team"
|
|
"github.com/facebook/ent/entc/integration/privacy/ent/user"
|
|
"github.com/facebook/ent/schema/field"
|
|
)
|
|
|
|
// TaskCreate is the builder for creating a Task entity.
|
|
type TaskCreate struct {
|
|
config
|
|
mutation *TaskMutation
|
|
hooks []Hook
|
|
}
|
|
|
|
// SetTitle sets the title field.
|
|
func (tc *TaskCreate) SetTitle(s string) *TaskCreate {
|
|
tc.mutation.SetTitle(s)
|
|
return tc
|
|
}
|
|
|
|
// SetDescription sets the description field.
|
|
func (tc *TaskCreate) SetDescription(s string) *TaskCreate {
|
|
tc.mutation.SetDescription(s)
|
|
return tc
|
|
}
|
|
|
|
// SetNillableDescription sets the description field if the given value is not nil.
|
|
func (tc *TaskCreate) SetNillableDescription(s *string) *TaskCreate {
|
|
if s != nil {
|
|
tc.SetDescription(*s)
|
|
}
|
|
return tc
|
|
}
|
|
|
|
// SetStatus sets the status field.
|
|
func (tc *TaskCreate) SetStatus(t task.Status) *TaskCreate {
|
|
tc.mutation.SetStatus(t)
|
|
return tc
|
|
}
|
|
|
|
// SetNillableStatus sets the status field if the given value is not nil.
|
|
func (tc *TaskCreate) SetNillableStatus(t *task.Status) *TaskCreate {
|
|
if t != nil {
|
|
tc.SetStatus(*t)
|
|
}
|
|
return tc
|
|
}
|
|
|
|
// AddTeamIDs adds the teams edge to Team by ids.
|
|
func (tc *TaskCreate) AddTeamIDs(ids ...int) *TaskCreate {
|
|
tc.mutation.AddTeamIDs(ids...)
|
|
return tc
|
|
}
|
|
|
|
// AddTeams adds the teams edges to Team.
|
|
func (tc *TaskCreate) AddTeams(t ...*Team) *TaskCreate {
|
|
ids := make([]int, len(t))
|
|
for i := range t {
|
|
ids[i] = t[i].ID
|
|
}
|
|
return tc.AddTeamIDs(ids...)
|
|
}
|
|
|
|
// SetOwnerID sets the owner edge to User by id.
|
|
func (tc *TaskCreate) SetOwnerID(id int) *TaskCreate {
|
|
tc.mutation.SetOwnerID(id)
|
|
return tc
|
|
}
|
|
|
|
// SetNillableOwnerID sets the owner edge to User by id if the given value is not nil.
|
|
func (tc *TaskCreate) SetNillableOwnerID(id *int) *TaskCreate {
|
|
if id != nil {
|
|
tc = tc.SetOwnerID(*id)
|
|
}
|
|
return tc
|
|
}
|
|
|
|
// SetOwner sets the owner edge to User.
|
|
func (tc *TaskCreate) SetOwner(u *User) *TaskCreate {
|
|
return tc.SetOwnerID(u.ID)
|
|
}
|
|
|
|
// 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) {
|
|
var (
|
|
err error
|
|
node *Task
|
|
)
|
|
tc.defaults()
|
|
if len(tc.hooks) == 0 {
|
|
if err = tc.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
node, err = tc.sqlSave(ctx)
|
|
} else {
|
|
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 = tc.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
tc.mutation = mutation
|
|
node, err = tc.sqlSave(ctx)
|
|
mutation.done = true
|
|
return node, err
|
|
})
|
|
for i := len(tc.hooks) - 1; i >= 0; i-- {
|
|
mut = tc.hooks[i](mut)
|
|
}
|
|
if _, err := mut.Mutate(ctx, tc.mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return node, err
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// defaults sets the default values of the builder before save.
|
|
func (tc *TaskCreate) defaults() {
|
|
if _, ok := tc.mutation.Status(); !ok {
|
|
v := task.DefaultStatus
|
|
tc.mutation.SetStatus(v)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (tc *TaskCreate) check() error {
|
|
if _, ok := tc.mutation.Title(); !ok {
|
|
return &ValidationError{Name: "title", err: errors.New("ent: missing required field \"title\"")}
|
|
}
|
|
if v, ok := tc.mutation.Title(); ok {
|
|
if err := task.TitleValidator(v); err != nil {
|
|
return &ValidationError{Name: "title", err: fmt.Errorf("ent: validator failed for field \"title\": %w", err)}
|
|
}
|
|
}
|
|
if _, ok := tc.mutation.Status(); !ok {
|
|
return &ValidationError{Name: "status", err: errors.New("ent: missing required field \"status\"")}
|
|
}
|
|
if v, ok := tc.mutation.Status(); ok {
|
|
if err := task.StatusValidator(v); err != nil {
|
|
return &ValidationError{Name: "status", err: fmt.Errorf("ent: validator failed for field \"status\": %w", err)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (tc *TaskCreate) sqlSave(ctx context.Context) (*Task, error) {
|
|
_node, _spec := tc.createSpec()
|
|
if err := sqlgraph.CreateNode(ctx, tc.driver, _spec); err != nil {
|
|
if cerr, ok := isSQLConstraintError(err); ok {
|
|
err = cerr
|
|
}
|
|
return nil, err
|
|
}
|
|
id := _spec.ID.Value.(int64)
|
|
_node.ID = int(id)
|
|
return _node, nil
|
|
}
|
|
|
|
func (tc *TaskCreate) createSpec() (*Task, *sqlgraph.CreateSpec) {
|
|
var (
|
|
_node = &Task{config: tc.config}
|
|
_spec = &sqlgraph.CreateSpec{
|
|
Table: task.Table,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: task.FieldID,
|
|
},
|
|
}
|
|
)
|
|
if value, ok := tc.mutation.Title(); ok {
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
Type: field.TypeString,
|
|
Value: value,
|
|
Column: task.FieldTitle,
|
|
})
|
|
_node.Title = value
|
|
}
|
|
if value, ok := tc.mutation.Description(); ok {
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
Type: field.TypeString,
|
|
Value: value,
|
|
Column: task.FieldDescription,
|
|
})
|
|
_node.Description = value
|
|
}
|
|
if value, ok := tc.mutation.Status(); ok {
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
Type: field.TypeEnum,
|
|
Value: value,
|
|
Column: task.FieldStatus,
|
|
})
|
|
_node.Status = value
|
|
}
|
|
if nodes := tc.mutation.TeamsIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2M,
|
|
Inverse: false,
|
|
Table: task.TeamsTable,
|
|
Columns: task.TeamsPrimaryKey,
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: team.FieldID,
|
|
},
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges = append(_spec.Edges, edge)
|
|
}
|
|
if nodes := tc.mutation.OwnerIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2O,
|
|
Inverse: true,
|
|
Table: task.OwnerTable,
|
|
Columns: []string{task.OwnerColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: user.FieldID,
|
|
},
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges = append(_spec.Edges, edge)
|
|
}
|
|
return _node, _spec
|
|
}
|
|
|
|
// TaskCreateBulk is the builder for creating a bulk of Task entities.
|
|
type TaskCreateBulk struct {
|
|
config
|
|
builders []*TaskCreate
|
|
}
|
|
|
|
// Save creates the Task entities in the database.
|
|
func (tcb *TaskCreateBulk) Save(ctx context.Context) ([]*Task, error) {
|
|
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
|
|
nodes[i], specs[i] = builder.createSpec()
|
|
var err error
|
|
if i < len(mutators)-1 {
|
|
_, err = mutators[i+1].Mutate(root, tcb.builders[i+1].mutation)
|
|
} else {
|
|
// Invoke the actual operation on the latest mutation in the chain.
|
|
if err = sqlgraph.BatchCreate(ctx, tcb.driver, &sqlgraph.BatchCreateSpec{Nodes: specs}); err != nil {
|
|
if cerr, ok := isSQLConstraintError(err); ok {
|
|
err = cerr
|
|
}
|
|
}
|
|
}
|
|
mutation.done = true
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
id := specs[i].ID.Value.(int64)
|
|
nodes[i].ID = int(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, tcb.builders[0].mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
func (tcb *TaskCreateBulk) SaveX(ctx context.Context) []*Task {
|
|
v, err := tcb.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|