-
Notifications
You must be signed in to change notification settings - Fork 209
/
pessoa.service.ts
56 lines (41 loc) · 1.36 KB
/
pessoa.service.ts
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
import { Http, Headers, URLSearchParams } from '@angular/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/toPromise';
export class PessoaFiltro {
nome: string;
pagina = 0;
itensPorPagina = 5;
}
@Injectable()
export class PessoaService {
pessoasUrl = 'http://localhost:8080/pessoas';
constructor(private http: Http) { }
pesquisar(filtro: PessoaFiltro): Promise<any> {
const params = new URLSearchParams();
const headers = new Headers();
headers.append('Authorization', 'Basic YWRtaW5AYWxnYW1vbmV5LmNvbTphZG1pbg==');
params.set('page', filtro.pagina.toString());
params.set('size', filtro.itensPorPagina.toString());
if (filtro.nome) {
params.set('nome', filtro.nome);
}
return this.http.get(`${this.pessoasUrl}`, { headers, search: params })
.toPromise()
.then(response => {
const responseJson = response.json();
const pessoas = responseJson.content;
const resultado = {
pessoas,
total: responseJson.totalElements
};
return resultado;
})
}
listarTodas(): Promise<any> {
const headers = new Headers();
headers.append('Authorization', 'Basic YWRtaW5AYWxnYW1vbmV5LmNvbTphZG1pbg==');
return this.http.get(this.pessoasUrl, { headers })
.toPromise()
.then(response => response.json().content);
}
}