]>
Commit | Line | Data |
---|---|---|
1 | import { Injectable } from '@angular/core'; | |
2 | import { Observable } from 'rxjs/Observable'; | |
3 | ||
4 | import { Friend } from './friend.model'; | |
5 | import { AuthHttp, RestExtractor, ResultList } from '../../../shared'; | |
6 | ||
7 | @Injectable() | |
8 | export class FriendService { | |
9 | private static BASE_FRIEND_URL: string = '/api/v1/pods/'; | |
10 | ||
11 | constructor ( | |
12 | private authHttp: AuthHttp, | |
13 | private restExtractor: RestExtractor | |
14 | ) {} | |
15 | ||
16 | getFriends() { | |
17 | return this.authHttp.get(FriendService.BASE_FRIEND_URL) | |
18 | .map(this.restExtractor.extractDataList) | |
19 | .map(this.extractFriends) | |
20 | .catch((res) => this.restExtractor.handleError(res)); | |
21 | } | |
22 | ||
23 | makeFriends(notEmptyHosts) { | |
24 | const body = { | |
25 | hosts: notEmptyHosts | |
26 | }; | |
27 | ||
28 | return this.authHttp.post(FriendService.BASE_FRIEND_URL + 'makefriends', body) | |
29 | .map(this.restExtractor.extractDataBool) | |
30 | .catch((res) => this.restExtractor.handleError(res)); | |
31 | } | |
32 | ||
33 | quitFriends() { | |
34 | return this.authHttp.get(FriendService.BASE_FRIEND_URL + 'quitfriends') | |
35 | .map(res => res.status) | |
36 | .catch((res) => this.restExtractor.handleError(res)); | |
37 | } | |
38 | ||
39 | private extractFriends(result: ResultList) { | |
40 | const friends: Friend[] = result.data; | |
41 | const totalFriends = result.total; | |
42 | ||
43 | return { friends, totalFriends }; | |
44 | } | |
45 | } |