From 7af28820ece347d59cb82cfea20cab8b0d918ed4 Mon Sep 17 00:00:00 2001 From: migudel <miguel.moras@estudiantes.uva.es> Date: Mon, 21 Oct 2024 22:58:44 +0200 Subject: [PATCH] PATCH room endpoint updated, angular client change to use the backend --- .../app/hotel-list/hotel-list.component.html | 7 ++- .../app/hotel-list/hotel-list.component.ts | 53 +++++++++++++------ .../app/shared/cliente-api-rest.service.ts | 2 +- .../Controllers/HotelController.java | 10 +++- 4 files changed, 52 insertions(+), 20 deletions(-) diff --git a/angular/RestClient/src/app/hotel-list/hotel-list.component.html b/angular/RestClient/src/app/hotel-list/hotel-list.component.html index 33fdff6..683c504 100644 --- a/angular/RestClient/src/app/hotel-list/hotel-list.component.html +++ b/angular/RestClient/src/app/hotel-list/hotel-list.component.html @@ -1,5 +1,8 @@ <div class="container"> <h2>Hotel List</h2> + @if (mostrarMensaje) { + <strong style="font-size: xx-large; color: red">{{ mensaje }}</strong> + } <mat-accordion> @for(hotel of hotels; track hotel.id) { <mat-expansion-panel> @@ -28,7 +31,9 @@ <td mat-cell *matCellDef="let room"> <mat-slide-toggle [checked]="room.available" - (change)="toggleRoomAvailability(hotel.id, room.id)" + (change)=" + toggleRoomAvailability(hotel.id, room.id, !room.available) + " ></mat-slide-toggle> </td> </ng-container> diff --git a/angular/RestClient/src/app/hotel-list/hotel-list.component.ts b/angular/RestClient/src/app/hotel-list/hotel-list.component.ts index f4dbbb4..de9b44c 100644 --- a/angular/RestClient/src/app/hotel-list/hotel-list.component.ts +++ b/angular/RestClient/src/app/hotel-list/hotel-list.component.ts @@ -45,11 +45,11 @@ export class HotelListComponent { } getHotelsResponse() { - this.hotels = hotels as Hotel[]; - return; + // this.hotels = hotels as Hotel[]; + // return; this.client.getAllHotels().subscribe({ next: (resp) => { - if (resp.body != null) this.hotels = resp.body; + if (resp.body != null) this.hotels = [...resp.body]; }, error(err) { console.log('Error al traer la lista: ' + err.message); @@ -60,8 +60,8 @@ export class HotelListComponent { deleteHotel(id: number) { if (!confirm(`Borrar hotel con id ${id}. Continuar?`)) return; - this.hotels = this.hotels.filter((h) => h.id !== id); - return; + // this.hotels = this.hotels.filter((h) => h.id !== id); + // return; this.client.deleteHotel(id).subscribe({ next: (resp) => { @@ -81,17 +81,38 @@ export class HotelListComponent { }); } - toggleRoomAvailability(hotelId: number, roomId: number) { - const target = hotels - .find((hotel) => hotel.id === hotelId)! - .rooms.find((room) => room.id === roomId); - if (!target) { - alert('Error'); - return; - } - const availability = !target.available; - target.available = availability; - alert(`Change availability from room ${roomId} to ${availability}`); + toggleRoomAvailability( + hotelId: number, + roomId: number, + availability: boolean + ) { + // const target = hotels + // .find((hotel) => hotel.id === hotelId)! + // .rooms.find((room) => room.id === roomId); + // if (!target) { + // alert('Error'); + // return; + // } + // const availability = !target.available; + // target.available = availability; + // alert(`Change availability from room ${roomId} to ${availability}`); + this.client.alterRoomAvailability(hotelId, roomId, availability).subscribe({ + next: (resp) => { + if (resp.status < 400) { + this.mostrarMensaje = true; + this.mensaje = resp.body as string; + this.getHotelsResponse(); + } else { + this.mostrarMensaje = true; + this.mensaje = 'Error al cambiar disponibilidad'; + console.error(this.mensaje); + } + }, + error: (error) => { + console.log('Error al cambiar disponibilidad: ' + error.message); + throw error; + }, + }); } goToEdit(hotelId: number): void { diff --git a/angular/RestClient/src/app/shared/cliente-api-rest.service.ts b/angular/RestClient/src/app/shared/cliente-api-rest.service.ts index 9924876..1def3e1 100644 --- a/angular/RestClient/src/app/shared/cliente-api-rest.service.ts +++ b/angular/RestClient/src/app/shared/cliente-api-rest.service.ts @@ -38,7 +38,7 @@ export class ClienteApiRestService { const url = `${ClienteApiRestService.HOTEL_URI}/${hotelId}/rooms/${roomId}`; return this.http.patch( url, - { availability }, + { available: availability }, { observe: 'response', responseType: 'text', diff --git a/java/roomBooking/src/main/java/com/uva/roomBooking/Controllers/HotelController.java b/java/roomBooking/src/main/java/com/uva/roomBooking/Controllers/HotelController.java index 4739c73..eea12eb 100644 --- a/java/roomBooking/src/main/java/com/uva/roomBooking/Controllers/HotelController.java +++ b/java/roomBooking/src/main/java/com/uva/roomBooking/Controllers/HotelController.java @@ -1,10 +1,12 @@ package com.uva.roomBooking.Controllers; import java.util.List; +import java.util.Map; import java.time.LocalDate; import com.uva.roomBooking.Exceptions.HotelNotFoundException; import com.uva.roomBooking.Exceptions.InvalidDateRangeException; +import com.uva.roomBooking.Exceptions.InvalidRequestException; import com.uva.roomBooking.Models.Hotel; import com.uva.roomBooking.Models.Room; import com.uva.roomBooking.Repositories.HotelRepository; @@ -79,12 +81,16 @@ public class HotelController { public ResponseEntity<Room> updateRoomAvailability( @PathVariable int hotelId, @PathVariable int roomId, - @RequestBody boolean available) { + @RequestBody Map<String, Boolean> body) { + + if (!body.containsKey("available")) { + throw new InvalidRequestException("El campo 'available' es obligatorio"); + } Room targetRoom = roomRepository.findByIdAndHotelId(roomId, hotelId) .orElseThrow(() -> new IllegalArgumentException("Habitación no encontrada")); - targetRoom.setAvailable(available); + targetRoom.setAvailable(body.get("available")); roomRepository.save(targetRoom); return new ResponseEntity<>(targetRoom, HttpStatus.OK); -- GitLab