From 209cf40dc790f330bd9a5ecae52bc8f167374b90 Mon Sep 17 00:00:00 2001 From: Krzysztof Nazar Date: Mon, 20 May 2024 09:30:32 +0200 Subject: [PATCH 01/14] RSWW-102 RabbitMQ communication works --- .../controllers/HotelsController.java | 32 ++++++++----- .../DeleteHotelReservationRequest.java | 16 +++++++ .../queues/config/QueuesConfig.java | 47 ++++++++++++++----- .../commands/CreateReservationCommand.java | 3 +- .../domain/entity/Reservation.java | 2 +- .../events/ReservationCreatedEvent.java | 6 +-- .../queues/config/QueuesHotelConfig.java | 10 +++- .../config/ReservationDeleteRequest.java | 17 +++++++ .../services/ReservationService.java | 30 +++++++----- .../services/saga/BookHotelsSaga.java | 10 ++++ 10 files changed, 128 insertions(+), 45 deletions(-) create mode 100644 hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/reservations/DeleteHotelReservationRequest.java create mode 100644 reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/ReservationDeleteRequest.java diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java index bc2494b1..e4474f3e 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java @@ -3,8 +3,8 @@ import lombok.RequiredArgsConstructor; import org.microarchitecturovisco.hotelservice.controllers.reservations.CheckHotelAvailabilityRequest; import org.microarchitecturovisco.hotelservice.controllers.reservations.CreateHotelReservationRequest; +import org.microarchitecturovisco.hotelservice.controllers.reservations.DeleteHotelReservationRequest; import org.microarchitecturovisco.hotelservice.model.cqrs.commands.CreateRoomReservationCommand; -import org.microarchitecturovisco.hotelservice.model.dto.HotelDto; import org.microarchitecturovisco.hotelservice.model.dto.RoomReservationDto; import org.microarchitecturovisco.hotelservice.model.dto.request.GetHotelDetailsRequestDto; import org.microarchitecturovisco.hotelservice.model.dto.request.GetHotelsBySearchQueryRequestDto; @@ -13,13 +13,12 @@ import org.microarchitecturovisco.hotelservice.queues.config.QueuesConfig; import org.microarchitecturovisco.hotelservice.services.HotelsCommandService; import org.microarchitecturovisco.hotelservice.services.HotelsService; +import org.microarchitecturovisco.hotelservice.utils.JsonConverter; import org.microarchitecturovisco.hotelservice.utils.JsonReader; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import org.microarchitecturovisco.hotelservice.utils.JsonConverter; -import java.time.Duration; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; @@ -74,22 +73,22 @@ public String consumeMessageCheckHotelAvailability(CheckHotelAvailabilityRequest return Boolean.toString(!hotels.getHotels().isEmpty()); } - @RabbitListener(queues = QueuesConfig.QUEUE_HOTEL_CREATE_RESERVATION_REQ) - public void consumeMessageCreateHotelReservation(CreateHotelReservationRequest request) { - System.out.println("Message received from queue: " + request); + @RabbitListener(queues = "#{handleCreateHotelReservationQueue.name}") + public void consumeMessageCreateHotelReservation(CreateHotelReservationRequest createHotelReservationRequest) { + System.out.println("Message received from queue createHotelReservationRequest: " + createHotelReservationRequest); - int numberOfRoomsInReservation = request.getRoomReservationsIds().size(); + int numberOfRoomsInReservation = createHotelReservationRequest.getRoomReservationsIds().size(); List roomReservations = new ArrayList<>(); for (int i = 0; i < numberOfRoomsInReservation; i++) { - UUID roomId = request.getRoomReservationsIds().get(i); + UUID roomId = createHotelReservationRequest.getRoomReservationsIds().get(i); RoomReservationDto roomReservation = new RoomReservationDto(); - roomReservation.setReservationId(request.getReservationId()); - roomReservation.setDateFrom(request.getHotelTimeFrom()); - roomReservation.setDateTo(request.getHotelTimeTo()); - roomReservation.setHotelId(request.getHotelId()); + roomReservation.setReservationId(createHotelReservationRequest.getReservationId()); + roomReservation.setDateFrom(createHotelReservationRequest.getHotelTimeFrom()); + roomReservation.setDateTo(createHotelReservationRequest.getHotelTimeTo()); + roomReservation.setHotelId(createHotelReservationRequest.getHotelId()); roomReservation.setRoomId(roomId); roomReservations.add(roomReservation); @@ -106,5 +105,14 @@ public void consumeMessageCreateHotelReservation(CreateHotelReservationRequest r ); } } + + @RabbitListener(queues = "#{handleDeleteHotelReservationQueue.name}") + public void consumeMessageDeleteHotelReservation(DeleteHotelReservationRequest request) { + System.out.println("Message received from queue DeleteHotelReservationRequest: " + request); + UUID reservationId = request.getReservationId(); + + // TODO: delete the reservation using + + } } diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/reservations/DeleteHotelReservationRequest.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/reservations/DeleteHotelReservationRequest.java new file mode 100644 index 00000000..4edf9729 --- /dev/null +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/reservations/DeleteHotelReservationRequest.java @@ -0,0 +1,16 @@ +package org.microarchitecturovisco.hotelservice.controllers.reservations; + +import lombok.*; + +import java.util.UUID; + +@Builder +@Data +@AllArgsConstructor +@NoArgsConstructor +@ToString +@Getter +@Setter +public class DeleteHotelReservationRequest { + private UUID reservationId; +} diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/queues/config/QueuesConfig.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/queues/config/QueuesConfig.java index 69a7c142..a9d9234c 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/queues/config/QueuesConfig.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/queues/config/QueuesConfig.java @@ -5,6 +5,8 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.util.UUID; + @Configuration public class QueuesConfig { public static final String EXCHANGE_HOTEL = "hotels.requests.checkAvailabilityByQuery.exchange"; @@ -18,7 +20,7 @@ public TopicExchange handleReservationExchange() { @Bean(name="handleReservationQueue") public Queue handleReservationQueue() { - return new Queue(QUEUE_HOTEL_CHECK_AVAILABILITY_REQ, false, false, true); + return new Queue(QUEUE_HOTEL_CHECK_AVAILABILITY_REQ, false); } @Bean @@ -28,25 +30,46 @@ public Binding handleReservationRequestBinding( return BindingBuilder.bind(handleReservationQueue).to(handleReservationExchange).with(QUEUE_HOTEL_CHECK_AVAILABILITY_REQ); } - public static final String QUEUE_HOTEL_CREATE_RESERVATION_REQ = "hotels.events.createHotelReservation.queue"; - public static final String EXCHANGE_HOTEL_FANOUT = "hotels.createReservation.exchange"; + public static final String QUEUE_HOTEL_CREATE_RESERVATION_REQ_PREFIX = "hotels.events.createHotelReservation.queue."; + public static final String EXCHANGE_HOTEL_FANOUT_CREATE_RESERVATION = "hotels.createReservation.exchange"; - @Bean - @Qualifier("fanoutExchange") - public FanoutExchange fanoutExchange() { - return new FanoutExchange(EXCHANGE_HOTEL_FANOUT); + @Bean(name="fanoutExchangeCreateHotelReservation") + public FanoutExchange fanoutExchangeCreateHotelReservation() { + return new FanoutExchange(EXCHANGE_HOTEL_FANOUT_CREATE_RESERVATION); } - @Bean - @Qualifier("handleCreateHotelReservationQueue") + @Bean(name="handleCreateHotelReservationQueue") public Queue handleCreateHotelReservationQueue() { - return new Queue(QUEUE_HOTEL_CREATE_RESERVATION_REQ, false, false, true); + String uniqueQueueName = QUEUE_HOTEL_CREATE_RESERVATION_REQ_PREFIX + UUID.randomUUID(); + return new Queue(uniqueQueueName, false, false, true); } @Bean public Binding handleCreateHotelReservationBinding( - @Qualifier("fanoutExchange") FanoutExchange fanoutExchange, + @Qualifier("fanoutExchangeCreateHotelReservation") FanoutExchange fanoutExchangeCreateHotelReservation, @Qualifier("handleCreateHotelReservationQueue") Queue handleCreateHotelReservationQueue) { - return BindingBuilder.bind(handleCreateHotelReservationQueue).to(fanoutExchange); + return BindingBuilder.bind(handleCreateHotelReservationQueue).to(fanoutExchangeCreateHotelReservation); + } + + + public static final String QUEUE_HOTEL_DELETE_RESERVATION_REQ_PREFIX = "hotels.events.deleteHotelReservation.queue."; + public static final String EXCHANGE_HOTEL_FANOUT_DELETE_RESERVATION = "hotels.deleteReservation.exchange"; + + @Bean(name="fanoutExchangeDeleteHotelReservation") + public FanoutExchange fanoutExchangeDeleteHotelReservation() { + return new FanoutExchange(EXCHANGE_HOTEL_FANOUT_DELETE_RESERVATION); + } + + @Bean(name="handleDeleteHotelReservationQueue") + public Queue handleDeleteHotelReservationQueue() { + String uniqueQueueName = QUEUE_HOTEL_DELETE_RESERVATION_REQ_PREFIX + UUID.randomUUID(); + return new Queue(uniqueQueueName, false, false, true); + } + + @Bean + public Binding handleDeleteHotelReservationBinding( + @Qualifier("fanoutExchangeDeleteHotelReservation") FanoutExchange fanoutExchangeDeleteHotelReservation, + @Qualifier("handleDeleteHotelReservationQueue") Queue handleDeleteHotelReservationQueue) { + return BindingBuilder.bind(handleDeleteHotelReservationQueue).to(fanoutExchangeDeleteHotelReservation); } } diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/commands/CreateReservationCommand.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/commands/CreateReservationCommand.java index 99f68b97..879f44d1 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/commands/CreateReservationCommand.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/commands/CreateReservationCommand.java @@ -9,7 +9,6 @@ import lombok.NoArgsConstructor; import java.time.LocalDateTime; -import java.util.Collection; import java.util.List; import java.util.UUID; @@ -29,7 +28,7 @@ public class CreateReservationCommand { private int adultsQuantity; private float price; private boolean paid; - private String hotelId; + private UUID hotelId; @ElementCollection private List roomReservationsIds; @ElementCollection diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/entity/Reservation.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/entity/Reservation.java index 195cf78d..d701fc90 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/entity/Reservation.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/entity/Reservation.java @@ -42,7 +42,7 @@ public class Reservation { private boolean paid; @NotNull - private String hotelId; + private UUID hotelId; @NotNull @ElementCollection(fetch = FetchType.EAGER) diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/events/ReservationCreatedEvent.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/events/ReservationCreatedEvent.java index 74a5ccdc..36dc185e 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/events/ReservationCreatedEvent.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/events/ReservationCreatedEvent.java @@ -2,13 +2,9 @@ import jakarta.persistence.ElementCollection; import jakarta.persistence.Entity; -import jakarta.persistence.Id; import lombok.*; -import lombok.experimental.SuperBuilder; -import org.microarchitecturovisco.reservationservice.domain.entity.Reservation; import java.time.LocalDateTime; -import java.util.Collection; import java.util.List; import java.util.UUID; @@ -28,7 +24,7 @@ public class ReservationCreatedEvent extends ReservationEvent { private int adultsQuantity; private float price; private boolean paid; - private String hotelId; + private UUID hotelId; @ElementCollection private List roomReservationsIds; @ElementCollection diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/QueuesHotelConfig.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/QueuesHotelConfig.java index a09a0836..2c4500c4 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/QueuesHotelConfig.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/QueuesHotelConfig.java @@ -20,7 +20,7 @@ public TopicExchange handleHotelExchange() { @Bean(name="handleReservationQueue") public Queue handleReservationQueue() { - return new Queue(QUEUE_HOTEL_CHECK_AVAILABILITY_REQ, false, false, true); + return new Queue(QUEUE_HOTEL_CHECK_AVAILABILITY_REQ, false); } @Bean @@ -37,4 +37,12 @@ public FanoutExchange fanoutExchange() { return new FanoutExchange(EXCHANGE_HOTEL_FANOUT); } + + public static final String EXCHANGE_HOTEL_FANOUT_DELETE_RESERVATION = "hotels.deleteReservation.exchange"; + + @Bean(name="fanoutExchangeDeleteReservation") + public FanoutExchange fanoutExchangeDeleteReservation() { + return new FanoutExchange(EXCHANGE_HOTEL_FANOUT_DELETE_RESERVATION); + } + } diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/ReservationDeleteRequest.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/ReservationDeleteRequest.java new file mode 100644 index 00000000..c3eb9572 --- /dev/null +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/ReservationDeleteRequest.java @@ -0,0 +1,17 @@ +package org.microarchitecturovisco.reservationservice.queues.config; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.util.UUID; + +@Builder +@Data +@AllArgsConstructor +@NoArgsConstructor +public class ReservationDeleteRequest implements Serializable { + private UUID id; +} diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java index 2c8a99a4..4838df26 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java @@ -14,6 +14,7 @@ import org.microarchitecturovisco.reservationservice.domain.model.ReservationConfirmationResponse; import org.microarchitecturovisco.reservationservice.domain.model.TransportReservationResponse; import org.microarchitecturovisco.reservationservice.queues.config.QueuesReservationConfig; +import org.microarchitecturovisco.reservationservice.queues.config.ReservationDeleteRequest; import org.microarchitecturovisco.reservationservice.queues.config.ReservationRequest; import org.microarchitecturovisco.reservationservice.repositories.ReservationRepository; import org.microarchitecturovisco.reservationservice.services.saga.BookHotelsSaga; @@ -63,7 +64,7 @@ public Reservation createReservation(LocalDateTime hotelTimeFrom, LocalDateTime .adultsQuantity(adultsQuantity) .price(price) .paid(false) - .hotelId(hotelId.toString()) + .hotelId(hotelId) .roomReservationsIds(roomReservationsIds) .transportReservationsIds(transportReservationsIds) .userId(userId) @@ -88,20 +89,12 @@ public UUID bookOrchestration(ReservationRequest reservationRequest) throws Rese bookTransportsSaga.createTransportReservation(reservationRequest); - // todo: Rozpoczyna się odliczanie do przedawnienia się rezerwacji - // (co skutkuje cofnięciem poprzednich operacji); - // do aplikacji klienckiej zwracany jest status 2xx oraz idReservation tego zamówienia - // dodać pole Timestamp stworzenia rezerwacji do klasy Reservation - - // todo: dodać rollback do rezerwacji hotelu - // todo: dodać rollback do rezerwacji transportu - // todo: // Tu jest niedokończony kod, który stanowi podstawę pod obsługę płatności // (reservationId będzie gdzieś z góry) Runnable paymentTimeoutRunnable = () -> { - paymentTimeout(reservationId.toString()); + paymentTimeout(reservationId); }; ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); executorService.schedule(paymentTimeoutRunnable, PAYMENT_TIMEOUT_SECONDS, TimeUnit.SECONDS); @@ -128,9 +121,22 @@ public void createReservationFromRequest(ReservationRequest reservationRequest) System.out.println("reservationCreated: " + reservationRequest.getId()); } - public void paymentTimeout(String reservationId) { + public void paymentTimeout(UUID reservationId) { ReservationService.logger.warning("PAYMENT TIMEOUT FOR ID: " + reservationId + " !"); + ReservationDeleteRequest reservationDeleteRequest = new ReservationDeleteRequest(reservationId); + + // todo: dodać rollback do rezerwacji transportu + + + + // todo: dodać rollback do rezerwacji hotelu + bookHotelsSaga.deleteHotelReservation(reservationDeleteRequest); + + + // todo: dodać rollback usuwania obiektu rezerwacji + + } public ReservationConfirmationResponse purchaseReservation(String reservationId, String cardNumber) { @@ -165,7 +171,7 @@ public ReservationConfirmationResponse purchaseReservation(String reservationId, .build(); } - private HotelInfo getHotelInformation(String hotelId) { + private HotelInfo getHotelInformation(UUID hotelId) { return HotelInfo.builder().hotelPrice(1500.0f).name("Hotel testowy").roomTypes(Map.of("Pokój dwuosobowy", 1)).build(); } diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/saga/BookHotelsSaga.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/saga/BookHotelsSaga.java index 6f4bb410..f4bddfa1 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/saga/BookHotelsSaga.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/saga/BookHotelsSaga.java @@ -2,6 +2,7 @@ import lombok.RequiredArgsConstructor; import org.microarchitecturovisco.reservationservice.queues.config.QueuesHotelConfig; +import org.microarchitecturovisco.reservationservice.queues.config.ReservationDeleteRequest; import org.microarchitecturovisco.reservationservice.queues.config.ReservationRequest; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.stereotype.Service; @@ -27,4 +28,13 @@ public void createHotelReservation(ReservationRequest reservationRequest) { reservationRequest ); } + + public void deleteHotelReservation(ReservationDeleteRequest reservationDeleteRequest) { + rabbitTemplate.convertAndSend( + QueuesHotelConfig.EXCHANGE_HOTEL_FANOUT_DELETE_RESERVATION, + "", // Routing key is ignored for FanoutExchange + reservationDeleteRequest + ); + } + } From 435d2bd1bb9816d9e3f457189989d9bf9b5109cb Mon Sep 17 00:00:00 2001 From: Krzysztof Nazar Date: Mon, 20 May 2024 09:37:23 +0200 Subject: [PATCH 02/14] RSWW-102 add fields to HotelReservationDeleteRequest --- .../hotelservice/controllers/HotelsController.java | 7 +++++-- .../reservations/DeleteHotelReservationRequest.java | 3 +++ ...leteRequest.java => HotelReservationDeleteRequest.java} | 7 +++++-- .../reservationservice/services/saga/BookHotelsSaga.java | 6 +++--- 4 files changed, 16 insertions(+), 7 deletions(-) rename reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/{ReservationDeleteRequest.java => HotelReservationDeleteRequest.java} (61%) diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java index e4474f3e..a1d72d84 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java @@ -109,9 +109,12 @@ public void consumeMessageCreateHotelReservation(CreateHotelReservationRequest c @RabbitListener(queues = "#{handleDeleteHotelReservationQueue.name}") public void consumeMessageDeleteHotelReservation(DeleteHotelReservationRequest request) { System.out.println("Message received from queue DeleteHotelReservationRequest: " + request); - UUID reservationId = request.getReservationId(); - // TODO: delete the reservation using + // TODO: delete the reservation using fields in request: + // private UUID reservationId; + // private UUID hotelId; + // private List roomIds; + } } diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/reservations/DeleteHotelReservationRequest.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/reservations/DeleteHotelReservationRequest.java index 4edf9729..d5804b7a 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/reservations/DeleteHotelReservationRequest.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/reservations/DeleteHotelReservationRequest.java @@ -2,6 +2,7 @@ import lombok.*; +import java.util.List; import java.util.UUID; @Builder @@ -13,4 +14,6 @@ @Setter public class DeleteHotelReservationRequest { private UUID reservationId; + private UUID hotelId; + private List roomIds; } diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/ReservationDeleteRequest.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/HotelReservationDeleteRequest.java similarity index 61% rename from reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/ReservationDeleteRequest.java rename to reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/HotelReservationDeleteRequest.java index c3eb9572..fbcece69 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/ReservationDeleteRequest.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/HotelReservationDeleteRequest.java @@ -6,12 +6,15 @@ import lombok.NoArgsConstructor; import java.io.Serializable; +import java.util.List; import java.util.UUID; @Builder @Data @AllArgsConstructor @NoArgsConstructor -public class ReservationDeleteRequest implements Serializable { - private UUID id; +public class HotelReservationDeleteRequest implements Serializable { + private UUID reservationId; + private UUID hotelId; + private List roomIds; } diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/saga/BookHotelsSaga.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/saga/BookHotelsSaga.java index f4bddfa1..b96267e0 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/saga/BookHotelsSaga.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/saga/BookHotelsSaga.java @@ -2,7 +2,7 @@ import lombok.RequiredArgsConstructor; import org.microarchitecturovisco.reservationservice.queues.config.QueuesHotelConfig; -import org.microarchitecturovisco.reservationservice.queues.config.ReservationDeleteRequest; +import org.microarchitecturovisco.reservationservice.queues.config.HotelReservationDeleteRequest; import org.microarchitecturovisco.reservationservice.queues.config.ReservationRequest; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.stereotype.Service; @@ -29,11 +29,11 @@ public void createHotelReservation(ReservationRequest reservationRequest) { ); } - public void deleteHotelReservation(ReservationDeleteRequest reservationDeleteRequest) { + public void deleteHotelReservation(HotelReservationDeleteRequest hotelReservationDeleteRequest) { rabbitTemplate.convertAndSend( QueuesHotelConfig.EXCHANGE_HOTEL_FANOUT_DELETE_RESERVATION, "", // Routing key is ignored for FanoutExchange - reservationDeleteRequest + hotelReservationDeleteRequest ); } From 87f1493002bf471b239fc2c8e24b174adffb41bd Mon Sep 17 00:00:00 2001 From: Krzysztof Nazar Date: Mon, 20 May 2024 10:27:41 +0200 Subject: [PATCH 03/14] RSWW-102 Implemented Room Reservation Delete command for CQRS, tested and works --- .../bootstrap/util/RoomReservationParser.java | 3 --- .../controllers/HotelsController.java | 15 +++++++++++- .../CreateHotelReservationRequest.java | 2 +- .../DeleteRoomReservationCommand.java | 16 +++++++++++++ .../events/RoomReservationDeletedEvent.java | 22 +++++++++++++++++ .../services/HotelEventProjector.java | 8 ++++++- .../services/HotelsCommandService.java | 24 +++++++++++++------ 7 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/cqrs/commands/DeleteRoomReservationCommand.java create mode 100644 hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/events/RoomReservationDeletedEvent.java diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/bootstrap/util/RoomReservationParser.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/bootstrap/util/RoomReservationParser.java index ec6714cb..924b1302 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/bootstrap/util/RoomReservationParser.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/bootstrap/util/RoomReservationParser.java @@ -1,10 +1,8 @@ package org.microarchitecturovisco.hotelservice.bootstrap.util; import org.microarchitecturovisco.hotelservice.model.dto.HotelDto; -import org.microarchitecturovisco.hotelservice.model.dto.LocationDto; import org.microarchitecturovisco.hotelservice.model.dto.RoomDto; import org.microarchitecturovisco.hotelservice.model.dto.RoomReservationDto; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.time.Duration; @@ -13,7 +11,6 @@ import java.time.ZoneOffset; import java.util.ArrayList; import java.util.List; -import java.util.Optional; import java.util.Random; import java.util.UUID; import java.util.logging.Logger; diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java index a1d72d84..69b25545 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java @@ -5,6 +5,7 @@ import org.microarchitecturovisco.hotelservice.controllers.reservations.CreateHotelReservationRequest; import org.microarchitecturovisco.hotelservice.controllers.reservations.DeleteHotelReservationRequest; import org.microarchitecturovisco.hotelservice.model.cqrs.commands.CreateRoomReservationCommand; +import org.microarchitecturovisco.hotelservice.model.cqrs.commands.DeleteRoomReservationCommand; import org.microarchitecturovisco.hotelservice.model.dto.RoomReservationDto; import org.microarchitecturovisco.hotelservice.model.dto.request.GetHotelDetailsRequestDto; import org.microarchitecturovisco.hotelservice.model.dto.request.GetHotelsBySearchQueryRequestDto; @@ -76,6 +77,7 @@ public String consumeMessageCheckHotelAvailability(CheckHotelAvailabilityRequest @RabbitListener(queues = "#{handleCreateHotelReservationQueue.name}") public void consumeMessageCreateHotelReservation(CreateHotelReservationRequest createHotelReservationRequest) { System.out.println("Message received from queue createHotelReservationRequest: " + createHotelReservationRequest); + System.out.println("Reservation id: " + createHotelReservationRequest.getId()); int numberOfRoomsInReservation = createHotelReservationRequest.getRoomReservationsIds().size(); @@ -85,7 +87,7 @@ public void consumeMessageCreateHotelReservation(CreateHotelReservationRequest c UUID roomId = createHotelReservationRequest.getRoomReservationsIds().get(i); RoomReservationDto roomReservation = new RoomReservationDto(); - roomReservation.setReservationId(createHotelReservationRequest.getReservationId()); + roomReservation.setReservationId(createHotelReservationRequest.getId()); roomReservation.setDateFrom(createHotelReservationRequest.getHotelTimeFrom()); roomReservation.setDateTo(createHotelReservationRequest.getHotelTimeTo()); roomReservation.setHotelId(createHotelReservationRequest.getHotelId()); @@ -115,6 +117,17 @@ public void consumeMessageDeleteHotelReservation(DeleteHotelReservationRequest r // private UUID hotelId; // private List roomIds; + for (UUID roomId : request.getRoomIds()){ + DeleteRoomReservationCommand command = DeleteRoomReservationCommand.builder() + .commandTimeStamp(LocalDateTime.now()) + .reservationId(request.getReservationId()) + .roomId(roomId) + .hotelId(request.getHotelId()) + .build(); + + hotelsCommandService.deleteReservation(command); + } + } } diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/reservations/CreateHotelReservationRequest.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/reservations/CreateHotelReservationRequest.java index 08490ae2..1c555653 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/reservations/CreateHotelReservationRequest.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/reservations/CreateHotelReservationRequest.java @@ -14,7 +14,7 @@ @Getter @Setter public class CreateHotelReservationRequest { - private UUID reservationId; + private UUID id; private LocalDateTime hotelTimeFrom; diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/cqrs/commands/DeleteRoomReservationCommand.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/cqrs/commands/DeleteRoomReservationCommand.java new file mode 100644 index 00000000..87d5446b --- /dev/null +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/cqrs/commands/DeleteRoomReservationCommand.java @@ -0,0 +1,16 @@ +package org.microarchitecturovisco.hotelservice.model.cqrs.commands; + +import lombok.Builder; +import lombok.Data; + +import java.time.LocalDateTime; +import java.util.UUID; + +@Data +@Builder +public class DeleteRoomReservationCommand { + private LocalDateTime commandTimeStamp; + private UUID reservationId; + private UUID roomId; + private UUID hotelId; +} diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/events/RoomReservationDeletedEvent.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/events/RoomReservationDeletedEvent.java new file mode 100644 index 00000000..3bf125da --- /dev/null +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/events/RoomReservationDeletedEvent.java @@ -0,0 +1,22 @@ +package org.microarchitecturovisco.hotelservice.model.events; + + +import jakarta.persistence.Entity; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.experimental.SuperBuilder; + +import java.util.UUID; + +@Entity +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +@SuperBuilder +public class RoomReservationDeletedEvent extends HotelEvent { + private UUID idRoomReservation; + private UUID idRoom; +} diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelEventProjector.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelEventProjector.java index 350fdf57..1a19ec7c 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelEventProjector.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelEventProjector.java @@ -11,7 +11,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.UUID; @Service @RequiredArgsConstructor @@ -37,6 +36,9 @@ public void project(List hotelEvents){ if (hotelEvent instanceof RoomReservationCreatedEvent){ apply((RoomReservationCreatedEvent) hotelEvent); } + if (hotelEvent instanceof RoomReservationDeletedEvent){ + apply((RoomReservationDeletedEvent) hotelEvent); + } } } @@ -111,4 +113,8 @@ private void apply(RoomReservationCreatedEvent event){ } + private void apply(RoomReservationDeletedEvent event){ + roomReservationRepository.deleteById(event.getIdRoomReservation()); + } + } diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelsCommandService.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelsCommandService.java index 4ad50b4e..ec2c6097 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelsCommandService.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelsCommandService.java @@ -1,16 +1,12 @@ package org.microarchitecturovisco.hotelservice.services; import lombok.RequiredArgsConstructor; - - import org.microarchitecturovisco.hotelservice.model.cqrs.commands.CreateHotelCommand; import org.microarchitecturovisco.hotelservice.model.cqrs.commands.CreateRoomReservationCommand; +import org.microarchitecturovisco.hotelservice.model.cqrs.commands.DeleteRoomReservationCommand; import org.microarchitecturovisco.hotelservice.model.dto.CateringOptionDto; import org.microarchitecturovisco.hotelservice.model.dto.RoomDto; -import org.microarchitecturovisco.hotelservice.model.events.CateringOptionCreatedEvent; -import org.microarchitecturovisco.hotelservice.model.events.HotelCreatedEvent; -import org.microarchitecturovisco.hotelservice.model.events.RoomCreatedEvent; -import org.microarchitecturovisco.hotelservice.model.events.RoomReservationCreatedEvent; +import org.microarchitecturovisco.hotelservice.model.events.*; import org.microarchitecturovisco.hotelservice.repositories.HotelEventStore; import org.springframework.stereotype.Service; @@ -62,9 +58,23 @@ public void createReservation(CreateRoomReservationCommand command){ .idHotel(command.getHotelId()) .idRoom(command.getRoomId()) .build(); - reservationCreatedEvent.setId(UUID.randomUUID()); + reservationCreatedEvent.setId(command.getRoomReservationDto().getReservationId()); hotelEventStore.save(reservationCreatedEvent); hotelEventProjector.project(List.of(reservationCreatedEvent)); } + public void deleteReservation(DeleteRoomReservationCommand command){ + RoomReservationDeletedEvent reservationDeletedEvent = RoomReservationDeletedEvent.builder() + .eventTimeStamp(command.getCommandTimeStamp()) + .idRoomReservation(command.getReservationId()) + .idHotel(command.getHotelId()) + .idRoom(command.getRoomId()) + .build(); + + reservationDeletedEvent.setId(UUID.randomUUID()); + + hotelEventStore.save(reservationDeletedEvent); + hotelEventProjector.project(List.of(reservationDeletedEvent)); + } + } From a0ea6b0ab9a4af1a6d51d1408b788d2278132dcd Mon Sep 17 00:00:00 2001 From: Krzysztof Nazar Date: Mon, 20 May 2024 10:37:17 +0200 Subject: [PATCH 04/14] RSWW-102 update rederivation service methods --- .../services/ReservationService.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java index 4838df26..f62d02e1 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java @@ -13,8 +13,8 @@ import org.microarchitecturovisco.reservationservice.domain.model.LocationReservationResponse; import org.microarchitecturovisco.reservationservice.domain.model.ReservationConfirmationResponse; import org.microarchitecturovisco.reservationservice.domain.model.TransportReservationResponse; +import org.microarchitecturovisco.reservationservice.queues.config.HotelReservationDeleteRequest; import org.microarchitecturovisco.reservationservice.queues.config.QueuesReservationConfig; -import org.microarchitecturovisco.reservationservice.queues.config.ReservationDeleteRequest; import org.microarchitecturovisco.reservationservice.queues.config.ReservationRequest; import org.microarchitecturovisco.reservationservice.repositories.ReservationRepository; import org.microarchitecturovisco.reservationservice.services.saga.BookHotelsSaga; @@ -38,7 +38,7 @@ @RequiredArgsConstructor public class ReservationService { - public static final int PAYMENT_TIMEOUT_SECONDS = 60; + public static final int PAYMENT_TIMEOUT_SECONDS = 15; public static Logger logger = Logger.getLogger(ReservationService.class.getName()); private final ReservationRepository reservationRepository; @@ -82,7 +82,6 @@ public UUID bookOrchestration(ReservationRequest reservationRequest) throws Rese UUID reservationId = UUID.randomUUID(); reservationRequest.setId(reservationId); - createReservationFromRequest(reservationRequest); bookHotelsSaga.createHotelReservation(reservationRequest); @@ -94,7 +93,7 @@ public UUID bookOrchestration(ReservationRequest reservationRequest) throws Rese // (reservationId będzie gdzieś z góry) Runnable paymentTimeoutRunnable = () -> { - paymentTimeout(reservationId); + paymentTimeout(reservationRequest); }; ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); executorService.schedule(paymentTimeoutRunnable, PAYMENT_TIMEOUT_SECONDS, TimeUnit.SECONDS); @@ -110,8 +109,8 @@ private void checkHotelAvailability(ReservationRequest reservationRequest) throw } private void checkTransportAvailability(ReservationRequest reservationRequest) throws ReservationFailException { - boolean transportIsAvailable = bookTransportsSaga.checkIfTransportIsAvailable(reservationRequest); -// boolean transportIsAvailable = true; // debug only +// boolean transportIsAvailable = bookTransportsSaga.checkIfTransportIsAvailable(reservationRequest); + boolean transportIsAvailable = true; // debug only System.out.println("transportIsAvailable: " + transportIsAvailable); if(!transportIsAvailable) { throw new ReservationFailException(); } } @@ -121,17 +120,21 @@ public void createReservationFromRequest(ReservationRequest reservationRequest) System.out.println("reservationCreated: " + reservationRequest.getId()); } - public void paymentTimeout(UUID reservationId) { - ReservationService.logger.warning("PAYMENT TIMEOUT FOR ID: " + reservationId + " !"); - - ReservationDeleteRequest reservationDeleteRequest = new ReservationDeleteRequest(reservationId); + public void paymentTimeout(ReservationRequest reservationRequest) { + ReservationService.logger.warning("PAYMENT TIMEOUT FOR ID: " + reservationRequest.getId() + " !"); // todo: dodać rollback do rezerwacji transportu // todo: dodać rollback do rezerwacji hotelu - bookHotelsSaga.deleteHotelReservation(reservationDeleteRequest); + + HotelReservationDeleteRequest hotelReservationDeleteRequest = HotelReservationDeleteRequest.builder() + .hotelId(reservationRequest.getHotelId()) + .reservationId(reservationRequest.getId()) + .roomIds(reservationRequest.getRoomReservationsIds()) + .build(); + bookHotelsSaga.deleteHotelReservation(hotelReservationDeleteRequest); // todo: dodać rollback usuwania obiektu rezerwacji From 483d09b5ce7c0fa296674d6a02ccb45481927bef Mon Sep 17 00:00:00 2001 From: Krzysztof Nazar Date: Mon, 20 May 2024 11:18:03 +0200 Subject: [PATCH 05/14] RSWW-102 add commands for reservation deletion in reservation module --- .../commands/DeleteReservationCommand.java | 37 ++++++++++++++++ .../events/ReservationDeletedEvent.java | 33 ++++++++++++++ .../queues/config/QueuesHotelConfig.java | 12 +++--- .../services/ReservationAggregate.java | 23 ++++++++++ .../services/ReservationProjector.java | 9 ++++ .../services/ReservationService.java | 43 ++++++++++++++++++- .../services/saga/BookHotelsSaga.java | 2 +- 7 files changed, 150 insertions(+), 9 deletions(-) create mode 100644 reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/commands/DeleteReservationCommand.java create mode 100644 reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/events/ReservationDeletedEvent.java diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/commands/DeleteReservationCommand.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/commands/DeleteReservationCommand.java new file mode 100644 index 00000000..1a6f1262 --- /dev/null +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/commands/DeleteReservationCommand.java @@ -0,0 +1,37 @@ +package org.microarchitecturovisco.reservationservice.domain.commands; + +import jakarta.persistence.ElementCollection; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.UUID; + +@Data +@Builder +@AllArgsConstructor +@Entity +@NoArgsConstructor +public class DeleteReservationCommand { + @Id + private UUID id; + private LocalDateTime hotelTimeFrom; + private LocalDateTime hotelTimeTo; + private int infantsQuantity; + private int kidsQuantity; + private int teensQuantity; + private int adultsQuantity; + private float price; + private boolean paid; + private UUID hotelId; + @ElementCollection + private List roomReservationsIds; + @ElementCollection + private List transportReservationsIds; + private UUID userId; +} diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/events/ReservationDeletedEvent.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/events/ReservationDeletedEvent.java new file mode 100644 index 00000000..c06719ce --- /dev/null +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/domain/events/ReservationDeletedEvent.java @@ -0,0 +1,33 @@ +package org.microarchitecturovisco.reservationservice.domain.events; + +import jakarta.persistence.ElementCollection; +import jakarta.persistence.Entity; +import lombok.*; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.UUID; + +@Getter +@Setter +@Entity +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class ReservationDeletedEvent extends ReservationEvent { + private UUID idReservation; + private LocalDateTime hotelTimeFrom; + private LocalDateTime hotelTimeTo; + private int infantsQuantity; + private int kidsQuantity; + private int teensQuantity; + private int adultsQuantity; + private float price; + private boolean paid; + private UUID hotelId; + @ElementCollection + private List roomReservationsIds; + @ElementCollection + private List transportReservationsIds; + private UUID userId; +} diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/QueuesHotelConfig.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/QueuesHotelConfig.java index 2c4500c4..887e317f 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/QueuesHotelConfig.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/queues/config/QueuesHotelConfig.java @@ -30,18 +30,18 @@ public Binding handleReservationRequestBinding(@Qualifier("handleHotelExchange") } - public static final String EXCHANGE_HOTEL_FANOUT = "hotels.createReservation.exchange"; + public static final String EXCHANGE_HOTEL_FANOUT_CREATE_RESERVATION = "hotels.createReservation.exchange"; - @Bean(name="fanoutExchangeHotel") - public FanoutExchange fanoutExchange() { - return new FanoutExchange(EXCHANGE_HOTEL_FANOUT); + @Bean(name="fanoutExchangeHotelCreateReservation") + public FanoutExchange fanoutExchangeHotelCreateReservation() { + return new FanoutExchange(EXCHANGE_HOTEL_FANOUT_CREATE_RESERVATION); } public static final String EXCHANGE_HOTEL_FANOUT_DELETE_RESERVATION = "hotels.deleteReservation.exchange"; - @Bean(name="fanoutExchangeDeleteReservation") - public FanoutExchange fanoutExchangeDeleteReservation() { + @Bean(name="fanoutExchangeHotelDeleteReservation") + public FanoutExchange fanoutExchangeHotelDeleteReservation() { return new FanoutExchange(EXCHANGE_HOTEL_FANOUT_DELETE_RESERVATION); } diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationAggregate.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationAggregate.java index 3feaf617..5455cfda 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationAggregate.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationAggregate.java @@ -2,9 +2,11 @@ import lombok.RequiredArgsConstructor; import org.microarchitecturovisco.reservationservice.domain.commands.CreateReservationCommand; +import org.microarchitecturovisco.reservationservice.domain.commands.DeleteReservationCommand; import org.microarchitecturovisco.reservationservice.domain.commands.UpdateReservationCommand; import org.microarchitecturovisco.reservationservice.domain.entity.Reservation; import org.microarchitecturovisco.reservationservice.domain.events.ReservationCreatedEvent; +import org.microarchitecturovisco.reservationservice.domain.events.ReservationDeletedEvent; import org.microarchitecturovisco.reservationservice.domain.events.ReservationEvent; import org.microarchitecturovisco.reservationservice.domain.events.ReservationUpdateEvent; import org.microarchitecturovisco.reservationservice.repositories.ReservationEventStore; @@ -44,6 +46,27 @@ public List handleCreateReservationCommand(CreateReservationCo return List.of(event); } + public List handleDeleteReservationCommand(DeleteReservationCommand command) { + ReservationDeletedEvent event = ReservationDeletedEvent.builder() + .idReservation(command.getId()) + .hotelTimeFrom(command.getHotelTimeFrom()) + .hotelTimeTo(command.getHotelTimeTo()) + .infantsQuantity(command.getInfantsQuantity()) + .kidsQuantity(command.getKidsQuantity()) + .teensQuantity(command.getTeensQuantity()) + .adultsQuantity(command.getAdultsQuantity()) + .price(command.getPrice()) + .paid(command.isPaid()) + .hotelId(command.getHotelId()) + .roomReservationsIds(command.getRoomReservationsIds()) + .transportReservationsIds(command.getTransportReservationsIds()) + .userId(command.getUserId()) + .build(); + reservationEventStore.save(event); + reservationProjector.project(List.of(event)); + return List.of(event); + } + public List handleReservationUpdateCommand(UpdateReservationCommand command) { Optional reservationOptional = reservationRepository.findById(command.getReservationId()); if(reservationOptional.isEmpty()) { diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationProjector.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationProjector.java index 92084228..65cea12b 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationProjector.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationProjector.java @@ -3,6 +3,7 @@ import lombok.RequiredArgsConstructor; import org.microarchitecturovisco.reservationservice.domain.entity.Reservation; import org.microarchitecturovisco.reservationservice.domain.events.ReservationCreatedEvent; +import org.microarchitecturovisco.reservationservice.domain.events.ReservationDeletedEvent; import org.microarchitecturovisco.reservationservice.domain.events.ReservationEvent; import org.microarchitecturovisco.reservationservice.domain.events.ReservationUpdateEvent; import org.microarchitecturovisco.reservationservice.repositories.ReservationRepository; @@ -23,6 +24,9 @@ public void project(List events) { if(event instanceof ReservationUpdateEvent) { apply((ReservationUpdateEvent) event); } + if(event instanceof ReservationDeletedEvent) { + apply((ReservationDeletedEvent) event); + } } } @@ -52,5 +56,10 @@ public void apply(ReservationUpdateEvent event) { reservationRepository.save(reservation); } + public void apply(ReservationDeletedEvent event) { + Reservation reservation = reservationRepository.findById(event.getIdReservation()).orElseThrow(RuntimeException::new); + reservationRepository.deleteById(reservation.getId()); + } + } diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java index f62d02e1..d9c9dfa1 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java @@ -2,6 +2,7 @@ import lombok.RequiredArgsConstructor; import org.microarchitecturovisco.reservationservice.domain.commands.CreateReservationCommand; +import org.microarchitecturovisco.reservationservice.domain.commands.DeleteReservationCommand; import org.microarchitecturovisco.reservationservice.domain.commands.UpdateReservationCommand; import org.microarchitecturovisco.reservationservice.domain.dto.HotelInfo; import org.microarchitecturovisco.reservationservice.domain.dto.PaymentRequestDto; @@ -38,7 +39,9 @@ @RequiredArgsConstructor public class ReservationService { - public static final int PAYMENT_TIMEOUT_SECONDS = 15; +// public static final int PAYMENT_TIMEOUT_SECONDS = 60; + public static final int PAYMENT_TIMEOUT_SECONDS = 10; // debug only + public static Logger logger = Logger.getLogger(ReservationService.class.getName()); private final ReservationRepository reservationRepository; @@ -73,6 +76,29 @@ public Reservation createReservation(LocalDateTime hotelTimeFrom, LocalDateTime return reservationRepository.findById(reservationId).orElseThrow(RuntimeException::new); } + private void deleteReservation(LocalDateTime hotelTimeFrom, LocalDateTime hotelTimeTo, + int infantsQuantity, int kidsQuantity, int teensQuantity, int adultsQuantity, + float price, UUID hotelId, List roomReservationsIds, + List transportReservationsIds, UUID userId, UUID reservationId) { + + DeleteReservationCommand command = DeleteReservationCommand.builder() + .id(reservationId) + .hotelTimeFrom(hotelTimeFrom) + .hotelTimeTo(hotelTimeTo) + .infantsQuantity(infantsQuantity) + .kidsQuantity(kidsQuantity) + .teensQuantity(teensQuantity) + .adultsQuantity(adultsQuantity) + .price(price) + .paid(false) + .hotelId(hotelId) + .roomReservationsIds(roomReservationsIds) + .transportReservationsIds(transportReservationsIds) + .userId(userId) + .build(); + reservationAggregate.handleDeleteReservationCommand(command); + } + public UUID bookOrchestration(ReservationRequest reservationRequest) throws ReservationFailException { @@ -135,7 +161,20 @@ public void paymentTimeout(ReservationRequest reservationRequest) { .roomIds(reservationRequest.getRoomReservationsIds()) .build(); bookHotelsSaga.deleteHotelReservation(hotelReservationDeleteRequest); - + deleteReservation( + reservationRequest.getHotelTimeFrom(), + reservationRequest.getHotelTimeTo(), + reservationRequest.getChildrenUnder3Quantity(), + reservationRequest.getChildrenUnder10Quantity(), + reservationRequest.getChildrenUnder18Quantity(), + reservationRequest.getAdultsQuantity(), + reservationRequest.getPrice(), + reservationRequest.getHotelId(), + reservationRequest.getRoomReservationsIds(), + reservationRequest.getTransportReservationsIds(), + reservationRequest.getUserId(), + reservationRequest.getId() + ); // todo: dodać rollback usuwania obiektu rezerwacji diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/saga/BookHotelsSaga.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/saga/BookHotelsSaga.java index b96267e0..aae6a0ba 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/saga/BookHotelsSaga.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/saga/BookHotelsSaga.java @@ -23,7 +23,7 @@ public boolean checkIfHotelIsAvailable(ReservationRequest reservationRequest) { } public void createHotelReservation(ReservationRequest reservationRequest) { rabbitTemplate.convertAndSend( - QueuesHotelConfig.EXCHANGE_HOTEL_FANOUT, + QueuesHotelConfig.EXCHANGE_HOTEL_FANOUT_CREATE_RESERVATION, "", // Routing key is ignored for FanoutExchange reservationRequest ); From 5aee409313b34bf8d9963546b69093169d64b0d4 Mon Sep 17 00:00:00 2001 From: Krzysztof Nazar Date: Mon, 20 May 2024 11:20:31 +0200 Subject: [PATCH 06/14] RSWW-102 update PAYMENT_TIMEOUT_SECONDS --- .../reservationservice/services/ReservationService.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java index d9c9dfa1..47f74bf1 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java @@ -39,8 +39,7 @@ @RequiredArgsConstructor public class ReservationService { -// public static final int PAYMENT_TIMEOUT_SECONDS = 60; - public static final int PAYMENT_TIMEOUT_SECONDS = 10; // debug only + public static final int PAYMENT_TIMEOUT_SECONDS = 60; public static Logger logger = Logger.getLogger(ReservationService.class.getName()); From 7d5a7a3bf63f1412918384b35c42d46101ab1ce1 Mon Sep 17 00:00:00 2001 From: Krzysztof Nazar Date: Mon, 20 May 2024 11:24:53 +0200 Subject: [PATCH 07/14] RSWW-102 update todo comments --- .../services/ReservationService.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java index 47f74bf1..abc5e402 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java @@ -148,18 +148,19 @@ public void createReservationFromRequest(ReservationRequest reservationRequest) public void paymentTimeout(ReservationRequest reservationRequest) { ReservationService.logger.warning("PAYMENT TIMEOUT FOR ID: " + reservationRequest.getId() + " !"); - // todo: dodać rollback do rezerwacji transportu + // Delete reservation in Transport service - // todo: dodać rollback do rezerwacji hotelu - + // Delete reservation in Hotel service HotelReservationDeleteRequest hotelReservationDeleteRequest = HotelReservationDeleteRequest.builder() .hotelId(reservationRequest.getHotelId()) .reservationId(reservationRequest.getId()) .roomIds(reservationRequest.getRoomReservationsIds()) .build(); bookHotelsSaga.deleteHotelReservation(hotelReservationDeleteRequest); + + // Delete reservation from the ReservationRepository in Reservation service deleteReservation( reservationRequest.getHotelTimeFrom(), reservationRequest.getHotelTimeTo(), @@ -175,9 +176,6 @@ public void paymentTimeout(ReservationRequest reservationRequest) { reservationRequest.getId() ); - // todo: dodać rollback usuwania obiektu rezerwacji - - } public ReservationConfirmationResponse purchaseReservation(String reservationId, String cardNumber) { From e07edee3ee2b7a5ef6ea1c3fa74a91084d55f577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Mon, 20 May 2024 12:23:22 +0200 Subject: [PATCH 08/14] RSWW-106 add cors mapping for post requests and add loggers in some places MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- .../src/main/resources/application.yml | 35 ++++++++++++++----- .../controllers/HotelsController.java | 34 ++++++++++++------ .../hotelservice/utils/JsonReader.java | 10 ++++++ .../controllers/OffersController.java | 17 +++++++-- .../offerprovider/services/OffersService.java | 7 ---- .../reservationservice/config/WebConfig.java | 16 +++++++++ .../controllers/ReservationController.java | 5 +-- .../services/saga/BookHotelsSaga.java | 5 +++ .../TransportsQueryController.java | 23 +++++++++--- 9 files changed, 115 insertions(+), 37 deletions(-) create mode 100644 reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/config/WebConfig.java diff --git a/api-gateway/src/main/resources/application.yml b/api-gateway/src/main/resources/application.yml index f3d49e85..5d0135bb 100644 --- a/api-gateway/src/main/resources/application.yml +++ b/api-gateway/src/main/resources/application.yml @@ -8,6 +8,13 @@ eureka: instance: prefer-ip-address: true + +logging: + level: + org: + springframework: + cloud: + gateway: TRACE spring: application: name: api-gateway @@ -15,16 +22,23 @@ spring: gateway: globalcors: cors-configurations: - "[/**]": + '[/**]': allowed-origins: - - "*" + - '*' + allowed-origin-patterns: + - '*' allowed-methods: - - "*" - "/**": + - '*' + '/**': allowed-origin-patterns: - - "*" + - '*' + allowed-origins: + - '*' + allowed-headers: + - '*' allowed-methods: - - "*" + - '*' + add-to-simple-url-handler-mapping: true routes: - id: hotel-service uri: lb://hotel-service @@ -54,11 +68,16 @@ spring: uri: lb://reservation-service predicates: - Path=/reservations/** - filters: - - RemoveRequestHeader=Cookie + metadata: + cors: + allowedOrigins: '*' + allowedMethods: '*' + allowedHeaders: '*' - id: payment-service uri: lb://payment-service predicates: - Path=/payments/** filters: - RemoveRequestHeader=Cookie + default-filters: + - DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java index bc2494b1..19240c69 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java @@ -24,6 +24,7 @@ import java.util.ArrayList; import java.util.List; import java.util.UUID; +import java.util.logging.Logger; @RestController() @RequestMapping("/hotels") @@ -36,11 +37,13 @@ public class HotelsController { @RabbitListener(queues = "hotels.requests.hotelsBySearchQuery") public String consumeGetHotelsRequest(String requestDtoJson) { + Logger logger = Logger.getLogger("getHotelsBySearchQuery"); + logger.info("Request: " + requestDtoJson); + GetHotelsBySearchQueryRequestDto requestDto = JsonReader.readGetHotelsBySearchQueryRequestFromJson(requestDtoJson); GetHotelsBySearchQueryResponseDto responseDto = hotelsService.GetHotelsBySearchQuery(requestDto); - System.out.println("Send hotels response size " + responseDto.getHotels().size()); - + logger.info("Response hotels size: " + responseDto.getHotels().size()); return JsonConverter.convertGetHotelsBySearchQueryResponseDto(responseDto); } @@ -48,29 +51,38 @@ public String consumeGetHotelsRequest(String requestDtoJson) { @RabbitListener(queues = "hotels.requests.getHotelDetails") public String consumeGetHotelDetails(String requestDtoJson) { + Logger logger = Logger.getLogger("getHotelDetails"); + GetHotelDetailsRequestDto requestDto = JsonReader.readGetHotelDetailsRequestFromJson(requestDtoJson); GetHotelDetailsResponseDto responseDto = hotelsService.getHotelDetails(requestDto); + logger.info("Response for hotel: " + responseDto.getHotelId() + " " + responseDto.getHotelName()); return JsonConverter.convertGetHotelDetailsResponseDto(responseDto); } @RabbitListener(queues = QueuesConfig.QUEUE_HOTEL_CHECK_AVAILABILITY_REQ) - public String consumeMessageCheckHotelAvailability(CheckHotelAvailabilityRequest request) { - System.out.println("Message received from queue: " + request); + public String consumeMessageCheckHotelAvailability(String request) { + + Logger logger = Logger.getLogger("checkHotelAvailability"); + logger.info("Request: " + request); + + CheckHotelAvailabilityRequest availabilityRequest = JsonReader.readDtoFromJson(request, CheckHotelAvailabilityRequest.class); GetHotelsBySearchQueryRequestDto query = GetHotelsBySearchQueryRequestDto.builder() - .dateFrom(request.getHotelTimeFrom()) - .dateTo(request.getHotelTimeTo()) - .arrivalLocationIds(request.getArrivalLocationIds()) - .adults(request.getAdultsQuantity()) - .childrenUnderThree(request.getChildrenUnder3Quantity()) - .childrenUnderTen(request.getChildrenUnder10Quantity()) - .childrenUnderEighteen(request.getChildrenUnder18Quantity()) + .dateFrom(availabilityRequest.getHotelTimeFrom()) + .dateTo(availabilityRequest.getHotelTimeTo()) + .arrivalLocationIds(availabilityRequest.getArrivalLocationIds()) + .adults(availabilityRequest.getAdultsQuantity()) + .childrenUnderThree(availabilityRequest.getChildrenUnder3Quantity()) + .childrenUnderTen(availabilityRequest.getChildrenUnder10Quantity()) + .childrenUnderEighteen(availabilityRequest.getChildrenUnder18Quantity()) .build(); GetHotelsBySearchQueryResponseDto hotels = hotelsService.GetHotelsBySearchQuery(query); + logger.info("Response size: " + hotels.getHotels().size()); + return Boolean.toString(!hotels.getHotels().isEmpty()); } diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/utils/JsonReader.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/utils/JsonReader.java index 2b906835..720b1ab5 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/utils/JsonReader.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/utils/JsonReader.java @@ -39,4 +39,14 @@ public static CreateRoomReservationCommand readCreateRoomReservationCommand(Stri throw new RuntimeException("Json reader failed"); } } + + public static T readDtoFromJson(String json, Class dtoClass) { + ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule()); + try { + return mapper.readValue(json, dtoClass); + } catch (JsonProcessingException e) { + e.printStackTrace(); + throw new RuntimeException("Json reader failed"); + } + } } diff --git a/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/controllers/OffersController.java b/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/controllers/OffersController.java index e237b26a..972202a5 100644 --- a/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/controllers/OffersController.java +++ b/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/controllers/OffersController.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.List; import java.util.UUID; +import java.util.logging.Logger; @RestController @RequiredArgsConstructor @@ -33,6 +34,9 @@ public List getOffersBasedOnSearchQuery( ) { + Logger logger = Logger.getLogger("getOffersBasedOnSearchQuery"); + logger.info("Request for arrivals: " + arrivals); + departureBuses = departureBuses != null ? departureBuses : new ArrayList<>(); departurePlane = departurePlane != null ? departurePlane : new ArrayList<>(); @@ -41,7 +45,7 @@ public List getOffersBasedOnSearchQuery( List arrivalsWithUUIDs = arrivals.stream().map(UUID::fromString).toList(); - return offersService.getOffersBasedOnSearchQuery(departureBusesWithUUIDs, + List offerDtos = offersService.getOffersBasedOnSearchQuery(departureBusesWithUUIDs, departurePlanesWithUUIDs, arrivalsWithUUIDs, dateFrom, @@ -50,6 +54,9 @@ public List getOffersBasedOnSearchQuery( infants, kids, teens); + + logger.info("Response size: " + offerDtos.size()); + return offerDtos; } @GetMapping("/{idHotel}") @@ -64,11 +71,17 @@ public GetOfferDetailsResponseDto getOfferDetails( @RequestParam(name = "kids") Integer kids, @RequestParam(name = "teens") Integer teens ) { + Logger logger = Logger.getLogger("getOfferDetails"); + logger.info("Request for hotel ID: " + idHotel); + departureBuses = departureBuses != null ? departureBuses : new ArrayList<>(); departurePlanes = departurePlanes != null ? departurePlanes : new ArrayList<>(); - return offersService.getOfferDetails( + GetOfferDetailsResponseDto responseDto = offersService.getOfferDetails( idHotel, dateFrom, dateTo, departureBuses, departurePlanes, adults, infants, kids, teens ); + logger.info("Response: " + responseDto); + + return responseDto; } } diff --git a/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/services/OffersService.java b/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/services/OffersService.java index a2261f56..5f5aafb4 100644 --- a/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/services/OffersService.java +++ b/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/services/OffersService.java @@ -69,8 +69,6 @@ public List getOffersBasedOnSearchQuery(List departureBuses, kids, teens); - System.out.println("Available transports: " + availableTransports); - List availableHotels = getAvailableHotelsBasedOnSearchQuery( dateFromString, dateToString, @@ -80,8 +78,6 @@ public List getOffersBasedOnSearchQuery(List departureBuses, kids, teens); - System.out.println("Available hotels: " + availableHotels); - Pair tripDates = parseDates(dateFromString, dateToString); List offers = new ArrayList<>(); @@ -212,7 +208,6 @@ private List getFilteredHotelsFromTransportModule( if(responseMessageB != null) { String responseMessage = (new String(responseMessageB)).replace("\\", ""); responseMessage = responseMessage.substring(1, responseMessage.length() - 1); - System.out.println(responseMessage); GetHotelsBySearchQueryResponseDto response = JsonReader.readHotelsBySearchQueryResponseDtoFromJson(responseMessage); return response.getHotels(); } @@ -260,8 +255,6 @@ private List> getFilteredTransportsFromTransportModule( if(responseMessage != null) { - System.out.println("Transports message: " + responseMessage); - TransportsBasedOnSearchQueryResponse transportDtoResponse = JsonReader.readTransportsBasedOnSearchQueryResponseFromJson(responseMessage); return transportDtoResponse.getTransportPairs(); } diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/config/WebConfig.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/config/WebConfig.java new file mode 100644 index 00000000..35b3263f --- /dev/null +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/config/WebConfig.java @@ -0,0 +1,16 @@ +package org.microarchitecturovisco.reservationservice.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +@EnableWebMvc +public class WebConfig implements WebMvcConfigurer { + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**"); + } +} \ No newline at end of file diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/controllers/ReservationController.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/controllers/ReservationController.java index ec34240f..7d2dd812 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/controllers/ReservationController.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/controllers/ReservationController.java @@ -8,10 +8,7 @@ import org.microarchitecturovisco.reservationservice.queues.config.ReservationRequest; import org.microarchitecturovisco.reservationservice.services.ReservationService; import org.springframework.amqp.rabbit.annotation.RabbitListener; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.UUID; diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/saga/BookHotelsSaga.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/saga/BookHotelsSaga.java index 6f4bb410..67c9780f 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/saga/BookHotelsSaga.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/saga/BookHotelsSaga.java @@ -6,6 +6,8 @@ import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.stereotype.Service; +import java.util.logging.Logger; + @Service @RequiredArgsConstructor public class BookHotelsSaga { @@ -18,6 +20,9 @@ public boolean checkIfHotelIsAvailable(ReservationRequest reservationRequest) { reservationRequest ); + Logger logger = Logger.getLogger("checkIfHotelIsAvailable"); + logger.info("Result from hotels " + result); + return Boolean.parseBoolean(result); } public void createHotelReservation(ReservationRequest reservationRequest) { diff --git a/transport-service/src/main/java/org/microarchitecturovisco/transport/controllers/TransportsQueryController.java b/transport-service/src/main/java/org/microarchitecturovisco/transport/controllers/TransportsQueryController.java index 9aabe761..9dd13367 100644 --- a/transport-service/src/main/java/org/microarchitecturovisco/transport/controllers/TransportsQueryController.java +++ b/transport-service/src/main/java/org/microarchitecturovisco/transport/controllers/TransportsQueryController.java @@ -29,6 +29,7 @@ import java.time.LocalDateTime; import java.util.List; import java.util.UUID; +import java.util.logging.Logger; @RestController() @RequestMapping("/transports") @@ -69,39 +70,51 @@ public String test() { @RabbitListener(queues = "transports.requests.getTransportsBySearchQuery") public String consumeGetTransportsRequest(String requestDtoJson) { - long startTime = System.currentTimeMillis(); + + Logger logger = Logger.getLogger("getTransportsBySearchQuery"); + logger.info("Request: " + requestDtoJson); GetTransportsBySearchQueryRequestDto requestDto = JsonReader.readGetTransportsBySearchQueryRequestFromJson(requestDtoJson); GetTransportsBySearchQueryResponseDto responseDto = transportsQueryService.getTransportsBySearchQuery(requestDto); - long endTime = System.currentTimeMillis(); - System.out.println("Send transports response size " + responseDto.getTransportDtoList().size()); - System.out.println("Service call took " + (endTime - startTime) + " ms"); + logger.info("Response size: " + responseDto.getTransportDtoList().size()); return JsonConverter.convertGetTransportsBySearchQueryResponseDto(responseDto); } @RabbitListener(queues = "transports.requests.getTransportsBetweenLocations") public String getTransportsBetweenLocations(String requestDtoJson) { + Logger logger = Logger.getLogger("getTransportsBetweenLocations"); + logger.info("Request: " + requestDtoJson); + GetTransportsBetweenLocationsRequestDto requestDto = JsonReader.readGetTransportsBetweenLocationsRequestDtoFromJson(requestDtoJson); GetTransportsBetweenLocationsResponseDto responseDto = transportsQueryService.getTransportsBetweenLocations(requestDto); + logger.info("Response size: " + responseDto.getTransportPairs().size()); + return JsonConverter.convertGetTransportsBetweenLocationsResponseDto(responseDto); } @RabbitListener(queues = "transports.requests.getTransportsBetweenMultipleLocations") public String getTransportsBetweenMultipleLocations(String requestDtoJson) { + Logger logger = Logger.getLogger("getTransportsBetweenMultipleLocations"); + logger.info("Request: " + requestDtoJson); + GetTransportsBetweenMultipleLocationsRequestDto requestDto = JsonReader.readDtoFromJson(requestDtoJson, GetTransportsBetweenMultipleLocationsRequestDto.class); GetTransportsBetweenLocationsResponseDto responseDto = transportsQueryService.getTransportsBetweenMultipleLocations(requestDto); + + logger.info("Response size: " + responseDto.getTransportPairs().size()); + return JsonConverter.convertGetTransportsBetweenLocationsResponseDto(responseDto); } @RabbitListener(queues = "#{handleCreateTransportReservationQueue.name}") public void consumeMessageCreateTransportReservation(ReservationRequest request) { - System.out.println("Message received from queue: " + request); + Logger logger = Logger.getLogger("createTransportReservation"); + logger.info("Request: " + request); int numberOfTransportsInReservation = request.getTransportReservationsIds().size(); From 0e47733dba66aa73b85bd636de4a9deecf20b754 Mon Sep 17 00:00:00 2001 From: Krzysztof Nazar Date: Mon, 20 May 2024 12:35:19 +0200 Subject: [PATCH 09/14] RSWW-102 fixed check hotel availability method --- .../controllers/HotelsController.java | 10 ++-- .../CheckHotelAvailabilityRequest.java | 6 +-- ...CheckHotelAvailabilityQueryRequestDto.java | 30 ++++++++++++ .../GetHotelsBySearchQueryRequestDto.java | 9 ++-- .../services/HotelEventProjector.java | 19 +++++++- .../hotelservice/services/HotelsService.java | 48 +++++++++++++++++++ 6 files changed, 110 insertions(+), 12 deletions(-) create mode 100644 hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/dto/request/CheckHotelAvailabilityQueryRequestDto.java diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java index 69b25545..583fb019 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/HotelsController.java @@ -7,6 +7,7 @@ import org.microarchitecturovisco.hotelservice.model.cqrs.commands.CreateRoomReservationCommand; import org.microarchitecturovisco.hotelservice.model.cqrs.commands.DeleteRoomReservationCommand; import org.microarchitecturovisco.hotelservice.model.dto.RoomReservationDto; +import org.microarchitecturovisco.hotelservice.model.dto.request.CheckHotelAvailabilityQueryRequestDto; import org.microarchitecturovisco.hotelservice.model.dto.request.GetHotelDetailsRequestDto; import org.microarchitecturovisco.hotelservice.model.dto.request.GetHotelsBySearchQueryRequestDto; import org.microarchitecturovisco.hotelservice.model.dto.response.GetHotelDetailsResponseDto; @@ -59,19 +60,20 @@ public String consumeGetHotelDetails(String requestDtoJson) { public String consumeMessageCheckHotelAvailability(CheckHotelAvailabilityRequest request) { System.out.println("Message received from queue: " + request); - GetHotelsBySearchQueryRequestDto query = GetHotelsBySearchQueryRequestDto.builder() + CheckHotelAvailabilityQueryRequestDto query = CheckHotelAvailabilityQueryRequestDto.builder() .dateFrom(request.getHotelTimeFrom()) .dateTo(request.getHotelTimeTo()) - .arrivalLocationIds(request.getArrivalLocationIds()) .adults(request.getAdultsQuantity()) .childrenUnderThree(request.getChildrenUnder3Quantity()) .childrenUnderTen(request.getChildrenUnder10Quantity()) .childrenUnderEighteen(request.getChildrenUnder18Quantity()) + .hotelId(request.getHotelId()) + .roomReservationsIds(request.getRoomReservationsIds()) .build(); - GetHotelsBySearchQueryResponseDto hotels = hotelsService.GetHotelsBySearchQuery(query); + boolean hotelAvailable = hotelsService.CheckHotelAvailability(query); - return Boolean.toString(!hotels.getHotels().isEmpty()); + return String.valueOf(hotelAvailable) ; } @RabbitListener(queues = "#{handleCreateHotelReservationQueue.name}") diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/reservations/CheckHotelAvailabilityRequest.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/reservations/CheckHotelAvailabilityRequest.java index 99311651..1dd201fe 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/reservations/CheckHotelAvailabilityRequest.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/controllers/reservations/CheckHotelAvailabilityRequest.java @@ -24,7 +24,7 @@ public class CheckHotelAvailabilityRequest { private int childrenUnder18Quantity; - private List departureLocationIdsByPlane; - private List departureLocationIdsByBus; - private List arrivalLocationIds; + private UUID hotelId; + + private List roomReservationsIds; } diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/dto/request/CheckHotelAvailabilityQueryRequestDto.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/dto/request/CheckHotelAvailabilityQueryRequestDto.java new file mode 100644 index 00000000..54dae77c --- /dev/null +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/dto/request/CheckHotelAvailabilityQueryRequestDto.java @@ -0,0 +1,30 @@ +package org.microarchitecturovisco.hotelservice.model.dto.request; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.time.LocalDateTime; +import java.util.List; +import java.util.UUID; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class CheckHotelAvailabilityQueryRequestDto implements Serializable { + + private LocalDateTime dateFrom; + private LocalDateTime dateTo; + + private Integer adults; + private Integer childrenUnderThree; + private Integer childrenUnderTen; + private Integer childrenUnderEighteen; + + private UUID hotelId; + + private List roomReservationsIds; +} diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/dto/request/GetHotelsBySearchQueryRequestDto.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/dto/request/GetHotelsBySearchQueryRequestDto.java index ebb6dbf2..dafb30b8 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/dto/request/GetHotelsBySearchQueryRequestDto.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/dto/request/GetHotelsBySearchQueryRequestDto.java @@ -4,7 +4,6 @@ import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; -import org.springframework.beans.factory.annotation.Autowired; import java.io.Serializable; import java.time.LocalDateTime; @@ -20,10 +19,14 @@ public class GetHotelsBySearchQueryRequestDto implements Serializable { private LocalDateTime dateFrom; private LocalDateTime dateTo; - private List arrivalLocationIds; - private Integer adults; private Integer childrenUnderThree; private Integer childrenUnderTen; private Integer childrenUnderEighteen; + + private UUID hotelId; + + private List roomReservationsIds; + + private List arrivalLocationIds; } diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelEventProjector.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelEventProjector.java index 1a19ec7c..5ad245b9 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelEventProjector.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelEventProjector.java @@ -10,7 +10,9 @@ import org.springframework.stereotype.Service; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; +import java.util.UUID; @Service @RequiredArgsConstructor @@ -112,9 +114,22 @@ private void apply(RoomReservationCreatedEvent event){ } + private void apply(RoomReservationDeletedEvent event) { + UUID roomReservationId = event.getIdRoomReservation(); - private void apply(RoomReservationDeletedEvent event){ - roomReservationRepository.deleteById(event.getIdRoomReservation()); + Room room = roomRepository.findById(event.getIdRoom()).orElseThrow(RuntimeException::new); + List roomReservations = room.getRoomReservations(); + + Iterator iterator = roomReservations.iterator(); + while (iterator.hasNext()) { + RoomReservation roomReservation = iterator.next(); + if (roomReservation.getId().equals(roomReservationId)) { + iterator.remove(); + roomReservationRepository.deleteById(roomReservationId); // Explicitly remove from RoomReservation repository + roomRepository.save(room); + break; + } + } } } diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelsService.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelsService.java index cd1e69bf..628817c4 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelsService.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelsService.java @@ -121,5 +121,53 @@ public GetHotelsBySearchQueryResponseDto GetHotelsBySearchQuery(GetHotelsBySearc .build(); } + public boolean CheckHotelAvailability(CheckHotelAvailabilityQueryRequestDto requestDto) { + // Step 1: Extract information from the request DTO + LocalDateTime dateFrom = requestDto.getDateFrom(); + LocalDateTime dateTo = requestDto.getDateTo(); + int numberOfGuests = requestDto.getAdults() + + requestDto.getChildrenUnderEighteen() + + requestDto.getChildrenUnderTen() + + requestDto.getChildrenUnderThree(); + UUID hotelId = requestDto.getHotelId(); + List roomIds = requestDto.getRoomReservationsIds(); + + // Step 2: Retrieve the hotel from the repository + Optional hotelOpt = hotelRepository.findById(hotelId); + if (hotelOpt.isEmpty()) { + return false; + } + + Hotel hotel = hotelOpt.get(); + + // Step 3: Filter rooms by room IDs + List specificRooms = hotel.getRooms().stream() + .filter(room -> roomIds.contains(room.getId())) + .collect(Collectors.toList()); + + // Step 4: Filter rooms by guest capacity + List roomsWithSufficientCapacity = specificRooms.stream() + .filter(room -> room.getGuestCapacity() >= numberOfGuests) + .collect(Collectors.toList()); + + // Step 5: Filter rooms by availability + List availableRooms = roomsWithSufficientCapacity.stream() + .filter(room -> isRoomAvailable(room, dateFrom, dateTo)) + .collect(Collectors.toList()); + + // Step 6: Determine availability + return !availableRooms.isEmpty(); + } + + private boolean isRoomAvailable(Room room, LocalDateTime dateFrom, LocalDateTime dateTo) { + for (RoomReservation reservation : room.getRoomReservations()) { + if (reservation.getDateFrom().isBefore(dateTo) && reservation.getDateTo().isAfter(dateFrom)) { + return false; + } + } + return true; + } + + } From c4bad8821b249a1524ff38c876c6761a5dc7ab01 Mon Sep 17 00:00:00 2001 From: CzajaLukasz Date: Mon, 20 May 2024 15:45:38 +0200 Subject: [PATCH 10/14] RSWW-109 change price --- .../offerprovider/services/OffersService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/services/OffersService.java b/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/services/OffersService.java index 5f5aafb4..feaf9294 100644 --- a/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/services/OffersService.java +++ b/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/services/OffersService.java @@ -135,7 +135,7 @@ public Float calculatePrice(int numberOfDays, int adults, int infants, int kids, final float LAST_MINUTE_DISCOUNT_FACTOR = 0.7f; final float HIGH_PRICE_FACTOR = 1.2f; - float adultHotelPrice = (pricePerAdultPerRoomPerDay + cateringPrice); + float adultHotelPrice = (pricePerAdultPerRoomPerDay / 10 + cateringPrice); float priceForAdultsPerDayPerRoom = adults * adultHotelPrice; float priceForInfantPerDayPerRoom = infants * adultHotelPrice * INFANT_DISCOUNT_FACTOR; From 7d6f7ad29721f683e0ad030624505c4edfec959b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Mon, 20 May 2024 20:22:05 +0200 Subject: [PATCH 11/14] RSWW-107 OffersService: offer details: return both elements of transport pair MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- .../responses/GetOfferDetailsResponseDto.java | 4 ++-- .../offerprovider/services/OffersService.java | 20 +++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/domain/responses/GetOfferDetailsResponseDto.java b/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/domain/responses/GetOfferDetailsResponseDto.java index cf11267b..09c7dad5 100644 --- a/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/domain/responses/GetOfferDetailsResponseDto.java +++ b/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/domain/responses/GetOfferDetailsResponseDto.java @@ -29,6 +29,6 @@ public class GetOfferDetailsResponseDto { private List cateringOptions; - private TransportDto departure; - private List possibleDepartures; + private List departure; + private List> possibleDepartures; } diff --git a/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/services/OffersService.java b/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/services/OffersService.java index feaf9294..7749ca72 100644 --- a/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/services/OffersService.java +++ b/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/services/OffersService.java @@ -310,16 +310,24 @@ public GetOfferDetailsResponseDto getOfferDetails( Logger logger = Logger.getLogger("Offer Provider"); logger.info("Offer details" + idHotel + ": Received hotel details"); - List transportsToHotel = getFilteredTransportsFromTransportModule( + List> transports = getFilteredTransportsFromTransportModule( departureBuses, departurePlanes, List.of(hotelResponseDto.getLocation().getIdLocation()), dates.getFirst(), dates.getSecond(), adults, infants, kids, teens - ).stream() + ).stream().sorted(Comparator.comparing(pair -> pair.getFirst().getPricePerAdult())).toList(); + + List transportsToHotel = transports + .stream() .filter(list -> list.size() > 1) .map(List::getFirst) - .sorted(Comparator.comparing(TransportDto::getPricePerAdult)) .toList(); + List transportsFromHotel = transports + .stream() + .filter(list -> list.size() > 1) + .map(List::getLast) + .toList(); + logger.info("Offer details " + idHotel + ": Received transports to hotel"); List> roomConfigs = hotelResponseDto @@ -342,12 +350,12 @@ public GetOfferDetailsResponseDto getOfferDetails( hotelResponseDto.getRoomsConfigurations().stream().sorted(Comparator.comparing(RoomsConfigurationDto::getPricePerAdult)).toList().getFirst().getPricePerAdult(), catering.isEmpty() ? 0.0f : catering.getFirst().getPrice(), (int) ChronoUnit.DAYS.between(dates.getFirst(), LocalDateTime.now()), - transportsToHotel.getFirst().getPricePerAdult()) + transportsToHotel.getFirst().getPricePerAdult() + transportsFromHotel.getFirst().getPricePerAdult()) ) .roomConfiguration(roomConfigs.getFirst()) .possibleRoomConfigurations(roomConfigs.subList(1, roomConfigs.size())) - .departure(transportsToHotel.getFirst()) - .possibleDepartures(transportsToHotel.subList(1, transportsToHotel.size())) + .departure(List.of(transportsToHotel.getFirst(), transportsFromHotel.getLast())) + .possibleDepartures(List.of(transportsToHotel.subList(1, transportsToHotel.size()), transportsFromHotel.subList(1, transportsFromHotel.size()))) .imageUrls(hotelResponseDto.getPhotos()) .cateringOptions(catering) .build(); From 52faf98926e7f4693126da8e7247a11d10717635 Mon Sep 17 00:00:00 2001 From: CzajaLukasz Date: Mon, 20 May 2024 22:29:43 +0200 Subject: [PATCH 12/14] RSWW-102 add multiple rooms reservations --- .../GetHotelsBySearchQueryRequestDto.java | 6 ++--- .../repositories/HotelEventStore.java | 4 +++- .../services/HotelEventProjector.java | 10 ++++++--- .../services/HotelsCommandService.java | 2 +- .../hotelservice/services/HotelsService.java | 22 ++++++------------- .../services/ReservationService.java | 2 +- 6 files changed, 21 insertions(+), 25 deletions(-) diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/dto/request/GetHotelsBySearchQueryRequestDto.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/dto/request/GetHotelsBySearchQueryRequestDto.java index dafb30b8..71e74292 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/dto/request/GetHotelsBySearchQueryRequestDto.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/model/dto/request/GetHotelsBySearchQueryRequestDto.java @@ -19,14 +19,12 @@ public class GetHotelsBySearchQueryRequestDto implements Serializable { private LocalDateTime dateFrom; private LocalDateTime dateTo; + private List arrivalLocationIds; + private Integer adults; private Integer childrenUnderThree; private Integer childrenUnderTen; private Integer childrenUnderEighteen; - private UUID hotelId; - private List roomReservationsIds; - - private List arrivalLocationIds; } diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/repositories/HotelEventStore.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/repositories/HotelEventStore.java index fc41eca3..d339f4a3 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/repositories/HotelEventStore.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/repositories/HotelEventStore.java @@ -1,9 +1,11 @@ package org.microarchitecturovisco.hotelservice.repositories; import org.microarchitecturovisco.hotelservice.model.events.HotelEvent; +import org.microarchitecturovisco.hotelservice.model.events.RoomReservationCreatedEvent; import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; import java.util.UUID; public interface HotelEventStore extends JpaRepository { - } diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelEventProjector.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelEventProjector.java index 5ad245b9..41e4eeb8 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelEventProjector.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelEventProjector.java @@ -107,7 +107,6 @@ private void apply(RoomReservationCreatedEvent event){ .dateTo(event.getDateTo()) .room(room) .build(); - room.getRoomReservations().add(roomReservation); roomReservationRepository.save(roomReservation); roomRepository.save(room); @@ -116,6 +115,11 @@ private void apply(RoomReservationCreatedEvent event){ } private void apply(RoomReservationDeletedEvent event) { UUID roomReservationId = event.getIdRoomReservation(); + List roomReservationIds = eventStore.findAll().stream().filter(e -> e instanceof RoomReservationCreatedEvent).toList(); + + List roomReservationIds2 = roomReservationIds.stream().filter(e -> ((RoomReservationCreatedEvent) e).getIdRoomReservation().equals(roomReservationId)).toList(); + List roomReservationIds3 = roomReservationIds2.stream().map(HotelEvent::getId).toList(); + Room room = roomRepository.findById(event.getIdRoom()).orElseThrow(RuntimeException::new); List roomReservations = room.getRoomReservations(); @@ -123,9 +127,9 @@ private void apply(RoomReservationDeletedEvent event) { Iterator iterator = roomReservations.iterator(); while (iterator.hasNext()) { RoomReservation roomReservation = iterator.next(); - if (roomReservation.getId().equals(roomReservationId)) { + if (roomReservationIds3.contains(roomReservation.getId())) { iterator.remove(); - roomReservationRepository.deleteById(roomReservationId); // Explicitly remove from RoomReservation repository + roomReservationRepository.deleteById(roomReservation.getId()); // Explicitly remove from RoomReservation repository roomRepository.save(room); break; } diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelsCommandService.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelsCommandService.java index ec2c6097..117aa0a4 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelsCommandService.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelsCommandService.java @@ -58,7 +58,7 @@ public void createReservation(CreateRoomReservationCommand command){ .idHotel(command.getHotelId()) .idRoom(command.getRoomId()) .build(); - reservationCreatedEvent.setId(command.getRoomReservationDto().getReservationId()); + reservationCreatedEvent.setId(UUID.randomUUID()); hotelEventStore.save(reservationCreatedEvent); hotelEventProjector.project(List.of(reservationCreatedEvent)); } diff --git a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelsService.java b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelsService.java index 628817c4..59dadf41 100644 --- a/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelsService.java +++ b/hotel-service/src/main/java/org/microarchitecturovisco/hotelservice/services/HotelsService.java @@ -125,10 +125,7 @@ public boolean CheckHotelAvailability(CheckHotelAvailabilityQueryRequestDto requ // Step 1: Extract information from the request DTO LocalDateTime dateFrom = requestDto.getDateFrom(); LocalDateTime dateTo = requestDto.getDateTo(); - int numberOfGuests = requestDto.getAdults() - + requestDto.getChildrenUnderEighteen() - + requestDto.getChildrenUnderTen() - + requestDto.getChildrenUnderThree(); + UUID hotelId = requestDto.getHotelId(); List roomIds = requestDto.getRoomReservationsIds(); @@ -143,20 +140,15 @@ public boolean CheckHotelAvailability(CheckHotelAvailabilityQueryRequestDto requ // Step 3: Filter rooms by room IDs List specificRooms = hotel.getRooms().stream() .filter(room -> roomIds.contains(room.getId())) - .collect(Collectors.toList()); + .toList(); - // Step 4: Filter rooms by guest capacity - List roomsWithSufficientCapacity = specificRooms.stream() - .filter(room -> room.getGuestCapacity() >= numberOfGuests) - .collect(Collectors.toList()); - // Step 5: Filter rooms by availability - List availableRooms = roomsWithSufficientCapacity.stream() - .filter(room -> isRoomAvailable(room, dateFrom, dateTo)) - .collect(Collectors.toList()); + // Step 4: check availability of all rooms + for (Room specificRoom :specificRooms) { + if (!isRoomAvailable(specificRoom, dateFrom, dateTo)) { return false;} + } - // Step 6: Determine availability - return !availableRooms.isEmpty(); + return true; } private boolean isRoomAvailable(Room room, LocalDateTime dateFrom, LocalDateTime dateTo) { diff --git a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java index abc5e402..50e5612a 100644 --- a/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java +++ b/reservation-service/src/main/java/org/microarchitecturovisco/reservationservice/services/ReservationService.java @@ -39,7 +39,7 @@ @RequiredArgsConstructor public class ReservationService { - public static final int PAYMENT_TIMEOUT_SECONDS = 60; + public static final int PAYMENT_TIMEOUT_SECONDS = 10; public static Logger logger = Logger.getLogger(ReservationService.class.getName()); From 2e37a0a7936564bd472937e16962ac61dd48b06a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Cichowski?= Date: Mon, 20 May 2024 22:56:46 +0200 Subject: [PATCH 13/14] RSWW-110 offer-provider: OffersService: offer details: fix wrong return transport MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Cichowski --- .../offerprovider/services/OffersService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/services/OffersService.java b/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/services/OffersService.java index 7749ca72..e96935ec 100644 --- a/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/services/OffersService.java +++ b/offer-provider-service/src/main/java/org/microarchitecturovisco/offerprovider/services/OffersService.java @@ -354,7 +354,7 @@ public GetOfferDetailsResponseDto getOfferDetails( ) .roomConfiguration(roomConfigs.getFirst()) .possibleRoomConfigurations(roomConfigs.subList(1, roomConfigs.size())) - .departure(List.of(transportsToHotel.getFirst(), transportsFromHotel.getLast())) + .departure(List.of(transportsToHotel.getFirst(), transportsFromHotel.getFirst())) .possibleDepartures(List.of(transportsToHotel.subList(1, transportsToHotel.size()), transportsFromHotel.subList(1, transportsFromHotel.size()))) .imageUrls(hotelResponseDto.getPhotos()) .cateringOptions(catering) From f8b401823227be06ba55f43b95eafcd050becf1b Mon Sep 17 00:00:00 2001 From: CzajaLukasz Date: Mon, 20 May 2024 23:18:19 +0200 Subject: [PATCH 14/14] RSWW-111 fix user bootstrap --- .../microarchitecturovisco/userservice/bootstrap/Bootstrap.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user-service/src/main/java/org/microarchitecturovisco/userservice/bootstrap/Bootstrap.java b/user-service/src/main/java/org/microarchitecturovisco/userservice/bootstrap/Bootstrap.java index 0bc826ae..d0c86b9c 100644 --- a/user-service/src/main/java/org/microarchitecturovisco/userservice/bootstrap/Bootstrap.java +++ b/user-service/src/main/java/org/microarchitecturovisco/userservice/bootstrap/Bootstrap.java @@ -26,7 +26,7 @@ public class Bootstrap implements CommandLineRunner { public void run(String... args) { Logger logger = Logger.getLogger("Bootstrap | User"); - List users = importUsersFromCSV(resourceLoader.getResource("initData/users.csv")); + List users = importUsersFromCSV(resourceLoader.getResource("classpath:initData/users.csv")); userRepository.saveAll(users);