372 lines
10 KiB
Go
372 lines
10 KiB
Go
// Code generated by ent, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
"tss-rocks-be/ent/media"
|
|
"tss-rocks-be/ent/user"
|
|
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/schema/field"
|
|
)
|
|
|
|
// MediaCreate is the builder for creating a Media entity.
|
|
type MediaCreate struct {
|
|
config
|
|
mutation *MediaMutation
|
|
hooks []Hook
|
|
}
|
|
|
|
// SetStorageID sets the "storage_id" field.
|
|
func (mc *MediaCreate) SetStorageID(s string) *MediaCreate {
|
|
mc.mutation.SetStorageID(s)
|
|
return mc
|
|
}
|
|
|
|
// SetOriginalName sets the "original_name" field.
|
|
func (mc *MediaCreate) SetOriginalName(s string) *MediaCreate {
|
|
mc.mutation.SetOriginalName(s)
|
|
return mc
|
|
}
|
|
|
|
// SetMimeType sets the "mime_type" field.
|
|
func (mc *MediaCreate) SetMimeType(s string) *MediaCreate {
|
|
mc.mutation.SetMimeType(s)
|
|
return mc
|
|
}
|
|
|
|
// SetSize sets the "size" field.
|
|
func (mc *MediaCreate) SetSize(i int64) *MediaCreate {
|
|
mc.mutation.SetSize(i)
|
|
return mc
|
|
}
|
|
|
|
// SetURL sets the "url" field.
|
|
func (mc *MediaCreate) SetURL(s string) *MediaCreate {
|
|
mc.mutation.SetURL(s)
|
|
return mc
|
|
}
|
|
|
|
// SetCreatedAt sets the "created_at" field.
|
|
func (mc *MediaCreate) SetCreatedAt(t time.Time) *MediaCreate {
|
|
mc.mutation.SetCreatedAt(t)
|
|
return mc
|
|
}
|
|
|
|
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
|
|
func (mc *MediaCreate) SetNillableCreatedAt(t *time.Time) *MediaCreate {
|
|
if t != nil {
|
|
mc.SetCreatedAt(*t)
|
|
}
|
|
return mc
|
|
}
|
|
|
|
// SetUpdatedAt sets the "updated_at" field.
|
|
func (mc *MediaCreate) SetUpdatedAt(t time.Time) *MediaCreate {
|
|
mc.mutation.SetUpdatedAt(t)
|
|
return mc
|
|
}
|
|
|
|
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
|
|
func (mc *MediaCreate) SetNillableUpdatedAt(t *time.Time) *MediaCreate {
|
|
if t != nil {
|
|
mc.SetUpdatedAt(*t)
|
|
}
|
|
return mc
|
|
}
|
|
|
|
// SetCreatedBy sets the "created_by" field.
|
|
func (mc *MediaCreate) SetCreatedBy(s string) *MediaCreate {
|
|
mc.mutation.SetCreatedBy(s)
|
|
return mc
|
|
}
|
|
|
|
// SetNillableCreatedBy sets the "created_by" field if the given value is not nil.
|
|
func (mc *MediaCreate) SetNillableCreatedBy(s *string) *MediaCreate {
|
|
if s != nil {
|
|
mc.SetCreatedBy(*s)
|
|
}
|
|
return mc
|
|
}
|
|
|
|
// SetOwnerID sets the "owner" edge to the User entity by ID.
|
|
func (mc *MediaCreate) SetOwnerID(id int) *MediaCreate {
|
|
mc.mutation.SetOwnerID(id)
|
|
return mc
|
|
}
|
|
|
|
// SetNillableOwnerID sets the "owner" edge to the User entity by ID if the given value is not nil.
|
|
func (mc *MediaCreate) SetNillableOwnerID(id *int) *MediaCreate {
|
|
if id != nil {
|
|
mc = mc.SetOwnerID(*id)
|
|
}
|
|
return mc
|
|
}
|
|
|
|
// SetOwner sets the "owner" edge to the User entity.
|
|
func (mc *MediaCreate) SetOwner(u *User) *MediaCreate {
|
|
return mc.SetOwnerID(u.ID)
|
|
}
|
|
|
|
// Mutation returns the MediaMutation object of the builder.
|
|
func (mc *MediaCreate) Mutation() *MediaMutation {
|
|
return mc.mutation
|
|
}
|
|
|
|
// Save creates the Media in the database.
|
|
func (mc *MediaCreate) Save(ctx context.Context) (*Media, error) {
|
|
mc.defaults()
|
|
return withHooks(ctx, mc.sqlSave, mc.mutation, mc.hooks)
|
|
}
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
func (mc *MediaCreate) SaveX(ctx context.Context) *Media {
|
|
v, err := mc.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (mc *MediaCreate) Exec(ctx context.Context) error {
|
|
_, err := mc.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (mc *MediaCreate) 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 *MediaCreate) defaults() {
|
|
if _, ok := mc.mutation.CreatedAt(); !ok {
|
|
v := media.DefaultCreatedAt()
|
|
mc.mutation.SetCreatedAt(v)
|
|
}
|
|
if _, ok := mc.mutation.UpdatedAt(); !ok {
|
|
v := media.DefaultUpdatedAt()
|
|
mc.mutation.SetUpdatedAt(v)
|
|
}
|
|
}
|
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
func (mc *MediaCreate) check() error {
|
|
if _, ok := mc.mutation.StorageID(); !ok {
|
|
return &ValidationError{Name: "storage_id", err: errors.New(`ent: missing required field "Media.storage_id"`)}
|
|
}
|
|
if v, ok := mc.mutation.StorageID(); ok {
|
|
if err := media.StorageIDValidator(v); err != nil {
|
|
return &ValidationError{Name: "storage_id", err: fmt.Errorf(`ent: validator failed for field "Media.storage_id": %w`, err)}
|
|
}
|
|
}
|
|
if _, ok := mc.mutation.OriginalName(); !ok {
|
|
return &ValidationError{Name: "original_name", err: errors.New(`ent: missing required field "Media.original_name"`)}
|
|
}
|
|
if v, ok := mc.mutation.OriginalName(); ok {
|
|
if err := media.OriginalNameValidator(v); err != nil {
|
|
return &ValidationError{Name: "original_name", err: fmt.Errorf(`ent: validator failed for field "Media.original_name": %w`, err)}
|
|
}
|
|
}
|
|
if _, ok := mc.mutation.MimeType(); !ok {
|
|
return &ValidationError{Name: "mime_type", err: errors.New(`ent: missing required field "Media.mime_type"`)}
|
|
}
|
|
if v, ok := mc.mutation.MimeType(); ok {
|
|
if err := media.MimeTypeValidator(v); err != nil {
|
|
return &ValidationError{Name: "mime_type", err: fmt.Errorf(`ent: validator failed for field "Media.mime_type": %w`, err)}
|
|
}
|
|
}
|
|
if _, ok := mc.mutation.Size(); !ok {
|
|
return &ValidationError{Name: "size", err: errors.New(`ent: missing required field "Media.size"`)}
|
|
}
|
|
if v, ok := mc.mutation.Size(); ok {
|
|
if err := media.SizeValidator(v); err != nil {
|
|
return &ValidationError{Name: "size", err: fmt.Errorf(`ent: validator failed for field "Media.size": %w`, err)}
|
|
}
|
|
}
|
|
if _, ok := mc.mutation.URL(); !ok {
|
|
return &ValidationError{Name: "url", err: errors.New(`ent: missing required field "Media.url"`)}
|
|
}
|
|
if v, ok := mc.mutation.URL(); ok {
|
|
if err := media.URLValidator(v); err != nil {
|
|
return &ValidationError{Name: "url", err: fmt.Errorf(`ent: validator failed for field "Media.url": %w`, err)}
|
|
}
|
|
}
|
|
if _, ok := mc.mutation.CreatedAt(); !ok {
|
|
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Media.created_at"`)}
|
|
}
|
|
if _, ok := mc.mutation.UpdatedAt(); !ok {
|
|
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Media.updated_at"`)}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (mc *MediaCreate) sqlSave(ctx context.Context) (*Media, 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 *MediaCreate) createSpec() (*Media, *sqlgraph.CreateSpec) {
|
|
var (
|
|
_node = &Media{config: mc.config}
|
|
_spec = sqlgraph.NewCreateSpec(media.Table, sqlgraph.NewFieldSpec(media.FieldID, field.TypeInt))
|
|
)
|
|
if value, ok := mc.mutation.StorageID(); ok {
|
|
_spec.SetField(media.FieldStorageID, field.TypeString, value)
|
|
_node.StorageID = value
|
|
}
|
|
if value, ok := mc.mutation.OriginalName(); ok {
|
|
_spec.SetField(media.FieldOriginalName, field.TypeString, value)
|
|
_node.OriginalName = value
|
|
}
|
|
if value, ok := mc.mutation.MimeType(); ok {
|
|
_spec.SetField(media.FieldMimeType, field.TypeString, value)
|
|
_node.MimeType = value
|
|
}
|
|
if value, ok := mc.mutation.Size(); ok {
|
|
_spec.SetField(media.FieldSize, field.TypeInt64, value)
|
|
_node.Size = value
|
|
}
|
|
if value, ok := mc.mutation.URL(); ok {
|
|
_spec.SetField(media.FieldURL, field.TypeString, value)
|
|
_node.URL = value
|
|
}
|
|
if value, ok := mc.mutation.CreatedAt(); ok {
|
|
_spec.SetField(media.FieldCreatedAt, field.TypeTime, value)
|
|
_node.CreatedAt = value
|
|
}
|
|
if value, ok := mc.mutation.UpdatedAt(); ok {
|
|
_spec.SetField(media.FieldUpdatedAt, field.TypeTime, value)
|
|
_node.UpdatedAt = value
|
|
}
|
|
if value, ok := mc.mutation.CreatedBy(); ok {
|
|
_spec.SetField(media.FieldCreatedBy, field.TypeString, value)
|
|
_node.CreatedBy = value
|
|
}
|
|
if nodes := mc.mutation.OwnerIDs(); len(nodes) > 0 {
|
|
edge := &sqlgraph.EdgeSpec{
|
|
Rel: sqlgraph.M2O,
|
|
Inverse: true,
|
|
Table: media.OwnerTable,
|
|
Columns: []string{media.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_media = &nodes[0]
|
|
_spec.Edges = append(_spec.Edges, edge)
|
|
}
|
|
return _node, _spec
|
|
}
|
|
|
|
// MediaCreateBulk is the builder for creating many Media entities in bulk.
|
|
type MediaCreateBulk struct {
|
|
config
|
|
err error
|
|
builders []*MediaCreate
|
|
}
|
|
|
|
// Save creates the Media entities in the database.
|
|
func (mcb *MediaCreateBulk) Save(ctx context.Context) ([]*Media, error) {
|
|
if mcb.err != nil {
|
|
return nil, mcb.err
|
|
}
|
|
specs := make([]*sqlgraph.CreateSpec, len(mcb.builders))
|
|
nodes := make([]*Media, 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.(*MediaMutation)
|
|
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 *MediaCreateBulk) SaveX(ctx context.Context) []*Media {
|
|
v, err := mcb.Save(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Exec executes the query.
|
|
func (mcb *MediaCreateBulk) Exec(ctx context.Context) error {
|
|
_, err := mcb.Save(ctx)
|
|
return err
|
|
}
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
func (mcb *MediaCreateBulk) ExecX(ctx context.Context) {
|
|
if err := mcb.Exec(ctx); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|