Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReadyForReview: RSWW-81 Checking transport availability - saga #25

Merged
merged 14 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -63,4 +63,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, false);
}
@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 @@ -9,6 +9,8 @@

@Builder
@Data
@Setter
Danzigerrr marked this conversation as resolved.
Show resolved Hide resolved
@Getter
@AllArgsConstructor
@NoArgsConstructor
@ToString
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.RequiredArgsConstructor;
import org.microarchitecturovisco.reservationservice.domain.commands.CreateReservationCommand;
import org.microarchitecturovisco.reservationservice.domain.entity.Reservation;
import org.microarchitecturovisco.reservationservice.domain.events.ReservationEvent;
import org.microarchitecturovisco.reservationservice.domain.exceptions.ReservationFailException;
import org.microarchitecturovisco.reservationservice.queues.config.QueuesReservationConfig;
import org.microarchitecturovisco.reservationservice.queues.hotels.ReservationRequest;
Expand Down Expand Up @@ -51,12 +52,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);
Danzigerrr marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -84,7 +85,6 @@ public UUID bookOrchestration(ReservationRequest reservationRequest) throws Rese
// dodać pole Timestamp stworzenia rezerwacji do klasy Reservation



return null; // reservationId
}

Expand All @@ -93,5 +93,4 @@ public void createReservationFromRequest(ReservationRequest reservationRequest,

rabbitTemplate.convertAndSend(QueuesReservationConfig.EXCHANGE_RESERVATION, "", reservationRequest);
}

}
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
Loading
Loading