Skip to content

Commit

Permalink
Always fill album's min_year if max_year is filled
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Apr 18, 2020
1 parent 4aeb63c commit 69dc4d9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
23 changes: 20 additions & 3 deletions persistence/album_repository.go
Expand Up @@ -2,6 +2,9 @@ package persistence

import (
"context"
"sort"
"strconv"
"strings"
"time"

. "github.com/Masterminds/squirrel"
Expand Down Expand Up @@ -108,12 +111,13 @@ func (r *albumRepository) Refresh(ids ...string) error {
CurrentId string
HasCoverArt bool
SongArtists string
Years string
}
var albums []refreshAlbum
sel := Select(`album_id as id, album as name, f.artist, f.album_artist, f.artist_id, f.album_artist_id,
f.compilation, f.genre, max(f.year) as max_year, min(f.year) as min_year, sum(f.duration) as duration,
count(*) as song_count, a.id as current_id, f.id as cover_art_id, f.path as cover_art_path,
group_concat(f.artist, ' ') as song_artists, f.has_cover_art`).
f.compilation, f.genre, max(f.year) as max_year, sum(f.duration) as duration,
count(*) as song_count, a.id as current_id, f.id as cover_art_id, f.path as cover_art_path, f.has_cover_art,
group_concat(f.artist, ' ') as song_artists, group_concat(f.year, ' ') as years`).
From("media_file f").
LeftJoin("album a on f.album_id = a.id").
Where(Eq{"f.album_id": ids}).GroupBy("album_id").OrderBy("f.id")
Expand All @@ -136,6 +140,7 @@ func (r *albumRepository) Refresh(ids ...string) error {
al.AlbumArtist = al.Artist
al.AlbumArtistID = al.ArtistID
}
al.MinYear = getMinYear(al.Years)
al.UpdatedAt = time.Now()
if al.CurrentId != "" {
toUpdate++
Expand All @@ -158,6 +163,18 @@ func (r *albumRepository) Refresh(ids ...string) error {
return err
}

func getMinYear(years string) int {
ys := strings.Fields(years)
sort.Strings(ys)
for _, y := range ys {
if y != "0" {
r, _ := strconv.Atoi(y)
return r
}
}
return 0
}

func (r *albumRepository) PurgeEmpty() error {
del := Delete(r.tableName).Where("id not in (select distinct(album_id) from media_file)")
c, err := r.executeSQL(del)
Expand Down
12 changes: 12 additions & 0 deletions persistence/album_repository_test.go
Expand Up @@ -73,4 +73,16 @@ var _ = Describe("AlbumRepository", func() {
})
})

Describe("getMinYear", func() {
It("returns 0 when there's no valid year", func() {
Expect(getMinYear("a b c")).To(Equal(0))
Expect(getMinYear("")).To(Equal(0))
})
It("returns 0 when all values are 0", func() {
Expect(getMinYear("0 0 0 ")).To(Equal(0))
})
It("returns the smallest value from the list", func() {
Expect(getMinYear("2000 0 1800")).To(Equal(1800))
})
})
})

0 comments on commit 69dc4d9

Please sign in to comment.