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