Skip to content

Commit

Permalink
Merge pull request #25 from Microarchitecturovisco/RSWW-81-Book-trans…
Browse files Browse the repository at this point in the history
…ports-saga

ReadyForReview: RSWW-81 Checking transport availability - saga
  • Loading branch information
Danzigerrr authored May 19, 2024
2 parents 8723248 + e8303d7 commit 1962950
Show file tree
Hide file tree
Showing 23 changed files with 204 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package org.microarchitecturovisco.hotelservice.controllers;

import lombok.RequiredArgsConstructor;
import org.microarchitecturovisco.hotelservice.model.cqrs.commands.CreateRoomReservationCommand;
import org.microarchitecturovisco.hotelservice.services.HotelsCommandService;
import org.microarchitecturovisco.hotelservice.utils.JsonReader;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RabbitMQConfig {
@Bean
Expand All @@ -20,15 +22,15 @@ public SimpleMessageListenerContainer listenerContainer(ConnectionFactory connec
return container;
}

@Bean(name="jsonMessageConverter")
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}

@Bean
public MessageListenerAdapter listenerAdapter(HotelsController consumer, MessageConverter messageConverter) {
public MessageListenerAdapter listenerAdapter(HotelsController consumer,@Qualifier("jsonMessageConverter") MessageConverter messageConverter) {
MessageListenerAdapter adapter = new MessageListenerAdapter(consumer, "consumeMessageFromQueue");
adapter.setMessageConverter(messageConverter);
return adapter;
}

@Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
public class Bootstrap implements CommandLineRunner {
@Override
public void run(String... args) {
// Your initialization logic here

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ public void consumeMessageCreateReservation(ReservationRequest reservationReques
);
System.out.println("Reservation created successfully: " + reservation.getId());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.List;
import java.util.UUID;


@Entity
@Getter
@Setter
Expand Down Expand Up @@ -55,4 +54,4 @@ public class Reservation {

@NotNull
private UUID userId;
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
package org.microarchitecturovisco.reservationservice.queues.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QueuesConfig {
public class QueuesHotelConfig {

public static final String EXCHANGE_HOTEL = "hotels.requests.checkAvailabilityByQuery.exchange";
public static final String QUEUE_HOTEL_BOOK_REQ = "hotels.requests.checkAvailabilityByQuery.queue";
public static final String ROUTING_KEY_HOTEL_BOOK_REQ = "hotels.requests.checkAvailabilityByQuery.routingKey";

public static final String EXCHANGE_TRANSPORT = "transports.requests.checkAvailabilityByQuery.exchange";
public static final String QUEUE_TRANSPORT_BOOK_REQ = "transports.requests.checkAvailabilityByQuery.queue";
public static final String ROUTING_KEY_TRANSPORT_BOOK_REQ = "transports.requests.checkAvailabilityByQuery.routingKey";


@Bean
@Bean(name="handleReservationExchange")
public TopicExchange handleReservationExchange() {
return new TopicExchange(EXCHANGE_HOTEL);
}


@Bean
public Queue handleReservationQueue() {
return new Queue(QUEUE_HOTEL_BOOK_REQ);
}
@Bean
public Binding handleReservationRequestBinding(TopicExchange handleReservationExchange, Queue handleReservationQueue) {
public Binding handleReservationRequestBinding(@Qualifier("handleReservationExchange") TopicExchange handleReservationExchange, Queue handleReservationQueue) {
return BindingBuilder.bind(handleReservationQueue).to(handleReservationExchange).with(ROUTING_KEY_HOTEL_BOOK_REQ);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.microarchitecturovisco.reservationservice.queues.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QueuesTransportConfig {

public static final String EXCHANGE_TRANSPORT = "transports.requests.checkAvailabilityByQuery.exchange";
public static final String QUEUE_TRANSPORT_BOOK_REQ = "transports.requests.checkAvailabilityByQuery.queue";
public static final String ROUTING_KEY_TRANSPORT_BOOK_REQ = "transports.requests.checkAvailabilityByQuery.routingKey";


@Bean(name="handleTransportReservationExchange")
public TopicExchange handleTransportReservationExchange() {
return new TopicExchange(EXCHANGE_TRANSPORT);
}
@Bean
public Queue handleTransportReservationQueue() {
return new Queue(QUEUE_TRANSPORT_BOOK_REQ);
}
@Bean
public Binding handleTransportReservationRequestBinding(@Qualifier("handleTransportReservationExchange") TopicExchange handleTransportReservationExchange, Queue handleTransportReservationQueue) {
return BindingBuilder.bind(handleTransportReservationQueue).to(handleTransportReservationExchange).with(ROUTING_KEY_TRANSPORT_BOOK_REQ);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class ReservationRequest implements Serializable {
private UUID id;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.microarchitecturovisco.reservationservice.domain.dto.PaymentRequestDto;
import org.microarchitecturovisco.reservationservice.domain.dto.PaymentResponseDto;
import org.microarchitecturovisco.reservationservice.domain.entity.Reservation;
import org.microarchitecturovisco.reservationservice.domain.events.ReservationEvent;
import org.microarchitecturovisco.reservationservice.domain.exceptions.PaymentProcessException;
import org.microarchitecturovisco.reservationservice.domain.exceptions.ReservationFailException;
import org.microarchitecturovisco.reservationservice.domain.exceptions.ReservationNotFoundAfterPaymentException;
Expand Down Expand Up @@ -72,12 +73,12 @@ public Reservation createReservation(LocalDateTime hotelTimeFrom, LocalDateTime
return reservationRepository.findById(reservationId).orElseThrow(RuntimeException::new);
}


public UUID bookOrchestration(ReservationRequest reservationRequest) throws ReservationFailException {

boolean hotelIsAvailable = bookHotelsSaga.checkIfHotelIsAvailable(reservationRequest);
// boolean hotelIsAvailable = true; // debug only
System.out.println("hotelIsAvailable: "+ hotelIsAvailable);

if(!hotelIsAvailable) { throw new ReservationFailException(); }

boolean transportIsAvailable = bookTransportsSaga.checkIfTransportIsAvailable(reservationRequest);
Expand Down Expand Up @@ -105,7 +106,7 @@ public UUID bookOrchestration(ReservationRequest reservationRequest) throws Rese
// dodać pole Timestamp stworzenia rezerwacji do klasy Reservation


// Tu jest nie dokończony kod, który stanowi podstawę pod obsługę płatności (reservationId będzie gdzieś z góry)
// 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());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.microarchitecturovisco.reservationservice.services.saga;

import lombok.RequiredArgsConstructor;
import org.microarchitecturovisco.reservationservice.queues.config.QueuesConfig;
import org.microarchitecturovisco.reservationservice.queues.config.QueuesHotelConfig;
import org.microarchitecturovisco.reservationservice.queues.hotels.ReservationRequest;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
Expand All @@ -13,11 +13,13 @@ public class BookHotelsSaga {

public boolean checkIfHotelIsAvailable(ReservationRequest reservationRequest) {
String result = (String) rabbitTemplate.convertSendAndReceive(
QueuesConfig.EXCHANGE_HOTEL,
QueuesConfig.ROUTING_KEY_HOTEL_BOOK_REQ,
QueuesHotelConfig.EXCHANGE_HOTEL,
QueuesHotelConfig.ROUTING_KEY_HOTEL_BOOK_REQ,
reservationRequest
);

System.out.println("checkIfHotelIsAvailable result:" + result);

return Boolean.parseBoolean(result);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.microarchitecturovisco.reservationservice.services.saga;

import lombok.RequiredArgsConstructor;
import org.microarchitecturovisco.reservationservice.queues.config.QueuesConfig;
import org.microarchitecturovisco.reservationservice.queues.config.QueuesTransportConfig;
import org.microarchitecturovisco.reservationservice.queues.hotels.ReservationRequest;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
Expand All @@ -13,8 +13,8 @@ public class BookTransportsSaga {

public boolean checkIfTransportIsAvailable(ReservationRequest reservationRequest) {
String result = (String) rabbitTemplate.convertSendAndReceive(
QueuesConfig.EXCHANGE_TRANSPORT,
QueuesConfig.ROUTING_KEY_TRANSPORT_BOOK_REQ,
QueuesTransportConfig.EXCHANGE_TRANSPORT,
QueuesTransportConfig.ROUTING_KEY_TRANSPORT_BOOK_REQ,
reservationRequest
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.microarchitecturovisco.transport.controllers;

import lombok.RequiredArgsConstructor;
import org.microarchitecturovisco.transport.controllers.reservations.CheckTransportAvailabilityRequest;
import org.microarchitecturovisco.transport.model.dto.LocationDto;
import org.microarchitecturovisco.transport.model.dto.TransportDto;
import org.microarchitecturovisco.transport.model.dto.request.GetTransportsBetweenLocationsRequestDto;
Expand All @@ -9,6 +10,7 @@
import org.microarchitecturovisco.transport.model.dto.response.AvailableTransportsDto;
import org.microarchitecturovisco.transport.model.dto.response.GetTransportsBetweenLocationsResponseDto;
import org.microarchitecturovisco.transport.model.dto.response.GetTransportsBySearchQueryResponseDto;
import org.microarchitecturovisco.transport.queues.config.QueuesConfig;
import org.microarchitecturovisco.transport.model.mappers.LocationMapper;
import org.microarchitecturovisco.transport.services.TransportsQueryService;
import org.microarchitecturovisco.transport.utils.json.JsonConverter;
Expand Down Expand Up @@ -90,4 +92,32 @@ public String getTransportsBetweenMultipleLocations(String requestDtoJson) {
GetTransportsBetweenLocationsResponseDto responseDto = transportsQueryService.getTransportsBetweenMultipleLocations(requestDto);
return JsonConverter.convertGetTransportsBetweenLocationsResponseDto(responseDto);
}

@RabbitListener(queues = QueuesConfig.QUEUE_TRANSPORT_BOOK_REQ)
public String consumeMessageFromQueue(CheckTransportAvailabilityRequest request) {
System.out.println("Message received from queue: " + request);

GetTransportsBySearchQueryRequestDto searchQuery = GetTransportsBySearchQueryRequestDto.builder()
.uuid(UUID.randomUUID())
.dateFrom(request.getHotelTimeFrom())
.dateTo(request.getHotelTimeTo())
.departureLocationIdsByPlane(request.getDepartureLocationIdsByPlane())
.departureLocationIdsByBus(request.getDepartureLocationIdsByBus())
.arrivalLocationIds(request.getArrivalLocationIds())
.adults(request.getAdultsQuantity())
.childrenUnderThree(request.getChildrenUnder3Quantity())
.childrenUnderTen(request.getChildrenUnder10Quantity())
.childrenUnderEighteen(request.getChildrenUnder18Quantity())
.build();
GetTransportsBySearchQueryResponseDto transports = transportsQueryService.getTransportsBySearchQuery(searchQuery);

System.out.println("TRANSPORTS: " + transports.toString());

if(transports.getTransportDtoList().size()>0){
return Boolean.toString(true);
}
else{
return Boolean.toString(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.microarchitecturovisco.transport.controllers.reservations;

import lombok.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class CheckTransportAvailabilityRequest {
private LocalDateTime hotelTimeFrom;

private LocalDateTime hotelTimeTo;

private int adultsQuantity;

private int childrenUnder3Quantity;

private int childrenUnder10Quantity;

private int childrenUnder18Quantity;

private List<UUID> departureLocationIdsByPlane;
private List<UUID> departureLocationIdsByBus;
private List<UUID> arrivalLocationIds;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@AllArgsConstructor
@NoArgsConstructor
public class GetTransportsBySearchQueryRequestDto implements Serializable {
private String uuid;
private UUID uuid;

private LocalDateTime dateFrom;
private LocalDateTime dateTo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

import java.io.Serializable;
import java.util.List;
import java.util.UUID;

@Data
@Builder
public class GetTransportsBySearchQueryResponseDto implements Serializable {
private String uuid;
private UUID uuid;
private List<TransportDto> transportDtoList;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.microarchitecturovisco.transport.rabbitmq.config;
package org.microarchitecturovisco.transport.queues.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.microarchitecturovisco.transport.queues.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QueuesConfig {

public static final String EXCHANGE_TRANSPORT = "transports.requests.checkAvailabilityByQuery.exchange";
public static final String QUEUE_TRANSPORT_BOOK_REQ = "transports.requests.checkAvailabilityByQuery.queue";

@Bean
public Queue handleReservationQueue() {
return new Queue(QUEUE_TRANSPORT_BOOK_REQ, false);
}

@Bean
public TopicExchange handleReservationExchange() {
return new TopicExchange(EXCHANGE_TRANSPORT);
}

@Bean
public Binding handleReservationRequestBinding(TopicExchange handleReservationExchange, Queue handleReservationQueue) {
return BindingBuilder.bind(handleReservationQueue).to(handleReservationExchange).with(QUEUE_TRANSPORT_BOOK_REQ);
}

@Bean
public Queue getTransportsRequest() {
return new Queue("transports.requests.getTransportsBySearchQuery", false);
}

@Bean
public Queue getTransportsResponse() {
return new Queue("transports.responses.getTransportsBySearchQuery", false);
}
}
Loading

0 comments on commit 1962950

Please sign in to comment.