Assignment 5
Assignment 5
Ans =
server.js
const express = require('express');
const mongoose = require('mongoose');
const hotelRoutes = require('./routes/hotelRoutes');
const bookingRoutes = require('./routes/bookingRoutes');
const app = express();
const PORT = 3000;
app.use(express.json());
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
hotel.js
const mongoose = require('mongoose');
booking.js
contorlleer.js
const Booking = require('../models/Booking');
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent,
HomeComponent,
HotelListComponent,
HotelDetailsComponent,
BookingComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
hotellist.component.ts
import { Component, OnInit } from '@angular/core';
import { HotelService } from '../../services/hotel.service';
@Component({
selector: 'app-hotel-list',
templateUrl: './hotel-list.component.html'
})
export class HotelListComponent implements OnInit {
hotels: any[] = [];
ngOnInit(): void {
this.hotelService.getHotels().subscribe(data => {
this.hotels = data;
});
}
}
hotel-details.ts
<h2>{{ hotel?.name }}</h2>
<p>{{ hotel?.description }}</p>
<p>Location: {{ hotel?.location }}</p>
<p>Rooms: {{ hotel?.rooms }}</p>
<p>Price: ${{ hotel?.price }}</p>
<a routerLink="/booking" [queryParams]="{ hotelId: hotel?._id }">Book Now</a>
Booking.html
<h2>Book a Room</h2>
<form (submit)="bookRoom()">
<div>
<label>Customer Name:</label>
<input type="text" [(ngModel)]="booking.customerName" name="customerName"
required>
</div>
<div>
<label>Check-in Date:</label>
<input type="date" [(ngModel)]="booking.checkInDate" name="checkInDate"
required>
</div>
<div>
<label>Check-out Date:</label>
<input type="date" [(ngModel)]="booking.checkOutDate" name="checkOutDate"
required>
</div>
<div>
<label>Room Type:</label>
<input type="text" [(ngModel)]="booking.roomType" name="roomType" required>
</div>
<div>
<input type="hidden" [(ngModel)]="booking.hotelId" name="hotelId">
</div>
<button type="submit">Book</button>
</form>
hotel-servive.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class HotelService {
private apiUrl = 'http://localhost:3000/api/hotels';
constructor(private http: HttpClient) { }
getHotels(): Observable<any[]> {
return this.http.get<any[]>(this.apiUrl);
}
2) Design web page to implement page navigation for books listing using
routing concepts.
Ans =
book.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class BookService {
private apiUrl = 'http://localhost:3000/api/books'; // Assuming you have a
backend API
getBooks(): Observable<any[]> {
return this.http.get<any[]>(this.apiUrl);
}
app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { BookListComponent } from
'./components/book-list/book-list.component';
import { BookDetailsComponent } from
'./components/book-details/book-details.component';
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Book-list.html
<h2>Book List</h2>
<ul>
<li *ngFor="let book of books">
<a [routerLink]="['/book', book.id]">{{ book.title }}</a>
</li>
</ul>
bookdetails.components.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { BookService } from '../../services/book.service';
@Component({
selector: 'app-book-details',
templateUrl: './book-details.component.html'
})
export class BookDetailsComponent implements OnInit {
book: any;
ngOnInit(): void {
const id = this.route.snapshot.paramMap.get('id');
this.bookService.getBookById(id).subscribe(data => {
this.book = data;
});
}
}
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent,
BookListComponent,
BookDetailsComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
App.component.html
<router-outlet></router-outlet>
Db.json
{
"books": [
{ "id": "1", "title": "Book One", "description": "Description of Book One", "author":
"Author One", "price": 10 },
{ "id": "2", "title": "Book Two", "description": "Description of Book Two", "author":
"Author Two", "price": 15 }
]
}