Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardSilveira/dda5a04a0f5b3d311c46a6907a20e818 to your computer and use it in GitHub Desktop.
Save RichardSilveira/dda5a04a0f5b3d311c46a6907a20e818 to your computer and use it in GitHub Desktop.
@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