Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
yanosea committed Feb 28, 2025
1 parent 08dde4e commit 25f3ce3
Show file tree
Hide file tree
Showing 13 changed files with 479 additions and 33 deletions.
26 changes: 13 additions & 13 deletions app/application/spotlike/get_all_albums_by_artist_id_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@ import (
"github.com/zmb3/spotify/v2"
)

// getAllAlbumsUseCase is a struct that contains the use case of getting for an artist.
type getAllAlbumsUseCase struct {
// getAllAlbumsByArtistIdUseCase is a struct that contains the use case of getting for an artist.
type getAllAlbumsByArtistIdUseCase struct {
Client *spotify.Client
}

// NewgetAllAlbumsUseCase returns a new instance of the getAllAlbumsUseCase struct.
func NewgetAllAlbumsUseCase(client *spotify.Client) *getAllAlbumsUseCase {
return &getAllAlbumsUseCase{
// NewGetAllAlbumsByArtistIdUseCase returns a new instance of the getAllAlbumsUseCase struct.
func NewGetAllAlbumsByArtistIdUseCase(client *spotify.Client) *getAllAlbumsByArtistIdUseCase {
return &getAllAlbumsByArtistIdUseCase{
Client: client,
}
}

// GetAllAlbumsUseCaseOutputDto is a DTO struct that contains the output data of the getAllAlbumsUseCase.
type GetAllAlbumsUseCaseOutputDto struct {
// GetAllAlbumsByArtistIdUseCaseOutputDto is a DTO struct that contains the output data of the getAllAlbumsUseCase.
type GetAllAlbumsByArtistIdUseCaseOutputDto struct {
ID string
Artists string
Name string
ReleaseDate time.Time
}

// Run returns the get result of the artist.
func (uc *getAllAlbumsUseCase) Run(id string) ([]*GetAllAlbumsUseCaseOutputDto, error) {
func (uc *getAllAlbumsByArtistIdUseCase) Run(id string) ([]*GetAllAlbumsByArtistIdUseCaseOutputDto, error) {
allAlbums, err := uc.Client.GetArtistAlbums(context.Background(), spotify.ID(id), nil)
if err != nil {
return nil, err
Expand All @@ -38,21 +38,21 @@ func (uc *getAllAlbumsUseCase) Run(id string) ([]*GetAllAlbumsUseCaseOutputDto,
return allAlbums.Albums[i].ReleaseDateTime().Before(allAlbums.Albums[j].ReleaseDateTime())
})

var getAllAlbumsUseCaseOutputDtos []*GetAllAlbumsUseCaseOutputDto
var getAllAlbumsByArtistIdUseCaseOutputDtos []*GetAllAlbumsByArtistIdUseCaseOutputDto
for _, album := range allAlbums.Albums {
getAllAlbumsUseCaseOutputDto := &GetAllAlbumsUseCaseOutputDto{
getAllAlbumsUseCaseOutputDto := &GetAllAlbumsByArtistIdUseCaseOutputDto{
ID: string(album.ID),
Artists: uc.combineArtistNames(album.Artists),
Name: album.Name,
ReleaseDate: album.ReleaseDateTime(),
}
getAllAlbumsUseCaseOutputDtos = append(getAllAlbumsUseCaseOutputDtos, getAllAlbumsUseCaseOutputDto)
getAllAlbumsByArtistIdUseCaseOutputDtos = append(getAllAlbumsByArtistIdUseCaseOutputDtos, getAllAlbumsUseCaseOutputDto)
}

return getAllAlbumsUseCaseOutputDtos, nil
return getAllAlbumsByArtistIdUseCaseOutputDtos, nil
}

func (uc *getAllAlbumsUseCase) combineArtistNames(artists []spotify.SimpleArtist) string {
func (uc *getAllAlbumsByArtistIdUseCase) combineArtistNames(artists []spotify.SimpleArtist) string {
var artistNames string
for index, artist := range artists {
artistNames += artist.Name
Expand Down
71 changes: 71 additions & 0 deletions app/application/spotlike/get_all_tracks_by_album_id_usecase.go
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 app/application/spotlike/get_all_tracks_by_artist_id_usecase.go
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
}
7 changes: 5 additions & 2 deletions app/application/spotlike/get_content_type_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package spotlike
import (
"context"
"errors"
"strconv"
"time"

"github.com/zmb3/spotify/v2"
Expand All @@ -13,8 +14,8 @@ type getContentTypeUseCase struct {
Client *spotify.Client
}

// NewgetContentTypeUseCase returns a new instance of the getContentTypeUseCase struct.
func NewgetContentTypeUseCase(client *spotify.Client) *getContentTypeUseCase {
// NewGetContentTypeUseCase returns a new instance of the getContentTypeUseCase struct.
func NewGetContentTypeUseCase(client *spotify.Client) *getContentTypeUseCase {
return &getContentTypeUseCase{
Client: client,
}
Expand All @@ -27,6 +28,7 @@ type GetContentTypeUseCaseOutputDto struct {
Name string
Artists string
Album string
TrackNumber string
ReleaseDate time.Time
}

Expand Down Expand Up @@ -77,6 +79,7 @@ func (uc *getContentTypeUseCase) Run(id string) (*GetContentTypeUseCaseOutputDto
Name: resultTrack.Name,
Artists: uc.combineArtistNames(resultTrack.Artists),
Album: resultTrack.Album.Name,
TrackNumber: strconv.Itoa(resultTrack.TrackNumber),
ReleaseDate: resultTrack.Album.ReleaseDateTime(),
}, nil
}
Expand Down
24 changes: 24 additions & 0 deletions app/application/spotlike/like_track_usecase.go
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))
}
3 changes: 3 additions & 0 deletions app/application/spotlike/search_track_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package spotlike

import (
"context"
"strconv"
"time"

"github.com/zmb3/spotify/v2"
Expand All @@ -25,6 +26,7 @@ type SearchTrackUseCaseOutputDto struct {
Artists string
Album string
Name string
TrackNumber string
ReleaseDate time.Time
}

Expand All @@ -44,6 +46,7 @@ func (uc *searchTrackUseCase) Run(keywords []string, max int) ([]*SearchTrackUse
Artists: uc.combineArtistNames(t.Artists),
Album: t.Album.Name,
Name: t.Name,
TrackNumber: strconv.Itoa(t.TrackNumber),
ReleaseDate: t.Album.ReleaseDateTime(),
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,7 @@ Available Subommands:
Flags:
-h, --help 🤝 help for completion
Use "spotlike completion [command] --help" for more information about a command.
`
)
22 changes: 11 additions & 11 deletions app/presentation/cli/spotlike/command/spotlike/like/album.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewAlbumCommand(
cmd.Flags().StringVarP(
&LikeAlbumOps.Artist,
"artist",
"a",
"A",
"",
"🆔 an ID of the artist to like all albums released by the artist",
)
Expand All @@ -51,7 +51,7 @@ func NewAlbumCommand(
"no-confirm",
"",
false,
"🚫 do not confirm before liking the artist",
"🚫 do not confirm before liking the album",
)
cmd.Flags().StringVarP(
&LikeAlbumOps.Format,
Expand Down Expand Up @@ -82,7 +82,7 @@ func runAlbum(output *string, args []string, conf *config.SpotlikeCliConfig) err
return err
}

gctuc := spotlikeApp.NewgetContentTypeUseCase(client)
gctuc := spotlikeApp.NewGetContentTypeUseCase(client)
var albums []*spotlikeApp.GetContentTypeUseCaseOutputDto
if LikeAlbumOps.Artist != "" {
gctucDto, err := gctuc.Run(LikeAlbumOps.Artist)
Expand All @@ -96,7 +96,7 @@ func runAlbum(output *string, args []string, conf *config.SpotlikeCliConfig) err
return nil
}

gaauc := spotlikeApp.NewgetAllAlbumsUseCase(client)
gaauc := spotlikeApp.NewGetAllAlbumsByArtistIdUseCase(client)
allAlbums, err := gaauc.Run(LikeAlbumOps.Artist)
if err != nil {
return err
Expand Down Expand Up @@ -138,7 +138,7 @@ func runAlbum(output *string, args []string, conf *config.SpotlikeCliConfig) err
return err
}
if alreadyLiked {
presenter.Print(os.Stdout, formatter.Yellow("⚡ Album "+album.Name+" ("+album.ID+") "+"is already liked. skipping..."))
presenter.Print(os.Stdout, formatter.Blue("⏩ Album "+album.Name+" ("+album.ID+")"+" by "+album.Artists+" is already liked. skipping..."))
continue
}

Expand Down Expand Up @@ -176,14 +176,14 @@ func runAlbum(output *string, args []string, conf *config.SpotlikeCliConfig) err
}

const (
likeAlbumHelpTemplate = `🤍📀 Like albums on Spotify by ID.
likeAlbumHelpTemplate = `🤍💿 Like albums on Spotify by ID.
You can like albums on Spotify by ID.
You can like tracks on Spotify by ID.
Before using this command,
you need to get the ID of the album you want to like by using the search command.
you need to get the ID of the track you want to like by using the search command.
Also, you can like all albums of the artist by specifying the ID of the artist with artist flag.
Also, you can like all albums released by the artist with specifying the ID of the artist with artist flag.
If you specify artist flag, the arguments would be ignored.
` + likeAlbumUsageTemplate
Expand All @@ -193,8 +193,8 @@ If you specify artist flag, the arguments would be ignored.
spotlike like a [flags] [arguments]
Flags:
-a, --artist 🆔 an ID of the artist to like all albums released by the artist
--no-confirm 🚫 do not confirm before liking the artist
-A, --artist 🆔 an ID of the artist to like all albums released by the artist
--no-confirm 🚫 do not confirm before liking the album
-f, --format 📝 format of the output (default "table", e.g: "plain")
Arguments:
Expand Down
Loading

0 comments on commit 25f3ce3

Please sign in to comment.