99 "github.com/jackc/pgx/v5/pgtype"
1010 "github.com/zeusWPI/scc/internal/database/model"
1111 "github.com/zeusWPI/scc/internal/database/sqlc"
12- "github.com/zeusWPI/scc/pkg/utils"
1312)
1413
1514type Song struct {
@@ -22,63 +21,199 @@ func (r *Repository) NewSong() *Song {
2221 }
2322}
2423
25- func (s * Song ) GetBySpotify (ctx context.Context , spotifyID string ) (* model.Song , error ) {
26- song , err := s .repo .queries (ctx ).SongGetBySpotify (ctx , spotifyID )
24+ func (s * Song ) GetLastPopulated (ctx context.Context ) (* model.Song , error ) {
25+ last , err := s .repo .queries (ctx ).SongGetLastPopulated (ctx )
2726 if err != nil {
2827 if errors .Is (err , sql .ErrNoRows ) {
2928 return nil , nil
3029 }
31- return nil , fmt .Errorf ("get song by spotify id %s | %w" , spotifyID , err )
30+ return nil , fmt .Errorf ("get last song populated %w" , err )
3231 }
3332
34- return model .SongModel (song ), nil
33+ song := model .SongModel (last [0 ].Song )
34+ song .PlayedAt = last [0 ].SongHistory .CreatedAt .Time
35+
36+ for _ , s := range last {
37+ song .Artists = append (song .Artists , * model .ArtistModel (s .SongArtist ))
38+ }
39+
40+ return song , nil
3541}
3642
37- func (s * Song ) GetArtistBySpotify (ctx context.Context , spotifyID string ) (* model.Artist , error ) {
38- artist , err := s .repo .queries (ctx ).SongArtistGetBySpotify (ctx , spotifyID )
43+ func (s * Song ) GetLast50 (ctx context.Context ) ([] * model.Song , error ) {
44+ lasts , err := s .repo .queries (ctx ).SongGetLast50 (ctx )
3945 if err != nil {
4046 if errors .Is (err , sql .ErrNoRows ) {
4147 return nil , nil
4248 }
43- return nil , fmt .Errorf ("get song artist by spotify id %s | %w" , spotifyID , err )
49+ return nil , fmt .Errorf ("get last 50 songs %w" , err )
4450 }
4551
46- return model .ArtistModel (artist ), nil
52+ songs := make ([]* model.Song , 0 , len (lasts ))
53+ for _ , last := range lasts {
54+ song := model .SongModel (last .Song )
55+ song .PlayCount = int (last .PlayCount )
56+
57+ songs = append (songs , song )
58+ }
59+
60+ return songs , nil
4761}
4862
49- func (s * Song ) GetGenreByGenre (ctx context.Context , genre string ) (* model.Genre , error ) {
50- genreDB , err := s .repo .queries (ctx ).SongGenreGetByGenre (ctx , genre )
63+ func (s * Song ) GetTopSongs (ctx context.Context ) ([] * model.Song , error ) {
64+ tops , err := s .repo .queries (ctx ).SongGetTop50 (ctx )
5165 if err != nil {
5266 if errors .Is (err , sql .ErrNoRows ) {
5367 return nil , nil
5468 }
55- return nil , fmt .Errorf ("get song genre by genre %s | %w" , genre , err )
69+ return nil , fmt .Errorf ("get top songs %w" , err )
5670 }
5771
58- return model .GenreModel (genreDB ), nil
72+ songs := make ([]* model.Song , 0 , len (tops ))
73+ for _ , top := range tops {
74+ song := model .SongModel (top .Song )
75+ song .PlayCount = int (top .PlayCount )
76+
77+ songs = append (songs , song )
78+ }
79+
80+ return songs , nil
81+ }
82+
83+ func (s * Song ) GetTopArtists (ctx context.Context ) ([]* model.Artist , error ) {
84+ tops , err := s .repo .queries (ctx ).SongArtistGetTop50 (ctx )
85+ if err != nil {
86+ if errors .Is (err , sql .ErrNoRows ) {
87+ return nil , nil
88+ }
89+ return nil , fmt .Errorf ("get top artists %w" , err )
90+ }
91+
92+ artists := make ([]* model.Artist , 0 , len (tops ))
93+ for _ , top := range tops {
94+ artist := model .ArtistModel (top .SongArtist )
95+ artist .PlayCount = int (top .PlayCount )
96+
97+ artists = append (artists , artist )
98+ }
99+
100+ return artists , nil
101+ }
102+
103+ func (s * Song ) GetTopGenres (ctx context.Context ) ([]* model.Genre , error ) {
104+ tops , err := s .repo .queries (ctx ).SongGenreGetTop50 (ctx )
105+ if err != nil {
106+ if errors .Is (err , sql .ErrNoRows ) {
107+ return nil , nil
108+ }
109+ return nil , fmt .Errorf ("get top genres %w" , err )
110+ }
111+
112+ genres := make ([]* model.Genre , 0 , len (tops ))
113+ for _ , top := range tops {
114+ genre := model .GenreModel (top .SongGenre )
115+ genre .PlayCount = int (top .PlayCount )
116+
117+ genres = append (genres , genre )
118+ }
119+
120+ return genres , nil
121+ }
122+
123+ func (s * Song ) GetTopSongsMonthly (ctx context.Context ) ([]* model.Song , error ) {
124+ tops , err := s .repo .queries (ctx ).SongGetTop50Monthly (ctx )
125+ if err != nil {
126+ if errors .Is (err , sql .ErrNoRows ) {
127+ return nil , nil
128+ }
129+ return nil , fmt .Errorf ("get top songs monthly %w" , err )
130+ }
131+
132+ songs := make ([]* model.Song , 0 , len (tops ))
133+ for _ , top := range tops {
134+ song := model .SongModel (top .Song )
135+ song .PlayCount = int (top .PlayCount )
136+
137+ songs = append (songs , song )
138+ }
139+
140+ return songs , nil
141+ }
142+
143+ func (s * Song ) GetTopArtistsMonthly (ctx context.Context ) ([]* model.Artist , error ) {
144+ tops , err := s .repo .queries (ctx ).SongArtistGetTop50Monthly (ctx )
145+ if err != nil {
146+ if errors .Is (err , sql .ErrNoRows ) {
147+ return nil , nil
148+ }
149+ return nil , fmt .Errorf ("get top artists montlhy %w" , err )
150+ }
151+
152+ artists := make ([]* model.Artist , 0 , len (tops ))
153+ for _ , top := range tops {
154+ artist := model .ArtistModel (top .SongArtist )
155+ artist .PlayCount = int (top .PlayCount )
156+
157+ artists = append (artists , artist )
158+ }
159+
160+ return artists , nil
161+ }
162+
163+ func (s * Song ) GetTopGenresMonthly (ctx context.Context ) ([]* model.Genre , error ) {
164+ tops , err := s .repo .queries (ctx ).SongGenreGetTop50Monthly (ctx )
165+ if err != nil {
166+ if errors .Is (err , sql .ErrNoRows ) {
167+ return nil , nil
168+ }
169+ return nil , fmt .Errorf ("get top genres monthly %w" , err )
170+ }
171+
172+ genres := make ([]* model.Genre , 0 , len (tops ))
173+ for _ , top := range tops {
174+ genre := model .GenreModel (top .SongGenre )
175+ genre .PlayCount = int (top .PlayCount )
176+
177+ genres = append (genres , genre )
178+ }
179+
180+ return genres , nil
59181}
60182
61- func (s * Song ) GetArtistsBySpotify (ctx context.Context , artists []model. Artist ) ([] * model.Artist , error ) {
62- artistsDB , err := s .repo .queries (ctx ).SongArtistGetBySpotifyIds (ctx , utils . SliceMap ( artists , func ( a model. Artist ) string { return a . SpotifyID }) )
183+ func (s * Song ) GetBySpotify (ctx context.Context , spotifyID string ) (* model.Song , error ) {
184+ song , err := s .repo .queries (ctx ).SongGetBySpotify (ctx , spotifyID )
63185 if err != nil {
64186 if errors .Is (err , sql .ErrNoRows ) {
65187 return nil , nil
66188 }
67- return nil , fmt .Errorf ("get artists by ids %+v | %w" , artists , err )
189+ return nil , fmt .Errorf ("get song by spotify id %s | %w" , spotifyID , err )
68190 }
69191
70- artistMap := make (map [int ]* model.Artist )
71- for _ , artist := range artistsDB {
72- a , ok := artistMap [int (artist .SongArtist .ID )]
73- if ! ok {
74- a = model .ArtistModel (artist .SongArtist )
75- artistMap [a .ID ] = a
192+ return model .SongModel (song ), nil
193+ }
194+
195+ func (s * Song ) GetArtistBySpotify (ctx context.Context , spotifyID string ) (* model.Artist , error ) {
196+ artist , err := s .repo .queries (ctx ).SongArtistGetBySpotify (ctx , spotifyID )
197+ if err != nil {
198+ if errors .Is (err , sql .ErrNoRows ) {
199+ return nil , nil
76200 }
201+ return nil , fmt .Errorf ("get song artist by spotify id %s | %w" , spotifyID , err )
202+ }
203+
204+ return model .ArtistModel (artist ), nil
205+ }
77206
78- a .Genres = append (a .Genres , * model .GenreModel (artist .SongGenre ))
207+ func (s * Song ) GetGenreByGenre (ctx context.Context , genre string ) (* model.Genre , error ) {
208+ genreDB , err := s .repo .queries (ctx ).SongGenreGetByGenre (ctx , genre )
209+ if err != nil {
210+ if errors .Is (err , sql .ErrNoRows ) {
211+ return nil , nil
212+ }
213+ return nil , fmt .Errorf ("get song genre by genre %s | %w" , genre , err )
79214 }
80215
81- return utils . MapValues ( artistMap ), nil
216+ return model . GenreModel ( genreDB ), nil
82217}
83218
84219func (s * Song ) Create (ctx context.Context , song * model.Song ) error {
0 commit comments