aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/app/videos/shared/video.service.ts
blob: 7b6519f007a01f807f7c4f9a83ff6b377a1b9106 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { Injectable } from '@angular/core';
import { Http, Response, URLSearchParams } from '@angular/http';
import { Observable } from 'rxjs/Rx';

import { Pagination } from './pagination.model';
import { Search } from '../../shared/index';
import { SortField } from './sort-field.type';
import { AuthService } from '../../users/index';
import { Video } from './video.model';

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

  constructor(
    private authService: AuthService,
    private http: Http
  ) {}

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

  getVideos(pagination: Pagination, sort: SortField) {
    const params = this.createPaginationParams(pagination);

    if (sort) params.set('sort', sort);

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

  removeVideo(id: string) {
    const options = this.authService.getAuthRequestOptions();
    return this.http.delete(VideoService.BASE_VIDEO_URL + id, options)
                    .map(res => <number> res.status)
                    .catch(this.handleError);
  }

  searchVideos(search: Search, pagination: Pagination, sort: SortField) {
    const params = this.createPaginationParams(pagination);

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

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

  private createPaginationParams(pagination: Pagination) {
    const params = new URLSearchParams();
    const start: number = (pagination.currentPage - 1) * pagination.itemsPerPage;
    const count: number = pagination.itemsPerPage;

    params.set('start', start.toString());
    params.set('count', count.toString());

    return params;
  }

  private extractVideos(body: any) {
    const videos_json = body.data;
    const totalVideos = body.total;
    const videos = [];
    for (const video_json of videos_json) {
      videos.push(new Video(video_json));
    }

    return { videos, totalVideos };
  }

  private handleError(error: Response) {
    console.error(error);
    return Observable.throw(error.json().error || 'Server error');
  }
}