-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcontact.resource.ts
More file actions
49 lines (40 loc) · 1.16 KB
/
contact.resource.ts
File metadata and controls
49 lines (40 loc) · 1.16 KB
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
import {Injectable} from "@angular/core";
import {Http, URLSearchParams} from "@angular/http";
@Injectable()
export class Contact {
private apiRoot: string = 'http://localhost:3000/contacts';
constructor(private http: Http) {
}
static toURLSearchParams(params): URLSearchParams {
let search = new URLSearchParams();
for (let key in params) search.append(key, params[key])
return search;
}
query(params?) {
let search = Contact.toURLSearchParams(params);
return this.http.get(this.apiRoot, {search})
.map(res => res.json())
.toPromise();
}
get(id, params?) {
let search = Contact.toURLSearchParams(params);
return this.http.get(this.apiRoot + '/' + id, {search})
.map(res => res.json())
.toPromise();
}
save(data: any) {
return this.http.post(this.apiRoot, data)
.map(res => res.json())
.toPromise();
}
update(data) {
return this.http.put(this.apiRoot + '/' + data.id, data)
.map(res => res.json())
.toPromise();
}
remove(data) {
return this.http.delete(this.apiRoot + '/' + data.id)
.map(res => res.json())
.toPromise();
}
}