]>
Commit | Line | Data |
---|---|---|
1 | import { HttpClient, HttpParams } from '@angular/common/http' | |
2 | import { Injectable } from '@angular/core' | |
3 | import { SortMeta } from 'primeng/primeng' | |
4 | import 'rxjs/add/operator/catch' | |
5 | import 'rxjs/add/operator/map' | |
6 | import { Observable } from 'rxjs/Observable' | |
7 | import { AccountFollow, ResultList } from '../../../../../../shared' | |
8 | import { environment } from '../../../../environments/environment' | |
9 | import { RestExtractor, RestPagination, RestService } from '../../../shared' | |
10 | ||
11 | @Injectable() | |
12 | export class FollowService { | |
13 | private static BASE_APPLICATION_URL = environment.apiUrl + '/api/v1/server' | |
14 | ||
15 | constructor ( | |
16 | private authHttp: HttpClient, | |
17 | private restService: RestService, | |
18 | private restExtractor: RestExtractor | |
19 | ) {} | |
20 | ||
21 | getFollowing (pagination: RestPagination, sort: SortMeta): Observable<ResultList<AccountFollow>> { | |
22 | let params = new HttpParams() | |
23 | params = this.restService.addRestGetParams(params, pagination, sort) | |
24 | ||
25 | return this.authHttp.get<ResultList<Account>>(FollowService.BASE_APPLICATION_URL + '/following', { params }) | |
26 | .map(res => this.restExtractor.convertResultListDateToHuman(res)) | |
27 | .catch(res => this.restExtractor.handleError(res)) | |
28 | } | |
29 | ||
30 | getFollowers (pagination: RestPagination, sort: SortMeta): Observable<ResultList<AccountFollow>> { | |
31 | let params = new HttpParams() | |
32 | params = this.restService.addRestGetParams(params, pagination, sort) | |
33 | ||
34 | return this.authHttp.get<ResultList<Account>>(FollowService.BASE_APPLICATION_URL + '/followers', { params }) | |
35 | .map(res => this.restExtractor.convertResultListDateToHuman(res)) | |
36 | .catch(res => this.restExtractor.handleError(res)) | |
37 | } | |
38 | ||
39 | follow (notEmptyHosts: string[]) { | |
40 | const body = { | |
41 | hosts: notEmptyHosts | |
42 | } | |
43 | ||
44 | return this.authHttp.post(FollowService.BASE_APPLICATION_URL + '/following', body) | |
45 | .map(this.restExtractor.extractDataBool) | |
46 | .catch(res => this.restExtractor.handleError(res)) | |
47 | } | |
48 | ||
49 | unfollow (follow: AccountFollow) { | |
50 | return this.authHttp.delete(FollowService.BASE_APPLICATION_URL + '/following/' + follow.following.id) | |
51 | .map(this.restExtractor.extractDataBool) | |
52 | .catch(res => this.restExtractor.handleError(res)) | |
53 | } | |
54 | } |