Skip to content
Snippets Groups Projects
Commit 7af28820 authored by migudel's avatar migudel :speech_balloon:
Browse files

PATCH room endpoint updated, angular client change to use the backend

parent 68a00d22
No related branches found
No related tags found
1 merge request!10Add ts types and json mocks, remove poblate scripts and fix the cascade...
<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>
......
......
......@@ -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;
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);
}
const availability = !target.available;
target.available = availability;
alert(`Change availability from room ${roomId} to ${availability}`);
},
error: (error) => {
console.log('Error al cambiar disponibilidad: ' + error.message);
throw error;
},
});
}
goToEdit(hotelId: number): void {
......
......
......@@ -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',
......
......
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);
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment