1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| import { HttpClient, HttpParams } from "@angular/common/http"; import { Inject, Injectable } from "@angular/core"; import { Observable } from "rxjs"; import { map } from "rxjs/internal/operators"; import { Song, SongSheet, SongUrl } from "./data-types/common.types"; import { API_CONFIG, ServicesModule } from "./services.module";
@Injectable({ providedIn: ServicesModule, }) export class SongService { constructor( private http: HttpClient, @Inject(API_CONFIG) private uri: string ) {}
getSongUrl(ids: string): Observable<SongUrl[]> { const params = new HttpParams().set("id", ids); return this.http .get(this.uri + "song/url", { params }) .pipe(map((res: { data: SongUrl[] }) => res.data)); }
getSongList(songs: Song | Song[]): Observable<Song[]> { const songArr = Array.isArray(songs) ? songs.slice() : [songs]; const ids = songArr.map((item) => item.id).join(","); return Observable.create((observer) => { this.getSongUrl(ids).subscribe((urls) => { observer.next(this.generateSongList(songArr, urls)); }); }); } generateSongList(songs: Song[], urls: SongUrl[]): Song[] { const result = []; songs.forEach((song) => { const url = urls.find((url) => url.id === song.id).url; if (url) { result.push({ ...song, url }); } }); return result; } }
|