import { Injectable, Inject , Output} from '@angular/core'; import { HttpClient, HttpParams } from '@angular/common/http'; import { map } from 'rxjs/operators'; import { Observable, of } from 'rxjs'; import { ConfigureUrl,Lookup,LookupEdit,ResultModel } from '../models'; import { Utils,AppSettingService } from '../shares'; @Injectable({ providedIn: 'root' }) export class LookupService { msg: string = "[lookupService] "; _lookupByStatus:any; _staffList:any; _clientList:any; _jobList:any; constructor(private http: HttpClient, private appSetting :AppSettingService ) { // this default when load look for previous login information // if no need then comment out downthere this.setAllNull(); } setAllNull() :void { // this._lhdList = undefined; this._lookupByStatus = {}; this._staffList = null; this._clientList = null; this._jobList = null; } loadLookupEditBystatus(type:string): Observable> { const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.lookupUrl + "/LoadLookupEdit"; const params = new HttpParams().append('type', type); console.log(this.msg + "go server this loadLookupEditBystatus is in cache ", type); return this.http.get>(baseUrl, {params}); } loadLookupById(id:number, type:string): Observable> { const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.lookupUrl; const params = new HttpParams() .append('id', id) .append('type', type); return this.http.get>(baseUrl,{params}); } saveLookup(item:LookupEdit): Observable> { //insert Lookup this.setAllNull(); let config = { headers : { 'Content-Type': 'application/json' } }; const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.lookupUrl; return this.http.post>(baseUrl, item, config); } deleteLookup(id:number): Observable>{ const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.lookupUrl; return this.http.delete>(baseUrl + "/" + id ); } loadLookupBystatus(type:string): Observable> { const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.lookupUrl + "/LoadJobs"; console.log(this.msg + "loadLookupBystatus", this._lookupByStatus); if (this._lookupByStatus[type]) { console.log(this.msg + "Not go server this LoadJobs is in cache ",type); return of(this._lookupByStatus[type]); } else { const params = new HttpParams().append('type', type); console.log(this.msg + "go server this loadLookupBystatus is in cache ", type); return this.http.get>(baseUrl, {params}) .pipe(map(resultModel => { this._lookupByStatus[type] = resultModel; return resultModel; }) ); } } loadStaffs(): Observable> { const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.lookupUrl + "/GetStaffs"; /* const params = new HttpParams() .append('id', id) .append('type', type); return this.http.get>(baseUrl,{params}); */ if (this._staffList != null) { return of(this._staffList); } else { return this.http.get>(baseUrl) .pipe(map( x => { this._staffList = x; return x; })); } } loadClients(): Observable> { const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.lookupUrl + "/GetClients"; /* const params = new HttpParams() .append('id', id) .append('type', type); return this.http.get>(baseUrl,{params}); */ if (this._clientList != null) { return of(this._clientList); } else { return this.http.get>(baseUrl) .pipe(map(resultModel => { this._clientList = resultModel; return resultModel; }) ); } } loadJobs(): Observable> { const baseUrl = this.appSetting.appSetting.baseUrl + "/"+ ConfigureUrl.lookupUrl + "/GetJobs"; /* const params = new HttpParams() .append('id', id) .append('type', type); return this.http.get>(baseUrl,{params}); */ if (this._jobList != null) { return of(this._jobList); } else { return this.http.get>(baseUrl) .pipe(map(x => { this._jobList = x; return x; })) } } }