stash/internal/identify/studio.go
DingDongSoLong4 1c13c9e1b1
SQLite model refactoring (#3791)
* 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
2023-06-15 12:46:09 +10:00

52 lines
1.2 KiB
Go

package identify
import (
"context"
"fmt"
"time"
"github.com/stashapp/stash/pkg/hash/md5"
"github.com/stashapp/stash/pkg/models"
)
type StudioCreator interface {
Create(ctx context.Context, newStudio *models.Studio) error
UpdateStashIDs(ctx context.Context, studioID int, stashIDs []models.StashID) error
}
func createMissingStudio(ctx context.Context, endpoint string, w StudioCreator, studio *models.ScrapedStudio) (*int, error) {
studioInput := scrapedToStudioInput(studio)
err := w.Create(ctx, &studioInput)
if err != nil {
return nil, fmt.Errorf("error creating studio: %w", err)
}
if endpoint != "" && studio.RemoteSiteID != nil {
if err := w.UpdateStashIDs(ctx, studioInput.ID, []models.StashID{
{
Endpoint: endpoint,
StashID: *studio.RemoteSiteID,
},
}); err != nil {
return nil, fmt.Errorf("error setting studio stash id: %w", err)
}
}
return &studioInput.ID, nil
}
func scrapedToStudioInput(studio *models.ScrapedStudio) models.Studio {
currentTime := time.Now()
ret := models.Studio{
Name: studio.Name,
Checksum: md5.FromString(studio.Name),
CreatedAt: currentTime,
UpdatedAt: currentTime,
}
if studio.URL != nil {
ret.URL = *studio.URL
}
return ret
}