diff --git a/src/app/hotel-details/hotel-details.component.ts b/src/app/hotel-details/hotel-details.component.ts index ef39b15..2d3c396 100644 --- a/src/app/hotel-details/hotel-details.component.ts +++ b/src/app/hotel-details/hotel-details.component.ts @@ -1,10 +1,11 @@ import { HttpClient } from '@angular/common/http'; import { Component, OnInit } from '@angular/core'; -import { ActivatedRoute } from '@angular/router'; +import { ActivatedRoute, Router } from '@angular/router'; import { Hotel } from '../HotelItem/hotel'; import { CurrencyPipe } from '@angular/common'; import { StarRatingComponent } from '../star-rating/star-rating.component'; import { HotelItem } from '../HotelItem/HotelItem.component'; +import { catchError, EMPTY } from 'rxjs'; @Component({ selector: 'app-hotel-details', @@ -16,14 +17,21 @@ import { HotelItem } from '../HotelItem/HotelItem.component'; export class HotelDetailsComponent implements OnInit { public hotel: any; - constructor(private route: ActivatedRoute, private http: HttpClient) { } + constructor(private route: ActivatedRoute, private http: HttpClient, private router: Router) { } ngOnInit(): void { const routeParams = this.route.snapshot.paramMap; const hotelId = routeParams.get("id"); - this.http.get("api/hotels/" + hotelId).subscribe(res => { + this.http.get("api/hotels/" + hotelId).pipe( + catchError(() => { + alert("Not Found"); + this.router.navigate(["/"]); + + return EMPTY; + }) + ).subscribe(res => { this.hotel = res; - }); + }) } }