aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/shared')
-rw-r--r--client/src/app/shared/auth/auth.service.ts22
-rw-r--r--client/src/app/shared/index.ts1
-rw-r--r--client/src/app/shared/rest/index.ts3
-rw-r--r--client/src/app/shared/rest/rest-extractor.service.ts46
-rw-r--r--client/src/app/shared/rest/rest-pagination.ts5
-rw-r--r--client/src/app/shared/rest/rest.service.ts27
6 files changed, 91 insertions, 13 deletions
diff --git a/client/src/app/shared/auth/auth.service.ts b/client/src/app/shared/auth/auth.service.ts
index 8eea0c4bf..2273048c8 100644
--- a/client/src/app/shared/auth/auth.service.ts
+++ b/client/src/app/shared/auth/auth.service.ts
@@ -1,10 +1,11 @@
1import { Injectable } from '@angular/core'; 1import { Injectable } from '@angular/core';
2import { Headers, Http, Response, URLSearchParams } from '@angular/http'; 2import { Headers, Http, URLSearchParams } from '@angular/http';
3import { Observable } from 'rxjs/Observable'; 3import { Observable } from 'rxjs/Observable';
4import { Subject } from 'rxjs/Subject'; 4import { Subject } from 'rxjs/Subject';
5 5
6import { AuthStatus } from './auth-status.model'; 6import { AuthStatus } from './auth-status.model';
7import { AuthUser } from './auth-user.model'; 7import { AuthUser } from './auth-user.model';
8import { RestExtractor } from '../rest';
8 9
9@Injectable() 10@Injectable()
10export class AuthService { 11export class AuthService {
@@ -19,15 +20,15 @@ export class AuthService {
19 private loginChanged: Subject<AuthStatus>; 20 private loginChanged: Subject<AuthStatus>;
20 private user: AuthUser = null; 21 private user: AuthUser = null;
21 22
22 constructor(private http: Http) { 23 constructor(private http: Http, private restExtractor: RestExtractor) {
23 this.loginChanged = new Subject<AuthStatus>(); 24 this.loginChanged = new Subject<AuthStatus>();
24 this.loginChangedSource = this.loginChanged.asObservable(); 25 this.loginChangedSource = this.loginChanged.asObservable();
25 26
26 // Fetch the client_id/client_secret 27 // Fetch the client_id/client_secret
27 // FIXME: save in local storage? 28 // FIXME: save in local storage?
28 this.http.get(AuthService.BASE_CLIENT_URL) 29 this.http.get(AuthService.BASE_CLIENT_URL)
29 .map(res => res.json()) 30 .map(this.restExtractor.extractDataGet)
30 .catch(this.handleError) 31 .catch((res) => this.restExtractor.handleError(res))
31 .subscribe( 32 .subscribe(
32 result => { 33 result => {
33 this.clientId = result.client_id; 34 this.clientId = result.client_id;
@@ -101,14 +102,14 @@ export class AuthService {
101 }; 102 };
102 103
103 return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options) 104 return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options)
104 .map(res => res.json()) 105 .map(this.restExtractor.extractDataGet)
105 .map(res => { 106 .map(res => {
106 res.username = username; 107 res.username = username;
107 return res; 108 return res;
108 }) 109 })
109 .flatMap(res => this.fetchUserInformations(res)) 110 .flatMap(res => this.fetchUserInformations(res))
110 .map(res => this.handleLogin(res)) 111 .map(res => this.handleLogin(res))
111 .catch(this.handleError); 112 .catch((res) => this.restExtractor.handleError(res));
112 } 113 }
113 114
114 logout() { 115 logout() {
@@ -139,9 +140,9 @@ export class AuthService {
139 }; 140 };
140 141
141 return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options) 142 return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options)
142 .map(res => res.json()) 143 .map(this.restExtractor.extractDataGet)
143 .map(res => this.handleRefreshToken(res)) 144 .map(res => this.handleRefreshToken(res))
144 .catch(this.handleError); 145 .catch((res) => this.restExtractor.handleError(res));
145 } 146 }
146 147
147 private fetchUserInformations (obj: any) { 148 private fetchUserInformations (obj: any) {
@@ -160,11 +161,6 @@ export class AuthService {
160 ); 161 );
161 } 162 }
162 163
163 private handleError (error: Response) {
164 console.error(error);
165 return Observable.throw(error.json() || { error: 'Server error' });
166 }
167
168 private handleLogin (obj: any) { 164 private handleLogin (obj: any) {
169 const id = obj.id; 165 const id = obj.id;
170 const username = obj.username; 166 const username = obj.username;
diff --git a/client/src/app/shared/index.ts b/client/src/app/shared/index.ts
index 9edf9b4a0..c362a0e4a 100644
--- a/client/src/app/shared/index.ts
+++ b/client/src/app/shared/index.ts
@@ -1,4 +1,5 @@
1export * from './auth'; 1export * from './auth';
2export * from './form-validators'; 2export * from './form-validators';
3export * from './rest';
3export * from './search'; 4export * from './search';
4export * from './users'; 5export * from './users';
diff --git a/client/src/app/shared/rest/index.ts b/client/src/app/shared/rest/index.ts
new file mode 100644
index 000000000..3c9509dc7
--- /dev/null
+++ b/client/src/app/shared/rest/index.ts
@@ -0,0 +1,3 @@
1export * from './rest-extractor.service';
2export * from './rest-pagination';
3export * from './rest.service';
diff --git a/client/src/app/shared/rest/rest-extractor.service.ts b/client/src/app/shared/rest/rest-extractor.service.ts
new file mode 100644
index 000000000..aa44799af
--- /dev/null
+++ b/client/src/app/shared/rest/rest-extractor.service.ts
@@ -0,0 +1,46 @@
1import { Injectable } from '@angular/core';
2import { Response } from '@angular/http';
3import { Observable } from 'rxjs/Observable';
4
5export interface ResultList {
6 data: any[];
7 total: number;
8}
9
10@Injectable()
11export class RestExtractor {
12
13 constructor () { ; }
14
15 extractDataBool(res: Response) {
16 return true;
17 }
18
19 extractDataList(res: Response) {
20 const body = res.json();
21
22 const ret: ResultList = {
23 data: body.data,
24 total: body.total
25 };
26
27 return ret;
28 }
29
30 extractDataGet(res: Response) {
31 return res.json();
32 }
33
34 handleError(res: Response) {
35 let text = 'Server error: ';
36 text += res.text();
37 let json = res.json();
38
39 const error = {
40 json,
41 text
42 };
43
44 return Observable.throw(error);
45 }
46}
diff --git a/client/src/app/shared/rest/rest-pagination.ts b/client/src/app/shared/rest/rest-pagination.ts
new file mode 100644
index 000000000..0cfa4f468
--- /dev/null
+++ b/client/src/app/shared/rest/rest-pagination.ts
@@ -0,0 +1,5 @@
1export interface RestPagination {
2 currentPage: number;
3 itemsPerPage: number;
4 totalItems: number;
5};
diff --git a/client/src/app/shared/rest/rest.service.ts b/client/src/app/shared/rest/rest.service.ts
new file mode 100644
index 000000000..16b47e957
--- /dev/null
+++ b/client/src/app/shared/rest/rest.service.ts
@@ -0,0 +1,27 @@
1import { Injectable } from '@angular/core';
2import { URLSearchParams } from '@angular/http';
3
4import { RestPagination } from './rest-pagination';
5
6@Injectable()
7export class RestService {
8
9 buildRestGetParams(pagination?: RestPagination, sort?: string) {
10 const params = new URLSearchParams();
11
12 if (pagination) {
13 const start: number = (pagination.currentPage - 1) * pagination.itemsPerPage;
14 const count: number = pagination.itemsPerPage;
15
16 params.set('start', start.toString());
17 params.set('count', count.toString());
18 }
19
20 if (sort) {
21 params.set('sort', sort);
22 }
23
24 return params;
25 }
26
27}