-
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
6 changed files
with
374 additions
and
13 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
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
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) | ||
} |
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.