// Code generated by ent, DO NOT EDIT. package ent import ( "context" "errors" "fmt" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" "github.com/xmdhs/authlib-skin/db/ent/texture" "github.com/xmdhs/authlib-skin/db/ent/userprofile" "github.com/xmdhs/authlib-skin/db/ent/usertexture" ) // UserTextureCreate is the builder for creating a UserTexture entity. type UserTextureCreate struct { config mutation *UserTextureMutation hooks []Hook } // SetUserProfileID sets the "user_profile_id" field. func (utc *UserTextureCreate) SetUserProfileID(i int) *UserTextureCreate { utc.mutation.SetUserProfileID(i) return utc } // SetTextureID sets the "texture_id" field. func (utc *UserTextureCreate) SetTextureID(i int) *UserTextureCreate { utc.mutation.SetTextureID(i) return utc } // SetType sets the "type" field. func (utc *UserTextureCreate) SetType(s string) *UserTextureCreate { utc.mutation.SetType(s) return utc } // SetVariant sets the "variant" field. func (utc *UserTextureCreate) SetVariant(s string) *UserTextureCreate { utc.mutation.SetVariant(s) return utc } // SetUserProfile sets the "user_profile" edge to the UserProfile entity. func (utc *UserTextureCreate) SetUserProfile(u *UserProfile) *UserTextureCreate { return utc.SetUserProfileID(u.ID) } // SetTexture sets the "texture" edge to the Texture entity. func (utc *UserTextureCreate) SetTexture(t *Texture) *UserTextureCreate { return utc.SetTextureID(t.ID) } // Mutation returns the UserTextureMutation object of the builder. func (utc *UserTextureCreate) Mutation() *UserTextureMutation { return utc.mutation } // Save creates the UserTexture in the database. func (utc *UserTextureCreate) Save(ctx context.Context) (*UserTexture, error) { return withHooks(ctx, utc.sqlSave, utc.mutation, utc.hooks) } // SaveX calls Save and panics if Save returns an error. func (utc *UserTextureCreate) SaveX(ctx context.Context) *UserTexture { v, err := utc.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (utc *UserTextureCreate) Exec(ctx context.Context) error { _, err := utc.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (utc *UserTextureCreate) ExecX(ctx context.Context) { if err := utc.Exec(ctx); err != nil { panic(err) } } // check runs all checks and user-defined validators on the builder. func (utc *UserTextureCreate) check() error { if _, ok := utc.mutation.UserProfileID(); !ok { return &ValidationError{Name: "user_profile_id", err: errors.New(`ent: missing required field "UserTexture.user_profile_id"`)} } if _, ok := utc.mutation.TextureID(); !ok { return &ValidationError{Name: "texture_id", err: errors.New(`ent: missing required field "UserTexture.texture_id"`)} } if _, ok := utc.mutation.GetType(); !ok { return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "UserTexture.type"`)} } if _, ok := utc.mutation.Variant(); !ok { return &ValidationError{Name: "variant", err: errors.New(`ent: missing required field "UserTexture.variant"`)} } if _, ok := utc.mutation.UserProfileID(); !ok { return &ValidationError{Name: "user_profile", err: errors.New(`ent: missing required edge "UserTexture.user_profile"`)} } if _, ok := utc.mutation.TextureID(); !ok { return &ValidationError{Name: "texture", err: errors.New(`ent: missing required edge "UserTexture.texture"`)} } return nil } func (utc *UserTextureCreate) sqlSave(ctx context.Context) (*UserTexture, error) { if err := utc.check(); err != nil { return nil, err } _node, _spec := utc.createSpec() if err := sqlgraph.CreateNode(ctx, utc.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) utc.mutation.id = &_node.ID utc.mutation.done = true return _node, nil } func (utc *UserTextureCreate) createSpec() (*UserTexture, *sqlgraph.CreateSpec) { var ( _node = &UserTexture{config: utc.config} _spec = sqlgraph.NewCreateSpec(usertexture.Table, sqlgraph.NewFieldSpec(usertexture.FieldID, field.TypeInt)) ) if value, ok := utc.mutation.GetType(); ok { _spec.SetField(usertexture.FieldType, field.TypeString, value) _node.Type = value } if value, ok := utc.mutation.Variant(); ok { _spec.SetField(usertexture.FieldVariant, field.TypeString, value) _node.Variant = value } if nodes := utc.mutation.UserProfileIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: false, Table: usertexture.UserProfileTable, Columns: []string{usertexture.UserProfileColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(userprofile.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.UserProfileID = nodes[0] _spec.Edges = append(_spec.Edges, edge) } if nodes := utc.mutation.TextureIDs(); len(nodes) > 0 { edge := &sqlgraph.EdgeSpec{ Rel: sqlgraph.M2O, Inverse: false, Table: usertexture.TextureTable, Columns: []string{usertexture.TextureColumn}, Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: sqlgraph.NewFieldSpec(texture.FieldID, field.TypeInt), }, } for _, k := range nodes { edge.Target.Nodes = append(edge.Target.Nodes, k) } _node.TextureID = nodes[0] _spec.Edges = append(_spec.Edges, edge) } return _node, _spec } // UserTextureCreateBulk is the builder for creating many UserTexture entities in bulk. type UserTextureCreateBulk struct { config builders []*UserTextureCreate } // Save creates the UserTexture entities in the database. func (utcb *UserTextureCreateBulk) Save(ctx context.Context) ([]*UserTexture, error) { specs := make([]*sqlgraph.CreateSpec, len(utcb.builders)) nodes := make([]*UserTexture, len(utcb.builders)) mutators := make([]Mutator, len(utcb.builders)) for i := range utcb.builders { func(i int, root context.Context) { builder := utcb.builders[i] var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*UserTextureMutation) 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, utcb.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, utcb.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, utcb.builders[0].mutation); err != nil { return nil, err } } return nodes, nil } // SaveX is like Save, but panics if an error occurs. func (utcb *UserTextureCreateBulk) SaveX(ctx context.Context) []*UserTexture { v, err := utcb.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (utcb *UserTextureCreateBulk) Exec(ctx context.Context) error { _, err := utcb.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (utcb *UserTextureCreateBulk) ExecX(ctx context.Context) { if err := utcb.Exec(ctx); err != nil { panic(err) } }