Files
stash/internal/identify/studio.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

54 lines
1.4 KiB
Go

package identify
import (
"context"
"database/sql"
"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) (*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) (*int64, error) {
created, err := w.Create(ctx, scrapedToStudioInput(studio))
if err != nil {
return nil, fmt.Errorf("error creating studio: %w", err)
}
if endpoint != "" && studio.RemoteSiteID != nil {
if err := w.UpdateStashIDs(ctx, created.ID, []models.StashID{
{
Endpoint: endpoint,
StashID: *studio.RemoteSiteID,
},
}); err != nil {
return nil, fmt.Errorf("error setting studio stash id: %w", err)
}
}
createdID := int64(created.ID)
return &createdID, nil
}
func scrapedToStudioInput(studio *models.ScrapedStudio) models.Studio {
currentTime := time.Now()
ret := models.Studio{
Name: sql.NullString{String: studio.Name, Valid: true},
Checksum: md5.FromString(studio.Name),
CreatedAt: models.SQLiteTimestamp{Timestamp: currentTime},
UpdatedAt: models.SQLiteTimestamp{Timestamp: currentTime},
}
if studio.URL != nil {
ret.URL = sql.NullString{String: *studio.URL, Valid: true}
}
return ret
}