File: /var/dev/nowruzgan/admin/src/app/services/ref.service.ts
import { Injectable } from '@angular/core';
import { GenericService } from './generic.service';
import { Ref, Batch } from '../data.types';
import { environment } from '../../environments/environment';
@Injectable()
export class RefService extends GenericService {
async list(refsBatch: Batch<Ref>) {
let skip: string = `skip=${refsBatch.offset || 0}`;
let limit: string = `limit=${refsBatch.limit || 30}`;
let sort: string = `sort=${refsBatch.sort}`;
let filter: string = refsBatch.filter ? `filter=${refsBatch.filter}` : '';
let response = await this.http
.get(`${environment.apiBase}/encyc/ref?${skip}&${limit}&${sort}&${filter}`, this.headers)
.toPromise().catch(e => {
this.handleError(e);
return {sum: 0, limit: 0, list: []};
});
refsBatch.absorb(response);
}
async load(id: string): Promise<Ref | null> {
let result: any = await this.http
.get(`${environment.apiBase}/encyc/ref/${id}`, this.headers)
.toPromise().catch(e => this.handleError(e));
if(!result) return null;
return new Ref(result);
}
async save(ref: Ref): Promise<boolean> {
ref.meta.busySave = true;
ref.meta.errorSave = false;
let result: any = await this.http
.patch(`${environment.apiBase}/encyc/ref/${ref.id}`, ref.trans, this.headers)
.toPromise()
.catch(error => this.handleError(error));
ref.meta.busySave = false;
ref.meta.errorSave = !result;
return !!result;
}
async create(ref: Ref): Promise<Ref> {
let refData: any = await this.http
.post(`${environment.apiBase}/encyc/ref`, ref.trans, this.headers)
.toPromise()
.catch(e => this.handleError(e));
return new Ref(refData);
}
async remove(ref: Ref): Promise<boolean> {
ref.meta.busyDelete = true;
ref.meta.errorDelete = false;
let result: any = await this.http
.delete(`${environment.apiBase}/encyc/ref/${ref.id}`, this.headers)
.toPromise()
.catch(error => this.handleError(error));
ref.meta.busyDelete = false;
ref.meta.errorDelete = !result;
return !!result;
}
async search(value: string): Promise<Ref[]> {
let response: any = await this.http
.get(`${environment.apiBase}/encyc/ref?filter=${value}`, this.headers)
.toPromise().catch(e => {this.handleError(e); return{list: []};});
return response.list.map((record: any) => new Ref(record));
}
}