Remove ability to filter album by artist, go via getArtist instead

This commit is contained in:
simojenki
2021-03-07 09:20:06 +11:00
parent c5a085d667
commit 3f607c5304
3 changed files with 9 additions and 99 deletions

View File

@@ -72,7 +72,6 @@ export const asResult = <T>([results, total]: [T[], number]) => ({
export type ArtistQuery = Paging; export type ArtistQuery = Paging;
export type AlbumQuery = Paging & { export type AlbumQuery = Paging & {
artistId?: string;
genre?: string; genre?: string;
}; };

View File

@@ -260,96 +260,14 @@ describe("InMemoryMusicService", () => {
}); });
}); });
describe("filtering by artist", () => {
describe("fetching all", () => {
it("should return all artist albums", async () => {
expect(
await musicLibrary.albums({
artistId: artist3.id,
_index: 0,
_count: 100,
})
).toEqual({
results: [
albumToAlbumSummary(artist3_album1),
albumToAlbumSummary(artist3_album2),
],
total: artist3.albums.length,
});
});
});
describe("when the artist has more albums than a single page", () => {
describe("can fetch a single page", () => {
it("should return only the albums for that page", async () => {
expect(
await musicLibrary.albums({
artistId: artist1.id,
_index: 1,
_count: 3,
})
).toEqual({
results: [
albumToAlbumSummary(artist1_album2),
albumToAlbumSummary(artist1_album3),
albumToAlbumSummary(artist1_album4),
],
total: artist1.albums.length,
});
});
});
describe("can fetch the last page", () => {
it("should return only the albums for the last page", async () => {
expect(
await musicLibrary.albums({
artistId: artist1.id,
_index: 4,
_count: 100,
})
).toEqual({
results: [albumToAlbumSummary(artist1_album5)],
total: artist1.albums.length,
});
});
});
});
it("should return empty list if the artists does not have any", async () => {
expect(
await musicLibrary.albums({
artistId: artistWithNoAlbums.id,
_index: 0,
_count: 100,
})
).toEqual({
results: [],
total: 0,
});
});
it("should return empty list if the artist id is not valid", async () => {
expect(
await musicLibrary.albums({
artistId: uuid(),
_index: 0,
_count: 100,
})
).toEqual({
results: [],
total: 0,
});
});
});
describe("filtering by genre", () => { describe("filtering by genre", () => {
describe("fetching all on one page", () => { describe("fetching all on one page", () => {
it.only("should return all the albums of that genre for all the artists", async () => { it("should return all the albums of that genre for all the artists", async () => {
expect( expect(
await musicLibrary.albums({ await musicLibrary.albums({
genre: "Pop",
_index: 0, _index: 0,
_count: 100, _count: 100,
genre: "Pop",
}) })
).toEqual({ ).toEqual({
results: [ results: [
@@ -370,14 +288,12 @@ describe("InMemoryMusicService", () => {
await musicLibrary.albums({ await musicLibrary.albums({
genre: "Pop", genre: "Pop",
_index: 1, _index: 1,
_count: 3, _count: 2,
}) })
).toEqual({ ).toEqual({
results: [ results: [
albumToAlbumSummary(artist1_album1),
albumToAlbumSummary(artist1_album4), albumToAlbumSummary(artist1_album4),
albumToAlbumSummary(artist1_album5), albumToAlbumSummary(artist1_album5),
albumToAlbumSummary(artist3_album2),
], ],
total: 4, total: 4,
}); });
@@ -388,13 +304,13 @@ describe("InMemoryMusicService", () => {
it("should return only the albums for the last page", async () => { it("should return only the albums for the last page", async () => {
expect( expect(
await musicLibrary.albums({ await musicLibrary.albums({
artistId: artist1.id, genre: "Pop",
_index: 4, _index: 3,
_count: 100, _count: 100,
}) })
).toEqual({ ).toEqual({
results: [albumToAlbumSummary(artist1_album5)], results: [albumToAlbumSummary(artist3_album2)],
total: artist1.albums.length, total: 4,
}); });
}); });
}); });

View File

@@ -23,9 +23,6 @@ import {
type P<T> = (t: T) => boolean; type P<T> = (t: T) => boolean;
const all: P<any> = (_: any) => true; const all: P<any> = (_: any) => true;
const albumByArtist = (id: string): P<[Artist, Album]> => ([artist, _]) =>
artist.id === id;
const albumWithGenre = (genre: string): P<[Artist, Album]> => ([_, album]) => const albumWithGenre = (genre: string): P<[Artist, Album]> => ([_, album]) =>
album.genre === genre; album.genre === genre;
@@ -75,10 +72,8 @@ export class InMemoryMusicService implements MusicService {
.flatMap((artist) => artist.albums.map((album) => [artist, album])) .flatMap((artist) => artist.albums.map((album) => [artist, album]))
.filter( .filter(
pipe( pipe(
pipe(O.fromNullable(q.artistId), O.map(albumByArtist)), O.fromNullable(q.genre),
O.alt(() => O.map(albumWithGenre),
pipe(O.fromNullable(q.genre), O.map(albumWithGenre))
),
O.getOrElse(() => all) O.getOrElse(() => all)
) )
) )