Skip to content

Commit

Permalink
Merge pull request #6 from pulnara/restaurant-service-efficiency
Browse files Browse the repository at this point in the history
Implement Restaurant service efficiency
  • Loading branch information
pulnara authored Jun 7, 2021
2 parents 8f9c742 + 8bdcf0c commit 18e0130
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 30 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ subprojects {
group = "net.chrisrichardson.ftgo"

repositories {
eventuateMavenRepoUrl.split('[ ,]').each { repoUrl -> maven { url repoUrl.trim() } }

mavenCentral()
jcenter()
eventuateMavenRepoUrl.split('[ ,]').each { repoUrl -> maven { url repoUrl.trim() } }



maven {
url 'https://jitpack.io'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
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.TicketLineItem;
import net.chrisrichardson.ftgo.kitchenservice.api.events.TicketDomainEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class KitchenService {

Expand All @@ -22,8 +26,10 @@ public class KitchenService {
@Autowired
private RestaurantRepository restaurantRepository;

public void createMenu(long id, RestaurantMenu menu) {
Restaurant restaurant = new Restaurant(id, menu.getMenuItems());
private Logger logger = LoggerFactory.getLogger(getClass());

public void createMenu(long id, RestaurantMenu menu, long efficiency) {
Restaurant restaurant = new Restaurant(id, menu.getMenuItems(), efficiency);
restaurantRepository.save(restaurant);
}

Expand All @@ -34,6 +40,18 @@ public void reviseMenu(long ticketId, RestaurantMenu revisedMenu) {
}

public Ticket createTicket(long restaurantId, Long ticketId, TicketDetails ticketDetails) {
Optional<Restaurant> restaurant = restaurantRepository.findById(restaurantId);
Optional<Integer> totalOrderQuantity = ticketDetails
.getLineItems()
.stream()
.map(TicketLineItem::getQuantity)
.reduce(Integer::sum);
if (restaurant.isPresent() && totalOrderQuantity.isPresent() &&
restaurant.get().getEfficiency() < totalOrderQuantity.get()) {
logger.info("Restaurant efficiency exceeded!");
throw new RestaurantCapacityExceededException();
}
logger.info("Creating ticket...");
ResultWithDomainEvents<Ticket, TicketDomainEvent> rwe = Ticket.create(restaurantId, ticketId, ticketDetails);
ticketRepository.save(rwe.result);
domainEventPublisher.publish(rwe.result, rwe.events);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,23 @@ public class Restaurant {
@CollectionTable(name = "kitchen_service_restaurant_menu_items")
private List<MenuItem> menuItems;

public Long getEfficiency() {
return efficiency;
}

public void setEfficiency(Long efficiency) {
this.efficiency = efficiency;
}

private Long efficiency;

private Restaurant() {
}

public Restaurant(long id, List<MenuItem> menuItems) {
public Restaurant(long id, List<MenuItem> menuItems, long efficiency) {
this.id = id;
this.menuItems = menuItems;
this.efficiency = efficiency;
}

public List<DomainEvent> reviseMenu(RestaurantMenu revisedMenu) {
Expand All @@ -40,6 +51,7 @@ public List<DomainEvent> reviseMenu(RestaurantMenu revisedMenu) {

public void verifyRestaurantDetails(TicketDetails ticketDetails) {
// TODO - implement me

}

public Long getId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package net.chrisrichardson.ftgo.kitchenservice.domain;

public class RestaurantCapacityExceededException extends RuntimeException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.eventuate.tram.messaging.common.Message;
import io.eventuate.tram.sagas.participant.SagaCommandHandlersBuilder;
import net.chrisrichardson.ftgo.kitchenservice.api.*;
import net.chrisrichardson.ftgo.kitchenservice.domain.RestaurantCapacityExceededException;
import net.chrisrichardson.ftgo.kitchenservice.domain.RestaurantDetailsVerificationException;
import net.chrisrichardson.ftgo.kitchenservice.domain.Ticket;
import net.chrisrichardson.ftgo.kitchenservice.domain.KitchenService;
Expand Down Expand Up @@ -48,7 +49,7 @@ private Message createTicket(CommandMessage<CreateTicket>
Ticket ticket = kitchenService.createTicket(restaurantId, ticketId, ticketDetails);
CreateTicketReply reply = new CreateTicketReply(ticket.getId());
return withLock(Ticket.class, ticket.getId()).withSuccess(reply);
} catch (RestaurantDetailsVerificationException e) {
} catch (RestaurantDetailsVerificationException | RestaurantCapacityExceededException e) {
return withFailure();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private void createMenu(DomainEventEnvelope<RestaurantCreated> de) {
String restaurantIds = de.getAggregateId();
long id = Long.parseLong(restaurantIds);
RestaurantMenu menu = new RestaurantMenu(RestaurantEventMapper.toMenuItems(de.getEvent().getMenu().getMenuItems()));
kitchenService.createMenu(id, menu);
kitchenService.createMenu(id, menu, de.getEvent().getEfficiency());
}

public void reviseMenu(DomainEventEnvelope<RestaurantMenuRevised> de) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private String baseUrl(String path) {
public void shouldCreateTicket() {

long restaurantId = System.currentTimeMillis();
Restaurant restaurant = new Restaurant(restaurantId, Collections.emptyList());
Restaurant restaurant = new Restaurant(restaurantId, Collections.emptyList(), 50);

restaurantRepository.save(restaurant);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public enum OrderState {
CANCEL_PENDING,
CANCELLED,
REVISION_PENDING,
COMPLETED
}
5 changes: 3 additions & 2 deletions ftgo-order-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ dependencies {
compile project(":common-swagger")
compile project(":ftgo-common-jpa")

compile 'org.jetbrains:annotations:13.0'
compile "io.eventuate.tram.core:eventuate-tram-aggregate-domain-events:$eventuateTramVersion"
compile "io.eventuate.tram.core:eventuate-tram-spring-jdbc-kafka:$eventuateTramVersion"
compile "io.eventuate.tram.core:eventuate-tram-spring-commands:$eventuateTramVersion"
Expand All @@ -154,8 +155,8 @@ dependencies {
compile "io.grpc:grpc-protobuf:${grpcVersion}"
compile "io.grpc:grpc-stub:${grpcVersion}"

compile "io.microservices.tools.canvas:microservice-canvas-springmvc:$microserviceCanvasVersion"
compile "io.microservices.tools.canvas:microservice-canvas-extractor-tram-sagas:$microserviceCanvasVersion"
//compile "io.microservices.tools.canvas:microservice-canvas-springmvc:$microserviceCanvasVersion"
//compile "io.microservices.tools.canvas:microservice-canvas-extractor-tram-sagas:$microserviceCanvasVersion"
compile "io.eventuate.tram.core:eventuate-tram-spring-messaging:$eventuateTramVersion"
compile "io.eventuate.tram.sagas:eventuate-tram-sagas-spring-orchestration:$eventuateTramSagasVersion"

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

import io.eventuate.tram.spring.jdbckafka.TramJdbcKafkaConfiguration;
import io.microservices.canvas.extractor.spring.annotations.ServiceDescription;
import io.microservices.canvas.springmvc.MicroserviceCanvasWebConfiguration;
//import io.microservices.canvas.extractor.spring.annotations.ServiceDescription;
//import io.microservices.canvas.springmvc.MicroserviceCanvasWebConfiguration;
import net.chrisrichardson.eventstore.examples.customersandorders.commonswagger.CommonSwaggerConfiguration;
import net.chrisrichardson.ftgo.orderservice.grpc.GrpcConfiguration;
import net.chrisrichardson.ftgo.orderservice.messaging.OrderServiceMessagingConfiguration;
Expand All @@ -14,9 +14,9 @@

@SpringBootApplication
@Import({OrderWebConfiguration.class, OrderCommandHandlersConfiguration.class, OrderServiceMessagingConfiguration.class,
TramJdbcKafkaConfiguration.class, CommonSwaggerConfiguration.class, GrpcConfiguration.class,
MicroserviceCanvasWebConfiguration.class})
@ServiceDescription(description="Manages Orders", capabilities = "Order Management")
TramJdbcKafkaConfiguration.class, CommonSwaggerConfiguration.class, GrpcConfiguration.class/*,
MicroserviceCanvasWebConfiguration.class*/})
//@ServiceDescription(description="Manages Orders", capabilities = "Order Management")
public class OrderServiceMain {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"name": {
"type": "string"
},
"efficiency": {
"type": "integer"
},
"address": {
"type": "object",
"properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public RestaurantDomainEventPublisher orderAggregateEventPublisher(DomainEventPu
private RestaurantDomainEventPublisher restaurantDomainEventPublisher;

protected void restaurantCreated() {
Restaurant restaurant = new Restaurant("Yummy Indian", new RestaurantMenu(Collections.emptyList()));
Restaurant restaurant = new Restaurant("Yummy Indian", new RestaurantMenu(Collections.emptyList()), 10L);
restaurant.setId(99L);
restaurantDomainEventPublisher.publish(restaurant,
Collections.singletonList(new RestaurantCreated(restaurant.getName(), new Address("1 Main Street", "Unit 99", "Oakland", "CA", "94611"),
restaurant.getMenu())));
restaurant.getMenu(), restaurant.getEfficiency())));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ public class CreateRestaurantRequest {
private String name;
private Address address;
private RestaurantMenu menu;
private Long efficiency; // how many orders can be processed within an hour

private CreateRestaurantRequest() {

}

public CreateRestaurantRequest(String name, Address address, RestaurantMenu menu) {
public CreateRestaurantRequest(String name, Address address, RestaurantMenu menu, Long efficiency) {
this.name = name;
this.address = address;
this.menu = menu;
this.efficiency = efficiency;
}

public String getName() {
Expand All @@ -37,4 +39,13 @@ public void setMenu(RestaurantMenu menu) {
public Address getAddress() {
return address;
}

public Long getEfficiency() {
return efficiency;
}

public void setEfficiency(Long efficiency) {
this.efficiency = efficiency;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ public class Restaurant {

private String name;

private Long efficiency;

@Embedded
private RestaurantMenu menu;

private Restaurant() {
}

public Restaurant(String name, RestaurantMenu menu, Long efficiency) {
this.name = name;
this.menu = menu;
this.efficiency = efficiency;
}

public void setId(Long id) {
this.id = id;
}
Expand All @@ -37,18 +45,20 @@ public void setName(String name) {
this.name = name;
}


public Restaurant(String name, RestaurantMenu menu) {
this.name = name;
this.menu = menu;
}


public Long getId() {
return id;
}

public RestaurantMenu getMenu() {
return menu;
}

public Long getEfficiency() {
return efficiency;
}

public void setEfficiency(Long efficiency) {
this.efficiency = efficiency;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public class RestaurantService {
private RestaurantDomainEventPublisher restaurantDomainEventPublisher;

public Restaurant create(CreateRestaurantRequest request) {
Restaurant restaurant = new Restaurant(request.getName(), request.getMenu());
Restaurant restaurant = new Restaurant(request.getName(), request.getMenu(), request.getEfficiency());
restaurantRepository.save(restaurant);
restaurantDomainEventPublisher.publish(restaurant, Collections.singletonList(new RestaurantCreated(request.getName(), request.getAddress(), request.getMenu())));
restaurantDomainEventPublisher.publish(restaurant, Collections.singletonList(new RestaurantCreated(request.getName(), request.getAddress(), request.getMenu(), request.getEfficiency())));
return restaurant;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,28 @@ public class RestaurantCreated implements RestaurantDomainEvent {
private Address address;
private RestaurantMenu menu;

public Long getEfficiency() {
return efficiency;
}

public void setEfficiency(Long efficiency) {
this.efficiency = efficiency;
}

private Long efficiency;

public String getName() {
return name;
}

private RestaurantCreated() {
}

public RestaurantCreated(String name, Address address, RestaurantMenu menu) {
public RestaurantCreated(String name, Address address, RestaurantMenu menu, Long efficiency) {
this.name = name;
this.address = address;
this.menu = menu;
this.efficiency = efficiency;
}

public RestaurantMenu getMenu() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void shouldSerialize() throws JSONException {
ValidatingJSONMapper mapper = ValidatingJSONMapper.forSchema("/ftgo-restaurant-service-api-spec/messages/RestaurantCreated.json");

RestaurantCreated event = new RestaurantCreated(AJANTA_RESTAURANT_NAME, RESTAURANT_ADDRESS,
new RestaurantMenu(Collections.singletonList(CHICKEN_VINDALOO_MENU_ITEM)));
new RestaurantMenu(Collections.singletonList(CHICKEN_VINDALOO_MENU_ITEM)), 10L);
String json = mapper.toJSON(event);
assertNotNull(json);
}
Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

deployUrl=file:///Users/cer/.m2/testdeploy

eventuateMavenRepoUrl=https://dl.bintray.com/eventuateio-oss/eventuate-maven-release,https://dl.bintray.com/eventuateio-oss/eventuate-maven-milestone,https://dl.bintray.com/eventuateio-oss/microservice-canvas-tools-release,file:///Users/cer/.m2/testdeploy,https://dl.bintray.com/eventuateio-oss/eventuate-maven-rc,https://dl.bintray.com/eventuateio-oss/microservice-canvas-tools-rc,https://snapshots.repositories.eventuate.io/repository
#eventuateMavenRepoUrl=https://dl.bintray.com/eventuateio-oss/eventuate-maven-release,https://dl.bintray.com/eventuateio-oss/eventuate-maven-milestone,https://dl.bintray.com/eventuateio-oss/microservice-canvas-tools-release,file:///Users/cer/.m2/testdeploy,https://dl.bintray.com/eventuateio-oss/eventuate-maven-rc,https://dl.bintray.com/eventuateio-oss/microservice-canvas-tools-rc,https://snapshots.repositories.eventuate.io/repository
eventuateMavenRepoUrl=file:///Users/cer/.m2/testdeploy,https://snapshots.repositories.eventuate.io/repository


springBootVersion=2.1.1.RELEASE
restAssuredVersion=2.9.0
Expand Down

0 comments on commit 18e0130

Please sign in to comment.