From 20c04abe4f232fe1164e3179c9369fd8b9090c73 Mon Sep 17 00:00:00 2001 From: csimonis Date: Tue, 19 Nov 2024 08:15:13 +0100 Subject: [PATCH] idk --- src/app/app.component.ts | 2 - src/app/app.routes.ts | 10 ++++- src/app/hotel/create-hotel.component.ts | 35 +++++++++++++++++ src/app/hotel/hotel.component.ts | 52 ++++++++++++++++++------- src/app/hotel/hotel.ts | 2 +- src/app/hotel/hotels.component.ts | 19 +++++---- src/app/service/HotelData.service.ts | 8 ++-- src/app/service/hotel.service.ts | 4 ++ 8 files changed, 102 insertions(+), 30 deletions(-) create mode 100644 src/app/hotel/create-hotel.component.ts diff --git a/src/app/app.component.ts b/src/app/app.component.ts index ffc14c4..2af0d0c 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -9,8 +9,6 @@ import {RouterOutlet} from "@angular/router"; standalone: true, imports: [HotelsComponent, IdkComponent, RouterOutlet], template: ` - - ` }) diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index 8d67ad2..621afb0 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -1,15 +1,21 @@ import { Routes } from '@angular/router'; import {HotelsComponent} from "./hotel/hotels.component"; import {HotelComponent} from "./hotel/hotel.component"; +import {CreateHotelComponent} from "./hotel/create-hotel.component"; export const routes: Routes = [ { - path: '/hotels', + path: 'hotels', component: HotelsComponent, title: 'Hotels', }, { - path: '/hotels/{id}', + path: 'hotels/new', + component: CreateHotelComponent, + title: 'New Hotel' + }, + { + path: 'hotels/:hotelId', component: HotelComponent, title: 'Hotel', } diff --git a/src/app/hotel/create-hotel.component.ts b/src/app/hotel/create-hotel.component.ts new file mode 100644 index 0000000..c0e4f19 --- /dev/null +++ b/src/app/hotel/create-hotel.component.ts @@ -0,0 +1,35 @@ +import { Component } from '@angular/core'; +import {FormsModule} from "@angular/forms"; + +@Component({ + selector: 'app-create-hotel', + standalone: true, + imports: [ + FormsModule + ], + template: ` + + +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+
+ +
+ `, +}) +export class CreateHotelComponent { + +} diff --git a/src/app/hotel/hotel.component.ts b/src/app/hotel/hotel.component.ts index ce34b53..8c661aa 100644 --- a/src/app/hotel/hotel.component.ts +++ b/src/app/hotel/hotel.component.ts @@ -1,9 +1,11 @@ -import {Component, Input} from "@angular/core"; +import {Component, inject, Input, OnInit} from "@angular/core"; import {Hotel} from "./hotel" -import {CurrencyPipe} from "@angular/common"; +import {CurrencyPipe, NgOptimizedImage} from "@angular/common"; import {Lang} from "../idek/lang"; import {StarComponent} from "../star/star.component"; +import {ActivatedRoute} from "@angular/router"; import {HotelService} from "../service/hotel.service"; +import {catchError, EMPTY, throwError} from "rxjs"; @Component({ @@ -11,24 +13,46 @@ import {HotelService} from "../service/hotel.service"; selector: 'app-hotel', imports: [ CurrencyPipe, - StarComponent + StarComponent, + NgOptimizedImage ], template: `
-

Name: {{ hotel.hotelName }}

-

Beschreibung: {{ hotel.description }}

-

Preis: {{ hotel.price | currency: currency.currency : 'symbol' : '2.2-2' : currency.code }}/nacht

-

Sterne:

- + @if (hotel && !alert) { +

Name: {{ hotel.hotelName }}

+

Beschreibung: {{ hotel.description }}

+

Preis: {{ hotel.price | currency: currency.currency : 'symbol' : '2.2-2' : currency.code }}/nacht

+

Sterne: + +

+ + } @else if(alert) { +

{{alert}}

+ } @else { +

loading

+ } +
` }) -export class HotelComponent { - @Input() - public currency: Lang = {name: 'de', code: 'de-DE', currency: 'EUR'} - @Input() - public hotel!: Hotel; +export class HotelComponent implements OnInit { + protected currency: Lang = {name: 'de', code: 'de-DE', currency: 'EUR'} - constructor() { + protected hotel!: Hotel; + + protected alert: string|undefined + + private route: ActivatedRoute = inject(ActivatedRoute); + + private hotelService: HotelService = inject(HotelService); + + ngOnInit(): void { + const hotelId = this.route.snapshot.params['hotelId']; + this.hotelService.getHotelById(hotelId) + .pipe(catchError((err) => { + this.alert = `Hotel could not be retrieved: ${err.message}` + return EMPTY; + })) + .subscribe({next: (hotel: Hotel) => {this.hotel = hotel}}) } } diff --git a/src/app/hotel/hotel.ts b/src/app/hotel/hotel.ts index a009af8..7a741b9 100644 --- a/src/app/hotel/hotel.ts +++ b/src/app/hotel/hotel.ts @@ -1,5 +1,5 @@ export interface Hotel { - hotelId: number; + id: number; hotelName: string; description: string; price: number; diff --git a/src/app/hotel/hotels.component.ts b/src/app/hotel/hotels.component.ts index 4542389..743531d 100644 --- a/src/app/hotel/hotels.component.ts +++ b/src/app/hotel/hotels.component.ts @@ -4,9 +4,11 @@ import {Hotel} from "./hotel"; import {FormsModule} from "@angular/forms"; import {Lang} from "../idek/lang"; import {HotelService} from "../service/hotel.service"; -import {Observable} from "rxjs"; +import {filter, Observable, toArray} from "rxjs"; import {AsyncPipe} from "@angular/common"; import {CurrencyComponent} from "../currency/currency.component"; +import {RouterLink} from "@angular/router"; +import {StarComponent} from "../star/star.component"; @Component({ standalone: true, @@ -15,14 +17,17 @@ import {CurrencyComponent} from "../currency/currency.component";
- @for (hotel of (matchingHotels | async); track hotel.hotelId) { - -
+ @for (hotel of (matchingHotels | async); track hotel.id) { +
{{hotel.hotelName}}
+ {{hotel.hotelName}} +
+ +
} @empty { -

no matching results for {{search}}

+

no matching results for {{ search }}

} `, - imports: [FormsModule, HotelComponent, AsyncPipe, CurrencyComponent], + imports: [FormsModule, HotelComponent, AsyncPipe, CurrencyComponent, RouterLink, StarComponent], providers: [HotelService], selector: 'app-hotels' }) @@ -38,6 +43,6 @@ export class HotelsComponent { public searchEvent(input: string) { this.search = input.toLowerCase(); - //this.matchingHotels.pipe(filter((hotel: Hotel) => hotel.hotelName.toLowerCase().includes(input.toLowerCase()))); + } } diff --git a/src/app/service/HotelData.service.ts b/src/app/service/HotelData.service.ts index 9859916..f6f5991 100644 --- a/src/app/service/HotelData.service.ts +++ b/src/app/service/HotelData.service.ts @@ -6,7 +6,7 @@ export class HotelDataService implements InMemoryDbService{ createDb(): Record { const hotels: Hotel[] = [ { - "hotelId": 1, + "id": 1, "hotelName": "Buea süßes Leben", "description": "Schöne Aussicht am Meer", "price": 230.5, @@ -14,7 +14,7 @@ export class HotelDataService implements InMemoryDbService{ "rating": 3.5 }, { - "hotelId": 2, + "id": 2, "hotelName": "Marrakesch", "description": "Genießen Sie den Blick auf die Berge", "price": 145.5, @@ -22,7 +22,7 @@ export class HotelDataService implements InMemoryDbService{ "rating": 5 }, { - "hotelId": 3, + "id": 3, "hotelName": "Abuja neuer Palast", "description": "Kompletter Aufenthalt mit Autoservice", "price": 120.12, @@ -30,7 +30,7 @@ export class HotelDataService implements InMemoryDbService{ "rating": 4 }, { - "hotelId": 4, + "id": 4, "hotelName": "OUR Hotel", "description": "Wunderschönes Ambiente für Ihren Aufenthalt", "price": 135.12, diff --git a/src/app/service/hotel.service.ts b/src/app/service/hotel.service.ts index dc1ff34..a55859f 100644 --- a/src/app/service/hotel.service.ts +++ b/src/app/service/hotel.service.ts @@ -12,4 +12,8 @@ export class HotelService { public getHotels(): Observable { return this.httpClient.get('/api/hotels'); } + + public getHotelById(id: number): Observable { + return this.httpClient.get(`/api/hotels/${id}`); + } }