From c4587b80aff3018e41ffc1276807a6763d75d2fd Mon Sep 17 00:00:00 2001 From: Philipp Wolfer Date: Sun, 10 Dec 2023 14:22:38 +0100 Subject: [PATCH] Introduce models.Entity type --- internal/backends/backends_test.go | 2 +- internal/cli/transfer.go | 4 ++-- internal/models/models.go | 6 ++++++ internal/storage/database.go | 5 +++-- internal/storage/database_test.go | 3 ++- internal/storage/models.go | 7 ++++--- 6 files changed, 18 insertions(+), 9 deletions(-) diff --git a/internal/backends/backends_test.go b/internal/backends/backends_test.go index ee71e6e..4d758eb 100644 --- a/internal/backends/backends_test.go +++ b/internal/backends/backends_test.go @@ -76,7 +76,7 @@ func TestGetBackends(t *testing.T) { } // If we got here the "dump" backend was not included - t.Errorf("GetBackends() did not return expected bacend \"dump\"") + t.Errorf("GetBackends() did not return expected backend \"dump\"") } func TestImplementsInterfaces(t *testing.T) { diff --git a/internal/cli/transfer.go b/internal/cli/transfer.go index 3816d96..5683bb6 100644 --- a/internal/cli/transfer.go +++ b/internal/cli/transfer.go @@ -38,7 +38,7 @@ func NewTransferCmd[ ]( cmd *cobra.Command, db *storage.Database, - entity string, + entity models.Entity, source string, target string, ) (TransferCmd[E, I, R], error) { @@ -57,7 +57,7 @@ func NewTransferCmd[ type TransferCmd[E models.Backend, I models.ImportBackend, R models.ListensResult | models.LovesResult] struct { cmd *cobra.Command db *storage.Database - entity string + entity models.Entity sourceName string targetName string ExpBackend E diff --git a/internal/models/models.go b/internal/models/models.go index 086b922..01830cb 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -27,6 +27,12 @@ import ( ) type MBID string +type Entity string + +const ( + Listens Entity = "listens" + Loves Entity = "loves" +) type AdditionalInfo map[string]any diff --git a/internal/storage/database.go b/internal/storage/database.go index 1548feb..d31a176 100644 --- a/internal/storage/database.go +++ b/internal/storage/database.go @@ -24,6 +24,7 @@ import ( "time" "github.com/glebarez/sqlite" + "go.uploadedlobster.com/scotty/internal/models" "golang.org/x/oauth2" "gorm.io/datatypes" "gorm.io/gorm" @@ -54,7 +55,7 @@ func New(dsn string) (db Database, err error) { return } -func (db Database) GetImportTimestamp(source string, target string, entity string) (time.Time, error) { +func (db Database) GetImportTimestamp(source string, target string, entity models.Entity) (time.Time, error) { result := ImportTimestamp{ SourceService: source, TargetService: target, @@ -64,7 +65,7 @@ func (db Database) GetImportTimestamp(source string, target string, entity strin return result.Timestamp, err } -func (db Database) SetImportTimestamp(source string, target string, entity string, timestamp time.Time) error { +func (db Database) SetImportTimestamp(source string, target string, entity models.Entity, timestamp time.Time) error { entry := ImportTimestamp{ SourceService: source, TargetService: target, diff --git a/internal/storage/database_test.go b/internal/storage/database_test.go index cd95149..606d334 100644 --- a/internal/storage/database_test.go +++ b/internal/storage/database_test.go @@ -23,6 +23,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.uploadedlobster.com/scotty/internal/models" "go.uploadedlobster.com/scotty/internal/storage" "golang.org/x/oauth2" ) @@ -33,7 +34,7 @@ func TestTimestampUpdate(t *testing.T) { source := "maloja" target := "funkwhale" - entity := "loves" + entity := models.Loves timestamp, err := db.GetImportTimestamp(source, target, entity) require.NoError(t, err) assert.Equal(t, time.Time{}, timestamp) diff --git a/internal/storage/models.go b/internal/storage/models.go index 9df85db..4b40e85 100644 --- a/internal/storage/models.go +++ b/internal/storage/models.go @@ -20,13 +20,14 @@ package storage import ( "time" + "go.uploadedlobster.com/scotty/internal/models" "gorm.io/datatypes" ) type ImportTimestamp struct { - SourceService string `gorm:"primaryKey"` - TargetService string `gorm:"primaryKey"` - Entity string `gorm:"primaryKey"` + SourceService string `gorm:"primaryKey"` + TargetService string `gorm:"primaryKey"` + Entity models.Entity `gorm:"primaryKey"` CreatedAt time.Time UpdatedAt time.Time Timestamp time.Time `gorm:"default:'1970-01-01T00:00:00'"`