Skip to content

Commit

Permalink
RSWW-56 transport-service: create event store
Browse files Browse the repository at this point in the history
Signed-off-by: Paweł Cichowski <[email protected]>
  • Loading branch information
pcichowski committed May 14, 2024
1 parent b9c8b91 commit 4901d2a
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 39 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class CreateTransportCommand {
private String uuid;
private LocalDateTime commandCreatedTime;
private LocalDateTime commandTimeStamp;

private TransportDto transportDto;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Builder
public class UpdateTransportCommand {
private String uuid;
private LocalDateTime commandCreatedTime;
private LocalDateTime commandTimeStamp;

private TransportDto transportDto;
}

This file was deleted.

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

import jakarta.persistence.*;
import lombok.Builder;
import lombok.RequiredArgsConstructor;

@Entity
@Table(name = "transport_created_events")
@RequiredArgsConstructor
@Builder
public class TransportCreatedEvent extends TransportEvent {

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

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;

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

@Data
@Builder
@AllArgsConstructor
@RequiredArgsConstructor
@Entity
public abstract class TransportEvent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

private LocalDateTime eventTimeStamp;

private UUID idTransport;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.microarchitecturovisco.transport.model.events;

import jakarta.persistence.*;
import lombok.*;

@Entity
@Table(name = "transports_reservation_created_events")
@Builder
@Getter
@Setter
@RequiredArgsConstructor
public class TransportReservationCreatedEvent extends TransportEvent {

private int idTransportReservation;
private int numberOfSeats;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.microarchitecturovisco.transport.repositories;

import org.microarchitecturovisco.transport.model.events.TransportEvent;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.UUID;

public interface TransportEventStore extends JpaRepository<TransportEvent, Integer> {
List<TransportEvent> findTransportEventsByIdTransport(UUID idTransport);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.microarchitecturovisco.transport.repositories;

import org.microarchitecturovisco.transport.model.domain.TransportReservation;
import org.springframework.data.jpa.repository.JpaRepository;

public interface TransportReservationRepository extends JpaRepository<TransportReservation, Integer> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.microarchitecturovisco.transport.services;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
@RequiredArgsConstructor
public class TransportCommandService {

private final TransportEventSourcingHandler transportEventSourcingHandler;

public void save(UUID idTransport) {
transportEventSourcingHandler.project(idTransport);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.microarchitecturovisco.transport.services;

import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.microarchitecturovisco.transport.model.domain.Transport;
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.repositories.TransportEventStore;
import org.microarchitecturovisco.transport.repositories.TransportRepository;
import org.microarchitecturovisco.transport.repositories.TransportReservationRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class TransportEventSourcingHandler {

private final TransportEventStore eventStore;

private final TransportRepository transportRepository;
private final TransportReservationRepository transportReservationRepository;

public void project(UUID idTransport) {
List<TransportEvent> events = eventStore.findTransportEventsByIdTransport(idTransport);

Transport transport = new Transport();

for (TransportEvent event : events) {
if (event instanceof TransportCreatedEvent) {
// TODO
}
if (event instanceof TransportReservationCreatedEvent) {
apply((TransportReservationCreatedEvent) event, transport);
}
}
}

private void apply(TransportReservationCreatedEvent event, Transport transport) {
TransportReservation transportReservation = TransportReservation.builder()
.id(event.getIdTransportReservation())
.numberOfSeats(event.getNumberOfSeats())
.transport(transport)
.build();
transport.getTransportReservations().add(transportReservation);

transportReservationRepository.save(transportReservation);
transportRepository.save(transport);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.microarchitecturovisco.transport.services;

import lombok.RequiredArgsConstructor;
import org.microarchitecturovisco.transport.model.domain.Transport;
import org.microarchitecturovisco.transport.repositories.TransportRepository;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class TransportQueryService {

// TODO
}

0 comments on commit 4901d2a

Please sign in to comment.