238 lines
6.4 KiB
Go
238 lines
6.4 KiB
Go
// 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/skin"
|
|
"github.com/xmdhs/authlib-skin/db/ent/user"
|
|
)
|
|
|
|
// SkinCreate is the builder for creating a Skin entity.
|
|
type SkinCreate struct {
|
|
config
|
|
mutation *SkinMutation
|
|
hooks []Hook
|
|
}
|
|
|
|
// SetSkinHash sets the "skin_hash" field.
|
|
func (sc *SkinCreate) SetSkinHash(s string) *SkinCreate {
|
|
sc.mutation.SetSkinHash(s)
|
|
return sc
|
|
}
|
|
|
|
// SetType sets the "type" field.
|
|
func (sc *SkinCreate) SetType(u uint8) *SkinCreate {
|
|
sc.mutation.SetType(u)
|
|
return sc
|
|
}
|
|
|
|
// SetVariant sets the "variant" field.
|
|
func (sc *SkinCreate) SetVariant(s string) *SkinCreate {
|
|
sc.mutation.SetVariant(s)
|
|
return sc
|
|
}
|
|
|
|
// SetCreatedUserID sets the "created_user" edge to the User entity by ID.
|
|
func (sc *SkinCreate) SetCreatedUserID(id int) *SkinCreate {
|
|
sc.mutation.SetCreatedUserID(id)
|
|
return sc
|
|
}
|
|
|
|
// SetCreatedUser sets the "created_user" edge to the User entity.
|
|
func (sc *SkinCreate) SetCreatedUser(u *User) *SkinCreate {
|
|
return sc.SetCreatedUserID(u.ID)
|
|
}
|
|
|
|
// Mutation returns the SkinMutation object of the builder.
|
|
func (sc *SkinCreate) Mutation() *SkinMutation {
|
|
return sc.mutation
|
|
}
|
|
|
|
// Save creates the Skin in the database.
|
|
func (sc *SkinCreate) Save(ctx context.Context) (*Skin, error) {
|
|
return withHooks(ctx, sc.sqlSave, sc.mutation, sc.hooks)
|
|
}
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
func (sc *SkinCreate) SaveX(ctx context.Context) *Skin {
|
|
v, err := sc.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (sc *SkinCreate) Exec(ctx context.Context) error {
|
|
_, err := sc.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (sc *SkinCreate) ExecX(ctx context.Context) {
|
|
if err := sc.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (sc *SkinCreate) check() error {
|
|
if _, ok := sc.mutation.SkinHash(); !ok {
|
|
return &ValidationError{Name: "skin_hash", err: errors.New(`ent: missing required field "Skin.skin_hash"`)}
|
|
}
|
|
if _, ok := sc.mutation.GetType(); !ok {
|
|
return &ValidationError{Name: "type", err: errors.New(`ent: missing required field "Skin.type"`)}
|
|
}
|
|
if _, ok := sc.mutation.Variant(); !ok {
|
|
return &ValidationError{Name: "variant", err: errors.New(`ent: missing required field "Skin.variant"`)}
|
|
}
|
|
if _, ok := sc.mutation.CreatedUserID(); !ok {
|
|
return &ValidationError{Name: "created_user", err: errors.New(`ent: missing required edge "Skin.created_user"`)}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (sc *SkinCreate) sqlSave(ctx context.Context) (*Skin, error) {
|
|
if err := sc.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
_node, _spec := sc.createSpec()
|
|
if err := sqlgraph.CreateNode(ctx, sc.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)
|
|
sc.mutation.id = &_node.ID
|
|
sc.mutation.done = true
|
|
return _node, nil
|
|
}
|
|
|
|
func (sc *SkinCreate) createSpec() (*Skin, *sqlgraph.CreateSpec) {
|
|
var (
|
|
_node = &Skin{config: sc.config}
|
|
_spec = sqlgraph.NewCreateSpec(skin.Table, sqlgraph.NewFieldSpec(skin.FieldID, field.TypeInt))
|
|
)
|
|
if value, ok := sc.mutation.SkinHash(); ok {
|
|
_spec.SetField(skin.FieldSkinHash, field.TypeString, value)
|
|
_node.SkinHash = value
|
|
}
|
|
if value, ok := sc.mutation.GetType(); ok {
|
|
_spec.SetField(skin.FieldType, field.TypeUint8, value)
|
|
_node.Type = value
|
|
}
|
|
if value, ok := sc.mutation.Variant(); ok {
|
|
_spec.SetField(skin.FieldVariant, field.TypeString, value)
|
|
_node.Variant = value
|
|
}
|
|
if nodes := sc.mutation.CreatedUserIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2O,
|
|
Inverse: false,
|
|
Table: skin.CreatedUserTable,
|
|
Columns: []string{skin.CreatedUserColumn},
|
|
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.skin_created_user = &nodes[0]
|
|
_spec.Edges = append(_spec.Edges, edge)
|
|
}
|
|
return _node, _spec
|
|
}
|
|
|
|
// SkinCreateBulk is the builder for creating many Skin entities in bulk.
|
|
type SkinCreateBulk struct {
|
|
config
|
|
builders []*SkinCreate
|
|
}
|
|
|
|
// Save creates the Skin entities in the database.
|
|
func (scb *SkinCreateBulk) Save(ctx context.Context) ([]*Skin, error) {
|
|
specs := make([]*sqlgraph.CreateSpec, len(scb.builders))
|
|
nodes := make([]*Skin, len(scb.builders))
|
|
mutators := make([]Mutator, len(scb.builders))
|
|
for i := range scb.builders {
|
|
func(i int, root context.Context) {
|
|
builder := scb.builders[i]
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*SkinMutation)
|
|
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, scb.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, scb.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, scb.builders[0].mutation); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
func (scb *SkinCreateBulk) SaveX(ctx context.Context) []*Skin {
|
|
v, err := scb.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (scb *SkinCreateBulk) Exec(ctx context.Context) error {
|
|
_, err := scb.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (scb *SkinCreateBulk) ExecX(ctx context.Context) {
|
|
if err := scb.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|