Skip to content

Commit

Permalink
feat: add api data provider
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammad-haji committed Aug 15, 2018
1 parent f42f912 commit 8355c92
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/app/@core/services/api/api-data-provider.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Created by Mohammad.hajiaghazadeh on 8/15/2018
*/
import {Injectable, OnInit} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {Observable} from "rxjs/Rx";
import {ApiConfigReaderService} from "./api-config-reader.service";

@Injectable()
export class ApiDataProviderService {
entityKey: string = '';
apiConfig: any;

constructor(private httpClient: HttpClient,
private apiConfigReaderService: ApiConfigReaderService) {
this.apiConfig = this.apiConfigReaderService.getConfig();
}

createApi(entityKey: string) {
this.entityKey = entityKey;
return this;
}

getAll(): Observable<any> {
if (this.isMethodConfigExists) {
return this.httpClient.get(this.apiConfig[this.entityKey].getAll.url);
}
}

getById(): Observable<any> {
if (this.isMethodConfigExists) {
return this.httpClient.get(this.apiConfig[this.entityKey].getById.url);
}
}

find(): Observable<any> {
if (this.isMethodConfigExists) {
return this.httpClient.get(this.apiConfig[this.entityKey].find.url);
}
}

create(payload: any): Observable<any> {
if (this.isMethodConfigExists) {
return this.httpClient.post(this.apiConfig[this.entityKey].create.url, payload);
}
}

update(): Observable<any> {
if (this.isMethodConfigExists) {
return this.httpClient.get(this.apiConfig[this.entityKey].update.url);
}
}

delete(): Observable<any> {
if (this.isMethodConfigExists) {
return this.httpClient.get(this.apiConfig[this.entityKey].delete.url);
}
}

isMethodConfigExists(methodName: string) {
return this.entityKey[methodName]
}
}

0 comments on commit 8355c92

Please sign in to comment.