Skip to content
This repository has been archived by the owner on May 9, 2019. It is now read-only.

Commit

Permalink
Started on web gateway and item service
Browse files Browse the repository at this point in the history
  • Loading branch information
jroper committed Sep 20, 2016
1 parent c35c433 commit f8253b6
Show file tree
Hide file tree
Showing 68 changed files with 1,683 additions and 1,333 deletions.
Expand Up @@ -9,8 +9,6 @@
import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;
import org.junit.*;
import org.pcollections.TreePVector;
import play.mvc.Results;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
Expand Down
39 changes: 28 additions & 11 deletions build.sbt
Expand Up @@ -29,7 +29,7 @@ lazy val biddingApi = project("bidding-api")

lazy val biddingImpl = project("bidding-impl")
.settings(version := "1.0-SNAPSHOT")
.enablePlugins(LagomJava)
// .enablePlugins(LagomJava)
.dependsOn(biddingApi, itemApi)
.settings(
libraryDependencies ++= Seq(
Expand All @@ -49,7 +49,7 @@ lazy val searchApi = project("search-api")

lazy val searchImpl = project("search-impl")
.settings(version := "1.0-SNAPSHOT")
.enablePlugins(LagomJava)
// .enablePlugins(LagomJava)
.dependsOn(searchApi, itemApi, biddingApi)
.settings(
libraryDependencies ++= Seq(
Expand All @@ -67,7 +67,7 @@ lazy val transactionApi = project("transaction-api")

lazy val transactionImpl = project("transaction-impl")
.settings(version := "1.0-SNAPSHOT")
.enablePlugins(LagomJava)
// .enablePlugins(LagomJava)
.dependsOn(transactionApi, biddingApi)
.settings(
libraryDependencies ++= Seq(
Expand All @@ -76,16 +76,31 @@ lazy val transactionImpl = project("transaction-impl")
)
)

def project(id: String) = Project(id, base = file(id))
.settings(eclipseSettings: _*)
.settings(javacOptions in compile ++= Seq("-encoding", "UTF-8", "-source", "1.8", "-target", "1.8", "-Xlint:unchecked", "-Xlint:deprecation"))
.settings(jacksonParameterNamesJavacSettings: _*) // applying it to every project even if not strictly needed.
lazy val userApi = project("user-api")
.settings(version := "1.0-SNAPSHOT")
.settings(
libraryDependencies += lagomJavadslApi
)

lazy val userImpl = project("user-impl")
.settings(version := "1.0-SNAPSHOT")
.enablePlugins(LagomJava)
.dependsOn(userApi)
.settings(
libraryDependencies += lagomJavadslPersistence
)

// See https://github.com/FasterXML/jackson-module-parameter-names
lazy val jacksonParameterNamesJavacSettings = Seq(
javacOptions in compile += "-parameters"
)
lazy val webGateway = project("web-gateway")
.settings(version := "1.0-SNAPSHOT")
.enablePlugins(PlayJava && LagomPlay)
.dependsOn(transactionApi, biddingApi, itemApi, searchApi, userApi)
.settings(
libraryDependencies += lagomJavadslClient
)

def project(id: String) = Project(id, base = file(id))
.settings(eclipseSettings: _*)
.settings(javacOptions ++= Seq("-encoding", "UTF-8", "-source", "1.8", "-target", "1.8", "-Xlint:unchecked", "-Xlint:deprecation", "-parameters"))

// Configuration of sbteclipse
// Needed for importing the project into Eclipse
Expand All @@ -100,3 +115,5 @@ lazy val eclipseSettings = Seq(
unmanagedSourceDirectories in Compile := Seq((javaSource in Compile).value),
unmanagedSourceDirectories in Test := Seq((javaSource in Test).value)
)

lagomCassandraCleanOnStart := false
113 changes: 101 additions & 12 deletions item-api/src/main/java/com/example/auction/item/api/Item.java
@@ -1,46 +1,135 @@
package com.example.auction.item.api;

import com.fasterxml.jackson.annotation.JsonCreator;
import org.pcollections.PSet;

import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
import java.util.UUID;

/**
* An item entity.
*/
public final class Item {

// null fields are temporarily null because they aren't implemented yet
private final UUID id;
private final UUID creator;
private final String title;
private final String description;
private final UUID categoryId;
private final UUID categoryId = null;
private final String currencyId;
private final Location location;
private final Location location = null;
private final int increment;
private final int reservePrice;
private final PSet<DeliveryOption> deliveryOptions;
private final PSet<PaymentOption> paymentOptions;
private final PSet<DeliveryOption> deliveryOptions = null;
private final PSet<PaymentOption> paymentOptions = null;
private final int price;
private final ItemStatus status;
private final Instant auctionStart;
private final Instant auctionEnd;
private final UUID auctionWinner;
private final Duration auctionDuration;
private final Optional<Instant> auctionStart;
private final Optional<Instant> auctionEnd;
private final Optional<UUID> auctionWinner;

public Item(UUID id, UUID creator, String title, String description, UUID categoryId, String currencyId, Location location, int reservePrice, PSet<DeliveryOption> deliveryOptions, PSet<PaymentOption> paymentOptions, int price, ItemStatus status, Instant auctionStart, Instant auctionEnd, UUID auctionWinner) {
/**
* Constructor that Jackson uses when deserialising items.
*/
@JsonCreator
private Item(Optional<UUID> id, UUID creator, String title, String description, String currencyId,
int increment, int reservePrice, Optional<Integer> price, Optional<ItemStatus> status, Duration auctionDuration,
Optional<Instant> auctionStart, Optional<Instant> auctionEnd, Optional<UUID> auctionWinner) {
this.id = id.orElse(null);
this.creator = creator;
this.title = title;
this.description = description;
this.currencyId = currencyId;
this.increment = increment;
this.reservePrice = reservePrice;
this.price = price.orElse(0);
this.status = status.orElse(null);
this.auctionDuration = auctionDuration;
this.auctionStart = auctionStart;
this.auctionEnd = auctionEnd;
this.auctionWinner = auctionWinner;
}

/**
* Constructor to use when creating new items.
*/
public Item(UUID creator, String title, String description, String currencyId, int increment, int reservePrice,
Duration auctionDuration) {
this(Optional.empty(), creator, title, description, currencyId, increment, reservePrice, Optional.empty(),
Optional.empty(), auctionDuration, Optional.empty(), Optional.empty(), Optional.empty());
}

public Item(UUID id, UUID creator, String title, String description, String currencyId, int increment, int reservePrice, int price,
ItemStatus status, Duration auctionDuration, Optional<Instant> auctionStart, Optional<Instant> auctionEnd,
Optional<UUID> auctionWinner) {
this.id = id;
this.creator = creator;
this.title = title;
this.description = description;
this.categoryId = categoryId;
this.currencyId = currencyId;
this.location = location;
this.increment = increment;
this.reservePrice = reservePrice;
this.deliveryOptions = deliveryOptions;
this.paymentOptions = paymentOptions;
this.price = price;
this.status = status;
this.auctionDuration = auctionDuration;
this.auctionStart = auctionStart;
this.auctionEnd = auctionEnd;
this.auctionWinner = auctionWinner;
}

public UUID getId() {
return id;
}

public UUID getCreator() {
return creator;
}

public String getTitle() {
return title;
}

public String getDescription() {
return description;
}

public String getCurrencyId() {
return currencyId;
}

public int getIncrement() {
return increment;
}

public int getReservePrice() {
return reservePrice;
}

public int getPrice() {
return price;
}

public ItemStatus getStatus() {
return status;
}

public Duration getAuctionDuration() {
return auctionDuration;
}

public Optional<Instant> getAuctionStart() {
return auctionStart;
}

public Optional<Instant> getAuctionEnd() {
return auctionEnd;
}

public Optional<UUID> getAuctionWinner() {
return auctionWinner;
}
}
Expand Up @@ -5,20 +5,23 @@

import static com.lightbend.lagom.javadsl.api.Service.named;
import static com.lightbend.lagom.javadsl.api.Service.pathCall;
import static com.lightbend.lagom.javadsl.api.Service.restCall;

import akka.Done;
import akka.NotUsed;
import com.lightbend.lagom.javadsl.api.Descriptor;
import com.lightbend.lagom.javadsl.api.Service;
import com.lightbend.lagom.javadsl.api.ServiceCall;
import com.lightbend.lagom.javadsl.api.deser.PathParamSerializers;
import com.lightbend.lagom.javadsl.api.transport.Method;
import org.pcollections.PSequence;

import java.util.Optional;
import java.util.UUID;

public interface ItemService extends Service {

ServiceCall<Item, Done> createItem();
ServiceCall<Item, Item> createItem();

ServiceCall<Item, Done> updateItem(UUID id);

Expand All @@ -32,6 +35,11 @@ public interface ItemService extends Service {

@Override
default Descriptor descriptor() {
return named("item");
return named("item").withCalls(
pathCall("/api/item", this::createItem),
restCall(Method.POST, "/api/item/:id/start", this::startAuction),
pathCall("/api/item/:id", this::getItem),
pathCall("/api/item?userId&pageNo&pageSize", this::getItemsForUser)
).withPathParamSerializer(UUID.class, PathParamSerializers.required("UUID", UUID::fromString, UUID::toString));
}
}
11 changes: 11 additions & 0 deletions item-impl/src/main/java/Module.java
@@ -0,0 +1,11 @@
import com.example.auction.item.api.ItemService;
import com.example.auction.item.impl.ItemServiceImpl;
import com.google.inject.AbstractModule;
import com.lightbend.lagom.javadsl.server.ServiceGuiceSupport;

public class Module extends AbstractModule implements ServiceGuiceSupport {
@Override
protected void configure() {
bindServices(serviceBinding(ItemService.class, ItemServiceImpl.class));
}
}
@@ -0,0 +1,100 @@
package com.example.auction.item.impl;

import akka.Done;
import akka.NotUsed;
import com.example.auction.item.api.Item;
import com.example.auction.item.api.ItemService;
import com.example.auction.item.api.ItemStatus;
import com.lightbend.lagom.javadsl.api.ServiceCall;
import com.lightbend.lagom.javadsl.api.transport.NotFound;
import com.lightbend.lagom.javadsl.persistence.PersistentEntityRef;
import com.lightbend.lagom.javadsl.persistence.PersistentEntityRegistry;
import org.pcollections.PSequence;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.Optional;
import java.util.UUID;

@Singleton
public class ItemServiceImpl implements ItemService {

private final PersistentEntityRegistry registry;

@Inject
public ItemServiceImpl(PersistentEntityRegistry registry) {
this.registry = registry;

registry.register(PItemEntity.class);
}

@Override
public ServiceCall<Item, Item> createItem() {
return item -> {
UUID itemId = UUID.randomUUID();
PItem pItem = new PItem(itemId, item.getCreator(), item.getTitle(), item.getDescription(),
item.getCurrencyId(), item.getIncrement(), item.getReservePrice(), item.getAuctionDuration());
return entityRef(itemId).ask(new PItemCommand.CreateItem(pItem)).thenApply(done -> convertItem(pItem));
};
}

@Override
public ServiceCall<Item, Done> updateItem(UUID id) {
return item -> {
// todo implement
return null;
};
}

@Override
public ServiceCall<NotUsed, Done> startAuction(UUID id) {
return req -> entityRef(id).ask(PItemCommand.StartAuction.INSTANCE);
}

@Override
public ServiceCall<NotUsed, Item> getItem(UUID id) {
return req -> entityRef(id).ask(PItemCommand.GetItem.INSTANCE).thenApply(maybeItem -> {
if (maybeItem.isPresent()) {
return convertItem(maybeItem.get());
} else {
throw new NotFound("Item " + id + " not found");
}
});
}

private Item convertItem(PItem item) {
return new Item(item.getId(), item.getCreator(), item.getTitle(), item.getDescription(),
item.getCurrencyId(), item.getIncrement(), item.getReservePrice(), item.getPrice(),
convertStatus(item.getStatus()), item.getAuctionDuration(), item.getAuctionStart(),
item.getAuctionEnd(), item.getAuctionWinner());
}

private ItemStatus convertStatus(PItemStatus status) {
switch (status) {
case NOT_CREATED:
throw new IllegalStateException("Publicly exposed item can't be not created");
case CREATED:
return ItemStatus.CREATED;
case AUCTION:
return ItemStatus.AUCTION;
case COMPLETED:
return ItemStatus.COMPLETED;
case CANCELLED:
return ItemStatus.CANCELLED;
default:
throw new IllegalArgumentException("Unknown status " + status);
}
}

@Override
public ServiceCall<NotUsed, PSequence<Item>> getItemsForUser(UUID id, Optional<Integer> pageNo, Optional<Integer> pageSize) {
return req -> {
// todo implement
return null;
};
}

private PersistentEntityRef<PItemCommand> entityRef(UUID itemId) {
return registry.refFor(PItemEntity.class, itemId.toString());
}
}

0 comments on commit f8253b6

Please sign in to comment.