tss-rocks/backend/ent/contributor_create.go
CDN 05ddc1f783
Some checks failed
Build Backend / Build Docker Image (push) Successful in 3m33s
Test Backend / test (push) Failing after 31s
[feature] migrate to monorepo
2025-02-21 00:49:20 +08:00

382 lines
11 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"tss-rocks-be/ent/contributor"
"tss-rocks-be/ent/contributorsociallink"
"tss-rocks-be/ent/postcontributor"
"tss-rocks-be/ent/user"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
)
// ContributorCreate is the builder for creating a Contributor entity.
type ContributorCreate struct {
config
mutation *ContributorMutation
hooks []Hook
}
// SetName sets the "name" field.
func (cc *ContributorCreate) SetName(s string) *ContributorCreate {
cc.mutation.SetName(s)
return cc
}
// SetAvatarURL sets the "avatar_url" field.
func (cc *ContributorCreate) SetAvatarURL(s string) *ContributorCreate {
cc.mutation.SetAvatarURL(s)
return cc
}
// SetNillableAvatarURL sets the "avatar_url" field if the given value is not nil.
func (cc *ContributorCreate) SetNillableAvatarURL(s *string) *ContributorCreate {
if s != nil {
cc.SetAvatarURL(*s)
}
return cc
}
// SetBio sets the "bio" field.
func (cc *ContributorCreate) SetBio(s string) *ContributorCreate {
cc.mutation.SetBio(s)
return cc
}
// SetNillableBio sets the "bio" field if the given value is not nil.
func (cc *ContributorCreate) SetNillableBio(s *string) *ContributorCreate {
if s != nil {
cc.SetBio(*s)
}
return cc
}
// SetCreatedAt sets the "created_at" field.
func (cc *ContributorCreate) SetCreatedAt(t time.Time) *ContributorCreate {
cc.mutation.SetCreatedAt(t)
return cc
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (cc *ContributorCreate) SetNillableCreatedAt(t *time.Time) *ContributorCreate {
if t != nil {
cc.SetCreatedAt(*t)
}
return cc
}
// SetUpdatedAt sets the "updated_at" field.
func (cc *ContributorCreate) SetUpdatedAt(t time.Time) *ContributorCreate {
cc.mutation.SetUpdatedAt(t)
return cc
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (cc *ContributorCreate) SetNillableUpdatedAt(t *time.Time) *ContributorCreate {
if t != nil {
cc.SetUpdatedAt(*t)
}
return cc
}
// SetUserID sets the "user" edge to the User entity by ID.
func (cc *ContributorCreate) SetUserID(id int) *ContributorCreate {
cc.mutation.SetUserID(id)
return cc
}
// SetNillableUserID sets the "user" edge to the User entity by ID if the given value is not nil.
func (cc *ContributorCreate) SetNillableUserID(id *int) *ContributorCreate {
if id != nil {
cc = cc.SetUserID(*id)
}
return cc
}
// SetUser sets the "user" edge to the User entity.
func (cc *ContributorCreate) SetUser(u *User) *ContributorCreate {
return cc.SetUserID(u.ID)
}
// AddSocialLinkIDs adds the "social_links" edge to the ContributorSocialLink entity by IDs.
func (cc *ContributorCreate) AddSocialLinkIDs(ids ...int) *ContributorCreate {
cc.mutation.AddSocialLinkIDs(ids...)
return cc
}
// AddSocialLinks adds the "social_links" edges to the ContributorSocialLink entity.
func (cc *ContributorCreate) AddSocialLinks(c ...*ContributorSocialLink) *ContributorCreate {
ids := make([]int, len(c))
for i := range c {
ids[i] = c[i].ID
}
return cc.AddSocialLinkIDs(ids...)
}
// AddPostIDs adds the "posts" edge to the PostContributor entity by IDs.
func (cc *ContributorCreate) AddPostIDs(ids ...int) *ContributorCreate {
cc.mutation.AddPostIDs(ids...)
return cc
}
// AddPosts adds the "posts" edges to the PostContributor entity.
func (cc *ContributorCreate) AddPosts(p ...*PostContributor) *ContributorCreate {
ids := make([]int, len(p))
for i := range p {
ids[i] = p[i].ID
}
return cc.AddPostIDs(ids...)
}
// Mutation returns the ContributorMutation object of the builder.
func (cc *ContributorCreate) Mutation() *ContributorMutation {
return cc.mutation
}
// Save creates the Contributor in the database.
func (cc *ContributorCreate) Save(ctx context.Context) (*Contributor, error) {
cc.defaults()
return withHooks(ctx, cc.sqlSave, cc.mutation, cc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (cc *ContributorCreate) SaveX(ctx context.Context) *Contributor {
v, err := cc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (cc *ContributorCreate) Exec(ctx context.Context) error {
_, err := cc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (cc *ContributorCreate) ExecX(ctx context.Context) {
if err := cc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (cc *ContributorCreate) defaults() {
if _, ok := cc.mutation.CreatedAt(); !ok {
v := contributor.DefaultCreatedAt()
cc.mutation.SetCreatedAt(v)
}
if _, ok := cc.mutation.UpdatedAt(); !ok {
v := contributor.DefaultUpdatedAt()
cc.mutation.SetUpdatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (cc *ContributorCreate) check() error {
if _, ok := cc.mutation.Name(); !ok {
return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Contributor.name"`)}
}
if v, ok := cc.mutation.Name(); ok {
if err := contributor.NameValidator(v); err != nil {
return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Contributor.name": %w`, err)}
}
}
if _, ok := cc.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Contributor.created_at"`)}
}
if _, ok := cc.mutation.UpdatedAt(); !ok {
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Contributor.updated_at"`)}
}
return nil
}
func (cc *ContributorCreate) sqlSave(ctx context.Context) (*Contributor, error) {
if err := cc.check(); err != nil {
return nil, err
}
_node, _spec := cc.createSpec()
if err := sqlgraph.CreateNode(ctx, cc.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)
cc.mutation.id = &_node.ID
cc.mutation.done = true
return _node, nil
}
func (cc *ContributorCreate) createSpec() (*Contributor, *sqlgraph.CreateSpec) {
var (
_node = &Contributor{config: cc.config}
_spec = sqlgraph.NewCreateSpec(contributor.Table, sqlgraph.NewFieldSpec(contributor.FieldID, field.TypeInt))
)
if value, ok := cc.mutation.Name(); ok {
_spec.SetField(contributor.FieldName, field.TypeString, value)
_node.Name = value
}
if value, ok := cc.mutation.AvatarURL(); ok {
_spec.SetField(contributor.FieldAvatarURL, field.TypeString, value)
_node.AvatarURL = value
}
if value, ok := cc.mutation.Bio(); ok {
_spec.SetField(contributor.FieldBio, field.TypeString, value)
_node.Bio = value
}
if value, ok := cc.mutation.CreatedAt(); ok {
_spec.SetField(contributor.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := cc.mutation.UpdatedAt(); ok {
_spec.SetField(contributor.FieldUpdatedAt, field.TypeTime, value)
_node.UpdatedAt = value
}
if nodes := cc.mutation.UserIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: contributor.UserTable,
Columns: []string{contributor.UserColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_node.user_contributors = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := cc.mutation.SocialLinksIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: contributor.SocialLinksTable,
Columns: []string{contributor.SocialLinksColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(contributorsociallink.FieldID, field.TypeInt),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if nodes := cc.mutation.PostsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: contributor.PostsTable,
Columns: []string{contributor.PostsColumn},
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
}
// ContributorCreateBulk is the builder for creating many Contributor entities in bulk.
type ContributorCreateBulk struct {
config
err error
builders []*ContributorCreate
}
// Save creates the Contributor entities in the database.
func (ccb *ContributorCreateBulk) Save(ctx context.Context) ([]*Contributor, error) {
if ccb.err != nil {
return nil, ccb.err
}
specs := make([]*sqlgraph.CreateSpec, len(ccb.builders))
nodes := make([]*Contributor, len(ccb.builders))
mutators := make([]Mutator, len(ccb.builders))
for i := range ccb.builders {
func(i int, root context.Context) {
builder := ccb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*ContributorMutation)
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, ccb.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, ccb.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, ccb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (ccb *ContributorCreateBulk) SaveX(ctx context.Context) []*Contributor {
v, err := ccb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (ccb *ContributorCreateBulk) Exec(ctx context.Context) error {
_, err := ccb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (ccb *ContributorCreateBulk) ExecX(ctx context.Context) {
if err := ccb.Exec(ctx); err != nil {
panic(err)
}
}