Added prettier and also prettied all the typescript files using prettier ESLint is using the AirBnB rules which are the most strict to lint the files.
77 lines
2.3 KiB
TypeScript
77 lines
2.3 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { Observable } from 'rxjs/internal/Observable';
|
|
import { catchError } from 'rxjs/operators';
|
|
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
|
|
import { Employee } from './employee';
|
|
import { ErrorLoggerService } from '../core/error-logger.service';
|
|
|
|
const httpOptions = {
|
|
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
|
|
};
|
|
|
|
const url = '/api/employees';
|
|
const serviceName = 'EmployeeService';
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
export class EmployeeService {
|
|
constructor(private http: HttpClient, private log: ErrorLoggerService) {}
|
|
|
|
get(id: string): Observable<Employee> {
|
|
const getUrl: string = id === null ? `${url}` : `${url}/${id}`;
|
|
return <Observable<Employee>>(
|
|
this.http
|
|
.get<Employee>(getUrl)
|
|
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`)))
|
|
);
|
|
}
|
|
|
|
list(): Observable<Employee[]> {
|
|
return <Observable<Employee[]>>(
|
|
this.http
|
|
.get<Employee[]>(`${url}/list`)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'list')))
|
|
);
|
|
}
|
|
|
|
save(employee: Employee): Observable<Employee> {
|
|
return <Observable<Employee>>(
|
|
this.http
|
|
.post<Employee>(`${url}`, employee, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'save')))
|
|
);
|
|
}
|
|
|
|
update(employee: Employee): Observable<Employee> {
|
|
return <Observable<Employee>>(
|
|
this.http
|
|
.put<Employee>(`${url}/${employee.id}`, employee, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'update')))
|
|
);
|
|
}
|
|
|
|
saveOrUpdate(employee: Employee): Observable<Employee> {
|
|
if (!employee.id) {
|
|
return this.save(employee);
|
|
} else {
|
|
return this.update(employee);
|
|
}
|
|
}
|
|
|
|
delete(id: string): Observable<Employee> {
|
|
return <Observable<Employee>>(
|
|
this.http
|
|
.delete<Employee>(`${url}/${id}`, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'delete')))
|
|
);
|
|
}
|
|
|
|
autocomplete(term: string): Observable<Employee[]> {
|
|
const options = { params: new HttpParams().set('q', term) };
|
|
return <Observable<Employee[]>>(
|
|
this.http
|
|
.get<Employee[]>(`${url}/query`, options)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'autocomplete')))
|
|
);
|
|
}
|
|
}
|