Skip to content

Instantly share code, notes, and snippets.

@ipabz
Last active May 21, 2018 12:45
Show Gist options
  • Save ipabz/e1010d72be5640c8a05db7e2201ca973 to your computer and use it in GitHub Desktop.
Save ipabz/e1010d72be5640c8a05db7e2201ca973 to your computer and use it in GitHub Desktop.
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/throw';
import { Observable } from 'rxjs/Observable';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { AppError } from '../errors/app-error';
import { BadInput } from '../errors/bad-input';
import { NotFoundError } from '../errors/not-found-error';
@Injectable()
export class DataService {
constructor(private url: string, private http: HttpClient) { }
getAll() {
return this.http.get(this.url)
.map(response => response)
.catch(this.handleError);
}
create(resource) {
return this.http.post(this.url, JSON.stringify(resource))
.map(response => response)
.catch(this.handleError);
}
update(resource) {
return this.http.patch(this.url + '/' + resource.id, JSON.stringify(resource))
.map(response => response)
.catch(this.handleError);
}
delete(id) {
return this.http.delete(this.url + '/' + id)
.map(response => response)
.catch(this.handleError);
}
private handleError(error: Response) {
if (error.status === 400) {
return Observable.throw(new BadInput(error.json()));
}
if (error.status === 404) {
return Observable.throw(new NotFoundError());
}
return Observable.throw(new AppError(error));
}
}
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from '../../../environments/environment';
import { DataService } from '../../shared/services/data.service';
@Injectable()
export class VendorInvoicesService extends DataService {
constructor(http: HttpClient) {
super(`${environment.baseUrl}/api/vendor_invoices`, http);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment