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

RSWW-136 transport get data from data generator #64

Merged
merged 3 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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,5 +1,6 @@
package cloud.project.datagenerator.rabbitmq.requests.transports;

import cloud.project.datagenerator.rabbitmq.requests.DataUpdateType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -15,7 +16,7 @@
@Builder
public class TransportUpdateRequest {

private String updateType;
private DataUpdateType updateType;

private UUID id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cloud.project.datagenerator.hotels.repositories.HotelRepository;
import cloud.project.datagenerator.rabbitmq.QueuesConfigTransports;
import cloud.project.datagenerator.rabbitmq.json.JsonConverter;
import cloud.project.datagenerator.rabbitmq.requests.DataUpdateType;
import cloud.project.datagenerator.rabbitmq.requests.transports.TransportUpdateRequest;
import cloud.project.datagenerator.transports.domain.Transport;
import cloud.project.datagenerator.transports.domain.TransportCourse;
Expand All @@ -26,10 +27,6 @@
public class TransportsDataGenerator {
private final HotelRepository hotelRepository;

enum DataUpdateType {
CREATE,
UPDATE
}

private final Random random = new Random();
private final RabbitTemplate rabbitTemplate;
Expand Down Expand Up @@ -100,7 +97,7 @@ private void updateRandomTransport() {

public void updateTransportDataInTransportModules(DataUpdateType updateType, Transport transport) {
TransportUpdateRequest transportUpdateRequest = TransportUpdateRequest.builder()
.updateType(String.valueOf(updateType))
.updateType(updateType)
.id(transport.getId())
.courseId(transport.getCourse().getId())
.courseLocationFromId(transport.getCourse().getDepartureFrom().getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.microarchitecturovisco.transport.model.dto.LocationDto;
import org.microarchitecturovisco.transport.model.dto.TransportDto;
import org.microarchitecturovisco.transport.model.dto.TransportReservationDto;
import org.microarchitecturovisco.transport.model.dto.data_generator.TransportUpdateRequest;
import org.microarchitecturovisco.transport.model.dto.request.*;
import org.microarchitecturovisco.transport.model.dto.response.AvailableTransportsDto;
import org.microarchitecturovisco.transport.model.dto.response.CheckTransportAvailabilityResponseDto;
Expand All @@ -32,6 +33,8 @@
import java.util.List;
import java.util.UUID;
import java.util.logging.Logger;
import org.microarchitecturovisco.transport.model.dto.data_generator.DataUpdateType;


@RestController()
@RequestMapping("/transports")
Expand Down Expand Up @@ -201,4 +204,35 @@ public void consumeMessageDeleteTransportReservation(String requestJson) {
transportCommandService.deleteReservation(command);
}
}

@RabbitListener(queues = "#{handleDataGeneratorCreateQueue}")
public void consumeDataGeneratorMessage(String requestJson) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plik nazywa się TransportQueryController więc nie powinno tu być logiki tworzenia ani aktualizacji

Logger logger = Logger.getLogger("TransportController");
logger.info("Got transport data generator: " + requestJson);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

przekopiowane z hoteli bez patrzenia, tam jeszcze na to pozwoliłem ale ten frazes moim zdaniem do zmiany


TransportUpdateRequest request = JsonReader.readDtoFromJson(requestJson, TransportUpdateRequest.class);

// create transport
if (request.getUpdateType() == DataUpdateType.CREATE) {
System.out.println("Created transport: " + request);
CzajaLukasz marked this conversation as resolved.
Show resolved Hide resolved

transportsQueryService.createTransport(request.getId(), request.getCourseId(), request.getDepartureDate(),
request.getCapacity(), request.getPricePerAdult());
return;
}

Transport transport = null;
transport = transportsQueryService.getTransportById(request.getId());
CzajaLukasz marked this conversation as resolved.
Show resolved Hide resolved
if (transport == null)
{
return;
}
// update transport
if (request.getUpdateType() == DataUpdateType.UPDATE) {
System.out.println("Updated transport: " + request);
CzajaLukasz marked this conversation as resolved.
Show resolved Hide resolved

transportsQueryService.updateTransport(request.getId(), request.getCapacity(), request.getPricePerAdult());

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.microarchitecturovisco.transport.model.dto.data_generator;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum DataUpdateType {
CREATE("CREATE"),
UPDATE("UPDATE");

private final String value;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.microarchitecturovisco.transport.model.dto.data_generator;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

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


@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class TransportUpdateRequest {

private DataUpdateType updateType;

private UUID id;

private UUID courseId;

private UUID courseLocationFromId;
private UUID courseLocationToId;

private LocalDateTime departureDate;

private int capacity;

private float pricePerAdult;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.microarchitecturovisco.transport.model.events;



import jakarta.persistence.Entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

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


@Entity
@NoArgsConstructor
@SuperBuilder
@Getter
@Setter
public class TransportOnlyCreatedEvent extends TransportEvent {
private UUID courseId;
private LocalDateTime departureDate;
private int capacity;
private float pricePerAdult;

public TransportOnlyCreatedEvent(UUID transportId, UUID courseId, LocalDateTime departureDate, int capacity,
float pricePerAdult) {
this.setIdTransport(transportId);
this.setEventTimeStamp(LocalDateTime.now());
this.courseId = courseId;
this.departureDate = departureDate;
this.capacity = capacity;
this.pricePerAdult = pricePerAdult;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.microarchitecturovisco.transport.model.events;

import jakarta.persistence.Entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

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


@Entity
@NoArgsConstructor
@SuperBuilder
@Getter
@Setter
public class TransportUpdateEvent extends TransportEvent {
private int capacity;
private float pricePerAdult;

public TransportUpdateEvent(UUID transportId, int capacity, float pricePerAdult){
this.setEventTimeStamp(LocalDateTime.now());
this.setIdTransport(transportId);
this.capacity = capacity;
this.pricePerAdult = pricePerAdult;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.microarchitecturovisco.transport.queues.config;


import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.Binding;
import java.util.UUID;

@Configuration
public class DataGeneratorExchangeConfig {

public static final String EXCHANGE_DATA_GENERATOR = "data.generate.transports.exchange";

@Bean
public FanoutExchange handleDataGeneratorExchange() {
return new FanoutExchange(EXCHANGE_DATA_GENERATOR);
}

@Bean
public Queue handleDataGeneratorCreateQueue() {
String uniqueQueueName = "data.generate.transports.queue" + UUID.randomUUID();
return new Queue(uniqueQueueName, false, false, false);
}

@Bean
public Binding handleDataGeneratorBinding(
FanoutExchange handleDataGeneratorExchange,
Queue handleDataGeneratorCreateQueue) {
return BindingBuilder.bind(handleDataGeneratorCreateQueue).to(handleDataGeneratorExchange);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
import org.microarchitecturovisco.transport.model.domain.TransportCourse;
import org.springframework.data.jpa.repository.JpaRepository;

public interface TransportCourseRepository extends JpaRepository<TransportCourse, Integer> {
import java.util.UUID;

public interface TransportCourseRepository extends JpaRepository<TransportCourse, UUID> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import org.microarchitecturovisco.transport.model.domain.Transport;
import org.microarchitecturovisco.transport.model.domain.TransportCourse;
import org.microarchitecturovisco.transport.model.domain.TransportReservation;
import org.microarchitecturovisco.transport.model.events.TransportCreatedEvent;
import org.microarchitecturovisco.transport.model.events.TransportEvent;
import org.microarchitecturovisco.transport.model.events.TransportReservationCreatedEvent;
import org.microarchitecturovisco.transport.model.events.TransportReservationDeletedEvent;
import org.microarchitecturovisco.transport.model.events.*;
import org.microarchitecturovisco.transport.repositories.LocationRepository;
import org.microarchitecturovisco.transport.repositories.TransportCourseRepository;
import org.microarchitecturovisco.transport.repositories.TransportRepository;
Expand Down Expand Up @@ -39,6 +36,12 @@ public void project(List<TransportEvent> transportEvents) {
if (transportEvent instanceof TransportReservationDeletedEvent) {
apply((TransportReservationDeletedEvent) transportEvent);
}
if (transportEvent instanceof TransportUpdateEvent){
apply((TransportUpdateEvent) transportEvent);
}
if (transportEvent instanceof TransportOnlyCreatedEvent){
apply((TransportOnlyCreatedEvent) transportEvent);
}
}
}

Expand Down Expand Up @@ -110,4 +113,27 @@ private void apply(TransportReservationDeletedEvent event) {
}

}

private void apply(TransportUpdateEvent event)
{
Transport transport = transportRepository.findById(event.getIdTransport()).orElseThrow(RuntimeException::new);
transport.setCapacity(event.getCapacity());
transport.setPricePerAdult(event.getPricePerAdult());
transportRepository.save(transport);
}

private void apply(TransportOnlyCreatedEvent event){
TransportCourse transportCourse = transportCourseRepository.findById(event.getCourseId()).orElseThrow(RuntimeException::new);

Transport transport = new Transport();
transport.setId(event.getIdTransport());
transport.setDepartureDate(event.getDepartureDate());
transport.setCourse(transportCourse);
transport.setCapacity(event.getCapacity());
transport.setPricePerAdult(event.getPricePerAdult());

transportRepository.save(transport);
transportCourseRepository.save(transportCourse);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
import lombok.RequiredArgsConstructor;
import org.microarchitecturovisco.transport.model.domain.*;
import org.microarchitecturovisco.transport.model.dto.TransportDto;
import org.microarchitecturovisco.transport.model.dto.data_generator.DataUpdateType;
import org.microarchitecturovisco.transport.model.dto.request.GetTransportsBetweenLocationsRequestDto;
import org.microarchitecturovisco.transport.model.dto.request.GetTransportsBetweenMultipleLocationsRequestDto;
import org.microarchitecturovisco.transport.model.dto.request.GetTransportsBySearchQueryRequestDto;
import org.microarchitecturovisco.transport.model.dto.response.AvailableTransportsDepartures;
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.model.events.TransportCreatedEvent;
import org.microarchitecturovisco.transport.model.events.TransportOnlyCreatedEvent;
import org.microarchitecturovisco.transport.model.events.TransportUpdateEvent;
import org.microarchitecturovisco.transport.model.mappers.LocationMapper;
import org.microarchitecturovisco.transport.model.mappers.TransportMapper;
import org.microarchitecturovisco.transport.repositories.LocationRepository;
import org.microarchitecturovisco.transport.repositories.TransportCourseRepository;
import org.microarchitecturovisco.transport.repositories.TransportEventStore;
import org.microarchitecturovisco.transport.repositories.TransportRepository;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
Expand All @@ -30,6 +36,8 @@ public class TransportsQueryService {
private final TransportCourseRepository transportCourseRepository;
private final TransportRepository transportRepository;
private final LocationRepository locationRepository;
private final TransportEventSourcingHandler transportEventSourcingHandler;
private final TransportEventStore transportEventStore;

public List<TransportDto> getAllTransports() {
List<Transport> transports = transportRepository.findAll();
Expand Down Expand Up @@ -265,6 +273,19 @@ public Integer getTransportOccupiedSeats(Transport transport) {
.sum();
}

public void updateTransport(UUID transportId, int capacity, float pricePerAdult) {
TransportUpdateEvent transportUpdateEvent = new TransportUpdateEvent(transportId, capacity, pricePerAdult);
transportEventStore.save(transportUpdateEvent);
transportEventSourcingHandler.project(List.of(transportUpdateEvent));
}

public void createTransport(UUID transportId, UUID courseId, LocalDateTime departureDate, int capacity,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ta funkcja istniała wcześniej w TransportCommandService (w ogóle nie powinno być takiej logiki w QueryService)

    public void createTransport(CreateTransportCommand command) {
        TransportCreatedEvent transportCreatedEvent = new TransportCreatedEvent(
                command.getUuid(), command.getCommandTimeStamp(), command.getTransportDto()
        );

        transportEventStore.save(transportCreatedEvent);
        eventSourcingHandler.project(List.of(transportCreatedEvent));
    }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i albo korzystamy z TransportCreatedEvent albo z TransportOnlyCreated, trzeba wybrać

float pricePerAdult) {

TransportOnlyCreatedEvent transportOnlyCreatedEvent = new TransportOnlyCreatedEvent(transportId,
courseId, departureDate, capacity, pricePerAdult);
transportEventStore.save(transportOnlyCreatedEvent);
transportEventSourcingHandler.project(List.of(transportOnlyCreatedEvent));
}

}
Loading