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 33fdff6300a46262b5782cc8b250b145c75ae0b5..683c504340fc8031fbd92e5e8cd4393d32a53748 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 f4dbbb46fc032dabc189c42b7c58bb6608944e72..de9b44c5500c0352c53132f07e72225902c546c0 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 9924876f6696f09331b480fae177ec5d161db5a1..1def3e16d28352b9458248b2045a68229d052f57 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 4739c7343d804c472f691636481e7dd3f7a208d4..eea12ebe70ad54c0095629223d90c5e459a82bcd 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);