src/app/api/places.service.ts
constructor(httpClient: HttpClient)
|
getPlacesList |
getPlacesList()
|
Returns:
Observable<Place[]>
|
getTopPlacesList |
getTopPlacesList()
|
Returns:
Observable<Place[]>
|
getFavouritePlacesList |
getFavouritePlacesList()
|
Returns:
Observable<Place[]>
|
getRecommendedPlaces |
getRecommendedPlaces()
|
Returns:
Observable<Place[]>
|
getPlacesByCategory |
getPlacesByCategory(category: string)
|
Returns:
Observable<Place[]>
|
getCategoryList |
getCategoryList()
|
Returns:
Observable<Category[]>
|
toggleFavourite |
toggleFavourite(place: Place)
|
Returns:
Observable<Place>
|
getTags |
getTags()
|
Returns:
Observable<string[]>
|
Private baseUrl |
baseUrl: |
Default value: http://localhost:5000
|
import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Observable} from 'rxjs'
import { map } from 'rxjs/operators';
import {Place} from '../Place';
import {Category} from '../Category';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
}
@Injectable({
providedIn: 'root'
})
export class PlacesService {
private baseUrl = 'http://localhost:5000';
constructor(private httpClient: HttpClient) {
}
getPlacesList(): Observable<Place[]> {
return this.httpClient.get<Place[]>(this.baseUrl + '/places');
}
getTopPlacesList(): Observable<Place[]> {
return this.httpClient.get<Place[]>(this.baseUrl + '/places')
.pipe(map((response: any) => response.filter((x: Place) => x.rating > 3)));
}
getFavouritePlacesList(): Observable<Place[]> {
return this.httpClient.get<Place[]>(this.baseUrl + '/places')
.pipe(map((response: any) => response.filter((x: Place) => x.isFavourite)));
}
getRecommendedPlaces(): Observable<Place[]> {
return this.httpClient.get<Place[]>(this.baseUrl + '/places')
.pipe(map((response: any) => response.filter((x: Place) => x.tags.includes("Recommended"))));
}
getPlacesByCategory(category: string): Observable<Place[]> {
return this.httpClient.get<Place[]>(this.baseUrl + '/places')
.pipe(map((response: any) => response.filter((x: Place) => x.category.includes(category))));
}
getCategoryList(): Observable<Category[]> {
return this.httpClient.get<Category[]>(this.baseUrl + '/categories');
}
toggleFavourite(place: Place): Observable<Place> {
const url = `${this.baseUrl}/places/${place.id}`;
return this.httpClient.put<Place>(url, place, httpOptions)
}
getTags(): Observable<string[]> {
const url = `${this.baseUrl}/tags`;
return this.httpClient.get<string[]>(url);
}
}