Tables List and Detail working
TODO: Save SortOrder also, Drag and Drop ordering
This commit is contained in:
65
bookie/src/app/tables/table.service.ts
Normal file
65
bookie/src/app/tables/table.service.ts
Normal file
@ -0,0 +1,65 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
|
||||
import {ErrorLoggerService} from '../core/error-logger.service';
|
||||
import {catchError} from 'rxjs/operators';
|
||||
import {Observable} from 'rxjs/internal/Observable';
|
||||
import {Table} from '../core/table';
|
||||
|
||||
const httpOptions = {
|
||||
headers: new HttpHeaders({'Content-Type': 'application/json'})
|
||||
};
|
||||
const url = '/v1/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}/new` : `${url}/${id}`;
|
||||
return <Observable<Table>>this.http.get<Table>(getUrl)
|
||||
.pipe(
|
||||
catchError(this.log.handleError(serviceName, `get id=${id}`))
|
||||
);
|
||||
}
|
||||
|
||||
list(): Observable<Table[]> {
|
||||
const options = {params: new HttpParams().set('l', '')};
|
||||
return <Observable<Table[]>>this.http.get<Table[]>(url, options)
|
||||
.pipe(
|
||||
catchError(this.log.handleError(serviceName, 'list'))
|
||||
);
|
||||
}
|
||||
|
||||
save(tables: Table): Observable<Table> {
|
||||
return <Observable<Table>>this.http.post<Table>(`${url}/new`, 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'))
|
||||
);
|
||||
}
|
||||
|
||||
saveOrUpdate(tables: Table): Observable<Table> {
|
||||
if (!tables.id) {
|
||||
return this.save(tables);
|
||||
} else {
|
||||
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'))
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user