aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/videos/shared/video.service.ts
blob: debc114aaca36cee3a068276c501631439ca0d4d (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';

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

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

  videoCategories: Array<{ id: number, label: string }> = [];

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

  loadVideoCategories() {
    return this.http.get(VideoService.BASE_VIDEO_URL + 'categories')
                    .map(this.restExtractor.extractDataGet)
                    .subscribe(data => {
                      Object.keys(data).forEach(categoryKey => {
                        this.videoCategories.push({
                          id: parseInt(categoryKey),
                          label: data[categoryKey]
                        });
                      });
                    });
  }

  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));
  }

  reportVideo(id: string, reason: string) {
    const url = VideoService.BASE_VIDEO_URL + id + '/abuse';
    const body = {
      reason
    };

    return this.authHttp.post(url, body)
                        .map(this.restExtractor.extractDataBool)
                        .catch((res) => this.restExtractor.handleError(res));
  }

  setVideoLike(id: string) {
    return this.setVideoRate(id, 'like');
  }

  setVideoDislike(id: string) {
    return this.setVideoRate(id, 'dislike');
  }

  getUserVideoRating(id: string) {
    const url = UserService.BASE_USERS_URL + '/me/videos/' + id + '/rating';

    return this.authHttp.get(url)
                        .map(this.restExtractor.extractDataGet)
                        .catch((res) => this.restExtractor.handleError(res));
  }

  private setVideoRate(id: string, rateType: RateType) {
    const url = VideoService.BASE_VIDEO_URL + id + '/rate';
    const body = {
      rating: rateType
    };

    return this.authHttp.put(url, body)
                        .map(this.restExtractor.extractDataBool)
                        .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 };
  }
}