import { Injectable } from '@angular/core'
import { Observable, ReplaySubject } from 'rxjs'
import { RestExtractor } from '../rest/rest-extractor.service'
-import { HttpClient } from '@angular/common/http'
+import { HttpClient, HttpParams } from '@angular/common/http'
import { VideoChannel as VideoChannelServer, VideoChannelCreate, VideoChannelUpdate } from '../../../../../shared/models/videos'
import { AccountService } from '../account/account.service'
import { ResultList } from '../../../../../shared'
import { environment } from '../../../environments/environment'
import { Account } from '@app/shared/account/account.model'
import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
+import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
+import { RestService } from '@app/shared/rest'
@Injectable()
export class VideoChannelService {
videoChannelLoaded = new ReplaySubject<VideoChannel>(1)
+ static extractVideoChannels (result: ResultList<VideoChannelServer>) {
+ const videoChannels: VideoChannel[] = []
+
+ for (const videoChannelJSON of result.data) {
+ videoChannels.push(new VideoChannel(videoChannelJSON))
+ }
+
+ return { data: videoChannels, total: result.total }
+ }
+
constructor (
private authHttp: HttpClient,
+ private restService: RestService,
private restExtractor: RestExtractor
- ) {}
+ ) { }
- getVideoChannel (videoChannelUUID: string) {
- return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelUUID)
+ getVideoChannel (videoChannelName: string) {
+ return this.authHttp.get<VideoChannel>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName)
.pipe(
map(videoChannelHash => new VideoChannel(videoChannelHash)),
tap(videoChannel => this.videoChannelLoaded.next(videoChannel)),
- catchError(res => this.restExtractor.handleError(res))
+ catchError(err => this.restExtractor.handleError(err))
)
}
- listAccountVideoChannels (account: Account): Observable<ResultList<VideoChannel>> {
- return this.authHttp.get<ResultList<VideoChannelServer>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels')
+ listAccountVideoChannels (account: Account, componentPagination?: ComponentPagination): Observable<ResultList<VideoChannel>> {
+ const pagination = componentPagination
+ ? this.restService.componentPaginationToRestPagination(componentPagination)
+ : { start: 0, count: 20 }
+
+ let params = new HttpParams()
+ params = this.restService.addRestGetParams(params, pagination)
+
+ const url = AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/video-channels'
+ return this.authHttp.get<ResultList<VideoChannelServer>>(url, { params })
.pipe(
- map(res => this.extractVideoChannels(res)),
- catchError((res) => this.restExtractor.handleError(res))
+ map(res => VideoChannelService.extractVideoChannels(res)),
+ catchError(err => this.restExtractor.handleError(err))
)
}
)
}
- updateVideoChannel (videoChannelUUID: string, videoChannel: VideoChannelUpdate) {
- return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelUUID, videoChannel)
+ updateVideoChannel (videoChannelName: string, videoChannel: VideoChannelUpdate) {
+ return this.authHttp.put(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName, videoChannel)
.pipe(
map(this.restExtractor.extractDataBool),
catchError(err => this.restExtractor.handleError(err))
)
}
- changeVideoChannelAvatar (videoChannelUUID: string, avatarForm: FormData) {
- const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelUUID + '/avatar/pick'
+ changeVideoChannelAvatar (videoChannelName: string, avatarForm: FormData) {
+ const url = VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannelName + '/avatar/pick'
return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
- .pipe(catchError(this.restExtractor.handleError))
+ .pipe(catchError(err => this.restExtractor.handleError(err)))
}
removeVideoChannel (videoChannel: VideoChannel) {
- return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.uuid)
+ return this.authHttp.delete(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost)
.pipe(
map(this.restExtractor.extractDataBool),
catchError(err => this.restExtractor.handleError(err))
)
}
-
- private extractVideoChannels (result: ResultList<VideoChannelServer>) {
- const videoChannels: VideoChannel[] = []
-
- for (const videoChannelJSON of result.data) {
- videoChannels.push(new VideoChannel(videoChannelJSON))
- }
-
- return { data: videoChannels, total: result.total }
- }
}