Skip to content

Commit 8758486

Browse files
committed
adds timezone support
replaces j.t.Instant with j.t.OffsetDateTime infos in https://stackoverflow.com/a/58434361 and https://jdbc.postgresql.org/documentation/query/#using-java-8-date-and-time-classes
1 parent 13c020f commit 8758486

File tree

6 files changed

+23
-29
lines changed

6 files changed

+23
-29
lines changed

src/main/java/com/example/eventsourcing/domain/OrderAggregate.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import lombok.ToString;
1212

1313
import java.math.BigDecimal;
14-
import java.time.Instant;
14+
import java.time.OffsetDateTime;
1515
import java.util.EnumSet;
1616
import java.util.List;
1717
import java.util.UUID;
@@ -25,10 +25,10 @@ public class OrderAggregate extends Aggregate {
2525
private BigDecimal price;
2626
private List<WaypointDto> route;
2727
private UUID driverId;
28-
private Instant placedDate;
29-
private Instant acceptedDate;
30-
private Instant completedDate;
31-
private Instant cancelledDate;
28+
private OffsetDateTime placedDate;
29+
private OffsetDateTime acceptedDate;
30+
private OffsetDateTime completedDate;
31+
private OffsetDateTime cancelledDate;
3232

3333
@JsonCreator
3434
public OrderAggregate(@NonNull UUID aggregateId, int version) {

src/main/java/com/example/eventsourcing/domain/event/Event.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import lombok.RequiredArgsConstructor;
66
import lombok.ToString;
77

8-
import java.time.Instant;
8+
import java.time.OffsetDateTime;
99
import java.util.UUID;
1010

1111
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
@@ -15,6 +15,6 @@ public class Event {
1515

1616
protected final UUID aggregateId;
1717
protected final int version;
18-
protected final Instant createdDate = Instant.now();
18+
protected final OffsetDateTime createdDate = OffsetDateTime.now();
1919
protected final EventType eventType = EventType.fromClass(this.getClass());
2020
}

src/main/java/com/example/eventsourcing/dto/OrderDto.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.annotation.JsonProperty;
44

55
import java.math.BigDecimal;
6+
import java.time.OffsetDateTime;
67
import java.util.List;
78
import java.util.UUID;
89

@@ -12,7 +13,7 @@ public record OrderDto(
1213
@JsonProperty("event_type")
1314
String eventType,
1415
@JsonProperty("event_timestamp")
15-
long eventTimestamp,
16+
OffsetDateTime eventTimestamp,
1617
@JsonProperty("version")
1718
int version,
1819
@JsonProperty("status")

src/main/java/com/example/eventsourcing/mapper/OrderMapper.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
import org.mapstruct.Mapper;
88
import org.mapstruct.Mapping;
99

10-
import java.time.Instant;
11-
import java.util.Optional;
12-
1310
@Mapper(componentModel = "spring")
1411
public interface OrderMapper {
1512

@@ -25,8 +22,4 @@ public interface OrderMapper {
2522
@Mapping(source = "order.route", target = "route")
2623
@Mapping(source = "order.driverId", target = "driverId")
2724
OrderDto toDto(Event event, OrderAggregate order);
28-
29-
default long toEpochMilli(Instant instant) {
30-
return Optional.ofNullable(instant).map(Instant::toEpochMilli).orElse(0L);
31-
}
3225
}

src/main/java/com/example/eventsourcing/projection/OrderProjection.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import java.io.Serializable;
1313
import java.math.BigDecimal;
14-
import java.time.Instant;
14+
import java.time.OffsetDateTime;
1515
import java.util.ArrayList;
1616
import java.util.List;
1717
import java.util.Objects;
@@ -37,10 +37,10 @@ public class OrderProjection implements Persistable<UUID>, Serializable {
3737
@ToString.Exclude
3838
private List<WaypointProjection> route = new ArrayList<>();
3939
private UUID driverId;
40-
private Instant placedDate;
41-
private Instant acceptedDate;
42-
private Instant completedDate;
43-
private Instant cancelledDate;
40+
private OffsetDateTime placedDate;
41+
private OffsetDateTime acceptedDate;
42+
private OffsetDateTime completedDate;
43+
private OffsetDateTime cancelledDate;
4444

4545
@JsonIgnore
4646
@Override

src/main/resources/db/migration/V1__init.sql

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ CREATE TABLE IF NOT EXISTS ES_EVENT_SUBSCRIPTION (
3737
);
3838

3939
CREATE TABLE IF NOT EXISTS RM_ORDER (
40-
ID UUID PRIMARY KEY,
41-
VERSION INTEGER NOT NULL,
42-
STATUS TEXT NOT NULL,
43-
RIDER_ID UUID NOT NULL,
44-
PRICE DECIMAL(19, 2) NOT NULL,
40+
ID UUID PRIMARY KEY,
41+
VERSION INTEGER NOT NULL,
42+
STATUS TEXT NOT NULL,
43+
RIDER_ID UUID NOT NULL,
44+
PRICE DECIMAL(19, 2) NOT NULL,
4545
DRIVER_ID UUID,
46-
PLACED_DATE TIMESTAMP NOT NULL,
47-
ACCEPTED_DATE TIMESTAMP,
48-
CANCELLED_DATE TIMESTAMP,
49-
COMPLETED_DATE TIMESTAMP
46+
PLACED_DATE TIMESTAMP WITH TIME ZONE NOT NULL,
47+
ACCEPTED_DATE TIMESTAMP WITH TIME ZONE,
48+
CANCELLED_DATE TIMESTAMP WITH TIME ZONE,
49+
COMPLETED_DATE TIMESTAMP WITH TIME ZONE
5050
);
5151

5252
CREATE TABLE IF NOT EXISTS RM_ORDER_ROUTE (

0 commit comments

Comments
 (0)