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 6a82598aa46c5f8cb07566677d88d6cd7aa5dffc..ccb40af588554c5aa42390c5adc9cc38ecf0332f 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 0000000000000000000000000000000000000000..643c08f272b6f1638ff2ecef077dc872e39438be --- /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 e905ed286c94f2396b109c9689d6668fe6a72b15..9718640bee0fd410244cd2a5d8b6c54cf80bf9d2 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 83e35b0557ff26e494830ec13bba22777f00af07..e6048081856b1c51c14371daf6c3d8349405336b 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 5f31a2a530da46c00460ad6cc6151b0769c1da61..b7c67a35813f734056706b33102745a2807cc68e 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 0d2a207cb5511995a96b0bf6a30413ac036bc8ca..69761fe8aef68f2b8e50f7798b2ac924f8b33f6d 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 559fcc8a0fccd4f3c7eb767c9ba156091fc332f1..db6bd173b554fa468b04f056dbbeda60c12e9b9d 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 b9e82584850795afa7c7392248e3a6472ce24ac0..0e1007db8471dba884440167ef99a8110a25a848 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 0000000000000000000000000000000000000000..b541a76ab002cf14123737e02058f77401b03d12 --- /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 a7f20f963bad558eb3df39ad42135be5f93c799b..6b748f1affc0ebdbcbb2c00aa465490579824479 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 35ee8ebfcf11c0510159b29f3916de0d5931be45..532e02ef25feae0d7f2bead32e7b92462984395a 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 0000000000000000000000000000000000000000..c763f9d82ca2c732429038a946565fd5e4b51ede --- /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 dd3fc60b59c3c5b75235f35a9a0f08258b0b67d4..a7bd5f863d5a12b5fd6ed0b3419fdc4acaec0de6 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 de293524b54a865ab0d58a9877936cd8aa4145d0..0000000000000000000000000000000000000000 --- 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 6bac3abe0bd2520488ad98225ed4b65a692e9c81..0000000000000000000000000000000000000000 --- 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 -}