]>
Commit | Line | Data |
---|---|---|
d3e91a5f C |
1 | import { Injectable } from '@angular/core' |
2 | import 'rxjs/add/operator/catch' | |
3 | import 'rxjs/add/operator/map' | |
4 | import { Observable } from 'rxjs/Observable' | |
5 | import { RestExtractor } from '../rest/rest-extractor.service' | |
6 | import { RestService } from '../rest/rest.service' | |
7 | import { HttpClient } from '@angular/common/http' | |
08c1efbe | 8 | import { VideoChannel as VideoChannelServer, VideoChannelCreate, VideoChannelUpdate } from '../../../../../shared/models/videos' |
d3e91a5f C |
9 | import { AccountService } from '../account/account.service' |
10 | import { ResultList } from '../../../../../shared' | |
11 | import { VideoChannel } from './video-channel.model' | |
170726f5 C |
12 | import { ReplaySubject } from 'rxjs/ReplaySubject' |
13 | import { environment } from '../../../environments/environment' | |
08c1efbe C |
14 | import { UserService } from '@app/+admin/users/shared/user.service' |
15 | import { User } from '@app/shared' | |
d3e91a5f C |
16 | |
17 | @Injectable() | |
18 | export class VideoChannelService { | |
170726f5 C |
19 | static BASE_VIDEO_CHANNEL_URL = environment.apiUrl + '/api/v1/video-channels/' |
20 | ||
21 | videoChannelLoaded = new ReplaySubject<VideoChannel>(1) | |
22 | ||
d3e91a5f C |
23 | constructor ( |
24 | private authHttp: HttpClient, | |
25 | private restExtractor: RestExtractor, | |
26 | private restService: RestService | |
27 | ) {} | |
28 | ||
170726f5 C |
29 | getVideoChannel (videoChannelUUID: string) { |
30 | return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelUUID) | |
31 | .map(videoChannelHash => new VideoChannel(videoChannelHash)) | |
32 | .do(videoChannel => this.videoChannelLoaded.next(videoChannel)) | |
33 | .catch((res) => this.restExtractor.handleError(res)) | |
34 | } | |
35 | ||
36 | listAccountVideoChannels (accountId: number): Observable<ResultList<VideoChannel>> { | |
d3e91a5f C |
37 | return this.authHttp.get<ResultList<VideoChannelServer>>(AccountService.BASE_ACCOUNT_URL + accountId + '/video-channels') |
38 | .map(res => this.extractVideoChannels(res)) | |
39 | .catch((res) => this.restExtractor.handleError(res)) | |
40 | } | |
41 | ||
08c1efbe C |
42 | createVideoChannel (videoChannel: VideoChannelCreate) { |
43 | return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel) | |
44 | .map(this.restExtractor.extractDataBool) | |
45 | .catch(err => this.restExtractor.handleError(err)) | |
46 | } | |
47 | ||
48 | updateVideoChannel (videoChannelUUID: string, videoChannel: VideoChannelUpdate) { | |
49 | return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelUUID, videoChannel) | |
50 | .map(this.restExtractor.extractDataBool) | |
51 | .catch(err => this.restExtractor.handleError(err)) | |
52 | } | |
53 | ||
54 | removeVideoChannel (videoChannel: VideoChannel) { | |
55 | return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.uuid) | |
56 | .map(this.restExtractor.extractDataBool) | |
57 | .catch(err => this.restExtractor.handleError(err)) | |
58 | } | |
59 | ||
d3e91a5f C |
60 | private extractVideoChannels (result: ResultList<VideoChannelServer>) { |
61 | const videoChannels: VideoChannel[] = [] | |
62 | ||
63 | for (const videoChannelJSON of result.data) { | |
64 | videoChannels.push(new VideoChannel(videoChannelJSON)) | |
65 | } | |
66 | ||
67 | return { data: videoChannels, total: result.total } | |
68 | } | |
69 | } |