Initial commit for the Angular part. We are nowhere yet.
This commit is contained in:
66
bookie/src/app/guest-book/guest-book.service.ts
Normal file
66
bookie/src/app/guest-book/guest-book.service.ts
Normal file
@ -0,0 +1,66 @@
|
||||
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 {GuestBook} from './guest-book';
|
||||
import {ErrorLoggerService} from '../core/error-logger.service';
|
||||
|
||||
const httpOptions = {
|
||||
headers: new HttpHeaders({'Content-Type': 'application/json'})
|
||||
};
|
||||
|
||||
const url = '/v1/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}/new` : `${url}/${id}`;
|
||||
return <Observable<GuestBook>>this.http.get<GuestBook>(getUrl)
|
||||
.pipe(
|
||||
catchError(this.log.handleError(serviceName, `get id=${id}`))
|
||||
);
|
||||
}
|
||||
|
||||
list(date: string): Observable<GuestBook[]> {
|
||||
const options = {params: new HttpParams().set('q', (date === null) ? '' : date)};
|
||||
const listUrl: string = `${url}/list`;
|
||||
return <Observable<GuestBook[]>>this.http.get<GuestBook[]>(listUrl, options)
|
||||
.pipe(
|
||||
catchError(this.log.handleError(serviceName, 'list'))
|
||||
);
|
||||
}
|
||||
|
||||
save(guestBook: GuestBook): Observable<GuestBook> {
|
||||
return <Observable<GuestBook>>this.http.put<GuestBook>(`${url}/new`, guestBook, httpOptions)
|
||||
.pipe(
|
||||
catchError(this.log.handleError(serviceName, 'save'))
|
||||
);
|
||||
}
|
||||
|
||||
update(guestBook: GuestBook): Observable<GuestBook> {
|
||||
return <Observable<GuestBook>>this.http.post<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);
|
||||
} else {
|
||||
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'))
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user