]>
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' |
c8487f3f | 5 | import { HttpClient, HttpParams } 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' |
52d9f792 | 12 | import { Avatar } from '../../../../../shared/models/avatars/avatar.model' |
440d39c5 | 13 | import { ComponentPaginationLight } from '@app/shared/rest/component-pagination.model' |
c8487f3f | 14 | import { RestService } from '@app/shared/rest' |
d3e91a5f C |
15 | |
16 | @Injectable() | |
17 | export class VideoChannelService { | |
170726f5 C |
18 | static BASE_VIDEO_CHANNEL_URL = environment.apiUrl + '/api/v1/video-channels/' |
19 | ||
20 | videoChannelLoaded = new ReplaySubject<VideoChannel>(1) | |
21 | ||
22a16e36 C |
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 | ||
f6eebcb3 C |
32 | constructor ( |
33 | private authHttp: HttpClient, | |
c8487f3f | 34 | private restService: RestService, |
f6eebcb3 C |
35 | private restExtractor: RestExtractor |
36 | ) { } | |
37 | ||
8a19bee1 C |
38 | getVideoChannel (videoChannelName: string) { |
39 | return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName) | |
db400f44 C |
40 | .pipe( |
41 | map(videoChannelHash => new VideoChannel(videoChannelHash)), | |
42 | tap(videoChannel => this.videoChannelLoaded.next(videoChannel)), | |
e4f0e92e | 43 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 44 | ) |
170726f5 C |
45 | } |
46 | ||
440d39c5 | 47 | listAccountVideoChannels (account: Account, componentPagination?: ComponentPaginationLight): Observable<ResultList<VideoChannel>> { |
c8487f3f C |
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 }) | |
db400f44 | 57 | .pipe( |
22a16e36 | 58 | map(res => VideoChannelService.extractVideoChannels(res)), |
e4f0e92e | 59 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 60 | ) |
d3e91a5f C |
61 | } |
62 | ||
08c1efbe C |
63 | createVideoChannel (videoChannel: VideoChannelCreate) { |
64 | return this.authHttp.post(VideoChannelService.BASE_VIDEO_CHANNEL_URL, videoChannel) | |
db400f44 C |
65 | .pipe( |
66 | map(this.restExtractor.extractDataBool), | |
67 | catchError(err => this.restExtractor.handleError(err)) | |
68 | ) | |
08c1efbe C |
69 | } |
70 | ||
22a16e36 C |
71 | updateVideoChannel (videoChannelName: string, videoChannel: VideoChannelUpdate) { |
72 | return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName, videoChannel) | |
db400f44 C |
73 | .pipe( |
74 | map(this.restExtractor.extractDataBool), | |
75 | catchError(err => this.restExtractor.handleError(err)) | |
76 | ) | |
08c1efbe C |
77 | } |
78 | ||
22a16e36 C |
79 | changeVideoChannelAvatar (videoChannelName: string, avatarForm: FormData) { |
80 | const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/avatar/pick' | |
52d9f792 C |
81 | |
82 | return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm) | |
e4f0e92e | 83 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
52d9f792 C |
84 | } |
85 | ||
08c1efbe | 86 | removeVideoChannel (videoChannel: VideoChannel) { |
4035d2b6 | 87 | return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost) |
db400f44 C |
88 | .pipe( |
89 | map(this.restExtractor.extractDataBool), | |
90 | catchError(err => this.restExtractor.handleError(err)) | |
91 | ) | |
08c1efbe | 92 | } |
d3e91a5f | 93 | } |