import { Injectable } from '@angular/core' ; import { HttpClient } from '@angular/common/http' ; import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { Book } from '../common/book' ; import { BookCategory } from '../common/book-category' ; @ Injectable ( { providedIn : 'root' } ) export class BookService { private baseUrl = "http://localhost:8080/api/v1/books" ; private categoryUrl = "http://localhost:8080/api/v1/book-category" ; constructor ( private httpClient ) { } getBooks ( theCategoryId ) { const searchUrl = `${ this . baseUrl }/search/categoryid?id=${ theCategoryId }` ; return this . getBooksList ( searchUrl ) ; } getBooksPaginate ( theCategoryId , currentPage , pageSize ) { const searchUrl = `${ this . baseUrl }/search/categoryid?id=${ theCategoryId }&page=${ currentPage }&size=${ pageSize }` ; return this . httpClient . get < GetResponseBooks > ( searchUrl ) ; } getBookCategories ( ) { return this . httpClient . get < GetResponseBookCategory > ( this . categoryUrl ) . pipe ( map ( response => response . _embedded . bookCateogry ) ) ; } searchBooks ( keyword , currentPage , pageSize ) { const searchUrl = `${ this . baseUrl }/search/searchbykeyword?name=${ keyword }&page=${ currentPage }&size=${ pageSize }` ; return this . httpClient . get < GetResponseBooks > ( searchUrl ) ; } private getBooksList ( searchUrl ) { return this . httpClient . get < GetResponseBooks > ( searchUrl ) . pipe ( map ( response => response . _embedded . books ) ) ; } get ( bookId ) { const bookDetailsUrl = `${ this . baseUrl }/${ bookId }` ; return this . httpClient . get < Book > ( bookDetailsUrl ) ; } } interface GetResponseBooks { _embedded : { books : Book [ ] ; } ; page : { size : number ; totalElements : number ; totalPages : number ; number : number ; } ; } interface GetResponseBookCategory { _embedded : { bookCateogry : BookCategory [ ] ; } ; }