Implemented filter

This commit is contained in:
grimsi
2022-08-05 23:01:07 +02:00
parent ddc1c036b6
commit 04febd13d1
9 changed files with 250 additions and 48 deletions
+31 -1
View File
@@ -1,9 +1,11 @@
import {Injectable} from '@angular/core';
import {GamesApi} from "../api/GamesApi";
import {HttpClient} from "@angular/common/http";
import {map, Observable} from "rxjs";
import {distinct, map, Observable} from "rxjs";
import {DetectedGameDto} from "../models/dtos/DetectedGameDto";
import {GameOverviewDto} from "../models/dtos/GameOverviewDto";
import {GenreDto} from "../models/dtos/GenreDto";
import {ThemeDto} from "../models/dtos/ThemeDto";
@Injectable({
providedIn: 'root'
@@ -42,6 +44,34 @@ export class GamesService implements GamesApi {
return this.http.get<DetectedGameDto>(`${this.apiPath}/game/${slug}`);
}
// TODO: This method of removing duplicates is most certainly an anti-pattern in RxJS
// TODO: However, I did not get the 'distinct()' pipe to work properly, so I have to take another look in the future
getAvailableGenres(): Observable<GenreDto[]> {
return this.getAllGames().pipe(
map<DetectedGameDto[], GenreDto[]>(
games => {
let availableGenresMap: Map<string, GenreDto> = new Map<string, GenreDto>;
games.map(game => game.genres === undefined ? [] : game.genres).flat().forEach(genre => availableGenresMap.set(genre.slug, genre));
return Array.from(availableGenresMap.values()).sort((g1, g2) => g1.name.localeCompare(g2.name));
}
)
);
}
// TODO: This method of removing duplicates is most certainly an anti-pattern in RxJS
// TODO: However, I did not get the 'distinct()' pipe to work properly, so I have to take another look in the future
getAvailableThemes(): Observable<ThemeDto[]> {
return this.getAllGames().pipe(
map<DetectedGameDto[], ThemeDto[]>(
games => {
let availableThemesMap: Map<string, ThemeDto> = new Map<string, GenreDto>;
games.map(game => game.themes === undefined ? [] : game.themes).flat().forEach(theme => availableThemesMap.set(theme.slug, theme));
return Array.from(availableThemesMap.values()).sort((t1, t2) => t1.name.localeCompare(t2.name));
}
)
);
}
downloadGame(slug: String): void {
window.open(`v1${this.apiPath}/game/${slug}/download`, '_top');
}