This repository has been archived by the owner on May 9, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started on web gateway and item service
- Loading branch information
Showing
68 changed files
with
1,683 additions
and
1,333 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
113 changes: 101 additions & 12 deletions
113
item-api/src/main/java/com/example/auction/item/api/Item.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 |
---|---|---|
@@ -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; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
item-impl/src/main/java/com/example/auction/item/impl/ItemServiceImpl.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,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()); | ||
} | ||
} |
Oops, something went wrong.