fix(scanner): fix records with album name same as artist

and never use db.Where() with a struct

gorm was seeing a query like

    db.Where(Album{Left: left, Right: right})

but if the `left` variable was empty, gorm couldn't differentiate it with an empty field in the struct
so it generated SQL that we weren't expected

like

    SELECT * FROM albums WHERE right=?

instead of

    SELECT * FROM albums WHERE left=? AND right=?

fixes #230
This commit is contained in:
sentriz
2022-09-09 13:05:53 +01:00
parent a11d6ab92d
commit fdbb28209b
5 changed files with 56 additions and 30 deletions

View File

@@ -72,22 +72,22 @@ func streamGetAudio(dbc *db.DB, podcastsPath string, user *db.User, id specid.ID
}
func streamUpdateStats(dbc *db.DB, userID, albumID int, playTime time.Time) error {
play := db.Play{
AlbumID: albumID,
UserID: userID,
}
var play db.Play
err := dbc.
Where(play).
Where("album_id=? AND user_id=?", albumID, userID).
First(&play).
Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return fmt.Errorf("find stat: %w", err)
}
play.AlbumID = albumID
play.UserID = userID
play.Count++ // for getAlbumList?type=frequent
if playTime.After(play.Time) {
play.Time = playTime // for getAlbumList?type=recent
}
if err := dbc.Save(&play).Error; err != nil {
return fmt.Errorf("save stat: %w", err)
}