85 lines
2.3 KiB
TypeScript
85 lines
2.3 KiB
TypeScript
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
|
import { Injectable } from '@angular/core';
|
|
import { Observable } from 'rxjs/internal/Observable';
|
|
import { catchError } from 'rxjs/operators';
|
|
|
|
import { ErrorLoggerService } from '../core/error-logger.service';
|
|
import { Table } from '../core/table';
|
|
|
|
const httpOptions = {
|
|
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
|
|
};
|
|
const url = '/api/tables';
|
|
const serviceName = 'TableService';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class TableService {
|
|
constructor(private http: HttpClient, private log: ErrorLoggerService) {}
|
|
|
|
get(id: string): Observable<Table> {
|
|
const getUrl: string = id === null ? url : `${url}/${id}`;
|
|
return <Observable<Table>>(
|
|
this.http
|
|
.get<Table>(getUrl)
|
|
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`)))
|
|
);
|
|
}
|
|
|
|
list(): Observable<Table[]> {
|
|
return <Observable<Table[]>>(
|
|
this.http
|
|
.get<Table[]>(`${url}/list`)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'list')))
|
|
);
|
|
}
|
|
|
|
running(): Observable<Table[]> {
|
|
return <Observable<Table[]>>(
|
|
this.http
|
|
.get<Table[]>(`${url}/running`)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'running')))
|
|
);
|
|
}
|
|
|
|
save(tables: Table): Observable<Table> {
|
|
return <Observable<Table>>(
|
|
this.http
|
|
.post<Table>(url, tables, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'save')))
|
|
);
|
|
}
|
|
|
|
update(tables: Table): Observable<Table> {
|
|
return <Observable<Table>>(
|
|
this.http
|
|
.put<Table>(`${url}/${tables.id}`, tables, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'update')))
|
|
);
|
|
}
|
|
|
|
updateSortOrder(list: Table[]): Observable<boolean> {
|
|
return <Observable<boolean>>(
|
|
this.http
|
|
.post<Table[]>(`${url}/list`, list, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'updateSortOrder')))
|
|
);
|
|
}
|
|
|
|
saveOrUpdate(tables: Table): Observable<Table> {
|
|
if (!tables.id) {
|
|
return this.save(tables);
|
|
}
|
|
return this.update(tables);
|
|
}
|
|
|
|
delete(id: string): Observable<Table> {
|
|
return <Observable<Table>>(
|
|
this.http
|
|
.delete<Table>(`${url}/${id}`, httpOptions)
|
|
.pipe(catchError(this.log.handleError(serviceName, 'delete')))
|
|
);
|
|
}
|
|
}
|