diff --git a/java/services/hotels/src/main/java/com/uva/monolith/HotelsApplication.java b/java/services/hotels/src/main/java/com/uva/api/HotelsApplication.java similarity index 91% rename from java/services/hotels/src/main/java/com/uva/monolith/HotelsApplication.java rename to java/services/hotels/src/main/java/com/uva/api/HotelsApplication.java index b458b0bf085d44574b803b30a1fedcc7f80f4daa..0d15b4f97491b1ffc6d6c2dac7ee4159825e5d06 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/HotelsApplication.java +++ b/java/services/hotels/src/main/java/com/uva/api/HotelsApplication.java @@ -1,4 +1,4 @@ -package com.uva.monolith; +package com.uva.api; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/java/services/hotels/src/main/java/com/uva/monolith/api/BookingAPI.java b/java/services/hotels/src/main/java/com/uva/api/apis/BookingAPI.java similarity index 98% rename from java/services/hotels/src/main/java/com/uva/monolith/api/BookingAPI.java rename to java/services/hotels/src/main/java/com/uva/api/apis/BookingAPI.java index 369066bd537e131c333b04ae0415f7527cff6885..53dc6558cff6154445c34980ed2896da73d163dc 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/api/BookingAPI.java +++ b/java/services/hotels/src/main/java/com/uva/api/apis/BookingAPI.java @@ -1,4 +1,4 @@ -package com.uva.monolith.api; +package com.uva.api.apis; import java.time.LocalDate; import java.util.HashSet; diff --git a/java/services/hotels/src/main/java/com/uva/monolith/api/ManagerAPI.java b/java/services/hotels/src/main/java/com/uva/api/apis/ManagerAPI.java similarity index 96% rename from java/services/hotels/src/main/java/com/uva/monolith/api/ManagerAPI.java rename to java/services/hotels/src/main/java/com/uva/api/apis/ManagerAPI.java index fda96949c48eb0cd159fd181efe6c845eff08767..a7eb79b6dd8a9b9b31bfe383363b2a8fa44d4a0d 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/api/ManagerAPI.java +++ b/java/services/hotels/src/main/java/com/uva/api/apis/ManagerAPI.java @@ -1,4 +1,4 @@ -package com.uva.monolith.api; +package com.uva.api.apis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; diff --git a/java/services/hotels/src/main/java/com/uva/monolith/config/RestTemplateConfig.java b/java/services/hotels/src/main/java/com/uva/api/config/RestTemplateConfig.java similarity index 90% rename from java/services/hotels/src/main/java/com/uva/monolith/config/RestTemplateConfig.java rename to java/services/hotels/src/main/java/com/uva/api/config/RestTemplateConfig.java index 43da2b78e915df7993cd4e123f57be22475b965f..5d215af870f9cfe8a1ab8386b6294b7e062bd56a 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/config/RestTemplateConfig.java +++ b/java/services/hotels/src/main/java/com/uva/api/config/RestTemplateConfig.java @@ -1,4 +1,4 @@ -package com.uva.monolith.config; +package com.uva.api.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/java/services/hotels/src/main/java/com/uva/monolith/config/SecurityConfig.java b/java/services/hotels/src/main/java/com/uva/api/config/SecurityConfig.java similarity index 92% rename from java/services/hotels/src/main/java/com/uva/monolith/config/SecurityConfig.java rename to java/services/hotels/src/main/java/com/uva/api/config/SecurityConfig.java index 6a82598aa46c5f8cb07566677d88d6cd7aa5dffc..41524e4e660c5f453fa916a62fb2bbe089d48277 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/config/SecurityConfig.java +++ b/java/services/hotels/src/main/java/com/uva/api/config/SecurityConfig.java @@ -1,4 +1,4 @@ -package com.uva.monolith.config; +package com.uva.api.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -8,8 +8,8 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe 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.api.filter.JwtAuthenticationFilter; +import com.uva.api.models.external.users.UserRol; @Configuration @EnableWebSecurity diff --git a/java/services/hotels/src/main/java/com/uva/api/controllers/HotelController.java b/java/services/hotels/src/main/java/com/uva/api/controllers/HotelController.java new file mode 100644 index 0000000000000000000000000000000000000000..ac95b7805793dd267c1e9aa04765e9b899301d90 --- /dev/null +++ b/java/services/hotels/src/main/java/com/uva/api/controllers/HotelController.java @@ -0,0 +1,91 @@ +package com.uva.api.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.api.apis.BookingAPI; +import com.uva.api.apis.ManagerAPI; +import com.uva.api.exceptions.HotelNotFoundException; +import com.uva.api.exceptions.InvalidDateRangeException; +import com.uva.api.exceptions.InvalidRequestException; +import com.uva.api.models.Hotel; +import com.uva.api.models.Room; +import com.uva.api.repositories.HotelRepository; +import com.uva.api.repositories.RoomRepository; +import com.uva.api.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/api/exceptions/GlobalExceptionHandler.java similarity index 98% rename from java/services/hotels/src/main/java/com/uva/monolith/exceptions/GlobalExceptionHandler.java rename to java/services/hotels/src/main/java/com/uva/api/exceptions/GlobalExceptionHandler.java index 9428c51a9c63c3623d44752c9e3cbe6cf78ac19f..5681a607c7b0885255a2259e809cb4bb29bf456d 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/exceptions/GlobalExceptionHandler.java +++ b/java/services/hotels/src/main/java/com/uva/api/exceptions/GlobalExceptionHandler.java @@ -1,4 +1,4 @@ -package com.uva.monolith.exceptions; +package com.uva.api.exceptions; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; diff --git a/java/services/hotels/src/main/java/com/uva/monolith/exceptions/HotelNotFoundException.java b/java/services/hotels/src/main/java/com/uva/api/exceptions/HotelNotFoundException.java similarity index 90% rename from java/services/hotels/src/main/java/com/uva/monolith/exceptions/HotelNotFoundException.java rename to java/services/hotels/src/main/java/com/uva/api/exceptions/HotelNotFoundException.java index 129a0b1086b4b78eb1f1725b9f241f51ce5540f8..dc466f6f5f91da47dbe73c34b97059c817937f13 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/exceptions/HotelNotFoundException.java +++ b/java/services/hotels/src/main/java/com/uva/api/exceptions/HotelNotFoundException.java @@ -1,4 +1,4 @@ -package com.uva.monolith.exceptions; +package com.uva.api.exceptions; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; diff --git a/java/services/hotels/src/main/java/com/uva/monolith/exceptions/InvalidDateRangeException.java b/java/services/hotels/src/main/java/com/uva/api/exceptions/InvalidDateRangeException.java similarity index 80% rename from java/services/hotels/src/main/java/com/uva/monolith/exceptions/InvalidDateRangeException.java rename to java/services/hotels/src/main/java/com/uva/api/exceptions/InvalidDateRangeException.java index 5fea986ef1e9279c459bc5aff10932049f283333..58bf97d7b6dceb1db771de7058da6f159480a5b9 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/exceptions/InvalidDateRangeException.java +++ b/java/services/hotels/src/main/java/com/uva/api/exceptions/InvalidDateRangeException.java @@ -1,4 +1,4 @@ -package com.uva.monolith.exceptions; +package com.uva.api.exceptions; public class InvalidDateRangeException extends RuntimeException { public InvalidDateRangeException(String message) { diff --git a/java/services/hotels/src/main/java/com/uva/monolith/exceptions/InvalidRequestException.java b/java/services/hotels/src/main/java/com/uva/api/exceptions/InvalidRequestException.java similarity index 88% rename from java/services/hotels/src/main/java/com/uva/monolith/exceptions/InvalidRequestException.java rename to java/services/hotels/src/main/java/com/uva/api/exceptions/InvalidRequestException.java index ca09e054420dd174c4d2c3424dcc8fe74b6c8576..677cc4b7cb71bb20c3a9644ff2a8d3552546ea2c 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/exceptions/InvalidRequestException.java +++ b/java/services/hotels/src/main/java/com/uva/api/exceptions/InvalidRequestException.java @@ -1,4 +1,4 @@ -package com.uva.monolith.exceptions; +package com.uva.api.exceptions; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; diff --git a/java/services/hotels/src/main/java/com/uva/monolith/filter/JwtAuthenticationFilter.java b/java/services/hotels/src/main/java/com/uva/api/filter/JwtAuthenticationFilter.java similarity index 97% rename from java/services/hotels/src/main/java/com/uva/monolith/filter/JwtAuthenticationFilter.java rename to java/services/hotels/src/main/java/com/uva/api/filter/JwtAuthenticationFilter.java index e905ed286c94f2396b109c9689d6668fe6a72b15..58d6e766705907c520543b4ff0349c54b4d4952e 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/filter/JwtAuthenticationFilter.java +++ b/java/services/hotels/src/main/java/com/uva/api/filter/JwtAuthenticationFilter.java @@ -1,10 +1,10 @@ -package com.uva.monolith.filter; +package com.uva.api.filter; 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.api.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/api/interceptor/AuthHttpInterceptor.java similarity index 89% rename from java/services/hotels/src/main/java/com/uva/monolith/interceptor/AuthHttpInterceptor.java rename to java/services/hotels/src/main/java/com/uva/api/interceptor/AuthHttpInterceptor.java index 83e35b0557ff26e494830ec13bba22777f00af07..490cab9660489a4880f4aa6b1146a73e960a309e 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/interceptor/AuthHttpInterceptor.java +++ b/java/services/hotels/src/main/java/com/uva/api/interceptor/AuthHttpInterceptor.java @@ -1,4 +1,4 @@ -package com.uva.monolith.interceptor; +package com.uva.api.interceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpRequest; @@ -7,8 +7,8 @@ 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.utils.JwtUtil; +import com.uva.api.models.external.users.UserRol; +import com.uva.api.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/api/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/api/models/Address.java index 5f31a2a530da46c00460ad6cc6151b0769c1da61..d654ce69441fdbaf46f7b03de059eacc7d9f61d2 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/api/models/Address.java @@ -1,4 +1,4 @@ -package com.uva.monolith.services.hotels.models; +package com.uva.api.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/api/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/api/models/Hotel.java index 0d2a207cb5511995a96b0bf6a30413ac036bc8ca..15bce18ef4f20920966ad3fce6e1275d7fa71a91 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/api/models/Hotel.java @@ -1,4 +1,4 @@ -package com.uva.monolith.services.hotels.models; +package com.uva.api.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/api/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/api/models/Room.java index 559fcc8a0fccd4f3c7eb767c9ba156091fc332f1..c3a309970fc7a744a8af161bfca84bd7e352b934 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/api/models/Room.java @@ -1,4 +1,4 @@ -package com.uva.monolith.services.hotels.models; +package com.uva.api.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/api/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/api/models/RoomType.java index b9e82584850795afa7c7392248e3a6472ce24ac0..4e988ccbc504b4ec7b0ccb05796b880ff7adacd0 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/api/models/RoomType.java @@ -1,4 +1,4 @@ -package com.uva.monolith.services.hotels.models; +package com.uva.api.models; public enum RoomType { SINGLE, diff --git a/java/services/hotels/src/main/java/com/uva/api/models/external/users/UserRol.java b/java/services/hotels/src/main/java/com/uva/api/models/external/users/UserRol.java new file mode 100644 index 0000000000000000000000000000000000000000..ab6efcc9875165bc751045a64b82bb69d326eaea --- /dev/null +++ b/java/services/hotels/src/main/java/com/uva/api/models/external/users/UserRol.java @@ -0,0 +1,5 @@ +package com.uva.api.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/api/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/api/repositories/HotelRepository.java index a7f20f963bad558eb3df39ad42135be5f93c799b..f216ba6b2184991d4a851d4b1fdc103dedc3a1aa 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/api/repositories/HotelRepository.java @@ -1,9 +1,10 @@ -package com.uva.monolith.services.hotels.repositories; +package com.uva.api.repositories; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; -import com.uva.monolith.services.hotels.models.Hotel; + +import com.uva.api.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/api/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/api/repositories/RoomRepository.java index 35ee8ebfcf11c0510159b29f3916de0d5931be45..e62066e38a440e120069ecffce6da8b30b176cd8 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/api/repositories/RoomRepository.java @@ -1,9 +1,9 @@ -package com.uva.monolith.services.hotels.repositories; +package com.uva.api.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.api.models.Room; import java.time.LocalDate; import java.util.List; diff --git a/java/services/hotels/src/main/java/com/uva/api/services/HotelService.java b/java/services/hotels/src/main/java/com/uva/api/services/HotelService.java new file mode 100644 index 0000000000000000000000000000000000000000..e5893bf639ba722527497056c591710f9bbb14ca --- /dev/null +++ b/java/services/hotels/src/main/java/com/uva/api/services/HotelService.java @@ -0,0 +1,107 @@ +package com.uva.api.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.api.apis.BookingAPI; +import com.uva.api.apis.ManagerAPI; +import com.uva.api.exceptions.HotelNotFoundException; +import com.uva.api.exceptions.InvalidDateRangeException; +import com.uva.api.exceptions.InvalidRequestException; +import com.uva.api.models.Hotel; +import com.uva.api.models.Room; +import com.uva.api.repositories.HotelRepository; +import com.uva.api.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/api/utils/JwtUtil.java similarity index 91% rename from java/services/hotels/src/main/java/com/uva/monolith/utils/JwtUtil.java rename to java/services/hotels/src/main/java/com/uva/api/utils/JwtUtil.java index dd3fc60b59c3c5b75235f35a9a0f08258b0b67d4..73580a488465230d1ff7e57ac0c4e3b0dad587c6 100644 --- a/java/services/hotels/src/main/java/com/uva/monolith/utils/JwtUtil.java +++ b/java/services/hotels/src/main/java/com/uva/api/utils/JwtUtil.java @@ -1,4 +1,4 @@ -package com.uva.monolith.utils; +package com.uva.api.utils; import java.util.Date; @@ -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.api.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 -}