2021-09-09 08:13:42 +00:00
|
|
|
package studio
|
|
|
|
|
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
2021-09-09 08:13:42 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
)
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
type NameFinderCreator interface {
|
|
|
|
FindByName(ctx context.Context, name string, nocase bool) (*models.Studio, error)
|
|
|
|
Create(ctx context.Context, newStudio models.Studio) (*models.Studio, error)
|
|
|
|
}
|
|
|
|
|
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.
|
2022-05-19 07:49:32 +00:00
|
|
|
func EnsureStudioNameUnique(ctx context.Context, id int, name string, qb Queryer) 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,
|
|
|
|
OtherStudio: sameNameStudio.Name.String,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func EnsureAliasesUnique(ctx context.Context, id int, aliases []string, qb Queryer) 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
|
|
|
|
}
|