-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathuser.service.js
56 lines (51 loc) · 1.64 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
54
55
56
// @flow
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';
type UserType = {
// TODO: use Mongoose model
id?: string;
_id?: string;
name?: string;
email?: string;
}
function handleError(err) {
return Observable.throw(err.json() || 'Server error');
}
@Injectable()
export class UserService {
AuthHttp;
static parameters = [AuthHttp];
constructor(<%= private() %>authHttp: AuthHttp) {
this.AuthHttp = authHttp;
}
query(): Observable<UserType[]> {
return this.AuthHttp.get('/api/users/')
.map((res:Response) => res.json())
.catch(handleError);
}
get(user<% if(filters.ts) { %>: UserType<% } %> = {id: 'me'}): Observable<UserType> {
return this.AuthHttp.get(`/api/users/${user.id || user._id}`)
.map((res:Response) => res.json())
.catch(handleError);
}
create(user: UserType) {
return this.AuthHttp.post('/api/users/', user)
.map((res:Response) => res.json())
.catch(handleError);
}
changePassword(user, oldPassword, newPassword) {
return this.AuthHttp.put(`/api/users/${user.id || user._id}/password`, {oldPassword, newPassword})
.map((res:Response) => res.json())
.catch(handleError);
}
remove(user) {
return this.AuthHttp.delete(`/api/users/${user.id || user._id}`)
.map(() => user)
.catch(handleError);
}
}