]>
Commit | Line | Data |
---|---|---|
792dbaf0 GS |
1 | import { Injectable } from '@angular/core' |
2 | import { HttpClient, HttpParams } from '@angular/common/http' | |
3 | import { Observable } from 'rxjs/Observable' | |
4 | import 'rxjs/add/operator/catch' | |
5 | import 'rxjs/add/operator/map' | |
6 | ||
7 | import { SortMeta } from 'primeng/components/common/sortmeta' | |
8 | ||
35bf0c83 C |
9 | import { RestExtractor, RestPagination, RestService } from '../rest' |
10 | import { Utils } from '../utils' | |
11 | import { BlacklistedVideo, ResultList } from '../../../../../shared' | |
792dbaf0 GS |
12 | |
13 | @Injectable() | |
35bf0c83 C |
14 | export class VideoBlacklistService { |
15 | private static BASE_VIDEOS_URL = API_URL + '/api/v1/videos/' | |
792dbaf0 GS |
16 | |
17 | constructor ( | |
18 | private authHttp: HttpClient, | |
19 | private restService: RestService, | |
20 | private restExtractor: RestExtractor | |
21 | ) {} | |
22 | ||
35bf0c83 | 23 | listBlacklist (pagination: RestPagination, sort: SortMeta): Observable<ResultList<BlacklistedVideo>> { |
792dbaf0 GS |
24 | let params = new HttpParams() |
25 | params = this.restService.addRestGetParams(params, pagination, sort) | |
26 | ||
35bf0c83 | 27 | return this.authHttp.get<ResultList<BlacklistedVideo>>(VideoBlacklistService.BASE_VIDEOS_URL + 'blacklist', { params }) |
792dbaf0 GS |
28 | .map(res => this.restExtractor.convertResultListDateToHuman(res)) |
29 | .map(res => this.restExtractor.applyToResultListData(res, this.formatBlacklistedVideo.bind(this))) | |
30 | .catch(res => this.restExtractor.handleError(res)) | |
31 | } | |
32 | ||
35bf0c83 C |
33 | removeVideoFromBlacklist (videoId: number) { |
34 | return this.authHttp.delete(VideoBlacklistService.BASE_VIDEOS_URL + videoId + '/blacklist') | |
792dbaf0 GS |
35 | .map(this.restExtractor.extractDataBool) |
36 | .catch(res => this.restExtractor.handleError(res)) | |
37 | } | |
38 | ||
35bf0c83 C |
39 | blacklistVideo (videoId: number) { |
40 | return this.authHttp.post(VideoBlacklistService.BASE_VIDEOS_URL + videoId + '/blacklist', {}) | |
41 | .map(this.restExtractor.extractDataBool) | |
42 | .catch(res => this.restExtractor.handleError(res)) | |
43 | } | |
44 | ||
792dbaf0 GS |
45 | private formatBlacklistedVideo (blacklistedVideo: BlacklistedVideo) { |
46 | return Object.assign(blacklistedVideo, { | |
47 | createdAt: Utils.dateToHuman(blacklistedVideo.createdAt) | |
48 | }) | |
49 | } | |
50 | } |