aboutsummaryrefslogblamecommitdiffhomepage
path: root/client/src/app/videos/shared/video.service.ts
blob: f173ef06be5fdf01a61fdc0118e09eda43ccc6d4 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                           
                                     
                                             
 
                                      
                                              

                                                                                                
                                      

             
                           
                                                    
 

                                     
                               


                                         

      
                                           
                                                          
                                                           
                                                             
                                                                         
   
 

                                                                         
 
                                                                         

                                            
                                                                         

   
                           
                                                                 

                                                                             

   

                                                                             
 
                                                        
 
                                                                                                                        
                                                            
                                            
                                                                         

   


                                             
                      

                                         

     
                                   
   
 
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';

import { Search } from '../../shared';
import { SortField } from './sort-field.type';
import { AuthService } from '../../core';
import { AuthHttp, RestExtractor, RestPagination, RestService, ResultList } from '../../shared';
import { Video } from './video.model';

@Injectable()
export class VideoService {
  private static BASE_VIDEO_URL = '/api/v1/videos/';

  constructor(
    private authService: AuthService,
    private authHttp: AuthHttp,
    private http: Http,
    private restExtractor: RestExtractor,
    private restService: RestService
  ) {}

  getVideo(id: string): Observable<Video> {
    return this.http.get(VideoService.BASE_VIDEO_URL + id)
                    .map(this.restExtractor.extractDataGet)
                    .map(video_hash => new Video(video_hash))
                    .catch((res) => this.restExtractor.handleError(res));
  }

  getVideos(pagination: RestPagination, sort: SortField) {
    const params = this.restService.buildRestGetParams(pagination, sort);

    return this.http.get(VideoService.BASE_VIDEO_URL, { search: params })
                    .map(res => res.json())
                    .map(this.extractVideos)
                    .catch((res) => this.restExtractor.handleError(res));
  }

  removeVideo(id: string) {
    return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id)
                        .map(this.restExtractor.extractDataBool)
                        .catch((res) => this.restExtractor.handleError(res));
  }

  searchVideos(search: Search, pagination: RestPagination, sort: SortField) {
    const params = this.restService.buildRestGetParams(pagination, sort);

    if (search.field) params.set('field', search.field);

    return this.http.get(VideoService.BASE_VIDEO_URL + 'search/' + encodeURIComponent(search.value), { search: params })
                    .map(this.restExtractor.extractDataList)
                    .map(this.extractVideos)
                    .catch((res) => this.restExtractor.handleError(res));
  }

  private extractVideos(result: ResultList) {
    const videosJson = result.data;
    const totalVideos = result.total;
    const videos = [];
    for (const videoJson of videosJson) {
      videos.push(new Video(videoJson));
    }

    return { videos, totalVideos };
  }
}