Skip to content

Commit

Permalink
microservices-patterns#51 Replace shared API classes with codegen fro…
Browse files Browse the repository at this point in the history
…m OpenAPI/JSON schema - Used JSON schema for Restaurant events in Kitchen Service
  • Loading branch information
cer committed Jan 1, 2020
1 parent 49adaf9 commit bcef65d
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 13 deletions.
22 changes: 17 additions & 5 deletions ftgo-kitchen-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ buildscript {
apply plugin: "io.spring.dependency-management"
apply plugin: 'spring-cloud-contract'
apply plugin: IntegrationTestsPlugin
apply plugin: FtgoJSONSchema2PojoPlugin

dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-contract-dependencies:$springCloudContractDependenciesVersion"
}
}

dependencies {
testCompile 'org.springframework.cloud:spring-cloud-starter-contract-verifier'
}

contracts {
contractsDslDir = new File("../ftgo-kitchen-service-contracts/src/main/resources/contracts")
packageWithBaseClasses = 'net.chrisrichardson.ftgo.kitchenservice.contract'
Expand All @@ -45,6 +42,7 @@ apply plugin: FtgoServicePlugin

dependencies {

ftgoApiSpecification project(":ftgo-restaurant-service-api-spec")

compile "io.eventuate.tram.core:eventuate-tram-jdbc-kafka:$eventuateTramVersion"
compile "io.eventuate.tram.core:eventuate-tram-events:$eventuateTramVersion"
Expand All @@ -53,7 +51,7 @@ dependencies {
compile project(":common-swagger")
compile project(":ftgo-common-jpa")
compile project(":ftgo-kitchen-service-api")
compile project(":ftgo-restaurant-service-api")
compile project(":ftgo-restaurant-service-api-temp")
compile "io.eventuate.tram.core:eventuate-tram-aggregate-domain-events:$eventuateTramVersion"

compile "org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion"
Expand All @@ -71,4 +69,18 @@ dependencies {
testCompile "com.jayway.restassured:rest-assured:$restAssuredVersion"
testCompile "com.jayway.jsonpath:json-path:2.3.0"
testCompile "io.eventuate.tram.core:eventuate-tram-testing-support-spring-cloud-contract:$eventuateTramVersion"

testCompile 'org.springframework.cloud:spring-cloud-starter-contract-verifier'
}

ftgoJsonSchema2Pojo {

ftgoRestaurantService {
source = files("${ftgoApiSpecsDir}/ftgo-restaurant-service-api-spec")
targetPackage = "net.chrisrichardson.ftgo.restaurantservice.events"
includeAdditionalProperties = false
generateBuilders = true
useLongIntegers = true
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.eventuate.tram.events.aggregates.ResultWithDomainEvents;
import net.chrisrichardson.ftgo.kitchenservice.api.TicketDetails;
import net.chrisrichardson.ftgo.kitchenservice.api.events.TicketDomainEvent;
import net.chrisrichardson.ftgo.restaurantservice.events.RestaurantMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

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

import net.chrisrichardson.ftgo.common.Money;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Embeddable;

@Embeddable
@Access(AccessType.FIELD)
public class MenuItem {

private String id;
private String name;
private Money price;

private MenuItem() {
}

public MenuItem(String id, String name, Money price) {
this.id = id;
this.name = name;
this.price = price;
}

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

@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}

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

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Money getPrice() {
return price;
}

public void setPrice(Money price) {
this.price = price;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import io.eventuate.tram.events.common.DomainEvent;
import net.chrisrichardson.ftgo.kitchenservice.api.TicketDetails;
import net.chrisrichardson.ftgo.restaurantservice.events.MenuItem;
import net.chrisrichardson.ftgo.restaurantservice.events.RestaurantMenu;

import javax.persistence.Access;
import javax.persistence.AccessType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.chrisrichardson.ftgo.kitchenservice.domain;

import java.util.List;

public class RestaurantMenu {
private List<MenuItem> menuItems;

public RestaurantMenu(List<MenuItem> menuItems) {
this.menuItems = menuItems;
}

public List<MenuItem> getMenuItems() {
return menuItems;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import io.eventuate.tram.events.subscriber.DomainEventHandlers;
import io.eventuate.tram.events.subscriber.DomainEventHandlersBuilder;
import net.chrisrichardson.ftgo.kitchenservice.domain.KitchenService;
import net.chrisrichardson.ftgo.kitchenservice.domain.RestaurantMenu;
import net.chrisrichardson.ftgo.restaurantservice.events.RestaurantCreated;
import net.chrisrichardson.ftgo.restaurantservice.events.RestaurantMenu;
import net.chrisrichardson.ftgo.restaurantservice.events.RestaurantMenuRevised;
import org.springframework.beans.factory.annotation.Autowired;

Expand All @@ -26,15 +26,14 @@ public DomainEventHandlers domainEventHandlers() {
private void createMenu(DomainEventEnvelope<RestaurantCreated> de) {
String restaurantIds = de.getAggregateId();
long id = Long.parseLong(restaurantIds);
RestaurantMenu menu = de.getEvent().getMenu();
RestaurantMenu menu = new RestaurantMenu(RestaurantEventMapper.toMenuItems(de.getEvent().getMenu().getMenuItems()));
kitchenService.createMenu(id, menu);
}

public void reviseMenu(DomainEventEnvelope<RestaurantMenuRevised> de) {

long id = Long.parseLong(de.getAggregateId());
RestaurantMenu revisedMenu = de.getEvent().getRevisedMenu();
kitchenService.reviseMenu(id, revisedMenu);
RestaurantMenu menu = new RestaurantMenu(RestaurantEventMapper.toMenuItems(de.getEvent().getMenu().getMenuItems()));
kitchenService.reviseMenu(id, menu);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.chrisrichardson.ftgo.kitchenservice.messagehandlers;

import net.chrisrichardson.ftgo.common.Money;
import net.chrisrichardson.ftgo.restaurantservice.events.MenuItem;

import java.util.List;
import java.util.stream.Collectors;

public class RestaurantEventMapper {

public static List<net.chrisrichardson.ftgo.kitchenservice.domain.MenuItem> toMenuItems(List<MenuItem> menuItems) {
return menuItems.stream().map(mi -> new net.chrisrichardson.ftgo.kitchenservice.domain.MenuItem(mi.getId(), mi.getName(), new Money(mi.getPrice()))).collect(Collectors.toList());
}

}

0 comments on commit bcef65d

Please sign in to comment.