2021-09-09 08:13:42 +00:00
|
|
|
package studio
|
|
|
|
|
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
2023-07-30 23:50:24 +00:00
|
|
|
"errors"
|
2021-09-09 08:13:42 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
)
|
|
|
|
|
2023-07-30 23:50:24 +00:00
|
|
|
var (
|
|
|
|
ErrStudioOwnAncestor = errors.New("studio cannot be an ancestor of itself")
|
|
|
|
)
|
|
|
|
|
2021-09-09 08:13:42 +00:00
|
|
|
type NameExistsError struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *NameExistsError) Error() string {
|
|
|
|
return fmt.Sprintf("studio with name '%s' already exists", e.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
type NameUsedByAliasError struct {
|
|
|
|
Name string
|
|
|
|
OtherStudio string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *NameUsedByAliasError) Error() string {
|
|
|
|
return fmt.Sprintf("name '%s' is used as alias for '%s'", e.Name, e.OtherStudio)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnsureStudioNameUnique returns an error if the studio name provided
|
|
|
|
// is used as a name or alias of another existing tag.
|
2023-09-01 00:39:29 +00:00
|
|
|
func EnsureStudioNameUnique(ctx context.Context, id int, name string, qb models.StudioQueryer) error {
|
2021-09-09 08:13:42 +00:00
|
|
|
// ensure name is unique
|
2022-05-19 07:49:32 +00:00
|
|
|
sameNameStudio, err := ByName(ctx, qb, name)
|
2021-09-09 08:13:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sameNameStudio != nil && id != sameNameStudio.ID {
|
|
|
|
return &NameExistsError{
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// query by alias
|
2022-05-19 07:49:32 +00:00
|
|
|
sameNameStudio, err = ByAlias(ctx, qb, name)
|
2021-09-09 08:13:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sameNameStudio != nil && id != sameNameStudio.ID {
|
|
|
|
return &NameUsedByAliasError{
|
|
|
|
Name: name,
|
2023-06-15 02:46:09 +00:00
|
|
|
OtherStudio: sameNameStudio.Name,
|
2021-09-09 08:13:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-01 00:39:29 +00:00
|
|
|
func EnsureAliasesUnique(ctx context.Context, id int, aliases []string, qb models.StudioQueryer) error {
|
2021-09-09 08:13:42 +00:00
|
|
|
for _, a := range aliases {
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := EnsureStudioNameUnique(ctx, id, a, qb); err != nil {
|
2021-09-09 08:13:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-30 23:50:24 +00:00
|
|
|
|
2023-09-01 00:39:29 +00:00
|
|
|
type ValidateModifyReader interface {
|
|
|
|
models.StudioGetter
|
|
|
|
models.StudioQueryer
|
|
|
|
models.AliasLoader
|
|
|
|
}
|
|
|
|
|
2023-07-30 23:50:24 +00:00
|
|
|
// Checks to make sure that:
|
|
|
|
// 1. The studio exists locally
|
|
|
|
// 2. The studio is not its own ancestor
|
|
|
|
// 3. The studio's aliases are unique
|
2023-09-01 00:39:29 +00:00
|
|
|
func ValidateModify(ctx context.Context, s models.StudioPartial, qb ValidateModifyReader) error {
|
2023-07-30 23:50:24 +00:00
|
|
|
existing, err := qb.Find(ctx, s.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if existing == nil {
|
|
|
|
return fmt.Errorf("studio with id %d not found", s.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
newParentID := s.ParentID.Ptr()
|
|
|
|
|
|
|
|
if newParentID != nil {
|
|
|
|
if err := validateParent(ctx, s.ID, *newParentID, qb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Aliases != nil {
|
|
|
|
if err := existing.LoadAliases(ctx, qb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
effectiveAliases := s.Aliases.EffectiveValues(existing.Aliases.List())
|
|
|
|
if err := EnsureAliasesUnique(ctx, s.ID, effectiveAliases, qb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-01 00:39:29 +00:00
|
|
|
func validateParent(ctx context.Context, studioID int, newParentID int, qb models.StudioGetter) error {
|
2023-07-30 23:50:24 +00:00
|
|
|
if newParentID == studioID {
|
|
|
|
return ErrStudioOwnAncestor
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure there is no cyclic dependency
|
|
|
|
parentStudio, err := qb.Find(ctx, newParentID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error finding parent studio: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if parentStudio == nil {
|
|
|
|
return fmt.Errorf("studio with id %d not found", newParentID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if parentStudio.ParentID != nil {
|
|
|
|
return validateParent(ctx, studioID, *parentStudio.ParentID, qb)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|