Skip to content

Commit 0006cdd

Browse files
committed
Basic DeliveryService
1 parent ed07b5c commit 0006cdd

File tree

23 files changed

+438
-8
lines changed

23 files changed

+438
-8
lines changed

docker-compose.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,27 @@ services:
165165
EVENTUATELOCAL_ZOOKEEPER_CONNECTION_STRING: zookeeper:2181
166166
JAVA_OPTS: -Xmx192m
167167
EVENTUATE_DATABASE_SCHEMA: ftgo_accounting_service
168+
ftgo-delivery-service:
169+
build: ./ftgo-delivery-service
170+
ports:
171+
- "8089:8080"
172+
depends_on:
173+
- mysql
174+
- kafka
175+
- cdc-service
176+
- zipkin
177+
environment:
178+
SPRING_DATASOURCE_URL: jdbc:mysql://mysql/ftgo_delivery_service
179+
SPRING_DATASOURCE_USERNAME: ftgo_delivery_service_user
180+
SPRING_DATASOURCE_PASSWORD: ftgo_delivery_service_password
181+
SPRING_DATASOURCE_DRIVER_CLASS_NAME: com.mysql.jdbc.Driver
182+
EVENTUATELOCAL_KAFKA_BOOTSTRAP_SERVERS: kafka:9092
183+
EVENTUATELOCAL_ZOOKEEPER_CONNECTION_STRING: zookeeper:2181
184+
JAVA_OPTS: -Xmx192m
185+
SPRING_SLEUTH_ENABLED: "true"
186+
SPRING_SLEUTH_SAMPLER_PROBABILITY: 1
187+
SPRING_ZIPKIN_BASE_URL: http://zipkin:9411/
188+
EVENTUATE_DATABASE_SCHEMA: ftgo_delivery_service
168189
ftgo-order-history-service:
169190
build: ./ftgo-order-history-service
170191
ports:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package net.chrisrichardson.ftgo.deliveryservice.api.web;
2+
3+
public class ActionInfo {
4+
private String type;
5+
6+
public ActionInfo() {
7+
}
8+
9+
public ActionInfo(String type) {
10+
this.type = type;
11+
}
12+
13+
public String getType() {
14+
return type;
15+
}
16+
17+
public void setType(String type) {
18+
this.type = type;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package net.chrisrichardson.ftgo.deliveryservice.api.web;
2+
3+
public class CourierAvailability {
4+
5+
private boolean available;
6+
7+
public CourierAvailability() {
8+
}
9+
10+
public CourierAvailability(boolean available) {
11+
this.available = available;
12+
}
13+
14+
public boolean isAvailable() {
15+
return available;
16+
}
17+
18+
public void setAvailable(boolean available) {
19+
this.available = available;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package net.chrisrichardson.ftgo.deliveryservice.api.web;
2+
3+
public class DeliveryInfo {
4+
5+
private long id;
6+
private String state;
7+
8+
public DeliveryInfo() {
9+
}
10+
11+
public DeliveryInfo(long id, String state) {
12+
13+
this.id = id;
14+
this.state = state;
15+
}
16+
17+
public long getId() {
18+
return id;
19+
}
20+
21+
public void setId(long id) {
22+
this.id = id;
23+
}
24+
25+
public String getState() {
26+
return state;
27+
}
28+
29+
public void setState(String state) {
30+
this.state = state;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package net.chrisrichardson.ftgo.deliveryservice.api.web;
2+
3+
import java.util.List;
4+
5+
public class DeliveryStatus {
6+
private DeliveryInfo deliveryInfo;
7+
private Long assignedCourier;
8+
private List<ActionInfo> courierActions;
9+
10+
public DeliveryStatus() {
11+
}
12+
13+
public DeliveryInfo getDeliveryInfo() {
14+
return deliveryInfo;
15+
}
16+
17+
public void setDeliveryInfo(DeliveryInfo deliveryInfo) {
18+
this.deliveryInfo = deliveryInfo;
19+
}
20+
21+
public Long getAssignedCourier() {
22+
return assignedCourier;
23+
}
24+
25+
public void setAssignedCourier(Long assignedCourier) {
26+
this.assignedCourier = assignedCourier;
27+
}
28+
29+
public List<ActionInfo> getCourierActions() {
30+
return courierActions;
31+
}
32+
33+
public void setCourierActions(List<ActionInfo> courierActions) {
34+
this.courierActions = courierActions;
35+
}
36+
37+
public DeliveryStatus(DeliveryInfo deliveryInfo, Long assignedCourier, List<ActionInfo> courierActions) {
38+
this.deliveryInfo = deliveryInfo;
39+
this.assignedCourier = assignedCourier;
40+
this.courierActions = courierActions;
41+
}
42+
}

ftgo-delivery-service/Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM openjdk:8u171-jre-alpine
2+
RUN apk --no-cache add curl
3+
HEALTHCHECK --start-period=30s --interval=5s CMD curl -f http://localhost:8080/actuator/health || exit 1
4+
CMD java ${JAVA_OPTS} -jar ftgo-delivery-service.jar
5+
COPY build/libs/ftgo-delivery-service.jar .

ftgo-delivery-service/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ dependencies {
1414
compile project(":ftgo-order-service-api")
1515
compile project(":ftgo-common")
1616
compile project(":ftgo-common-jpa")
17+
compile project(":common-swagger")
1718

1819
compile "io.eventuate.tram.core:eventuate-tram-events:$eventuateTramVersion"
1920
compile "org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion"
21+
compile "io.eventuate.tram.core:eventuate-tram-jdbc-kafka:$eventuateTramVersion"
22+
23+
compile "org.springframework.boot:spring-boot-starter-actuator:$springBootVersion"
24+
compile "io.micrometer:micrometer-registry-prometheus:$micrometerVersion"
2025

2126
testCompile "org.springframework.boot:spring-boot-starter-test:$springBootVersion"
2227

ftgo-delivery-service/src/integration-test/java/net/chrisrichardson/ftgo/deliveryservice/domain/CourierJpaTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,26 @@ public void shouldFindAllAvailable() {
6060

6161
courierRepository.save(courier1);
6262
courierRepository.save(courier2);
63-
63+
6464
List<Courier> availableCouriers = courierRepository.findAllAvailable();
6565

6666
assertTrue(availableCouriers.stream().anyMatch(c -> c.getId() == courierId1));
6767
assertFalse(availableCouriers.stream().anyMatch(c -> c.getId() == courierId2));
6868
}
6969

70+
@Test
71+
public void shouldFindOrCreate() {
72+
long courierId = System.currentTimeMillis();
73+
transactionTemplate.execute((ts) -> {
74+
Courier courier = courierRepository.findOrCreateCourier(courierId);
75+
assertNotNull(courier);
76+
return null;
77+
});
78+
transactionTemplate.execute((ts) -> {
79+
Courier courier2 = courierRepository.findOrCreateCourier(courierId);
80+
assertNotNull(courier2);
81+
return null;
82+
});
83+
}
84+
7085
}

ftgo-delivery-service/src/main/java/net/chrisrichardson/ftgo/deliveryservice/domain/Courier.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.chrisrichardson.ftgo.deliveryservice.domain;
22

33
import javax.persistence.*;
4+
import java.util.List;
45

56
@Entity
67
@Access(AccessType.FIELD)
@@ -54,4 +55,8 @@ public long getId() {
5455
public void noteUnavailable() {
5556
this.available = false;
5657
}
58+
59+
public List<Action> actionsForDelivery(long deliveryId) {
60+
return plan.actionsForDelivery(deliveryId);
61+
}
5762
}

ftgo-delivery-service/src/main/java/net/chrisrichardson/ftgo/deliveryservice/domain/CustomCourierRepositoryImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public class CustomCourierRepositoryImpl implements CustomCourierRepository {
1717

1818
@Override
1919
public Courier findOrCreateCourier(long courierId) {
20-
return null;
20+
Courier courier = entityManager.find(Courier.class, courierId);
21+
if (courier == null) {
22+
courier = Courier.create(courierId);
23+
entityManager.persist(courier);
24+
}
25+
return courier;
2126
}
2227
}

ftgo-delivery-service/src/main/java/net/chrisrichardson/ftgo/deliveryservice/domain/DeliveryService.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package net.chrisrichardson.ftgo.deliveryservice.domain;
22

33
import net.chrisrichardson.ftgo.common.Address;
4+
import net.chrisrichardson.ftgo.deliveryservice.api.web.ActionInfo;
5+
import net.chrisrichardson.ftgo.deliveryservice.api.web.DeliveryInfo;
6+
import net.chrisrichardson.ftgo.deliveryservice.api.web.DeliveryStatus;
47
import org.springframework.dao.DuplicateKeyException;
8+
import org.springframework.transaction.annotation.Transactional;
59

610
import java.time.LocalDateTime;
11+
import java.util.Collections;
712
import java.util.List;
813
import java.util.Random;
14+
import java.util.stream.Collectors;
915

1016
public class DeliveryService {
1117

@@ -63,6 +69,10 @@ void noteAvailable(long courierId) {
6369
courierRepository.findOrCreateCourier(courierId).noteAvailable();
6470
}
6571

72+
void noteUnavailable(long courierId) {
73+
courierRepository.findOrCreateCourier(courierId).noteUnavailable();
74+
}
75+
6676
private Courier findOrCreateCourier(long courierId) {
6777
Courier courier = Courier.create(courierId);
6878
try {
@@ -72,8 +82,38 @@ private Courier findOrCreateCourier(long courierId) {
7282
}
7383
}
7484

75-
// noteUnavailable
85+
@Transactional
86+
public void updateAvailability(long courierId, boolean available) {
87+
if (available)
88+
noteAvailable(courierId);
89+
else
90+
noteUnavailable(courierId);
91+
}
92+
7693

7794
// getCourierRoute()
7895

96+
@Transactional
97+
public DeliveryStatus getDeliveryInfo(long deliveryId) {
98+
Delivery delivery = deliveryRepository.findById(deliveryId).get();
99+
Long assignedCourier = delivery.getAssignedCourier();
100+
List<Action> courierActions = Collections.EMPTY_LIST;
101+
if (assignedCourier != null) {
102+
Courier courier = courierRepository.findById(assignedCourier).get();
103+
courierActions = courier.actionsForDelivery(deliveryId);
104+
}
105+
return makeDeliveryStatus(delivery, assignedCourier, courierActions);
106+
}
107+
108+
private DeliveryStatus makeDeliveryStatus(Delivery delivery, Long assignedCourier, List<Action> courierActions) {
109+
return new DeliveryStatus(makeDeliveryInfo(delivery), assignedCourier, courierActions.stream().map(action -> makeActionInfo(action)).collect(Collectors.toList()));
110+
}
111+
112+
private DeliveryInfo makeDeliveryInfo(Delivery delivery) {
113+
return new DeliveryInfo(delivery.getId(), delivery.getState().name());
114+
}
115+
116+
private ActionInfo makeActionInfo(Action action) {
117+
return new ActionInfo(action.getType().name());
118+
}
79119
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.chrisrichardson.ftgo.deliveryservice.domain;
2+
3+
import org.springframework.boot.autoconfigure.domain.EntityScan;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
7+
import org.springframework.transaction.annotation.EnableTransactionManagement;
8+
9+
@Configuration
10+
@EntityScan
11+
@EnableJpaRepositories
12+
@EnableTransactionManagement
13+
public class DeliveryServiceDomainConfiguration {
14+
15+
@Bean
16+
public DeliveryService deliveryService(RestaurantRepository restaurantRepository, DeliveryRepository deliveryRepository, CourierRepository courierRepository) {
17+
return new DeliveryService(restaurantRepository, deliveryRepository, courierRepository);
18+
}
19+
}

ftgo-delivery-service/src/main/java/net/chrisrichardson/ftgo/deliveryservice/domain/Plan.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ public void removeDelivery(long deliveryId) {
2121
public List<Action> getActions() {
2222
return actions;
2323
}
24+
25+
public List<Action> actionsForDelivery(long deliveryId) {
26+
return actions.stream().filter(action -> action.actionFor(deliveryId)).collect(Collectors.toList());
27+
}
2428
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11
package net.chrisrichardson.ftgo.deliveryservice.main;
22

3+
import io.eventuate.tram.jdbckafka.TramJdbcKafkaConfiguration;
4+
import net.chrisrichardson.eventstore.examples.customersandorders.commonswagger.CommonSwaggerConfiguration;
5+
import net.chrisrichardson.ftgo.deliveryservice.messaging.DeliveryServiceMessagingConfiguration;
6+
import net.chrisrichardson.ftgo.deliveryservice.web.DeliveryServiceWebConfiguration;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
9+
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.context.annotation.Import;
11+
12+
@Configuration
13+
@EnableAutoConfiguration
14+
@Import({DeliveryServiceMessagingConfiguration.class, DeliveryServiceWebConfiguration.class,
15+
TramJdbcKafkaConfiguration.class, CommonSwaggerConfiguration.class
16+
})
317
public class DeliveryServiceMain {
418

519
public static void main(String[] args) {
6-
20+
SpringApplication.run(DeliveryServiceMain.class, args);
721
}
822
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package net.chrisrichardson.ftgo.deliveryservice.messaging;
2+
3+
import io.eventuate.tram.events.subscriber.DomainEventDispatcher;
4+
import io.eventuate.tram.events.subscriber.DomainEventDispatcherFactory;
5+
import io.eventuate.tram.events.subscriber.TramEventSubscriberConfiguration;
6+
import net.chrisrichardson.ftgo.common.CommonConfiguration;
7+
import net.chrisrichardson.ftgo.deliveryservice.domain.DeliveryService;
8+
import net.chrisrichardson.ftgo.deliveryservice.domain.DeliveryServiceDomainConfiguration;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.context.annotation.Import;
12+
13+
@Configuration
14+
@Import({DeliveryServiceDomainConfiguration.class, TramEventSubscriberConfiguration.class, CommonConfiguration.class})
15+
public class DeliveryServiceMessagingConfiguration {
16+
17+
@Bean
18+
public DeliveryMessageHandlers deliveryMessageHandlers(DeliveryService deliveryService) {
19+
return new DeliveryMessageHandlers(deliveryService);
20+
}
21+
22+
@Bean
23+
public DomainEventDispatcher domainEventDispatcher(DeliveryMessageHandlers deliveryMessageHandlers, DomainEventDispatcherFactory domainEventDispatcherFactory) {
24+
return domainEventDispatcherFactory.make("deliveryService-deliveryMessageHandlers", deliveryMessageHandlers.domainEventHandlers());
25+
}
26+
}

0 commit comments

Comments
 (0)