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 543398e commit c762eaf
Show file tree
Hide file tree
Showing 6 changed files with 374 additions and 13 deletions.
3 changes: 2 additions & 1 deletion app/domain/spotify/album/album_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (

// AlbumRepository is an interface that provides the repository for the album on Spotify.
type AlbumRepository interface {
FindByArtistId(ctx context.Context, id spotify.ID) ([]*Album, error)
FindById(ctx context.Context, id spotify.ID) (*Album, error)
FindByName(ctx context.Context, name string) (*Album, error)
FindByNameLimit(ctx context.Context, name string, limit int) ([]*Album, error)
IsLiked(ctx context.Context, id spotify.ID) (bool, error)
Like(ctx context.Context, id spotify.ID) error
Unlike(ctx context.Context, id spotify.ID) error
Expand Down
2 changes: 1 addition & 1 deletion app/domain/spotify/artist/artist_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// ArtistRepository is an interface that provides the repository for the artist on Spotify.
type ArtistRepository interface {
FindById(ctx context.Context, id spotify.ID) (*Artist, error)
FindByNameLimit(ctx context.Context, name string, limit int) (*Artist, error)
FindByNameLimit(ctx context.Context, name string, limit int) ([]*Artist, error)
IsLiked(ctx context.Context, id spotify.ID) (bool, error)
Like(ctx context.Context, id spotify.ID) error
Unlike(ctx context.Context, id spotify.ID) error
Expand Down
4 changes: 3 additions & 1 deletion app/domain/spotify/track/track_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (

// TrackRepository is an interface that provides the repository for the track on Spotify.
type TrackRepository interface {
FindByAlbumId(ctx context.Context, id spotify.ID) ([]*Track, error)
FindByArtistId(ctx context.Context, id spotify.ID) ([]*Track, error)
FindById(ctx context.Context, id spotify.ID) (*Track, error)
FindByName(ctx context.Context, name string) (*Track, error)
FindByNameLimit(ctx context.Context, name string, limit int) ([]*Track, error)
IsLiked(ctx context.Context, id spotify.ID) (bool, error)
Like(ctx context.Context, id spotify.ID) error
Unlike(ctx context.Context, id spotify.ID) error
Expand Down
156 changes: 156 additions & 0 deletions app/infrastructure/spotify/repository/album_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package repository

import (
"context"

"github.com/yanosea/spotlike/app/domain/spotify/album"
"github.com/yanosea/spotlike/app/infrastructure/spotify/api"
"github.com/zmb3/spotify/v2"
)

// albumRepository is a struct that implements the AlbumRepository interface.
type albumRepository struct {
clientManager api.ClientManager
}

// NewAlbumRepository returns a new instance of the albumRepository struct.
func NewAlbumRepository() album.AlbumRepository {
return &albumRepository{
clientManager: api.NewClientManager(),
}
}

// FindByArtistId returns the albums by the artist ID.
func (r *albumRepository) FindByArtistId(ctx context.Context, id spotify.ID) ([]*album.Album, error) {
c, err := r.clientManager.GetClient()
if err != nil {
return nil, err
}

client, err := c.Open()
if err != nil {
return nil, err
}

result, err := client.GetArtistAlbums(ctx, id)
if err != nil {
return nil, err
}

var albums []*album.Album
for _, a := range result.Albums {
albums = append(albums, &album.Album{
ID: a.ID,
Name: a.Name,
Artists: a.Artists,
ReleaseDate: a.ReleaseDate,
})
}

return albums, nil
}

// FindById returns the album by the ID.
func (r *albumRepository) FindById(ctx context.Context, id spotify.ID) (*album.Album, error) {
c, err := r.clientManager.GetClient()
if err != nil {
return nil, err
}

client, err := c.Open()
if err != nil {
return nil, err
}

album, err := client.GetAlbum(ctx, id)
if err != nil {
return nil, err
}

return &album.Album{
ID: album.ID,
Name: album.Name,
Artsts: album.Artists,
ReleaseDate: album.ReleaseDate,
}, nil
}

// FindByNameLimit returns the album by the name with the limit.
func (r *albumRepository) FindByNameLimit(ctx context.Context, name string, limit int) ([]*album.Album, error) {
c, err := r.clientManager.GetClient()
if err != nil {
return nil, err
}

client, err := c.Open()
if err != nil {
return nil, err
}

result, err := client.Search(ctx, name, name, spotify.SearchTypeAlbum, limit)
if err != nil {
return nil, err
}

var albums []*album.Album
for _, a := range result.Albums.Albums {
albums = append(albums, &album.Album{
ID: a.ID,
Name: a.Name,
Artists: a.Artists,
ReleaseDate: a.ReleaseDate,
})
}

return albums, nil
}

// IsLiked returns whether the album is liked.
func (r *albumRepository) IsLiked(ctx context.Context, id spotify.ID) (bool, error) {
c, err := r.clientManager.GetClient()
if err != nil {
return false, err
}

client, err := c.Open()
if err != nil {
return false, err
}

result, err := client.UserHasAlbums(ctx, id)
if err != nil {
return false, err
}

return result[0], nil
}

// Like likes the album.
func (r *albumRepository) Like(ctx context.Context, id spotify.ID) error {
c, err := r.clientManager.GetClient()
if err != nil {
return err
}

client, err := c.Open()
if err != nil {
return err
}

return client.AddAddAlbumsToLibrary(ctx, id)
}

// Unlike unlikes the album.
func (r *albumRepository) Unlike(ctx context.Context, id spotify.ID) error {
c, err := r.clientManager.GetClient()
if err != nil {
return err
}

client, err := c.Open()
if err != nil {
return err
}

return client.RemoveAlbumsFromLibrary(ctx, id)
}
25 changes: 15 additions & 10 deletions app/infrastructure/spotify/repository/artist_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewArtistRepository() artist.ArtistRepository {
}

// FindById returns the artist by the ID.
func (r *artistRepository) FindById(ctx context.Context, id string) (*artist.Artist, error) {
func (r *artistRepository) FindById(ctx context.Context, id spotify.ID) (*artist.Artist, error) {
c, err := r.clientManager.GetClient()
if err != nil {
return nil, err
Expand All @@ -43,8 +43,8 @@ func (r *artistRepository) FindById(ctx context.Context, id string) (*artist.Art
}, nil
}

// FindByName returns the artist by the name.
func (r *artistRepository) FindByName(ctx context.Context, name string, limt int) (*artist.Artist, error) {
// FindByNameLimit returns the artist by the name with the limit.
func (r *artistRepository) FindByNameLimit(ctx context.Context, name string, limit int) ([]*artist.Artist, error) {
c, err := r.clientManager.GetClient()
if err != nil {
return nil, err
Expand All @@ -60,14 +60,19 @@ func (r *artistRepository) FindByName(ctx context.Context, name string, limt int
return nil, err
}

return &artist.Artist{
ID: result.Artists.Artists.ID,
Name: result.Artists.Artists.Name,
}, nil
var artists []*artist.Artist
for _, artist := range result.Artists.Artists {
artists = append(artists, &artist.Artist{
ID: artist.ID,
Name: artist.Name,
})
}

return artists, nil
}

// IsLiked returns whether the artist is liked.
func (r *artistRepository) IsLiked(ctx context.Context, id string) (bool, error) {
func (r *artistRepository) IsLiked(ctx context.Context, id spotify.ID) (bool, error) {
c, err := r.clientManager.GetClient()
if err != nil {
return false, err
Expand All @@ -87,7 +92,7 @@ func (r *artistRepository) IsLiked(ctx context.Context, id string) (bool, error)
}

// Like likes the artist.
func (r *artistRepository) Like(ctx context.Context, id string) error {
func (r *artistRepository) Like(ctx context.Context, id spotify.ID) error {
c, err := r.clientManager.GetClient()
if err != nil {
return err
Expand All @@ -102,7 +107,7 @@ func (r *artistRepository) Like(ctx context.Context, id string) error {
}

// Unlike unlikes the artist.
func (r *artistRepository) Unlike(ctx context.Context, id string) error {
func (r *artistRepository) Unlike(ctx context.Context, id spotify.ID) error {
c, err := r.clientManager.GetClient()
if err != nil {
return err
Expand Down
Loading

0 comments on commit c762eaf

Please sign in to comment.