From f3a552fed6ad1ca901f82960fabb08c697d5e7c3 Mon Sep 17 00:00:00 2001 From: Hugo <hugo.cubino@estudiantes.uva.es> Date: Tue, 24 Dec 2024 15:02:44 +0100 Subject: [PATCH] =?UTF-8?q?Cambiada=20estructura=20de=20paquetes=20e=20inc?= =?UTF-8?q?lu=C3=ADdo=20m=C3=A9todo=20HotelService?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hotels}/HotelsApplication.java | 0 .../hotels}/api/BookingAPI.java | 0 .../hotels}/api/ManagerAPI.java | 0 .../hotels}/config/RestTemplateConfig.java | 0 .../hotels}/config/SecurityConfig.java | 2 +- .../hotels/controllers/HotelController.java | 91 ++++++++++ .../exceptions/GlobalExceptionHandler.java | 0 .../exceptions/HotelNotFoundException.java | 0 .../exceptions/InvalidDateRangeException.java | 0 .../exceptions/InvalidRequestException.java | 0 .../filter/JwtAuthenticationFilter.java | 2 +- .../interceptor/AuthHttpInterceptor.java | 2 +- .../hotels/models/Address.java | 2 +- .../hotels/models/Hotel.java | 2 +- .../services => apis}/hotels/models/Room.java | 2 +- .../hotels/models/RoomType.java | 2 +- .../hotels/models/external/users/UserRol.java | 5 + .../hotels/repositories/HotelRepository.java | 4 +- .../hotels/repositories/RoomRepository.java | 4 +- .../apis/hotels/services/HotelService.java | 107 ++++++++++++ .../hotels}/utils/JwtUtil.java | 2 +- .../hotels/controllers/HotelController.java | 164 ------------------ .../hotels/models/external/users/UserRol.java | 5 - 23 files changed, 215 insertions(+), 181 deletions(-) rename java/services/hotels/src/main/java/com/uva/{monolith => apis/hotels}/HotelsApplication.java (100%) rename java/services/hotels/src/main/java/com/uva/{monolith => apis/hotels}/api/BookingAPI.java (100%) rename java/services/hotels/src/main/java/com/uva/{monolith => apis/hotels}/api/ManagerAPI.java (100%) rename java/services/hotels/src/main/java/com/uva/{monolith => apis/hotels}/config/RestTemplateConfig.java (100%) rename java/services/hotels/src/main/java/com/uva/{monolith => apis/hotels}/config/SecurityConfig.java (96%) create mode 100644 java/services/hotels/src/main/java/com/uva/apis/hotels/controllers/HotelController.java rename java/services/hotels/src/main/java/com/uva/{monolith => apis/hotels}/exceptions/GlobalExceptionHandler.java (100%) rename java/services/hotels/src/main/java/com/uva/{monolith => apis/hotels}/exceptions/HotelNotFoundException.java (100%) rename java/services/hotels/src/main/java/com/uva/{monolith => apis/hotels}/exceptions/InvalidDateRangeException.java (100%) rename java/services/hotels/src/main/java/com/uva/{monolith => apis/hotels}/exceptions/InvalidRequestException.java (100%) rename java/services/hotels/src/main/java/com/uva/{monolith => apis/hotels}/filter/JwtAuthenticationFilter.java (98%) rename java/services/hotels/src/main/java/com/uva/{monolith => apis/hotels}/interceptor/AuthHttpInterceptor.java (95%) rename java/services/hotels/src/main/java/com/uva/{monolith/services => apis}/hotels/models/Address.java (97%) rename java/services/hotels/src/main/java/com/uva/{monolith/services => apis}/hotels/models/Hotel.java (96%) rename java/services/hotels/src/main/java/com/uva/{monolith/services => apis}/hotels/models/Room.java (95%) rename java/services/hotels/src/main/java/com/uva/{monolith/services => apis}/hotels/models/RoomType.java (55%) create mode 100644 java/services/hotels/src/main/java/com/uva/apis/hotels/models/external/users/UserRol.java rename java/services/hotels/src/main/java/com/uva/{monolith/services => apis}/hotels/repositories/HotelRepository.java (66%) rename java/services/hotels/src/main/java/com/uva/{monolith/services => apis}/hotels/repositories/RoomRepository.java (79%) create mode 100644 java/services/hotels/src/main/java/com/uva/apis/hotels/services/HotelService.java rename java/services/hotels/src/main/java/com/uva/{monolith => apis/hotels}/utils/JwtUtil.java (93%) delete mode 100644 java/services/hotels/src/main/java/com/uva/monolith/services/hotels/controllers/HotelController.java delete mode 100644 java/services/hotels/src/main/java/com/uva/monolith/services/hotels/models/external/users/UserRol.java diff --git a/java/services/hotels/src/main/java/com/uva/monolith/HotelsApplication.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/HotelsApplication.java similarity index 100% rename from java/services/hotels/src/main/java/com/uva/monolith/HotelsApplication.java rename to java/services/hotels/src/main/java/com/uva/apis/hotels/HotelsApplication.java diff --git a/java/services/hotels/src/main/java/com/uva/monolith/api/BookingAPI.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/api/BookingAPI.java similarity index 100% rename from java/services/hotels/src/main/java/com/uva/monolith/api/BookingAPI.java rename to java/services/hotels/src/main/java/com/uva/apis/hotels/api/BookingAPI.java diff --git a/java/services/hotels/src/main/java/com/uva/monolith/api/ManagerAPI.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/api/ManagerAPI.java similarity index 100% rename from java/services/hotels/src/main/java/com/uva/monolith/api/ManagerAPI.java rename to java/services/hotels/src/main/java/com/uva/apis/hotels/api/ManagerAPI.java diff --git a/java/services/hotels/src/main/java/com/uva/monolith/config/RestTemplateConfig.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/config/RestTemplateConfig.java similarity index 100% rename from java/services/hotels/src/main/java/com/uva/monolith/config/RestTemplateConfig.java rename to java/services/hotels/src/main/java/com/uva/apis/hotels/config/RestTemplateConfig.java diff --git a/java/services/hotels/src/main/java/com/uva/monolith/config/SecurityConfig.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/config/SecurityConfig.java similarity index 96% rename from java/services/hotels/src/main/java/com/uva/monolith/config/SecurityConfig.java rename to java/services/hotels/src/main/java/com/uva/apis/hotels/config/SecurityConfig.java index 6a82598..ccb40af 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/config/SecurityConfig.java +++ b/java/services/hotels/src/main/java/com/uva/apis/hotels/config/SecurityConfig.java @@ -9,7 +9,7 @@ import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import com.uva.monolith.filter.JwtAuthenticationFilter; -import com.uva.monolith.services.hotels.models.external.users.UserRol; +import com.uva.monolith.models.external.users.UserRol; @Configuration @EnableWebSecurity diff --git a/java/services/hotels/src/main/java/com/uva/apis/hotels/controllers/HotelController.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/controllers/HotelController.java new file mode 100644 index 0000000..643c08f --- /dev/null +++ b/java/services/hotels/src/main/java/com/uva/apis/hotels/controllers/HotelController.java @@ -0,0 +1,91 @@ +package com.uva.monolith.controllers; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.time.LocalDate; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.*; + +import com.uva.monolith.api.BookingAPI; +import com.uva.monolith.api.ManagerAPI; +import com.uva.monolith.exceptions.HotelNotFoundException; +import com.uva.monolith.exceptions.InvalidDateRangeException; +import com.uva.monolith.exceptions.InvalidRequestException; +import com.uva.monolith.models.Hotel; +import com.uva.monolith.models.Room; +import com.uva.monolith.repositories.HotelRepository; +import com.uva.monolith.repositories.RoomRepository; +import com.uva.monolith.services.HotelService; + +@RestController +@RequestMapping("hotels") +@CrossOrigin(origins = "*") +public class HotelController { + @Autowired + private HotelService hotelService; + + @GetMapping + public List<Hotel> getAllHotels( + @RequestParam(required = false) Integer managerId, + @RequestParam(required = false) LocalDate start, + @RequestParam(required = false) LocalDate end) { + return hotelService.getAllHotels(managerId, start, end); + } + + @PostMapping + public ResponseEntity<?> addHotel(@RequestBody Hotel hotel) { + Hotel savedHotel = hotelService.addHotel(hotel); + return new ResponseEntity<>(savedHotel, HttpStatus.CREATED); + } + + @GetMapping("/{id}") + public Hotel getHotelById(@PathVariable int id) { + return hotelService.getHotelById(id); + } + + @DeleteMapping + public ResponseEntity<?> deleteHotelsByManagerId( + @RequestParam(required = true) Integer managerId) { + List<Hotel> hotels = hotelService.deleteHotelsByManagerId(managerId); + return new ResponseEntity<>(hotels, HttpStatus.OK); + } + + @DeleteMapping("/{id}") + @Transactional + public ResponseEntity<Void> deleteHotel(@PathVariable Integer id) { + hotelService.deleteHotel(id); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + + @GetMapping("/{hotelId}/rooms") + public ResponseEntity<List<Room>> getRoomsFromHotel( + @PathVariable int hotelId, + @RequestParam(required = false) LocalDate start, + @RequestParam(required = false) LocalDate end) { + List<Room> rooms = hotelService.getRoomsFromHotel(hotelId, start, end); + return new ResponseEntity<>(rooms, HttpStatus.OK); + } + + @PatchMapping("/{hotelId}/rooms/{roomId}") + public ResponseEntity<Room> updateRoomAvailability( + @PathVariable int hotelId, + @PathVariable int roomId, + @RequestBody Map<String, Boolean> body) { + if (!body.containsKey("available")) { + throw new InvalidRequestException("El campo 'available' es obligatorio"); + } + Room updatedRoom = hotelService.updateRoomAvailability(hotelId, roomId, body.get("available")); + return new ResponseEntity<>(updatedRoom, HttpStatus.OK); + } + + @GetMapping("/{hotelId}/rooms/{roomId}") + public Room getRoomByIdFromHotel( + @PathVariable int hotelId, @PathVariable int roomId) { + return hotelService.getRoomByIdFromHotel(hotelId, roomId); + } +} diff --git a/java/services/hotels/src/main/java/com/uva/monolith/exceptions/GlobalExceptionHandler.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/exceptions/GlobalExceptionHandler.java similarity index 100% rename from java/services/hotels/src/main/java/com/uva/monolith/exceptions/GlobalExceptionHandler.java rename to java/services/hotels/src/main/java/com/uva/apis/hotels/exceptions/GlobalExceptionHandler.java diff --git a/java/services/hotels/src/main/java/com/uva/monolith/exceptions/HotelNotFoundException.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/exceptions/HotelNotFoundException.java similarity index 100% rename from java/services/hotels/src/main/java/com/uva/monolith/exceptions/HotelNotFoundException.java rename to java/services/hotels/src/main/java/com/uva/apis/hotels/exceptions/HotelNotFoundException.java diff --git a/java/services/hotels/src/main/java/com/uva/monolith/exceptions/InvalidDateRangeException.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/exceptions/InvalidDateRangeException.java similarity index 100% rename from java/services/hotels/src/main/java/com/uva/monolith/exceptions/InvalidDateRangeException.java rename to java/services/hotels/src/main/java/com/uva/apis/hotels/exceptions/InvalidDateRangeException.java diff --git a/java/services/hotels/src/main/java/com/uva/monolith/exceptions/InvalidRequestException.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/exceptions/InvalidRequestException.java similarity index 100% rename from java/services/hotels/src/main/java/com/uva/monolith/exceptions/InvalidRequestException.java rename to java/services/hotels/src/main/java/com/uva/apis/hotels/exceptions/InvalidRequestException.java diff --git a/java/services/hotels/src/main/java/com/uva/monolith/filter/JwtAuthenticationFilter.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/filter/JwtAuthenticationFilter.java similarity index 98% rename from java/services/hotels/src/main/java/com/uva/monolith/filter/JwtAuthenticationFilter.java rename to java/services/hotels/src/main/java/com/uva/apis/hotels/filter/JwtAuthenticationFilter.java index e905ed2..9718640 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/filter/JwtAuthenticationFilter.java +++ b/java/services/hotels/src/main/java/com/uva/apis/hotels/filter/JwtAuthenticationFilter.java @@ -4,7 +4,7 @@ import com.auth0.jwt.JWT; import com.auth0.jwt.JWTVerifier; import com.auth0.jwt.algorithms.Algorithm; import com.auth0.jwt.interfaces.DecodedJWT; -import com.uva.monolith.services.hotels.models.external.users.UserRol; +import com.uva.monolith.models.external.users.UserRol; import com.auth0.jwt.exceptions.JWTVerificationException; import org.springframework.beans.factory.annotation.Value; diff --git a/java/services/hotels/src/main/java/com/uva/monolith/interceptor/AuthHttpInterceptor.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/interceptor/AuthHttpInterceptor.java similarity index 95% rename from java/services/hotels/src/main/java/com/uva/monolith/interceptor/AuthHttpInterceptor.java rename to java/services/hotels/src/main/java/com/uva/apis/hotels/interceptor/AuthHttpInterceptor.java index 83e35b0..e604808 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/interceptor/AuthHttpInterceptor.java +++ b/java/services/hotels/src/main/java/com/uva/apis/hotels/interceptor/AuthHttpInterceptor.java @@ -7,7 +7,7 @@ import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.stereotype.Component; -import com.uva.monolith.services.hotels.models.external.users.UserRol; +import com.uva.monolith.models.external.users.UserRol; import com.uva.monolith.utils.JwtUtil; import java.io.IOException; diff --git a/java/services/hotels/src/main/java/com/uva/monolith/services/hotels/models/Address.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/models/Address.java similarity index 97% rename from java/services/hotels/src/main/java/com/uva/monolith/services/hotels/models/Address.java rename to java/services/hotels/src/main/java/com/uva/apis/hotels/models/Address.java index 5f31a2a..b7c67a3 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/services/hotels/models/Address.java +++ b/java/services/hotels/src/main/java/com/uva/apis/hotels/models/Address.java @@ -1,4 +1,4 @@ -package com.uva.monolith.services.hotels.models; +package com.uva.monolith.models; import com.fasterxml.jackson.annotation.JsonIgnore; diff --git a/java/services/hotels/src/main/java/com/uva/monolith/services/hotels/models/Hotel.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/models/Hotel.java similarity index 96% rename from java/services/hotels/src/main/java/com/uva/monolith/services/hotels/models/Hotel.java rename to java/services/hotels/src/main/java/com/uva/apis/hotels/models/Hotel.java index 0d2a207..69761fe 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/services/hotels/models/Hotel.java +++ b/java/services/hotels/src/main/java/com/uva/apis/hotels/models/Hotel.java @@ -1,4 +1,4 @@ -package com.uva.monolith.services.hotels.models; +package com.uva.monolith.models; import java.util.List; diff --git a/java/services/hotels/src/main/java/com/uva/monolith/services/hotels/models/Room.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/models/Room.java similarity index 95% rename from java/services/hotels/src/main/java/com/uva/monolith/services/hotels/models/Room.java rename to java/services/hotels/src/main/java/com/uva/apis/hotels/models/Room.java index 559fcc8..db6bd17 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/services/hotels/models/Room.java +++ b/java/services/hotels/src/main/java/com/uva/apis/hotels/models/Room.java @@ -1,4 +1,4 @@ -package com.uva.monolith.services.hotels.models; +package com.uva.monolith.models; import com.fasterxml.jackson.annotation.JsonIgnore; diff --git a/java/services/hotels/src/main/java/com/uva/monolith/services/hotels/models/RoomType.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/models/RoomType.java similarity index 55% rename from java/services/hotels/src/main/java/com/uva/monolith/services/hotels/models/RoomType.java rename to java/services/hotels/src/main/java/com/uva/apis/hotels/models/RoomType.java index b9e8258..0e1007d 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/services/hotels/models/RoomType.java +++ b/java/services/hotels/src/main/java/com/uva/apis/hotels/models/RoomType.java @@ -1,4 +1,4 @@ -package com.uva.monolith.services.hotels.models; +package com.uva.monolith.models; public enum RoomType { SINGLE, diff --git a/java/services/hotels/src/main/java/com/uva/apis/hotels/models/external/users/UserRol.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/models/external/users/UserRol.java new file mode 100644 index 0000000..b541a76 --- /dev/null +++ b/java/services/hotels/src/main/java/com/uva/apis/hotels/models/external/users/UserRol.java @@ -0,0 +1,5 @@ +package com.uva.monolith.models.external.users; + +public enum UserRol { + ADMIN, CLIENT, HOTEL_ADMIN +} diff --git a/java/services/hotels/src/main/java/com/uva/monolith/services/hotels/repositories/HotelRepository.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/repositories/HotelRepository.java similarity index 66% rename from java/services/hotels/src/main/java/com/uva/monolith/services/hotels/repositories/HotelRepository.java rename to java/services/hotels/src/main/java/com/uva/apis/hotels/repositories/HotelRepository.java index a7f20f9..6b748f1 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/services/hotels/repositories/HotelRepository.java +++ b/java/services/hotels/src/main/java/com/uva/apis/hotels/repositories/HotelRepository.java @@ -1,9 +1,9 @@ -package com.uva.monolith.services.hotels.repositories; +package com.uva.monolith.repositories; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; -import com.uva.monolith.services.hotels.models.Hotel; +import com.uva.monolith.models.Hotel; public interface HotelRepository extends JpaRepository<Hotel, Integer> { List<Hotel> findAllByManagerId(Integer managerId); diff --git a/java/services/hotels/src/main/java/com/uva/monolith/services/hotels/repositories/RoomRepository.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/repositories/RoomRepository.java similarity index 79% rename from java/services/hotels/src/main/java/com/uva/monolith/services/hotels/repositories/RoomRepository.java rename to java/services/hotels/src/main/java/com/uva/apis/hotels/repositories/RoomRepository.java index 35ee8eb..532e02e 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/services/hotels/repositories/RoomRepository.java +++ b/java/services/hotels/src/main/java/com/uva/apis/hotels/repositories/RoomRepository.java @@ -1,9 +1,9 @@ -package com.uva.monolith.services.hotels.repositories; +package com.uva.monolith.repositories; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; -import com.uva.monolith.services.hotels.models.Room; +import com.uva.monolith.models.Room; import java.time.LocalDate; import java.util.List; diff --git a/java/services/hotels/src/main/java/com/uva/apis/hotels/services/HotelService.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/services/HotelService.java new file mode 100644 index 0000000..c763f9d --- /dev/null +++ b/java/services/hotels/src/main/java/com/uva/apis/hotels/services/HotelService.java @@ -0,0 +1,107 @@ +package com.uva.monolith.services; + +import java.time.LocalDate; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.uva.monolith.api.BookingAPI; +import com.uva.monolith.api.ManagerAPI; +import com.uva.monolith.exceptions.HotelNotFoundException; +import com.uva.monolith.exceptions.InvalidDateRangeException; +import com.uva.monolith.exceptions.InvalidRequestException; +import com.uva.monolith.models.Hotel; +import com.uva.monolith.models.Room; +import com.uva.monolith.repositories.HotelRepository; +import com.uva.monolith.repositories.RoomRepository; + +@Service +public class HotelService { + @Autowired + private HotelRepository hotelRepository; + @Autowired + private RoomRepository roomRepository; + @Autowired + private BookingAPI bookingAPI; + @Autowired + private ManagerAPI managerAPI; + + public List<Hotel> getAllHotels(Integer managerId, LocalDate start, LocalDate end) { + List<Hotel> hotels = (managerId != null) + ? hotelRepository.findAllByManagerId(managerId) + : hotelRepository.findAll(); + if (start != null && end != null) { + if (!start.isBefore(end)) { + throw new InvalidDateRangeException("La fecha de inicio debe ser anterior a la fecha de fin"); + } + Set<Integer> notAvailableRoomsId = bookingAPI.getNotAvailableRooms(start, end); + hotels = hotels.stream().map(h -> { + List<Room> rooms = h.getRooms().stream() + .filter(r -> notAvailableRoomsId.contains(r.getId())) + .toList(); + h.setRooms(rooms); + return h; + }).filter(h -> !h.getRooms().isEmpty()).toList(); + } + return hotels; + } + + public Hotel addHotel(Hotel hotel) { + boolean exist = managerAPI.existsManagerById(hotel.getManagerId()); + if (!exist) { + throw new InvalidRequestException("No existe el manager con id " + hotel.getManagerId()); + } + return hotelRepository.save(hotel); + } + + public Hotel getHotelById(int id) { + return hotelRepository.findById(id) + .orElseThrow(() -> new HotelNotFoundException(id)); + } + + public List<Hotel> deleteHotelsByManagerId(int managerId) { + List<Hotel> hotels = hotelRepository.findAllByManagerId(managerId); + if (hotels.isEmpty()) { + throw new InvalidRequestException("No hay hoteles para el manager con id " + managerId); + } + bookingAPI.deleteAllByManagerId(managerId); + hotelRepository.deleteAll(hotels); + return hotels; + } + + public void deleteHotel(int id) { + Hotel target = hotelRepository.findById(id) + .orElseThrow(() -> new HotelNotFoundException(id)); + bookingAPI.deleteAllByHotelId(id); + hotelRepository.delete(target); + } + + public List<Room> getRoomsFromHotel(int hotelId, LocalDate start, LocalDate end) { + List<Room> rooms = roomRepository.findAllByHotelId(hotelId); + if (start != null && end != null) { + if (!start.isBefore(end)) { + throw new InvalidDateRangeException("La fecha de inicio debe ser anterior a la fecha de fin"); + } + Set<Integer> notAvailableRoomsId = bookingAPI.getNotAvailableRooms(hotelId, start, end); + rooms = rooms.stream() + .filter(r -> !notAvailableRoomsId.contains(r.getId())) + .toList(); + } + return rooms; + } + + public Room updateRoomAvailability(int hotelId, int roomId, boolean available) { + Room targetRoom = roomRepository.findByIdAndHotelId(roomId, hotelId) + .orElseThrow(() -> new InvalidRequestException("Habitación no encontrada")); + targetRoom.setAvailable(available); + return roomRepository.save(targetRoom); + } + + public Room getRoomByIdFromHotel(int hotelId, int roomId) { + return roomRepository.findByIdAndHotelId(roomId, hotelId) + .orElseThrow(() -> new HotelNotFoundException(hotelId)); + } +} diff --git a/java/services/hotels/src/main/java/com/uva/monolith/utils/JwtUtil.java b/java/services/hotels/src/main/java/com/uva/apis/hotels/utils/JwtUtil.java similarity index 93% rename from java/services/hotels/src/main/java/com/uva/monolith/utils/JwtUtil.java rename to java/services/hotels/src/main/java/com/uva/apis/hotels/utils/JwtUtil.java index dd3fc60..a7bd5f8 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/utils/JwtUtil.java +++ b/java/services/hotels/src/main/java/com/uva/apis/hotels/utils/JwtUtil.java @@ -7,7 +7,7 @@ import org.springframework.stereotype.Component; import com.auth0.jwt.JWT; import com.auth0.jwt.algorithms.Algorithm; -import com.uva.monolith.services.hotels.models.external.users.UserRol; +import com.uva.monolith.models.external.users.UserRol; @Component public class JwtUtil { diff --git a/java/services/hotels/src/main/java/com/uva/monolith/services/hotels/controllers/HotelController.java b/java/services/hotels/src/main/java/com/uva/monolith/services/hotels/controllers/HotelController.java deleted file mode 100644 index de29352..0000000 --- a/java/services/hotels/src/main/java/com/uva/monolith/services/hotels/controllers/HotelController.java +++ /dev/null @@ -1,164 +0,0 @@ -package com.uva.monolith.services.hotels.controllers; - -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.time.LocalDate; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.bind.annotation.*; - -import com.uva.monolith.api.BookingAPI; -import com.uva.monolith.api.ManagerAPI; -import com.uva.monolith.exceptions.HotelNotFoundException; -import com.uva.monolith.exceptions.InvalidDateRangeException; -import com.uva.monolith.exceptions.InvalidRequestException; -import com.uva.monolith.services.hotels.models.Hotel; -import com.uva.monolith.services.hotels.models.Room; -import com.uva.monolith.services.hotels.repositories.HotelRepository; -import com.uva.monolith.services.hotels.repositories.RoomRepository; - -@RestController -@RequestMapping("hotels") -@CrossOrigin(origins = "*") -public class HotelController { - @Autowired - private HotelRepository hotelRepository; - @Autowired - private RoomRepository roomRepository; - @Autowired - private BookingAPI bookingAPI; - @Autowired - private ManagerAPI managerAPI; - - // Obtener todos los hoteles - @GetMapping - public List<Hotel> getAllHotels( - @RequestParam(required = false) Integer managerId, - @RequestParam(required = false) LocalDate start, - @RequestParam(required = false) LocalDate end) { - List<Hotel> hotels = (managerId != null) - ? hotelRepository.findAllByManagerId(managerId) - : hotelRepository.findAll(); - if (start != null && end != null) { - // Filtramos para los hoteles que - // tengan habitaciones disponibles para ese rango de fechas - Set<Integer> notAvailableRoomsId = bookingAPI.getNotAvailableRooms(start, end); - hotels = hotels.stream().map(h -> { - if (h.getRooms().size() == 0) - return h; - List<Room> rooms = h.getRooms().stream() - .filter(r -> notAvailableRoomsId.contains(r.getId())).toList(); - h.setRooms(rooms); - return h; - }).filter(h -> h.getRooms().size() >= 0).toList(); - } - return hotels; - } - - // Añadir un hotel con sus habitaciones - @PostMapping - public ResponseEntity<?> addHotel(@RequestBody Hotel hotel) { - try { - - boolean exist = managerAPI.existsManagerById(hotel.getManagerId()); - String message = "No existe el manager con id " + String.valueOf(hotel.getManagerId()); - if (!exist) { - return new ResponseEntity<>(message, HttpStatus.BAD_REQUEST); - } - Hotel savedHotel = hotelRepository.save(hotel); - return new ResponseEntity<>(savedHotel, HttpStatus.CREATED); - } catch (Exception e) { - e.printStackTrace(System.err); - throw e; - } - } - - // Obtener un hotel por su ID - @GetMapping("/{id}") - public Hotel getHotelById(@PathVariable int id) { - return hotelRepository.findById(id) - .orElseThrow(() -> new HotelNotFoundException(id)); - } - - // Borrar hoteles administrados por un manager concreto - @DeleteMapping - public ResponseEntity<?> deleteHotelsByManagerId( - @RequestParam(required = true) Integer managerId) { - List<Hotel> hotels = hotelRepository.findAllByManagerId(managerId); - if (hotels.isEmpty()) { - return new ResponseEntity<>("No hay hoteles para el manager con id " + managerId, HttpStatus.BAD_REQUEST); - } - bookingAPI.deleteAllByManagerId(managerId); - hotelRepository.deleteAll(hotels); - return new ResponseEntity<>(hotels, HttpStatus.OK); - } - - // Borrar un hotel junto con sus habitaciones (borrado en cascada) - @DeleteMapping("/{id}") - @Transactional - public ResponseEntity<Void> deleteHotel(@PathVariable Integer id) { - Hotel target = hotelRepository.findById(id) - .orElseThrow(() -> new HotelNotFoundException(id)); - bookingAPI.deleteAllByHotelId(id); - hotelRepository.delete(target); - return new ResponseEntity<>(HttpStatus.NO_CONTENT); - } - - // Obtener habitaciones de un hotel según disponibilidad y fechas - @GetMapping("/{hotelId}/rooms") - public ResponseEntity<List<Room>> getRoomsFromHotel( - @PathVariable int hotelId, - @RequestParam(required = false) LocalDate start, - @RequestParam(required = false) LocalDate end) { - - List<Room> rooms; - boolean dateMode = start != null && end != null; - if (dateMode) { - if (!start.isBefore(end)) { - throw new InvalidDateRangeException("La fecha de inicio debe ser anterior a la fecha de fin"); - } - } - rooms = roomRepository.findAllByHotelId(hotelId); - - if (dateMode) { - // Consultar el set de ids ocupados del id - Set<Integer> notAvailableRoomsId = bookingAPI.getNotAvailableRooms(hotelId, start, end); - rooms = rooms.stream() - .filter(r -> notAvailableRoomsId.contains(r.getId())).toList(); - } - - return new ResponseEntity<>(rooms, HttpStatus.OK); - } - - // Actualizar disponibilidad de una habitación específica en un hotel - @PatchMapping("/{hotelId}/rooms/{roomId}") - public ResponseEntity<Room> updateRoomAvailability( - @PathVariable int hotelId, - @PathVariable int roomId, - @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(body.get("available")); - roomRepository.save(targetRoom); - - return new ResponseEntity<>(targetRoom, HttpStatus.OK); - } - - // Obtener los detalles de una habitación específica en un hotel - @GetMapping("/{hotelId}/rooms/{roomId}") - public Room getRoomByIdFromHotel( - @PathVariable int hotelId, @PathVariable int roomId) { - return roomRepository.findByIdAndHotelId(roomId, hotelId) - .orElseThrow(() -> new HotelNotFoundException(hotelId)); - } -} diff --git a/java/services/hotels/src/main/java/com/uva/monolith/services/hotels/models/external/users/UserRol.java b/java/services/hotels/src/main/java/com/uva/monolith/services/hotels/models/external/users/UserRol.java deleted file mode 100644 index 6bac3ab..0000000 --- a/java/services/hotels/src/main/java/com/uva/monolith/services/hotels/models/external/users/UserRol.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.uva.monolith.services.hotels.models.external.users; - -public enum UserRol { - ADMIN, CLIENT, HOTEL_ADMIN -} -- GitLab