-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
479 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
app/application/spotlike/get_all_tracks_by_album_id_usecase.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package spotlike | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/zmb3/spotify/v2" | ||
) | ||
|
||
// getAllTracksByAlbumIdUseCase is a struct that contains the use case of getting for an artist. | ||
type getAllTracksByAlbumIdUseCase struct { | ||
Client *spotify.Client | ||
} | ||
|
||
// NewGetAllTracksByAlbumIdUseCase returns a new instance of the getAllTracksUseCase struct. | ||
func NewGetAllTracksByAlbumIdUseCase(client *spotify.Client) *getAllTracksByAlbumIdUseCase { | ||
return &getAllTracksByAlbumIdUseCase{ | ||
Client: client, | ||
} | ||
} | ||
|
||
// GetAllTracksByAlbumIdUseCaseOutputDto is a DTO struct that contains the output data of the getAllTracksUseCase. | ||
type GetAllTracksByAlbumIdUseCaseOutputDto struct { | ||
ID string | ||
Artists string | ||
Album string | ||
Name string | ||
TrackNumber string | ||
ReleaseDate time.Time | ||
} | ||
|
||
// Run returns the get result of the artist. | ||
func (uc *getAllTracksByAlbumIdUseCase) Run(id string) ([]*GetAllTracksByAlbumIdUseCaseOutputDto, error) { | ||
var getAllTracksByAlbumIdUseCaseOutputDtos []*GetAllTracksByAlbumIdUseCaseOutputDto | ||
allTracks, err := uc.Client.GetAlbumTracks(context.Background(), spotify.ID(id), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
sort.Slice(allTracks.Tracks, func(i, j int) bool { | ||
return allTracks.Tracks[i].TrackNumber < allTracks.Tracks[j].TrackNumber | ||
}) | ||
|
||
for _, track := range allTracks.Tracks { | ||
getAllTracksByAlbumIdUseCaseOutputDto := &GetAllTracksByAlbumIdUseCaseOutputDto{ | ||
ID: string(track.ID), | ||
Artists: uc.combineArtistNames(track.Artists), | ||
Album: track.Album.Name, | ||
Name: track.Name, | ||
TrackNumber: strconv.Itoa(track.TrackNumber), | ||
ReleaseDate: track.Album.ReleaseDateTime(), | ||
} | ||
getAllTracksByAlbumIdUseCaseOutputDtos = append(getAllTracksByAlbumIdUseCaseOutputDtos, getAllTracksByAlbumIdUseCaseOutputDto) | ||
} | ||
|
||
return getAllTracksByAlbumIdUseCaseOutputDtos, nil | ||
} | ||
|
||
func (uc *getAllTracksByAlbumIdUseCase) combineArtistNames(artists []spotify.SimpleArtist) string { | ||
var artistNames string | ||
for index, artist := range artists { | ||
artistNames += artist.Name | ||
if index+1 != len(artists) { | ||
// if the arg is not the last one, add a comma and a space | ||
artistNames += ", " | ||
} | ||
} | ||
|
||
return artistNames | ||
} |
81 changes: 81 additions & 0 deletions
81
app/application/spotlike/get_all_tracks_by_artist_id_usecase.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package spotlike | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/zmb3/spotify/v2" | ||
) | ||
|
||
// getAllTracksByArtistIdUseCase is a struct that contains the use case of getting for an artist. | ||
type getAllTracksByArtistIdUseCase struct { | ||
Client *spotify.Client | ||
} | ||
|
||
// NewGetAllTracksByArtistIdUseCase returns a new instance of the getAllTracksUseCase struct. | ||
func NewGetAllTracksByArtistIdUseCase(client *spotify.Client) *getAllTracksByArtistIdUseCase { | ||
return &getAllTracksByArtistIdUseCase{ | ||
Client: client, | ||
} | ||
} | ||
|
||
// GetAllTracksByArtistIdUseCaseOutputDto is a DTO struct that contains the output data of the getAllTracksUseCase. | ||
type GetAllTracksByArtistIdUseCaseOutputDto struct { | ||
ID string | ||
Artists string | ||
Album string | ||
Name string | ||
TrackNumber string | ||
ReleaseDate time.Time | ||
} | ||
|
||
// Run returns the get result of the artist. | ||
func (uc *getAllTracksByArtistIdUseCase) Run(id string) ([]*GetAllTracksByAlbumIdUseCaseOutputDto, error) { | ||
allAlbums, err := uc.Client.GetArtistAlbums(context.Background(), spotify.ID(id), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
sort.Slice(allAlbums.Albums, func(i, j int) bool { | ||
return allAlbums.Albums[i].ReleaseDateTime().Before(allAlbums.Albums[j].ReleaseDateTime()) | ||
}) | ||
|
||
var getAllTracksByArtistIdUseCaseOutputDtos []*GetAllTracksByAlbumIdUseCaseOutputDto | ||
for _, album := range allAlbums.Albums { | ||
allTracks, err := uc.Client.GetAlbumTracks(context.Background(), album.ID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
sort.Slice(allTracks.Tracks, func(i, j int) bool { | ||
return allTracks.Tracks[i].TrackNumber < allTracks.Tracks[j].TrackNumber | ||
}) | ||
|
||
for _, track := range allTracks.Tracks { | ||
getAllTracksByArtistIdUseCaseOutputDto := &GetAllTracksByAlbumIdUseCaseOutputDto{ | ||
ID: string(track.ID), | ||
Artists: uc.combineArtistNames(track.Artists), | ||
Album: album.Name, | ||
Name: track.Name, | ||
TrackNumber: strconv.Itoa(track.TrackNumber), | ||
ReleaseDate: track.Album.ReleaseDateTime(), | ||
} | ||
getAllTracksByArtistIdUseCaseOutputDtos = append(getAllTracksByArtistIdUseCaseOutputDtos, getAllTracksByArtistIdUseCaseOutputDto) | ||
} | ||
} | ||
|
||
return getAllTracksByArtistIdUseCaseOutputDtos, nil | ||
} | ||
|
||
func (uc *getAllTracksByArtistIdUseCase) combineArtistNames(artists []spotify.SimpleArtist) string { | ||
var artistNames string | ||
for index, artist := range artists { | ||
artistNames += artist.Name | ||
if index+1 != len(artists) { | ||
// if the arg is not the last one, add a comma and a space | ||
artistNames += ", " | ||
} | ||
} | ||
|
||
return artistNames | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package spotlike | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/zmb3/spotify/v2" | ||
) | ||
|
||
// likeTrackUseCase is a struct that contains the use case of likeing for an artist. | ||
type likeTrackUseCase struct { | ||
Client *spotify.Client | ||
} | ||
|
||
// NewLikeTrackUseCase returns a new instance of the likeTrackUseCase struct. | ||
func NewLikeTrackUseCase(client *spotify.Client) *likeTrackUseCase { | ||
return &likeTrackUseCase{ | ||
Client: client, | ||
} | ||
} | ||
|
||
// Run returns the like result of the artist. | ||
func (uc *likeTrackUseCase) Run(id string) error { | ||
return uc.Client.AddTracksToLibrary(context.Background(), spotify.ID(id)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.