]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video-channel/video-channel.service.ts
Upgrade Angular first step
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video-channel / video-channel.service.ts
CommitLineData
d3e91a5f
C
1import { Injectable } from '@angular/core'
2import 'rxjs/add/operator/catch'
3import 'rxjs/add/operator/map'
4import { Observable } from 'rxjs/Observable'
5import { RestExtractor } from '../rest/rest-extractor.service'
6import { RestService } from '../rest/rest.service'
7import { HttpClient } from '@angular/common/http'
08c1efbe 8import { VideoChannel as VideoChannelServer, VideoChannelCreate, VideoChannelUpdate } from '../../../../../shared/models/videos'
d3e91a5f
C
9import { AccountService } from '../account/account.service'
10import { ResultList } from '../../../../../shared'
11import { VideoChannel } from './video-channel.model'
170726f5
C
12import { ReplaySubject } from 'rxjs/ReplaySubject'
13import { environment } from '../../../environments/environment'
08c1efbe
C
14import { UserService } from '@app/+admin/users/shared/user.service'
15import { User } from '@app/shared'
d3e91a5f
C
16
17@Injectable()
18export 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}