]>
Commit | Line | Data |
---|---|---|
1 | import { catchError, map, tap } from 'rxjs/operators' | |
2 | import { Injectable } from '@angular/core' | |
3 | import { Observable, ReplaySubject } from 'rxjs' | |
4 | import { RestExtractor } from '../rest/rest-extractor.service' | |
5 | import { HttpClient, HttpParams } from '@angular/common/http' | |
6 | import { VideoChannel as VideoChannelServer, VideoChannelCreate, VideoChannelUpdate } from '../../../../../shared/models/videos' | |
7 | import { AccountService } from '../account/account.service' | |
8 | import { ResultList } from '../../../../../shared' | |
9 | import { VideoChannel } from './video-channel.model' | |
10 | import { environment } from '../../../environments/environment' | |
11 | import { Account } from '@app/shared/account/account.model' | |
12 | import { Avatar } from '../../../../../shared/models/avatars/avatar.model' | |
13 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | |
14 | import { RestService } from '@app/shared/rest' | |
15 | ||
16 | @Injectable() | |
17 | export class VideoChannelService { | |
18 | static BASE_VIDEO_CHANNEL_URL = environment.apiUrl + '/api/v1/video-channels/' | |
19 | ||
20 | videoChannelLoaded = new ReplaySubject<VideoChannel>(1) | |
21 | ||
22 | static extractVideoChannels (result: ResultList<VideoChannelServer>) { | |
23 | const videoChannels: VideoChannel[] = [] | |
24 | ||
25 | for (const videoChannelJSON of result.data) { | |
26 | videoChannels.push(new VideoChannel(videoChannelJSON)) | |
27 | } | |
28 | ||
29 | return { data: videoChannels, total: result.total } | |
30 | } | |
31 | ||
32 | constructor ( | |
33 | private authHttp: HttpClient, | |
34 | private restService: RestService, | |
35 | private restExtractor: RestExtractor | |
36 | ) { } | |
37 | ||
38 | getVideoChannel (videoChannelName: string) { | |
39 | return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName) | |
40 | .pipe( | |
41 | map(videoChannelHash => new VideoChannel(videoChannelHash)), | |
42 | tap(videoChannel => this.videoChannelLoaded.next(videoChannel)), | |
43 | catchError(err => this.restExtractor.handleError(err)) | |
44 | ) | |
45 | } | |
46 | ||
47 | listAccountVideoChannels (account: Account, componentPagination?: ComponentPagination): Observable<ResultList<VideoChannel>> { | |
48 | const pagination = componentPagination | |
49 | ? this.restService.componentPaginationToRestPagination(componentPagination) | |
50 | : { start: 0, count: 20 } | |
51 | ||
52 | let params = new HttpParams() | |
53 | params = this.restService.addRestGetParams(params, pagination) | |
54 | ||
55 | const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels' | |
56 | return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params }) | |
57 | .pipe( | |
58 | map(res => VideoChannelService.extractVideoChannels(res)), | |
59 | catchError(err => this.restExtractor.handleError(err)) | |
60 | ) | |
61 | } | |
62 | ||
63 | createVideoChannel (videoChannel: VideoChannelCreate) { | |
64 | return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel) | |
65 | .pipe( | |
66 | map(this.restExtractor.extractDataBool), | |
67 | catchError(err => this.restExtractor.handleError(err)) | |
68 | ) | |
69 | } | |
70 | ||
71 | updateVideoChannel (videoChannelName: string, videoChannel: VideoChannelUpdate) { | |
72 | return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName, videoChannel) | |
73 | .pipe( | |
74 | map(this.restExtractor.extractDataBool), | |
75 | catchError(err => this.restExtractor.handleError(err)) | |
76 | ) | |
77 | } | |
78 | ||
79 | changeVideoChannelAvatar (videoChannelName: string, avatarForm: FormData) { | |
80 | const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/avatar/pick' | |
81 | ||
82 | return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm) | |
83 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
84 | } | |
85 | ||
86 | removeVideoChannel (videoChannel: VideoChannel) { | |
87 | return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost) | |
88 | .pipe( | |
89 | map(this.restExtractor.extractDataBool), | |
90 | catchError(err => this.restExtractor.handleError(err)) | |
91 | ) | |
92 | } | |
93 | } |