Created
February 1, 2018 14:46
-
-
Save RichardSilveira/dda5a04a0f5b3d311c46a6907a20e818 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Injectable() | |
export class ProcessamentoService { | |
private headers: Headers; | |
private baseUrl: string; | |
private historicoProcessamentosSubject = new Subject<Processamento[]>(); | |
historicoProcessamentos$: Observable<Processamento[]> = this.historicoProcessamentosSubject.asObservable(); | |
constructor(private http: Http) { | |
this.headers = new Headers({'Content-Type': 'application/json'}); | |
this.baseUrl = `${environment.processamento_api_endpoint}/processamento`; | |
} | |
processar(processamento: Processamento): Observable<Processamento> { | |
const processamento$ = this.http.post(this.baseUrl, processamento, new RequestOptions({headers: this.headers})) | |
.map(res => res.json()) | |
.catch((error: any) => { | |
return Observable.throw(error.json() || 'Erro Interno de Servidor'); | |
}); | |
this.atualizarHistoricoProcessamentos(); | |
return processamento$; | |
} | |
reprocessar(idPedidoRemesssa: number) { | |
const processamento$ = this.http.post(`${this.baseUrl}/${idPedidoRemesssa}/reprocessar`, null, new RequestOptions({headers: this.headers})) | |
.map(res => res.json()) | |
.catch((error: any) => { | |
return Observable.throw(error.json() || 'Erro Interno de Servidor'); | |
}); | |
this.atualizarHistoricoProcessamentos(); | |
return processamento$; | |
} | |
atualizarHistoricoProcessamentos() { | |
this.obterHistoricoProcessamentos() | |
.do((res) => this.historicoProcessamentosSubject.next(res)); | |
} | |
private obterHistoricoProcessamentos(): Observable<Processamento[]> { | |
return this.http.get(`${this.baseUrl}`, new RequestOptions({headers: this.headers})) | |
.map(res => res.json()) | |
.do((r) => console.log(`processamentos: ${JSON.stringify(r)}`)) | |
.catch((error: any) => { | |
return Observable.throw(error.json() || 'Erro Interno de Servidor'); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment