Skip to content

Commit

Permalink
Merge pull request #9 from music10/feature/8
Browse files Browse the repository at this point in the history
#8 add search playlists
  • Loading branch information
dergunovd authored Jan 16, 2021
2 parents 69611f9 + 1adebfc commit 54dc5df
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"eslint": "^7.16.0",
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-prettier": "^3.3.0",
"eslint-plugin-react-hooks": "^4.2.0",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"supertest": "^6.0.1",
Expand Down
18 changes: 8 additions & 10 deletions src/modules/api/spotify/api.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,14 @@ describe('SpotifyApiService', () => {
});

it('should search playlists', async () => {
expect(await service.searchPlaylists('рус')).toStrictEqual([
{
id: '27f5HDjqkWIOxX7xA3T95p',
name: 'Русский рэп',
},
{
id: '3CjOptqIHEVcfNEP9GAuMz',
name: 'Русский Рок',
},
]);
expect(await service.searchPlaylists('русский')).toEqual(
expect.arrayContaining([
{
id: expect.any(String),
name: expect.any(String),
},
]),
);
});

it('should get playlist by id', async () => {
Expand Down
21 changes: 18 additions & 3 deletions src/modules/api/spotify/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,30 @@ export class ApiService implements MusicApi {
.toPromise()
.then(({ data }) => data);
}

/**
* Search playlists by query-string
* @param query
* @return
*/
async searchPlaylists(query) {
return (await this.getPlaylists()).filter((playlist) =>
new RegExp(query, 'ig').test(playlist.name),
);
return this.httpService
.get(`/search`, {
params: {
q: query,
type: 'playlist',
},
})
.toPromise()
.then(({ data }) =>
data.playlists.items
.filter((playlist) => playlist.tracks.total > 40)
.slice(0, 20)
.map(({ id, name }) => ({
id,
name,
})),
);
}

/**
Expand Down

0 comments on commit 54dc5df

Please sign in to comment.