@@ -9,6 +9,7 @@ package db
9
9
import (
10
10
"path"
11
11
"path/filepath"
12
+ "sort"
12
13
"strings"
13
14
"time"
14
15
@@ -46,7 +47,7 @@ type Artist struct {
46
47
ID int `gorm:"primary_key"`
47
48
Name string `gorm:"not null; unique_index"`
48
49
NameUDec string `sql:"default: null"`
49
- Albums []* Album `gorm:"foreignkey:TagArtistID "`
50
+ Albums []* Album `gorm:"many2many:album_artists "`
50
51
AlbumCount int `sql:"-"`
51
52
Cover string `sql:"default: null"`
52
53
ArtistStar * ArtistStar
@@ -89,9 +90,7 @@ type Track struct {
89
90
Filename string `gorm:"not null; unique_index:idx_folder_filename" sql:"default: null"`
90
91
FilenameUDec string `sql:"default: null"`
91
92
Album * Album
92
- AlbumID int `gorm:"not null; unique_index:idx_folder_filename" sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
93
- Artist * Artist
94
- ArtistID int `gorm:"not null" sql:"default: null; type:int REFERENCES artists(id) ON DELETE CASCADE"`
93
+ AlbumID int `gorm:"not null; unique_index:idx_folder_filename" sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
95
94
Genres []* Genre `gorm:"many2many:track_genres"`
96
95
Size int `sql:"default: null"`
97
96
Length int `sql:"default: null"`
@@ -118,10 +117,6 @@ func (t *Track) AlbumSID() *specid.ID {
118
117
return & specid.ID {Type : specid .Album , Value : t .AlbumID }
119
118
}
120
119
121
- func (t * Track ) ArtistSID () * specid.ID {
122
- return & specid.ID {Type : specid .Artist , Value : t .ArtistID }
123
- }
124
-
125
120
func (t * Track ) Ext () string {
126
121
return filepath .Ext (t .Filename )
127
122
}
@@ -190,7 +185,7 @@ type Play struct {
190
185
AlbumID int `gorm:"not null; index" sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
191
186
Time time.Time `sql:"default: null"`
192
187
Count int
193
- Length int
188
+ Length int
194
189
}
195
190
196
191
type Album struct {
@@ -202,16 +197,15 @@ type Album struct {
202
197
RightPath string `gorm:"not null; unique_index:idx_album_abs_path" sql:"default: null"`
203
198
RightPathUDec string `sql:"default: null"`
204
199
Parent * Album
205
- ParentID int `sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
206
- RootDir string `gorm:"unique_index:idx_album_abs_path" sql:"default: null"`
207
- Genres []* Genre `gorm:"many2many:album_genres"`
208
- Cover string `sql:"default: null"`
209
- TagArtist * Artist
210
- TagArtistID int `gorm:"index" sql:"default: null; type:int REFERENCES artists(id) ON DELETE CASCADE"`
211
- TagTitle string `sql:"default: null"`
212
- TagTitleUDec string `sql:"default: null"`
213
- TagBrainzID string `sql:"default: null"`
214
- TagYear int `sql:"default: null"`
200
+ ParentID int `sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
201
+ RootDir string `gorm:"unique_index:idx_album_abs_path" sql:"default: null"`
202
+ Genres []* Genre `gorm:"many2many:album_genres"`
203
+ Cover string `sql:"default: null"`
204
+ Artists []* Artist `gorm:"many2many:album_artists"`
205
+ TagTitle string `sql:"default: null"`
206
+ TagTitleUDec string `sql:"default: null"`
207
+ TagBrainzID string `sql:"default: null"`
208
+ TagYear int `sql:"default: null"`
215
209
Tracks []* Track
216
210
ChildCount int `sql:"-"`
217
211
Duration int `sql:"-"`
@@ -243,6 +237,18 @@ func (a *Album) GenreStrings() []string {
243
237
return strs
244
238
}
245
239
240
+ func (a * Album ) ArtistsString () string {
241
+ var artists = append ([]* Artist (nil ), a .Artists ... )
242
+ sort .Slice (artists , func (i , j int ) bool {
243
+ return artists [i ].ID < artists [j ].ID
244
+ })
245
+ var names []string
246
+ for _ , artist := range artists {
247
+ names = append (names , artist .Name )
248
+ }
249
+ return strings .Join (names , " & " )
250
+ }
251
+
246
252
type PlayQueue struct {
247
253
ID int `gorm:"primary_key"`
248
254
CreatedAt time.Time
@@ -275,6 +281,13 @@ type TranscodePreference struct {
275
281
Profile string `gorm:"not null" sql:"default: null"`
276
282
}
277
283
284
+ type AlbumArtist struct {
285
+ Album * Album
286
+ AlbumID int `gorm:"not null; unique_index:idx_album_id_artist_id" sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
287
+ Artist * Artist
288
+ ArtistID int `gorm:"not null; unique_index:idx_album_id_artist_id" sql:"default: null; type:int REFERENCES artists(id) ON DELETE CASCADE"`
289
+ }
290
+
278
291
type TrackGenre struct {
279
292
Track * Track
280
293
TrackID int `gorm:"not null; unique_index:idx_track_id_genre_id" sql:"default: null; type:int REFERENCES tracks(id) ON DELETE CASCADE"`
0 commit comments