Skip to content

Instantly share code, notes, and snippets.

@filipemansano
Last active April 16, 2020 20:17
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 filipemansano/658fab511f84d57397e407f3e07bc486 to your computer and use it in GitHub Desktop.
Save filipemansano/658fab511f84d57397e407f3e07bc486 to your computer and use it in GitHub Desktop.
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { CrudInterface } from "@app/core/crud.interface";
import { environment } from "@env/environment";
import { Observable } from "rxjs/Observable";
@Injectable()
export class CRUD<T> implements CrudInterface<T>{
endpoint: string;
constructor(private http: HttpClient, routeDir: string){
this.endpoint = `${environment.endpoint}/${routeDir}`;
}
getAll(): Observable<T[]> {
return this.http.get<T[]>(`${this.endpoint}`);
}
get(id: number): Observable<T> {
return this.http.get<T>(`${this.endpoint}/${id}`);
}
create(object: T): Observable<T> {
return this.http.post<T>(`${this.endpoint}`, object);
}
update(object: T): Observable<T> {
return this.http.put<T>(`${this.endpoint}`, object);
}
delete(id: number): Observable<any> {
return this.http.delete<T>(`${this.endpoint}/${id}`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment