1
0
api/ent/ma_create.go
2024-04-25 14:05:59 +09:00

628 lines
15 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
}
// SetCidRoot sets the "cid_root" field.
func (mc *MaCreate) SetCidRoot(s string) *MaCreate {
mc.mutation.SetCidRoot(s)
return mc
}
// SetNillableCidRoot sets the "cid_root" field if the given value is not nil.
func (mc *MaCreate) SetNillableCidRoot(s *string) *MaCreate {
if s != nil {
mc.SetCidRoot(*s)
}
return mc
}
// SetURIRoot sets the "uri_root" field.
func (mc *MaCreate) SetURIRoot(s string) *MaCreate {
mc.mutation.SetURIRoot(s)
return mc
}
// SetNillableURIRoot sets the "uri_root" field if the given value is not nil.
func (mc *MaCreate) SetNillableURIRoot(s *string) *MaCreate {
if s != nil {
mc.SetURIRoot(*s)
}
return mc
}
// SetRoot sets the "root" field.
func (mc *MaCreate) SetRoot(s string) *MaCreate {
mc.mutation.SetRoot(s)
return mc
}
// SetNillableRoot sets the "root" field if the given value is not nil.
func (mc *MaCreate) SetNillableRoot(s *string) *MaCreate {
if s != nil {
mc.SetRoot(*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
}
// SetComment sets the "comment" field.
func (mc *MaCreate) SetComment(s string) *MaCreate {
mc.mutation.SetComment(s)
return mc
}
// SetNillableComment sets the "comment" field if the given value is not nil.
func (mc *MaCreate) SetNillableComment(s *string) *MaCreate {
if s != nil {
mc.SetComment(*s)
}
return mc
}
// SetBlog sets the "blog" field.
func (mc *MaCreate) SetBlog(s string) *MaCreate {
mc.mutation.SetBlog(s)
return mc
}
// SetNillableBlog sets the "blog" field if the given value is not nil.
func (mc *MaCreate) SetNillableBlog(s *string) *MaCreate {
if s != nil {
mc.SetBlog(*s)
}
return mc
}
// SetBlogURL sets the "blog_url" field.
func (mc *MaCreate) SetBlogURL(s string) *MaCreate {
mc.mutation.SetBlogURL(s)
return mc
}
// SetNillableBlogURL sets the "blog_url" field if the given value is not nil.
func (mc *MaCreate) SetNillableBlogURL(s *string) *MaCreate {
if s != nil {
mc.SetBlogURL(*s)
}
return mc
}
// SetDomain sets the "domain" field.
func (mc *MaCreate) SetDomain(s string) *MaCreate {
mc.mutation.SetDomain(s)
return mc
}
// SetNillableDomain sets the "domain" field if the given value is not nil.
func (mc *MaCreate) SetNillableDomain(s *string) *MaCreate {
if s != nil {
mc.SetDomain(*s)
}
return mc
}
// SetHost sets the "host" field.
func (mc *MaCreate) SetHost(s string) *MaCreate {
mc.mutation.SetHost(s)
return mc
}
// SetNillableHost sets the "host" field if the given value is not nil.
func (mc *MaCreate) SetNillableHost(s *string) *MaCreate {
if s != nil {
mc.SetHost(*s)
}
return mc
}
// SetFeed sets the "feed" field.
func (mc *MaCreate) SetFeed(s string) *MaCreate {
mc.mutation.SetFeed(s)
return mc
}
// SetNillableFeed sets the "feed" field if the given value is not nil.
func (mc *MaCreate) SetNillableFeed(s *string) *MaCreate {
if s != nil {
mc.SetFeed(*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.CidRoot(); ok {
_spec.SetField(ma.FieldCidRoot, field.TypeString, value)
_node.CidRoot = value
}
if value, ok := mc.mutation.URIRoot(); ok {
_spec.SetField(ma.FieldURIRoot, field.TypeString, value)
_node.URIRoot = value
}
if value, ok := mc.mutation.Root(); ok {
_spec.SetField(ma.FieldRoot, field.TypeString, value)
_node.Root = 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.Comment(); ok {
_spec.SetField(ma.FieldComment, field.TypeString, value)
_node.Comment = value
}
if value, ok := mc.mutation.Blog(); ok {
_spec.SetField(ma.FieldBlog, field.TypeString, value)
_node.Blog = value
}
if value, ok := mc.mutation.BlogURL(); ok {
_spec.SetField(ma.FieldBlogURL, field.TypeString, value)
_node.BlogURL = value
}
if value, ok := mc.mutation.Domain(); ok {
_spec.SetField(ma.FieldDomain, field.TypeString, value)
_node.Domain = value
}
if value, ok := mc.mutation.Host(); ok {
_spec.SetField(ma.FieldHost, field.TypeString, value)
_node.Host = value
}
if value, ok := mc.mutation.Feed(); ok {
_spec.SetField(ma.FieldFeed, field.TypeString, value)
_node.Feed = 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)
}
}