// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/safedep/vet/ent/reportpackage"
"github.com/safedep/vet/ent/reportproject"
"github.com/safedep/vet/ent/reportscorecard"
)
// ReportProjectCreate is the builder for creating a ReportProject entity.
type ReportProjectCreate struct {
config
mutation *ReportProjectMutation
hooks []Hook
}
// SetName sets the "name" field.
func (rpc *ReportProjectCreate) SetName(s string) *ReportProjectCreate {
rpc.mutation.SetName(s)
return rpc
}
// SetURL sets the "url" field.
func (rpc *ReportProjectCreate) SetURL(s string) *ReportProjectCreate {
rpc.mutation.SetURL(s)
return rpc
}
// SetNillableURL sets the "url" field if the given value is not nil.
func (rpc *ReportProjectCreate) SetNillableURL(s *string) *ReportProjectCreate {
if s != nil {
rpc.SetURL(*s)
}
return rpc
}
// SetDescription sets the "description" field.
func (rpc *ReportProjectCreate) SetDescription(s string) *ReportProjectCreate {
rpc.mutation.SetDescription(s)
return rpc
}
// SetNillableDescription sets the "description" field if the given value is not nil.
func (rpc *ReportProjectCreate) SetNillableDescription(s *string) *ReportProjectCreate {
if s != nil {
rpc.SetDescription(*s)
}
return rpc
}
// SetStars sets the "stars" field.
func (rpc *ReportProjectCreate) SetStars(i int32) *ReportProjectCreate {
rpc.mutation.SetStars(i)
return rpc
}
// SetNillableStars sets the "stars" field if the given value is not nil.
func (rpc *ReportProjectCreate) SetNillableStars(i *int32) *ReportProjectCreate {
if i != nil {
rpc.SetStars(*i)
}
return rpc
}
// SetForks sets the "forks" field.
func (rpc *ReportProjectCreate) SetForks(i int32) *ReportProjectCreate {
rpc.mutation.SetForks(i)
return rpc
}
// SetNillableForks sets the "forks" field if the given value is not nil.
func (rpc *ReportProjectCreate) SetNillableForks(i *int32) *ReportProjectCreate {
if i != nil {
rpc.SetForks(*i)
}
return rpc
}
// SetCreatedAt sets the "created_at" field.
func (rpc *ReportProjectCreate) SetCreatedAt(t time.Time) *ReportProjectCreate {
rpc.mutation.SetCreatedAt(t)
return rpc
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (rpc *ReportProjectCreate) SetNillableCreatedAt(t *time.Time) *ReportProjectCreate {
if t != nil {
rpc.SetCreatedAt(*t)
}
return rpc
}
// SetUpdatedAt sets the "updated_at" field.
func (rpc *ReportProjectCreate) SetUpdatedAt(t time.Time) *ReportProjectCreate {
rpc.mutation.SetUpdatedAt(t)
return rpc
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (rpc *ReportProjectCreate) SetNillableUpdatedAt(t *time.Time) *ReportProjectCreate {
if t != nil {
rpc.SetUpdatedAt(*t)
}
return rpc
}
// SetPackageID sets the "package" edge to the ReportPackage entity by ID.
func (rpc *ReportProjectCreate) SetPackageID(id int) *ReportProjectCreate {
rpc.mutation.SetPackageID(id)
return rpc
}
// SetNillablePackageID sets the "package" edge to the ReportPackage entity by ID if the given value is not nil.
func (rpc *ReportProjectCreate) SetNillablePackageID(id *int) *ReportProjectCreate {
if id != nil {
rpc = rpc.SetPackageID(*id)
}
return rpc
}
// SetPackage sets the "package" edge to the ReportPackage entity.
func (rpc *ReportProjectCreate) SetPackage(r *ReportPackage) *ReportProjectCreate {
return rpc.SetPackageID(r.ID)
}
// SetScorecardID sets the "scorecard" edge to the ReportScorecard entity by ID.
func (rpc *ReportProjectCreate) SetScorecardID(id int) *ReportProjectCreate {
rpc.mutation.SetScorecardID(id)
return rpc
}
// SetNillableScorecardID sets the "scorecard" edge to the ReportScorecard entity by ID if the given value is not nil.
func (rpc *ReportProjectCreate) SetNillableScorecardID(id *int) *ReportProjectCreate {
if id != nil {
rpc = rpc.SetScorecardID(*id)
}
return rpc
}
// SetScorecard sets the "scorecard" edge to the ReportScorecard entity.
func (rpc *ReportProjectCreate) SetScorecard(r *ReportScorecard) *ReportProjectCreate {
return rpc.SetScorecardID(r.ID)
}
// Mutation returns the ReportProjectMutation object of the builder.
func (rpc *ReportProjectCreate) Mutation() *ReportProjectMutation {
return rpc.mutation
}
// Save creates the ReportProject in the database.
func (rpc *ReportProjectCreate) Save(ctx context.Context) (*ReportProject, error) {
return withHooks(ctx, rpc.sqlSave, rpc.mutation, rpc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (rpc *ReportProjectCreate) SaveX(ctx context.Context) *ReportProject {
v, err := rpc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (rpc *ReportProjectCreate) Exec(ctx context.Context) error {
_, err := rpc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (rpc *ReportProjectCreate) ExecX(ctx context.Context) {
if err := rpc.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (rpc *ReportProjectCreate) check() error {
if _, ok := rpc.mutation.Name(); !ok {
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "ReportProject.name"`)}
}
if v, ok := rpc.mutation.Name(); ok {
if err := reportproject.NameValidator(v); err != nil {
return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "ReportProject.name": %w`, err)}
}
}
return nil
}
func (rpc *ReportProjectCreate) sqlSave(ctx context.Context) (*ReportProject, error) {
if err := rpc.check(); err != nil {
return nil, err
}
_node, _spec := rpc.createSpec()
if err := sqlgraph.CreateNode(ctx, rpc.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)
rpc.mutation.id = &_node.ID
rpc.mutation.done = true
return _node, nil
}
func (rpc *ReportProjectCreate) createSpec() (*ReportProject, *sqlgraph.CreateSpec) {
var (
_node = &ReportProject{config: rpc.config}
_spec = sqlgraph.NewCreateSpec(reportproject.Table, sqlgraph.NewFieldSpec(reportproject.FieldID, field.TypeInt))
)
if value, ok := rpc.mutation.Name(); ok {
_spec.SetField(reportproject.FieldName, field.TypeString, value)
_node.Name = value
}
if value, ok := rpc.mutation.URL(); ok {
_spec.SetField(reportproject.FieldURL, field.TypeString, value)
_node.URL = value
}
if value, ok := rpc.mutation.Description(); ok {
_spec.SetField(reportproject.FieldDescription, field.TypeString, value)
_node.Description = value
}
if value, ok := rpc.mutation.Stars(); ok {
_spec.SetField(reportproject.FieldStars, field.TypeInt32, value)
_node.Stars = value
}
if value, ok := rpc.mutation.Forks(); ok {
_spec.SetField(reportproject.FieldForks, field.TypeInt32, value)
_node.Forks = value
}
if value, ok := rpc.mutation.CreatedAt(); ok {
_spec.SetField(reportproject.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := rpc.mutation.UpdatedAt(); ok {
_spec.SetField(reportproject.FieldUpdatedAt, field.TypeTime, value)
_node.UpdatedAt = value
}
if nodes := rpc.mutation.PackageIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: reportproject.PackageTable,
Columns: []string{reportproject.PackageColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(reportpackage.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.report_package_projects = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := rpc.mutation.ScorecardIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2O,
Inverse: false,
Table: reportproject.ScorecardTable,
Columns: []string{reportproject.ScorecardColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(reportscorecard.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// ReportProjectCreateBulk is the builder for creating many ReportProject entities in bulk.
type ReportProjectCreateBulk struct {
config
err error
builders []*ReportProjectCreate
}
// Save creates the ReportProject entities in the database.
func (rpcb *ReportProjectCreateBulk) Save(ctx context.Context) ([]*ReportProject, error) {
if rpcb.err != nil {
return nil, rpcb.err
}
specs := make([]*sqlgraph.CreateSpec, len(rpcb.builders))
nodes := make([]*ReportProject, len(rpcb.builders))
mutators := make([]Mutator, len(rpcb.builders))
for i := range rpcb.builders {
func(i int, root context.Context) {
builder := rpcb.builders[i]
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*ReportProjectMutation)
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, rpcb.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, rpcb.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, rpcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (rpcb *ReportProjectCreateBulk) SaveX(ctx context.Context) []*ReportProject {
v, err := rpcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (rpcb *ReportProjectCreateBulk) Exec(ctx context.Context) error {
_, err := rpcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (rpcb *ReportProjectCreateBulk) ExecX(ctx context.Context) {
if err := rpcb.Exec(ctx); err != nil {
panic(err)
}
}