Files
barker/bookie/src/app/guest-book/guest-book.service.ts
tanshu d677cfb1ea Blacked and isorted the python files
Prettied and eslinted the typescript/html files
2020-10-11 10:56:29 +05:30

71 lines
2.2 KiB
TypeScript

import { HttpClient, HttpHeaders, HttpParams } 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 { GuestBook } from './guest-book';
import { GuestBookList } from './guest-book-list';
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
};
const url = '/api/guest-book';
const serviceName = 'GuestBookService';
@Injectable({ providedIn: 'root' })
export class GuestBookService {
constructor(private http: HttpClient, private log: ErrorLoggerService) {}
get(id: string): Observable<GuestBook> {
const getUrl: string = id === null ? url : `${url}/${id}`;
return <Observable<GuestBook>>(
this.http
.get<GuestBook>(getUrl)
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`)))
);
}
list(date: string): Observable<GuestBookList> {
const options = { params: new HttpParams().set('q', date === null ? '' : date) };
return <Observable<GuestBookList>>(
this.http
.get<GuestBookList>(`${url}/list`, options)
.pipe(catchError(this.log.handleError(serviceName, 'list')))
);
}
save(guestBook: GuestBook): Observable<GuestBook> {
return <Observable<GuestBook>>(
this.http
.post<GuestBook>(url, guestBook, httpOptions)
.pipe(catchError(this.log.handleError(serviceName, 'save')))
);
}
update(guestBook: GuestBook): Observable<GuestBook> {
return <Observable<GuestBook>>(
this.http
.put<GuestBook>(`${url}/${guestBook.id}`, guestBook, httpOptions)
.pipe(catchError(this.log.handleError(serviceName, 'update')))
);
}
saveOrUpdate(guestBook: GuestBook): Observable<GuestBook> {
if (!guestBook.id) {
return this.save(guestBook);
}
return this.update(guestBook);
}
delete(id: string): Observable<GuestBook> {
return <Observable<GuestBook>>(
this.http
.delete<GuestBook>(`${url}/${id}`, httpOptions)
.pipe(catchError(this.log.handleError(serviceName, 'delete')))
);
}
}