Ability to browse Random Albums

This commit is contained in:
simojenki
2021-03-23 10:42:49 +11:00
parent 8f5905c16f
commit 4730511a84
8 changed files with 185 additions and 89 deletions

View File

@@ -3,6 +3,7 @@ import * as A from "fp-ts/Array";
import { fromEquals } from "fp-ts/lib/Eq";
import { pipe } from "fp-ts/lib/function";
import { ordString, fromCompare } from "fp-ts/lib/Ord";
import { shuffle } from "underscore";
import {
MusicService,
@@ -17,17 +18,10 @@ import {
asResult,
artistToArtistSummary,
albumToAlbumSummary,
Album,
Track,
Genre,
} from "../src/music_service";
type P<T> = (t: T) => boolean;
const all: P<any> = (_: any) => true;
const albumWithGenre = (genreId: string): P<[Artist, Album]> => ([_, album]) =>
album.genre?.id === genreId;
export class InMemoryMusicService implements MusicService {
users: Record<string, string> = {};
artists: Artist[] = [];
@@ -75,17 +69,25 @@ export class InMemoryMusicService implements MusicService {
),
albums: (q: AlbumQuery) =>
Promise.resolve(
this.artists
.flatMap((artist) => artist.albums.map((album) => [artist, album]))
.filter(
pipe(
O.fromNullable(q.genre),
O.map(albumWithGenre),
O.getOrElse(() => all)
)
)
this.artists.flatMap((artist) =>
artist.albums.map((album) => ({ artist, album }))
)
)
.then((matches) => matches.map(([_, album]) => album as Album))
.then((artist2Album) => {
switch (q.type) {
case "alphabeticalByArtist":
return artist2Album;
case "byGenre":
return artist2Album.filter(
(it) => it.album.genre?.id === q.genre
);
case "random":
return shuffle(artist2Album);
default:
return [];
}
})
.then((matches) => matches.map((it) => it.album))
.then((it) => it.map(albumToAlbumSummary))
.then(slice2(q))
.then(asResult),