Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract DeliveryService - split code
- Loading branch information
Showing
32 changed files
with
530 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...i/src/main/java/net/chrisrichardson/ftgo/deliveryservice/api/service/DeliveryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package net.chrisrichardson.ftgo.deliveryservice.api.service; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public interface DeliveryService { | ||
void scheduleDelivery(LocalDateTime readyBy, Long orderId); | ||
|
||
void cancelDelivery(long orderId); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
dependencies { | ||
compile project(":ftgo-delivery-service-api") | ||
compile project(":ftgo-common") | ||
|
||
compile "org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion" | ||
compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion" | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...elivery-service/src/main/java/net/chrisrichardson/ftgo/deliveryservice/domain/Action.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package net.chrisrichardson.ftgo.deliveryservice.domain; | ||
|
||
import javax.persistence.*; | ||
import java.time.LocalDateTime; | ||
|
||
@Embeddable | ||
public class Action { | ||
|
||
@Enumerated(EnumType.STRING) | ||
private ActionType type; | ||
private LocalDateTime time; | ||
|
||
@ManyToOne | ||
@JoinColumn(name="order_id", nullable=false) | ||
private Delivery order; | ||
|
||
private Action() { | ||
} | ||
|
||
public Action(ActionType type, Delivery order, LocalDateTime time) { | ||
this.type = type; | ||
this.order = order; | ||
this.time = time; | ||
} | ||
|
||
public boolean actionFor(Delivery order) { | ||
return this.order.getId().equals(order.getId()); | ||
} | ||
|
||
public static Action makePickup(Delivery order) { | ||
return new Action(ActionType.PICKUP, order, null); | ||
} | ||
|
||
public static Action makeDropoff(Delivery order, LocalDateTime deliveryTime) { | ||
return new Action(ActionType.DROPOFF, order, deliveryTime); | ||
} | ||
|
||
|
||
public ActionType getType() { | ||
return type; | ||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
...ery-service/src/main/java/net/chrisrichardson/ftgo/deliveryservice/domain/ActionType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package net.chrisrichardson.ftgo.deliveryservice.domain; | ||
|
||
public enum ActionType { PICKUP, DROPOFF | ||
} |
7 changes: 7 additions & 0 deletions
7
...java/net/chrisrichardson/ftgo/deliveryservice/domain/CustomDeliveryCourierRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package net.chrisrichardson.ftgo.deliveryservice.domain; | ||
|
||
public interface CustomDeliveryCourierRepository { | ||
|
||
DeliveryCourier findOrCreateCourier(long courierId); | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
.../net/chrisrichardson/ftgo/deliveryservice/domain/CustomDeliveryCourierRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package net.chrisrichardson.ftgo.deliveryservice.domain; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import javax.persistence.EntityManager; | ||
|
||
public class CustomDeliveryCourierRepositoryImpl implements CustomDeliveryCourierRepository { | ||
|
||
@Autowired | ||
private EntityManager entityManager; | ||
|
||
@Override | ||
public DeliveryCourier findOrCreateCourier(long courierId) { | ||
DeliveryCourier courier = entityManager.find(DeliveryCourier.class, courierId); | ||
if (courier == null) { | ||
courier = DeliveryCourier.create(courierId); | ||
entityManager.persist(courier); | ||
} | ||
return courier; | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...ivery-service/src/main/java/net/chrisrichardson/ftgo/deliveryservice/domain/Delivery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package net.chrisrichardson.ftgo.deliveryservice.domain; | ||
|
||
import net.chrisrichardson.ftgo.common.Address; | ||
import net.chrisrichardson.ftgo.common.UnsupportedStateTransitionException; | ||
import org.hibernate.annotations.DynamicUpdate; | ||
|
||
import javax.persistence.*; | ||
import java.time.LocalDateTime; | ||
|
||
@Entity | ||
@Table(name = "orders") | ||
@Access(AccessType.FIELD) | ||
@DynamicUpdate | ||
public class Delivery { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Enumerated(EnumType.STRING) | ||
@Column(name="orderState") | ||
private DeliveryState deliveryState; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
private DeliveryRestaurant restaurant; | ||
|
||
@ManyToOne | ||
private DeliveryCourier assignedCourier; | ||
|
||
@Embedded | ||
@AttributeOverrides({ | ||
@AttributeOverride(name="street1", column = @Column(name="delivery_address_street1")), | ||
@AttributeOverride(name="street2", column = @Column(name="delivery_address_street2")), | ||
@AttributeOverride(name="city", column = @Column(name="delivery_address_city")), | ||
@AttributeOverride(name="state", column = @Column(name="delivery_address_state")), | ||
@AttributeOverride(name="zip", column = @Column(name="delivery_address_zip")), | ||
} | ||
) | ||
private Address deliveryAddress; | ||
private LocalDateTime deliveredTime; | ||
private LocalDateTime pickedUpTime; | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
|
||
public void schedule(DeliveryCourier assignedCourier) { | ||
this.assignedCourier = assignedCourier; | ||
} | ||
|
||
|
||
public void notePickedUp() { | ||
switch (deliveryState) { | ||
case READY_FOR_PICKUP: | ||
this.deliveryState = DeliveryState.PICKED_UP; | ||
this.pickedUpTime = LocalDateTime.now(); | ||
return; | ||
default: | ||
throw new UnsupportedStateTransitionException(deliveryState); | ||
} | ||
} | ||
|
||
public void noteDelivered() { | ||
switch (deliveryState) { | ||
case PICKED_UP: | ||
this.deliveryState = DeliveryState.DELIVERED; | ||
this.deliveredTime = LocalDateTime.now(); | ||
return; | ||
default: | ||
throw new UnsupportedStateTransitionException(deliveryState); | ||
} | ||
} | ||
|
||
|
||
public void cancel() { | ||
// TODO should be a state change: this.state = DeliveryState.CANCELLED; | ||
this.assignedCourier = null; | ||
} | ||
|
||
public DeliveryCourier getAssignedCourier() { | ||
return assignedCourier; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...ervice/src/main/java/net/chrisrichardson/ftgo/deliveryservice/domain/DeliveryCourier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package net.chrisrichardson.ftgo.deliveryservice.domain; | ||
|
||
import org.hibernate.annotations.DynamicUpdate; | ||
|
||
import javax.persistence.*; | ||
import java.util.List; | ||
|
||
@Entity | ||
@Access(AccessType.FIELD) | ||
@DynamicUpdate | ||
@Table(name = "courier") | ||
public class DeliveryCourier { | ||
|
||
@Id | ||
private long id; | ||
|
||
@Embedded | ||
private Plan plan; | ||
|
||
private Boolean available; | ||
|
||
private DeliveryCourier() { | ||
} | ||
|
||
public DeliveryCourier(long courierId) { | ||
this.id = courierId; | ||
this.plan = new Plan(); | ||
} | ||
|
||
public static DeliveryCourier create(long courierId) { | ||
return new DeliveryCourier(courierId); | ||
} | ||
|
||
public void noteAvailable() { | ||
this.available = true; | ||
|
||
} | ||
|
||
public void addAction(Action action) { | ||
plan.add(action); | ||
} | ||
|
||
public void cancelDelivery(Delivery order) { | ||
plan.removeDelivery(order); | ||
} | ||
|
||
public boolean isAvailable() { | ||
return available; | ||
} | ||
|
||
public Plan getPlan() { | ||
return plan; | ||
} | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public void noteUnavailable() { | ||
this.available = false; | ||
} | ||
|
||
public List<Action> actionsForDelivery(Delivery order) { | ||
return plan.actionsForDelivery(order); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
.../main/java/net/chrisrichardson/ftgo/deliveryservice/domain/DeliveryCourierRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package net.chrisrichardson.ftgo.deliveryservice.domain; | ||
|
||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface DeliveryCourierRepository extends CrudRepository<DeliveryCourier, Long>, CustomDeliveryCourierRepository { | ||
|
||
@Query("SELECT c FROM DeliveryCourier c WHERE c.available = true") | ||
List<DeliveryCourier> findAllAvailable(); | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...src/main/java/net/chrisrichardson/ftgo/deliveryservice/domain/DeliveryCourierService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package net.chrisrichardson.ftgo.deliveryservice.domain; | ||
|
||
public interface DeliveryCourierService { | ||
void notePickedUp(long orderId); | ||
|
||
void noteDelivered(long orderId); | ||
|
||
void updateAvailability(long courierId, boolean available); | ||
} |
6 changes: 6 additions & 0 deletions
6
...ice/src/main/java/net/chrisrichardson/ftgo/deliveryservice/domain/DeliveryRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package net.chrisrichardson.ftgo.deliveryservice.domain; | ||
|
||
import org.springframework.data.repository.CrudRepository; | ||
|
||
public interface DeliveryRepository extends CrudRepository<Delivery, Long> { | ||
} |
24 changes: 24 additions & 0 deletions
24
...ice/src/main/java/net/chrisrichardson/ftgo/deliveryservice/domain/DeliveryRestaurant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package net.chrisrichardson.ftgo.deliveryservice.domain; | ||
|
||
import net.chrisrichardson.ftgo.common.Address; | ||
import org.hibernate.annotations.DynamicUpdate; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
@Table(name = "restaurant") | ||
@Access(AccessType.FIELD) | ||
@DynamicUpdate | ||
public class DeliveryRestaurant { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String name; | ||
|
||
@Embedded | ||
private Address address; | ||
|
||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...a/net/chrisrichardson/ftgo/deliveryservice/domain/DeliveryServiceDomainConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package net.chrisrichardson.ftgo.deliveryservice.domain; | ||
|
||
import net.chrisrichardson.ftgo.deliveryservice.api.service.DeliveryService; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.domain.EntityScan; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | ||
|
||
@Configuration | ||
@EnableJpaRepositories | ||
@EnableAutoConfiguration | ||
@EntityScan | ||
public class DeliveryServiceDomainConfiguration { | ||
|
||
@Bean | ||
public DeliveryService deliveryService(DeliveryRepository deliveryRepository, DeliveryCourierRepository courierRepository) { | ||
return new DeliveryServiceImpl(courierRepository, deliveryRepository); | ||
} | ||
} |
Oops, something went wrong.