-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathuser.service.js
53 lines (49 loc) · 1.44 KB
/
user.service.js
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
'use strict';
import { Injectable } from '@angular/core';
import { Response } from '@angular/http';
import { AuthHttp } from 'angular2-jwt';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/toPromise';
// @flow
type UserType = {
// TODO: use Mongoose model
name: string;
email: string;
}
@Injectable()
export class UserService {
static parameters = [AuthHttp];
constructor(authHttp: AuthHttp) {
this.AuthHttp = authHttp;
}
handleError(err) {
Observable.throw(err.json().error || 'Server error');
}
query(): Observable<UserType[]> {
return this.AuthHttp.get('/api/users/')
.map((res:Response) => res.json())
.catch(this.handleError);
}
get(user = {id: 'me'}): Observable<UserType> {
return this.AuthHttp.get(`/api/users/${user.id}`)
.map((res:Response) => res.json())
.catch(this.handleError);
}
create(user: UserType) {
return this.AuthHttp.post('/api/users/', user)
.map((res:Response) => res.json())
.catch(this.handleError);
}
changePassword(user, oldPassword, newPassword) {
return this.AuthHttp.put(`/api/users/${user.id}/password`, {oldPassword, newPassword})
.map((res:Response) => res.json())
.catch(this.handleError);
}
remove(user) {
return this.AuthHttp.delete(`/api/users/${user.id}`)
.map((res:Response) => res.json())
.catch(this.handleError);
}
}