59 lines
1.7 KiB
Go
59 lines
1.7 KiB
Go
package useCases
|
|
|
|
import (
|
|
"RenewCMS/internal/domain/audit"
|
|
"RenewCMS/internal/domain/image"
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
type CreateVariantCommand struct {
|
|
Name string
|
|
VariantDefinitionCommand
|
|
GenerateOnUpload *bool
|
|
Enabled *bool
|
|
}
|
|
|
|
type CreateVariantUseCase struct {
|
|
variants image.VariantRepository
|
|
renderer image.VariantRenderer
|
|
now func() time.Time
|
|
recorder audit.Recorder
|
|
}
|
|
|
|
func NewCreateVariantUseCase(variants image.VariantRepository, renderer image.VariantRenderer, now func() time.Time, recorder audit.Recorder) *CreateVariantUseCase {
|
|
return &CreateVariantUseCase{variants, renderer, now, recorder}
|
|
}
|
|
|
|
func (u *CreateVariantUseCase) EncodableFormats() []image.VariantFormat {
|
|
return u.renderer.EncodableFormats()
|
|
}
|
|
|
|
func (u *CreateVariantUseCase) Create(ctx context.Context, command CreateVariantCommand) (VariantSummary, error) {
|
|
variant, err := u.build(command)
|
|
if err != nil {
|
|
return VariantSummary{}, err
|
|
}
|
|
|
|
event := audit.NewEvent(audit.ActionVariantCreate, audit.CategoryMedia)
|
|
event.Target = audit.VariantTarget(variant.Name)
|
|
created, err := u.variants.Create(variant)
|
|
if err != nil {
|
|
recordFailure(ctx, u.recorder, event, err)
|
|
return VariantSummary{}, err
|
|
}
|
|
event.Params = audit.VariantParams(created.Settings())
|
|
u.recorder.Record(ctx, event)
|
|
return VariantSummary{Variant: created}, nil
|
|
}
|
|
|
|
func (u *CreateVariantUseCase) build(command CreateVariantCommand) (image.Variant, error) {
|
|
settings, err := command.settings(u.renderer.EncodableFormats())
|
|
if err != nil {
|
|
return image.Variant{}, err
|
|
}
|
|
generateOnUpload := command.GenerateOnUpload != nil && *command.GenerateOnUpload
|
|
enabled := command.Enabled == nil || *command.Enabled
|
|
return image.NewVariant(command.Name, settings, generateOnUpload, enabled, u.now())
|
|
}
|