1
0
api/ent/ma_create.go
2024-04-06 03:58:54 +09:00

466 lines
12 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"api/ent/ma"
"api/ent/user"
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
)
// MaCreate is the builder for creating a Ma entity.
type MaCreate struct {
config
mutation *MaMutation
hooks []Hook
}
// SetPassword sets the "password" field.
func (mc *MaCreate) SetPassword(s string) *MaCreate {
mc.mutation.SetPassword(s)
return mc
}
// SetToken sets the "token" field.
func (mc *MaCreate) SetToken(s string) *MaCreate {
mc.mutation.SetToken(s)
return mc
}
// SetNillableToken sets the "token" field if the given value is not nil.
func (mc *MaCreate) SetNillableToken(s *string) *MaCreate {
if s != nil {
mc.SetToken(*s)
}
return mc
}
// SetLimit sets the "limit" field.
func (mc *MaCreate) SetLimit(b bool) *MaCreate {
mc.mutation.SetLimit(b)
return mc
}
// SetNillableLimit sets the "limit" field if the given value is not nil.
func (mc *MaCreate) SetNillableLimit(b *bool) *MaCreate {
if b != nil {
mc.SetLimit(*b)
}
return mc
}
// SetCount sets the "count" field.
func (mc *MaCreate) SetCount(i int) *MaCreate {
mc.mutation.SetCount(i)
return mc
}
// SetNillableCount sets the "count" field if the given value is not nil.
func (mc *MaCreate) SetNillableCount(i *int) *MaCreate {
if i != nil {
mc.SetCount(*i)
}
return mc
}
// SetHandle sets the "handle" field.
func (mc *MaCreate) SetHandle(s string) *MaCreate {
mc.mutation.SetHandle(s)
return mc
}
// SetNillableHandle sets the "handle" field if the given value is not nil.
func (mc *MaCreate) SetNillableHandle(s *string) *MaCreate {
if s != nil {
mc.SetHandle(*s)
}
return mc
}
// SetText sets the "text" field.
func (mc *MaCreate) SetText(s string) *MaCreate {
mc.mutation.SetText(s)
return mc
}
// SetNillableText sets the "text" field if the given value is not nil.
func (mc *MaCreate) SetNillableText(s *string) *MaCreate {
if s != nil {
mc.SetText(*s)
}
return mc
}
// SetDid sets the "did" field.
func (mc *MaCreate) SetDid(s string) *MaCreate {
mc.mutation.SetDid(s)
return mc
}
// SetNillableDid sets the "did" field if the given value is not nil.
func (mc *MaCreate) SetNillableDid(s *string) *MaCreate {
if s != nil {
mc.SetDid(*s)
}
return mc
}
// SetAvatar sets the "avatar" field.
func (mc *MaCreate) SetAvatar(s string) *MaCreate {
mc.mutation.SetAvatar(s)
return mc
}
// SetNillableAvatar sets the "avatar" field if the given value is not nil.
func (mc *MaCreate) SetNillableAvatar(s *string) *MaCreate {
if s != nil {
mc.SetAvatar(*s)
}
return mc
}
// SetCid sets the "cid" field.
func (mc *MaCreate) SetCid(s string) *MaCreate {
mc.mutation.SetCid(s)
return mc
}
// SetNillableCid sets the "cid" field if the given value is not nil.
func (mc *MaCreate) SetNillableCid(s *string) *MaCreate {
if s != nil {
mc.SetCid(*s)
}
return mc
}
// SetURI sets the "uri" field.
func (mc *MaCreate) SetURI(s string) *MaCreate {
mc.mutation.SetURI(s)
return mc
}
// SetNillableURI sets the "uri" field if the given value is not nil.
func (mc *MaCreate) SetNillableURI(s *string) *MaCreate {
if s != nil {
mc.SetURI(*s)
}
return mc
}
// SetRkey sets the "rkey" field.
func (mc *MaCreate) SetRkey(s string) *MaCreate {
mc.mutation.SetRkey(s)
return mc
}
// SetNillableRkey sets the "rkey" field if the given value is not nil.
func (mc *MaCreate) SetNillableRkey(s *string) *MaCreate {
if s != nil {
mc.SetRkey(*s)
}
return mc
}
// SetBskyURL sets the "bsky_url" field.
func (mc *MaCreate) SetBskyURL(s string) *MaCreate {
mc.mutation.SetBskyURL(s)
return mc
}
// SetNillableBskyURL sets the "bsky_url" field if the given value is not nil.
func (mc *MaCreate) SetNillableBskyURL(s *string) *MaCreate {
if s != nil {
mc.SetBskyURL(*s)
}
return mc
}
// SetUpdatedAt sets the "updated_at" field.
func (mc *MaCreate) SetUpdatedAt(t time.Time) *MaCreate {
mc.mutation.SetUpdatedAt(t)
return mc
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (mc *MaCreate) SetNillableUpdatedAt(t *time.Time) *MaCreate {
if t != nil {
mc.SetUpdatedAt(*t)
}
return mc
}
// SetCreatedAt sets the "created_at" field.
func (mc *MaCreate) SetCreatedAt(t time.Time) *MaCreate {
mc.mutation.SetCreatedAt(t)
return mc
}
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (mc *MaCreate) SetNillableCreatedAt(t *time.Time) *MaCreate {
if t != nil {
mc.SetCreatedAt(*t)
}
return mc
}
// SetOwnerID sets the "owner" edge to the User entity by ID.
func (mc *MaCreate) SetOwnerID(id int) *MaCreate {
mc.mutation.SetOwnerID(id)
return mc
}
// SetOwner sets the "owner" edge to the User entity.
func (mc *MaCreate) SetOwner(u *User) *MaCreate {
return mc.SetOwnerID(u.ID)
}
// Mutation returns the MaMutation object of the builder.
func (mc *MaCreate) Mutation() *MaMutation {
return mc.mutation
}
// Save creates the Ma in the database.
func (mc *MaCreate) Save(ctx context.Context) (*Ma, error) {
mc.defaults()
return withHooks[*Ma, MaMutation](ctx, mc.sqlSave, mc.mutation, mc.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (mc *MaCreate) SaveX(ctx context.Context) *Ma {
v, err := mc.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (mc *MaCreate) Exec(ctx context.Context) error {
_, err := mc.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (mc *MaCreate) ExecX(ctx context.Context) {
if err := mc.Exec(ctx); err != nil {
panic(err)
}
}
// defaults sets the default values of the builder before save.
func (mc *MaCreate) defaults() {
if _, ok := mc.mutation.Limit(); !ok {
v := ma.DefaultLimit
mc.mutation.SetLimit(v)
}
if _, ok := mc.mutation.CreatedAt(); !ok {
v := ma.DefaultCreatedAt()
mc.mutation.SetCreatedAt(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (mc *MaCreate) check() error {
if _, ok := mc.mutation.Password(); !ok {
return &ValidationError{Name: "password", err: errors.New(`ent: missing required field "Ma.password"`)}
}
if v, ok := mc.mutation.Password(); ok {
if err := ma.PasswordValidator(v); err != nil {
return &ValidationError{Name: "password", err: fmt.Errorf(`ent: validator failed for field "Ma.password": %w`, err)}
}
}
if _, ok := mc.mutation.OwnerID(); !ok {
return &ValidationError{Name: "owner", err: errors.New(`ent: missing required edge "Ma.owner"`)}
}
return nil
}
func (mc *MaCreate) sqlSave(ctx context.Context) (*Ma, error) {
if err := mc.check(); err != nil {
return nil, err
}
_node, _spec := mc.createSpec()
if err := sqlgraph.CreateNode(ctx, mc.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)
mc.mutation.id = &_node.ID
mc.mutation.done = true
return _node, nil
}
func (mc *MaCreate) createSpec() (*Ma, *sqlgraph.CreateSpec) {
var (
_node = &Ma{config: mc.config}
_spec = sqlgraph.NewCreateSpec(ma.Table, sqlgraph.NewFieldSpec(ma.FieldID, field.TypeInt))
)
if value, ok := mc.mutation.Password(); ok {
_spec.SetField(ma.FieldPassword, field.TypeString, value)
_node.Password = value
}
if value, ok := mc.mutation.Token(); ok {
_spec.SetField(ma.FieldToken, field.TypeString, value)
_node.Token = value
}
if value, ok := mc.mutation.Limit(); ok {
_spec.SetField(ma.FieldLimit, field.TypeBool, value)
_node.Limit = value
}
if value, ok := mc.mutation.Count(); ok {
_spec.SetField(ma.FieldCount, field.TypeInt, value)
_node.Count = value
}
if value, ok := mc.mutation.Handle(); ok {
_spec.SetField(ma.FieldHandle, field.TypeString, value)
_node.Handle = value
}
if value, ok := mc.mutation.Text(); ok {
_spec.SetField(ma.FieldText, field.TypeString, value)
_node.Text = value
}
if value, ok := mc.mutation.Did(); ok {
_spec.SetField(ma.FieldDid, field.TypeString, value)
_node.Did = value
}
if value, ok := mc.mutation.Avatar(); ok {
_spec.SetField(ma.FieldAvatar, field.TypeString, value)
_node.Avatar = value
}
if value, ok := mc.mutation.Cid(); ok {
_spec.SetField(ma.FieldCid, field.TypeString, value)
_node.Cid = value
}
if value, ok := mc.mutation.URI(); ok {
_spec.SetField(ma.FieldURI, field.TypeString, value)
_node.URI = value
}
if value, ok := mc.mutation.Rkey(); ok {
_spec.SetField(ma.FieldRkey, field.TypeString, value)
_node.Rkey = value
}
if value, ok := mc.mutation.BskyURL(); ok {
_spec.SetField(ma.FieldBskyURL, field.TypeString, value)
_node.BskyURL = value
}
if value, ok := mc.mutation.UpdatedAt(); ok {
_spec.SetField(ma.FieldUpdatedAt, field.TypeTime, value)
_node.UpdatedAt = value
}
if value, ok := mc.mutation.CreatedAt(); ok {
_spec.SetField(ma.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if nodes := mc.mutation.OwnerIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: ma.OwnerTable,
Columns: []string{ma.OwnerColumn},
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_ma = &nodes[0]
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// MaCreateBulk is the builder for creating many Ma entities in bulk.
type MaCreateBulk struct {
config
builders []*MaCreate
}
// Save creates the Ma entities in the database.
func (mcb *MaCreateBulk) Save(ctx context.Context) ([]*Ma, error) {
specs := make([]*sqlgraph.CreateSpec, len(mcb.builders))
nodes := make([]*Ma, len(mcb.builders))
mutators := make([]Mutator, len(mcb.builders))
for i := range mcb.builders {
func(i int, root context.Context) {
builder := mcb.builders[i]
builder.defaults()
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*MaMutation)
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, mcb.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, mcb.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, mcb.builders[0].mutation); err != nil {
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
func (mcb *MaCreateBulk) SaveX(ctx context.Context) []*Ma {
v, err := mcb.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (mcb *MaCreateBulk) Exec(ctx context.Context) error {
_, err := mcb.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (mcb *MaCreateBulk) ExecX(ctx context.Context) {
if err := mcb.Exec(ctx); err != nil {
panic(err)
}
}