stash/pkg/studio/update.go
WithoutPants 964b559309 Restructure data layer (#2532)
* Add new txn manager interface
* Add txn management to sqlite
* Rename get to getByID
* Add contexts to repository methods
* Update query builders
* Add context to reader writer interfaces
* Use repository in resolver
* Tighten interfaces
* Tighten interfaces in dlna
* Tighten interfaces in match package
* Tighten interfaces in scraper package
* Tighten interfaces in scan code
* Tighten interfaces on autotag package
* Remove ReaderWriter usage
* Merge database package into sqlite
2022-09-06 07:03:40 +00:00

71 lines
1.5 KiB
Go

package studio
import (
"context"
"fmt"
"github.com/stashapp/stash/pkg/models"
)
type NameFinderCreator interface {
FindByName(ctx context.Context, name string, nocase bool) (*models.Studio, error)
Create(ctx context.Context, newStudio models.Studio) (*models.Studio, error)
}
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.
func EnsureStudioNameUnique(ctx context.Context, id int, name string, qb Queryer) error {
// ensure name is unique
sameNameStudio, err := ByName(ctx, qb, name)
if err != nil {
return err
}
if sameNameStudio != nil && id != sameNameStudio.ID {
return &NameExistsError{
Name: name,
}
}
// query by alias
sameNameStudio, err = ByAlias(ctx, qb, name)
if err != nil {
return err
}
if sameNameStudio != nil && id != sameNameStudio.ID {
return &NameUsedByAliasError{
Name: name,
OtherStudio: sameNameStudio.Name.String,
}
}
return nil
}
func EnsureAliasesUnique(ctx context.Context, id int, aliases []string, qb Queryer) error {
for _, a := range aliases {
if err := EnsureStudioNameUnique(ctx, id, a, qb); err != nil {
return err
}
}
return nil
}