Skip to content

Commit

Permalink
microservices-patterns#91: Changed order api to improve documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dartartem committed Apr 29, 2020
1 parent 0500747 commit eca1f79
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package net.chrisrichardson.ftgo.common;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import java.util.Objects;

public class RevisedOrderLineItem {
private int quantity;
private String menuItemId;

public RevisedOrderLineItem() {
}

public RevisedOrderLineItem(int quantity, String menuItemId) {
this.quantity = quantity;
this.menuItemId = menuItemId;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public String getMenuItemId() {
return menuItemId;
}

public void setMenuItemId(String menuItemId) {
this.menuItemId = menuItemId;
}

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}

@Override
public boolean equals(Object o) {
return EqualsBuilder.reflectionEquals(this, o);
}

@Override
public int hashCode() {
return Objects.hash(quantity, menuItemId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.chrisrichardson.ftgo.common.Address;
import net.chrisrichardson.ftgo.common.CommonJsonMapperInitializer;
import net.chrisrichardson.ftgo.common.Money;
import net.chrisrichardson.ftgo.common.RevisedOrderLineItem;
import net.chrisrichardson.ftgo.deliveryservice.api.web.CourierAvailability;
import net.chrisrichardson.ftgo.kitchenservice.api.web.TicketAcceptance;
import net.chrisrichardson.ftgo.orderservice.api.events.OrderState;
Expand Down Expand Up @@ -176,7 +177,7 @@ private void verifyOrderRevised(int orderId) {

private void reviseOrder(int orderId) {
given().
body(new ReviseOrderRequest(Collections.singletonMap(CHICKED_VINDALOO_MENU_ITEM_ID, revisedQuantityOfChickenVindaloo)))
body(new ReviseOrderRequest(new RevisedOrderLineItem[] {new RevisedOrderLineItem(revisedQuantityOfChickenVindaloo, CHICKED_VINDALOO_MENU_ITEM_ID)}))
.contentType("application/json").
when().
post(orderBaseUrl(Integer.toString(orderId), "revise")).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package net.chrisrichardson.ftgo.kitchenservice.api;

import io.eventuate.tram.commands.common.Command;
import net.chrisrichardson.ftgo.common.RevisedOrderLineItem;

import java.util.Map;

public class BeginReviseTicketCommand implements Command {
private long restaurantId;
private Long orderId;
private Map<String, Integer> revisedLineItemQuantities;
private RevisedOrderLineItem[] revisedOrderLineItems;

private BeginReviseTicketCommand() {
}

public BeginReviseTicketCommand(long restaurantId, Long orderId, Map<String, Integer> revisedLineItemQuantities) {
public BeginReviseTicketCommand(long restaurantId, Long orderId, RevisedOrderLineItem[] revisedOrderLineItems) {
this.restaurantId = restaurantId;
this.orderId = orderId;
this.revisedLineItemQuantities = revisedLineItemQuantities;
this.revisedOrderLineItems = revisedOrderLineItems;
}

public long getRestaurantId() {
Expand All @@ -34,11 +35,11 @@ public void setOrderId(Long orderId) {
this.orderId = orderId;
}

public Map<String, Integer> getRevisedLineItemQuantities() {
return revisedLineItemQuantities;
public RevisedOrderLineItem[] getRevisedOrderLineItems() {
return revisedOrderLineItems;
}

public void setRevisedLineItemQuantities(Map<String, Integer> revisedLineItemQuantities) {
this.revisedLineItemQuantities = revisedLineItemQuantities;
public void setRevisedOrderLineItems(RevisedOrderLineItem[] revisedOrderLineItems) {
this.revisedOrderLineItems = revisedOrderLineItems;
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package net.chrisrichardson.ftgo.kitchenservice.api;

import io.eventuate.tram.commands.common.Command;
import net.chrisrichardson.ftgo.common.RevisedOrderLineItem;

import java.util.Map;

public class ConfirmReviseTicketCommand implements Command {
private long restaurantId;
private long orderId;
private Map<String, Integer> revisedLineItemQuantities;
private RevisedOrderLineItem[] revisedOrderLineItems;

private ConfirmReviseTicketCommand() {
}

public ConfirmReviseTicketCommand(long restaurantId, Long orderId, Map<String, Integer> revisedLineItemQuantities) {
public ConfirmReviseTicketCommand(long restaurantId, Long orderId, RevisedOrderLineItem[] revisedOrderLineItems) {

this.restaurantId = restaurantId;
this.orderId = orderId;
this.revisedLineItemQuantities = revisedLineItemQuantities;
this.revisedOrderLineItems = revisedOrderLineItems;
}

public long getOrderId() {
Expand All @@ -35,11 +36,11 @@ public void setRestaurantId(long restaurantId) {
this.restaurantId = restaurantId;
}

public Map<String, Integer> getRevisedLineItemQuantities() {
return revisedLineItemQuantities;
public RevisedOrderLineItem[] getRevisedOrderLineItems() {
return revisedOrderLineItems;
}

public void setRevisedLineItemQuantities(Map<String, Integer> revisedLineItemQuantities) {
this.revisedLineItemQuantities = revisedLineItemQuantities;
public void setRevisedOrderLineItems(RevisedOrderLineItem[] revisedOrderLineItems) {
this.revisedOrderLineItems = revisedOrderLineItems;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.chrisrichardson.ftgo.kitchenservice.domain;

import io.eventuate.tram.events.aggregates.ResultWithDomainEvents;
import net.chrisrichardson.ftgo.common.RevisedOrderLineItem;
import net.chrisrichardson.ftgo.kitchenservice.api.TicketDetails;
import net.chrisrichardson.ftgo.kitchenservice.api.events.TicketDomainEvent;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -88,11 +89,11 @@ public void undoCancel(long restaurantId, long ticketId) {

}

public void beginReviseOrder(long restaurantId, Long ticketId, Map<String, Integer> revisedLineItemQuantities) {
public void beginReviseOrder(long restaurantId, Long ticketId, RevisedOrderLineItem[] revisedOrderLineItems) {
Ticket ticket = ticketRepository.findById(ticketId)
.orElseThrow(() -> new TicketNotFoundException(ticketId));
// TODO - verify restaurant id
List<TicketDomainEvent> events = ticket.beginReviseOrder(revisedLineItemQuantities);
List<TicketDomainEvent> events = ticket.beginReviseOrder(revisedOrderLineItems);
domainEventPublisher.publish(ticket, events);

}
Expand All @@ -105,11 +106,11 @@ public void undoBeginReviseOrder(long restaurantId, Long ticketId) {
domainEventPublisher.publish(ticket, events);
}

public void confirmReviseTicket(long restaurantId, long ticketId, Map<String, Integer> revisedLineItemQuantities) {
public void confirmReviseTicket(long restaurantId, long ticketId, RevisedOrderLineItem[] revisedOrderLineItems) {
Ticket ticket = ticketRepository.findById(ticketId)
.orElseThrow(() -> new TicketNotFoundException(ticketId));
// TODO - verify restaurant id
List<TicketDomainEvent> events = ticket.confirmReviseTicket(revisedLineItemQuantities);
List<TicketDomainEvent> events = ticket.confirmReviseTicket(revisedOrderLineItems);
domainEventPublisher.publish(ticket, events);
}

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

import io.eventuate.tram.events.aggregates.ResultWithDomainEvents;
import net.chrisrichardson.ftgo.common.NotYetImplementedException;
import net.chrisrichardson.ftgo.common.RevisedOrderLineItem;
import net.chrisrichardson.ftgo.common.UnsupportedStateTransitionException;
import net.chrisrichardson.ftgo.kitchenservice.api.TicketDetails;
import net.chrisrichardson.ftgo.kitchenservice.api.TicketLineItem;
Expand Down Expand Up @@ -173,7 +174,7 @@ public List<TicketDomainEvent> undoCancel() {
}
}

public List<TicketDomainEvent> beginReviseOrder(Map<String, Integer> revisedLineItemQuantities) {
public List<TicketDomainEvent> beginReviseOrder(RevisedOrderLineItem[] revisedOrderLineItems) {
switch (state) {
case AWAITING_ACCEPTANCE:
case ACCEPTED:
Expand All @@ -195,7 +196,7 @@ public List<TicketDomainEvent> undoBeginReviseOrder() {
}
}

public List<TicketDomainEvent> confirmReviseTicket(Map<String, Integer> revisedLineItemQuantities) {
public List<TicketDomainEvent> confirmReviseTicket(RevisedOrderLineItem[] revisedOrderLineItems) {
switch (state) {
case REVISION_PENDING:
this.state = this.previousState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private Message undoBeginCancelTicket(CommandMessage<UndoBeginCancelTicketComman
}

public Message beginReviseTicket(CommandMessage<BeginReviseTicketCommand> cm) {
kitchenService.beginReviseOrder(cm.getCommand().getRestaurantId(), cm.getCommand().getOrderId(), cm.getCommand().getRevisedLineItemQuantities());
kitchenService.beginReviseOrder(cm.getCommand().getRestaurantId(), cm.getCommand().getOrderId(), cm.getCommand().getRevisedOrderLineItems());
return withSuccess();
}

Expand All @@ -93,7 +93,7 @@ public Message undoBeginReviseTicket(CommandMessage<UndoBeginReviseTicketCommand
}

public Message confirmReviseTicket(CommandMessage<ConfirmReviseTicketCommand> cm) {
kitchenService.confirmReviseTicket(cm.getCommand().getRestaurantId(), cm.getCommand().getOrderId(), cm.getCommand().getRevisedLineItemQuantities());
kitchenService.confirmReviseTicket(cm.getCommand().getRestaurantId(), cm.getCommand().getOrderId(), cm.getCommand().getRevisedOrderLineItems());
return withSuccess();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package net.chrisrichardson.ftgo.orderservice.api.web;

import java.util.Map;
import net.chrisrichardson.ftgo.common.RevisedOrderLineItem;

public class ReviseOrderRequest {
private Map<String, Integer> revisedLineItemQuantities;
private RevisedOrderLineItem[] revisedOrderLineItems;

private ReviseOrderRequest() {
}

public ReviseOrderRequest(Map<String, Integer> revisedLineItemQuantities) {
this.revisedLineItemQuantities = revisedLineItemQuantities;
public ReviseOrderRequest(RevisedOrderLineItem[] revisedOrderLineItems) {
this.revisedOrderLineItems = revisedOrderLineItems;
}

public Map<String, Integer> getRevisedLineItemQuantities() {
return revisedLineItemQuantities;
public RevisedOrderLineItem[] getRevisedOrderLineItems() {
return revisedOrderLineItems;
}

public void setRevisedLineItemQuantities(Map<String, Integer> revisedLineItemQuantities) {
this.revisedLineItemQuantities = revisedLineItemQuantities;
public void setRevisedOrderLineItems(RevisedOrderLineItem[] revisedOrderLineItems) {
this.revisedOrderLineItems = revisedOrderLineItems;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public List<OrderDomainEvent> confirmRevision(OrderRevision orderRevision) {

orderRevision.getDeliveryInformation().ifPresent(newDi -> this.deliveryInformation = newDi);

if (!orderRevision.getRevisedLineItemQuantities().isEmpty()) {
if (orderRevision.getRevisedOrderLineItems() != null && orderRevision.getRevisedOrderLineItems().length > 0) {
orderLineItems.updateLineItems(orderRevision);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import net.chrisrichardson.ftgo.common.Money;
import net.chrisrichardson.ftgo.orderservice.api.events.OrderLineItem;
import net.chrisrichardson.ftgo.common.RevisedOrderLineItem;

import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Embeddable;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

@Embeddable
Expand Down Expand Up @@ -38,17 +42,25 @@ OrderLineItem findOrderLineItem(String lineItemId) {
Money changeToOrderTotal(OrderRevision orderRevision) {
AtomicReference<Money> delta = new AtomicReference<>(Money.ZERO);

orderRevision.getRevisedLineItemQuantities().forEach((lineItemId, newQuantity) -> {
OrderLineItem lineItem = findOrderLineItem(lineItemId);
delta.set(delta.get().add(lineItem.deltaForChangedQuantity(newQuantity)));
Arrays.asList(orderRevision.getRevisedOrderLineItems()).forEach(item -> {
OrderLineItem lineItem = findOrderLineItem(item.getMenuItemId());
delta.set(delta.get().add(lineItem.deltaForChangedQuantity(item.getQuantity())));
});
return delta.get();
}

void updateLineItems(OrderRevision orderRevision) {
getLineItems().stream().forEach(li -> {
Integer revised = orderRevision.getRevisedLineItemQuantities().get(li.getMenuItemId());
li.setQuantity(revised);

Optional<Integer> revised = Arrays
.asList(orderRevision.getRevisedOrderLineItems())
.stream()
.filter(item -> Objects.equals(li.getMenuItemId(), item.getMenuItemId()))
.map(RevisedOrderLineItem::getQuantity)
.findFirst();

li.setQuantity(revised.orElseThrow(() ->
new IllegalArgumentException(String.format("menu item id not found.", li.getMenuItemId()))));
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
package net.chrisrichardson.ftgo.orderservice.domain;

import java.util.Map;
import net.chrisrichardson.ftgo.common.RevisedOrderLineItem;

import java.util.Optional;

public class OrderRevision {

private Optional<DeliveryInformation> deliveryInformation = Optional.empty();
private Map<String, Integer> revisedLineItemQuantities;
private RevisedOrderLineItem[] revisedOrderLineItems;

private OrderRevision() {
}

public OrderRevision(Optional<DeliveryInformation> deliveryInformation, Map<String, Integer> revisedLineItemQuantities) {
public OrderRevision(Optional<DeliveryInformation> deliveryInformation, RevisedOrderLineItem[] revisedOrderLineItems) {
this.deliveryInformation = deliveryInformation;
this.revisedLineItemQuantities = revisedLineItemQuantities;
this.revisedOrderLineItems = revisedOrderLineItems;
}

public void setDeliveryInformation(Optional<DeliveryInformation> deliveryInformation) {
this.deliveryInformation = deliveryInformation;
}

public void setRevisedLineItemQuantities(Map<String, Integer> revisedLineItemQuantities) {
this.revisedLineItemQuantities = revisedLineItemQuantities;
}

public Optional<DeliveryInformation> getDeliveryInformation() {
return deliveryInformation;
}

public RevisedOrderLineItem[] getRevisedOrderLineItems() {
return revisedOrderLineItems;
}

public Map<String, Integer> getRevisedLineItemQuantities() {
return revisedLineItemQuantities;
public void setRevisedOrderLineItems(RevisedOrderLineItem[] revisedOrderLineItems) {
this.revisedOrderLineItems = revisedOrderLineItems;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private CommandWithDestination confirmOrderRevision(ReviseOrderSagaData data) {
}

private CommandWithDestination confirmTicketRevision(ReviseOrderSagaData data) {
return send(new ConfirmReviseTicketCommand(data.getRestaurantId(), data.getOrderId(), data.getOrderRevision().getRevisedLineItemQuantities()))
return send(new ConfirmReviseTicketCommand(data.getRestaurantId(), data.getOrderId(), data.getOrderRevision().getRevisedOrderLineItems()))
.to(KitchenServiceChannels.COMMAND_CHANNEL)
.build();

Expand All @@ -84,7 +84,7 @@ private CommandWithDestination undoBeginReviseTicket(ReviseOrderSagaData data) {
}

private CommandWithDestination beginReviseTicket(ReviseOrderSagaData data) {
return send(new BeginReviseTicketCommand(data.getRestaurantId(), data.getOrderId(), data.getOrderRevision().getRevisedLineItemQuantities()))
return send(new BeginReviseTicketCommand(data.getRestaurantId(), data.getOrderId(), data.getOrderRevision().getRevisedOrderLineItems()))
.to(KitchenServiceChannels.COMMAND_CHANNEL)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public ResponseEntity<GetOrderResponse> cancel(@PathVariable long orderId) {
@RequestMapping(path = "/{orderId}/revise", method = RequestMethod.POST)
public ResponseEntity<GetOrderResponse> revise(@PathVariable long orderId, @RequestBody ReviseOrderRequest request) {
try {
Order order = orderService.reviseOrder(orderId, new OrderRevision(Optional.empty(), request.getRevisedLineItemQuantities()));
Order order = orderService.reviseOrder(orderId, new OrderRevision(Optional.empty(), request.getRevisedOrderLineItems()));
return new ResponseEntity<>(makeGetOrderResponse(order), HttpStatus.OK);
} catch (OrderNotFoundException e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
Expand Down
Loading

0 comments on commit eca1f79

Please sign in to comment.