220 lines
6.3 KiB
Go
220 lines
6.3 KiB
Go
// Code generated by ent, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"tss-rocks-be/ent/contributorrole"
|
|
"tss-rocks-be/ent/postcontributor"
|
|
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/schema/field"
|
|
)
|
|
|
|
// ContributorRoleCreate is the builder for creating a ContributorRole entity.
|
|
type ContributorRoleCreate struct {
|
|
config
|
|
mutation *ContributorRoleMutation
|
|
hooks []Hook
|
|
}
|
|
|
|
// SetName sets the "name" field.
|
|
func (crc *ContributorRoleCreate) SetName(s string) *ContributorRoleCreate {
|
|
crc.mutation.SetName(s)
|
|
return crc
|
|
}
|
|
|
|
// AddPostContributorIDs adds the "post_contributors" edge to the PostContributor entity by IDs.
|
|
func (crc *ContributorRoleCreate) AddPostContributorIDs(ids ...int) *ContributorRoleCreate {
|
|
crc.mutation.AddPostContributorIDs(ids...)
|
|
return crc
|
|
}
|
|
|
|
// AddPostContributors adds the "post_contributors" edges to the PostContributor entity.
|
|
func (crc *ContributorRoleCreate) AddPostContributors(p ...*PostContributor) *ContributorRoleCreate {
|
|
ids := make([]int, len(p))
|
|
for i := range p {
|
|
ids[i] = p[i].ID
|
|
}
|
|
return crc.AddPostContributorIDs(ids...)
|
|
}
|
|
|
|
// Mutation returns the ContributorRoleMutation object of the builder.
|
|
func (crc *ContributorRoleCreate) Mutation() *ContributorRoleMutation {
|
|
return crc.mutation
|
|
}
|
|
|
|
// Save creates the ContributorRole in the database.
|
|
func (crc *ContributorRoleCreate) Save(ctx context.Context) (*ContributorRole, error) {
|
|
return withHooks(ctx, crc.sqlSave, crc.mutation, crc.hooks)
|
|
}
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
func (crc *ContributorRoleCreate) SaveX(ctx context.Context) *ContributorRole {
|
|
v, err := crc.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (crc *ContributorRoleCreate) Exec(ctx context.Context) error {
|
|
_, err := crc.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (crc *ContributorRoleCreate) ExecX(ctx context.Context) {
|
|
if err := crc.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (crc *ContributorRoleCreate) check() error {
|
|
if _, ok := crc.mutation.Name(); !ok {
|
|
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "ContributorRole.name"`)}
|
|
}
|
|
if v, ok := crc.mutation.Name(); ok {
|
|
if err := contributorrole.NameValidator(v); err != nil {
|
|
return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "ContributorRole.name": %w`, err)}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (crc *ContributorRoleCreate) sqlSave(ctx context.Context) (*ContributorRole, error) {
|
|
if err := crc.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
_node, _spec := crc.createSpec()
|
|
if err := sqlgraph.CreateNode(ctx, crc.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)
|
|
crc.mutation.id = &_node.ID
|
|
crc.mutation.done = true
|
|
return _node, nil
|
|
}
|
|
|
|
func (crc *ContributorRoleCreate) createSpec() (*ContributorRole, *sqlgraph.CreateSpec) {
|
|
var (
|
|
_node = &ContributorRole{config: crc.config}
|
|
_spec = sqlgraph.NewCreateSpec(contributorrole.Table, sqlgraph.NewFieldSpec(contributorrole.FieldID, field.TypeInt))
|
|
)
|
|
if value, ok := crc.mutation.Name(); ok {
|
|
_spec.SetField(contributorrole.FieldName, field.TypeString, value)
|
|
_node.Name = value
|
|
}
|
|
if nodes := crc.mutation.PostContributorsIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.O2M,
|
|
Inverse: false,
|
|
Table: contributorrole.PostContributorsTable,
|
|
Columns: []string{contributorrole.PostContributorsColumn},
|
|
Bidi: false,
|
|
Target: &sqlgraph.EdgeTarget{
|
|
IDSpec: sqlgraph.NewFieldSpec(postcontributor.FieldID, field.TypeInt),
|
|
},
|
|
}
|
|
for _, k := range nodes {
|
|
edge.Target.Nodes = append(edge.Target.Nodes, k)
|
|
}
|
|
_spec.Edges = append(_spec.Edges, edge)
|
|
}
|
|
return _node, _spec
|
|
}
|
|
|
|
// ContributorRoleCreateBulk is the builder for creating many ContributorRole entities in bulk.
|
|
type ContributorRoleCreateBulk struct {
|
|
config
|
|
err error
|
|
builders []*ContributorRoleCreate
|
|
}
|
|
|
|
// Save creates the ContributorRole entities in the database.
|
|
func (crcb *ContributorRoleCreateBulk) Save(ctx context.Context) ([]*ContributorRole, error) {
|
|
if crcb.err != nil {
|
|
return nil, crcb.err
|
|
}
|
|
specs := make([]*sqlgraph.CreateSpec, len(crcb.builders))
|
|
nodes := make([]*ContributorRole, len(crcb.builders))
|
|
mutators := make([]Mutator, len(crcb.builders))
|
|
for i := range crcb.builders {
|
|
func(i int, root context.Context) {
|
|
builder := crcb.builders[i]
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*ContributorRoleMutation)
|
|
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, crcb.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, crcb.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, crcb.builders[0].mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (crcb *ContributorRoleCreateBulk) SaveX(ctx context.Context) []*ContributorRole {
|
|
v, err := crcb.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (crcb *ContributorRoleCreateBulk) Exec(ctx context.Context) error {
|
|
_, err := crcb.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (crcb *ContributorRoleCreateBulk) ExecX(ctx context.Context) {
|
|
if err := crcb.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|