mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 20:34:37 +03:00
* Remove ID from PerformerPartial * Separate studio model from sqlite model * Separate movie model from sqlite model * Separate tag model from sqlite model * Separate saved filter model from sqlite model * Separate scene marker model from sqlite model * Separate gallery chapter model from sqlite model * Move ErrNoRows checks into sqlite, improve empty result error messages * Move SQLiteDate and SQLiteTimestamp to sqlite * Use changesetTranslator everywhere, refactor for consistency * Make PerformerStore.DestroyImage private * Fix rating on movie create
72 lines
1.5 KiB
Go
72 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) 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,
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|