diff --git a/core/scrobbler/play_tracker_test.go b/core/scrobbler/play_tracker_test.go index 74b4be893..831b0ce0d 100644 --- a/core/scrobbler/play_tracker_test.go +++ b/core/scrobbler/play_tracker_test.go @@ -290,7 +290,7 @@ var _ = Describe("PlayTracker", func() { Expect(mockScrobble.RecordedScrobbles).To(HaveLen(1)) Expect(mockScrobble.RecordedScrobbles[0].MediaFileID).To(Equal("123")) Expect(mockScrobble.RecordedScrobbles[0].UserID).To(Equal("u-1")) - Expect(mockScrobble.RecordedScrobbles[0].SubmissionTime).To(Equal(ts)) + Expect(mockScrobble.RecordedScrobbles[0].SubmissionTime).To(Equal(ts.Unix())) }) It("does not record scrobble when history is disabled", func() { diff --git a/model/scrobble.go b/model/scrobble.go index 175d4c945..a8022fc16 100644 --- a/model/scrobble.go +++ b/model/scrobble.go @@ -3,10 +3,10 @@ package model import "time" type Scrobble struct { - ID int64 `structs:"id" json:"id"` - MediaFileID string `structs:"media_file_id" json:"mediaFileId"` - UserID string `json:"-"` - SubmissionTime time.Time `structs:"submission_time" json:"submissionTime"` + ID int64 `structs:"id" json:"id"` + MediaFileID string `structs:"media_file_id" json:"mediaFileId"` + UserID string `json:"-"` + SubmissionTime int64 `structs:"submission_time" json:"submissionTime"` } type ScrobbleRepository interface { diff --git a/persistence/persistence_suite_test.go b/persistence/persistence_suite_test.go index 7f7fed809..4f2fd7fe2 100644 --- a/persistence/persistence_suite_test.go +++ b/persistence/persistence_suite_test.go @@ -159,9 +159,9 @@ var ( ) var ( - firstScrobble = model.Scrobble{ID: 1, MediaFileID: "1001", UserID: "userid", SubmissionTime: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)} - secondScrobble = model.Scrobble{ID: 2, MediaFileID: "1003", UserID: "2222", SubmissionTime: time.Date(1970, 2, 1, 0, 0, 0, 0, time.UTC)} - thirdScrobble = model.Scrobble{ID: 3, MediaFileID: "1002", UserID: "userid", SubmissionTime: time.Date(1970, 3, 1, 0, 0, 0, 0, time.UTC)} + firstScrobble = model.Scrobble{ID: 1, MediaFileID: "1001", UserID: "userid", SubmissionTime: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC).Unix()} + secondScrobble = model.Scrobble{ID: 2, MediaFileID: "1003", UserID: "2222", SubmissionTime: time.Date(1970, 2, 1, 0, 0, 0, 0, time.UTC).Unix()} + thirdScrobble = model.Scrobble{ID: 3, MediaFileID: "1002", UserID: "userid", SubmissionTime: time.Date(1970, 3, 1, 0, 0, 0, 0, time.UTC).Unix()} scrobbles = model.Scrobbles{firstScrobble, secondScrobble, thirdScrobble} ) @@ -318,7 +318,7 @@ var _ = BeforeSuite(func() { _, err := scrobbleRepo.executeSQL(squirrel.Insert("scrobbles").SetMap(map[string]any{ "media_file_id": s.MediaFileID, "user_id": s.UserID, - "submission_time": s.SubmissionTime.Unix(), + "submission_time": s.SubmissionTime, })) if err != nil { panic(err) diff --git a/persistence/scrobble_repository.go b/persistence/scrobble_repository.go index 18715cdb4..7cc60ae23 100644 --- a/persistence/scrobble_repository.go +++ b/persistence/scrobble_repository.go @@ -7,7 +7,6 @@ import ( . "github.com/Masterminds/squirrel" "github.com/deluan/rest" "github.com/navidrome/navidrome/model" - "github.com/navidrome/navidrome/utils/slice" "github.com/pocketbase/dbx" ) @@ -15,28 +14,6 @@ type scrobbleRepository struct { sqlRepository } -type dbScrobble struct { - ID int64 `db:"id"` - MediaFileID string `db:"media_file_id"` - SubmissionTime int64 `db:"submission_time"` -} - -func (m dbScrobble) toScrobble() model.Scrobble { - return model.Scrobble{ - MediaFileID: m.MediaFileID, - ID: m.ID, - SubmissionTime: time.Unix(m.SubmissionTime, 0), - } -} - -type dbScrobbles []dbScrobble - -func (m dbScrobbles) toModels() model.Scrobbles { - return slice.Map(m, func(db dbScrobble) model.Scrobble { - return db.toScrobble() - }) -} - func fromTs(_ string, value any) Sqlizer { return GtOrEq{"scrobbles.submission_time": value} } @@ -90,23 +67,16 @@ func (r *scrobbleRepository) Count(options ...rest.QueryOptions) (int64, error) func (r *scrobbleRepository) Get(id string) (*model.Scrobble, error) { sel := r.baseQuery().Where(Eq{"id": id}) - var res dbScrobble + var res model.Scrobble err := r.queryOne(sel, &res) - if err != nil { - return nil, err - } - asModel := res.toScrobble() - return &asModel, err + return &res, err } func (r *scrobbleRepository) GetAll(options ...model.QueryOptions) (model.Scrobbles, error) { sel := r.baseQuery(options...) - var scrobbles dbScrobbles + var scrobbles model.Scrobbles err := r.queryAll(sel, &scrobbles) - if err != nil { - return nil, err - } - return scrobbles.toModels(), nil + return scrobbles, err } func (r *scrobbleRepository) Read(id string) (any, error) { diff --git a/persistence/scrobble_repository_test.go b/persistence/scrobble_repository_test.go index bf95ab3a2..e9103b127 100644 --- a/persistence/scrobble_repository_test.go +++ b/persistence/scrobble_repository_test.go @@ -106,19 +106,17 @@ var _ = Describe("ScrobbleRepository", func() { Expect(err).To(BeNil()) Expect(scrobble.ID).To(Equal(int64(1))) Expect(scrobble.MediaFileID).To(Equal("1001")) - Expect(scrobble.SubmissionTime).To(BeTemporally("==", firstScrobble.SubmissionTime)) + Expect(scrobble.SubmissionTime).To(Equal(firstScrobble.SubmissionTime)) }) It("does not return a scrobble that exists for another user", func() { - scrobble, err := repo.Get("2") - Expect(scrobble).To(BeNil()) + _, err := repo.Get("2") Expect(err).To(MatchError(model.ErrNotFound)) }) It("does not return a scrobble that does not exist", func() { - scrobble, err := repo.Get("444") - Expect(scrobble).To(BeNil()) + _, err := repo.Get("444") Expect(err).To(MatchError(model.ErrNotFound)) }) }) @@ -134,11 +132,11 @@ var _ = Describe("ScrobbleRepository", func() { Expect(scrobbles[0].ID).To(Equal(int64(3))) Expect(scrobbles[0].MediaFileID).To(Equal("1002")) - Expect(scrobbles[0].SubmissionTime).To(BeTemporally("==", thirdScrobble.SubmissionTime)) + Expect(scrobbles[0].SubmissionTime).To(Equal(thirdScrobble.SubmissionTime)) Expect(scrobbles[1].ID).To(Equal(int64(1))) Expect(scrobbles[1].MediaFileID).To(Equal("1001")) - Expect(scrobbles[1].SubmissionTime).To(BeTemporally("==", firstScrobble.SubmissionTime)) + Expect(scrobbles[1].SubmissionTime).To(Equal(firstScrobble.SubmissionTime)) }) It("returns scrobbles in a range", func() { @@ -150,7 +148,7 @@ var _ = Describe("ScrobbleRepository", func() { Expect(scrobbles[0].ID).To(Equal(int64(3))) Expect(scrobbles[0].MediaFileID).To(Equal("1002")) - Expect(scrobbles[0].SubmissionTime).To(BeTemporally("==", thirdScrobble.SubmissionTime)) + Expect(scrobbles[0].SubmissionTime).To(Equal(thirdScrobble.SubmissionTime)) }) }) }) @@ -177,18 +175,16 @@ var _ = Describe("ScrobbleRepository", func() { Expect(err).To(BeNil()) Expect(scrobble.ID).To(Equal(int64(2))) Expect(scrobble.MediaFileID).To(Equal("1003")) - Expect(scrobble.SubmissionTime).To(BeTemporally("==", secondScrobble.SubmissionTime)) + Expect(scrobble.SubmissionTime).To(Equal(secondScrobble.SubmissionTime)) }) It("does not return a scrobble that exists for another user", func() { - scrobble, err := repo.Get("1") - Expect(scrobble).To(BeNil()) + _, err := repo.Get("1") Expect(err).To(MatchError(model.ErrNotFound)) }) It("does not return a scrobble that does not exist", func() { - scrobble, err := repo.Get("444") - Expect(scrobble).To(BeNil()) + _, err := repo.Get("444") Expect(err).To(MatchError(model.ErrNotFound)) }) }) @@ -204,7 +200,7 @@ var _ = Describe("ScrobbleRepository", func() { Expect(scrobbles[0].ID).To(Equal(int64(2))) Expect(scrobbles[0].MediaFileID).To(Equal("1003")) - Expect(scrobbles[0].SubmissionTime).To(BeTemporally("==", secondScrobble.SubmissionTime)) + Expect(scrobbles[0].SubmissionTime).To(Equal(secondScrobble.SubmissionTime)) }) It("returns scrobbles in a range", func() { @@ -216,7 +212,7 @@ var _ = Describe("ScrobbleRepository", func() { Expect(scrobbles[0].ID).To(Equal(int64(2))) Expect(scrobbles[0].MediaFileID).To(Equal("1003")) - Expect(scrobbles[0].SubmissionTime).To(BeTemporally("==", secondScrobble.SubmissionTime)) + Expect(scrobbles[0].SubmissionTime).To(Equal(secondScrobble.SubmissionTime)) }) }) }) diff --git a/tests/mock_scrobble_repo.go b/tests/mock_scrobble_repo.go index 44d9728d8..d6d88d221 100644 --- a/tests/mock_scrobble_repo.go +++ b/tests/mock_scrobble_repo.go @@ -37,7 +37,7 @@ func (m *MockScrobbleRepo) RecordScrobble(fileID string, submissionTime time.Tim m.RecordedScrobbles = append(m.RecordedScrobbles, model.Scrobble{ MediaFileID: fileID, UserID: user.ID, - SubmissionTime: submissionTime, + SubmissionTime: submissionTime.Unix(), }) return nil }