mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-16 18:59:21 +00:00
docs: update Hexagonal docs (#2932)
This commit is contained in:
-1
@@ -25,7 +25,6 @@
|
||||
package com.iluwatar.hexagonal.administration;
|
||||
|
||||
import com.iluwatar.hexagonal.domain.LotteryAdministration;
|
||||
import com.iluwatar.hexagonal.domain.LotteryNumbers;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
|
||||
@@ -29,6 +29,7 @@ import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import com.mongodb.client.model.UpdateOptions;
|
||||
import java.util.ArrayList;
|
||||
import lombok.Getter;
|
||||
import org.bson.Document;
|
||||
|
||||
/**
|
||||
@@ -39,8 +40,11 @@ public class MongoBank implements WireTransfers {
|
||||
private static final String DEFAULT_DB = "lotteryDB";
|
||||
private static final String DEFAULT_ACCOUNTS_COLLECTION = "accounts";
|
||||
|
||||
@Getter
|
||||
private MongoClient mongoClient;
|
||||
@Getter
|
||||
private MongoDatabase database;
|
||||
@Getter
|
||||
private MongoCollection<Document> accountsCollection;
|
||||
|
||||
/**
|
||||
@@ -77,34 +81,6 @@ public class MongoBank implements WireTransfers {
|
||||
accountsCollection = database.getCollection(accountsCollectionName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mongo client.
|
||||
*
|
||||
* @return mongo client
|
||||
*/
|
||||
public MongoClient getMongoClient() {
|
||||
return mongoClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mongo database.
|
||||
*
|
||||
* @return mongo database
|
||||
*/
|
||||
public MongoDatabase getMongoDatabase() {
|
||||
return database;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get accounts collection.
|
||||
*
|
||||
* @return accounts collection
|
||||
*/
|
||||
public MongoCollection<Document> getAccountsCollection() {
|
||||
return accountsCollection;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setFunds(String bankAccount, int amount) {
|
||||
var search = new Document("_id", bankAccount);
|
||||
|
||||
@@ -37,6 +37,7 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.Getter;
|
||||
import org.bson.Document;
|
||||
|
||||
/**
|
||||
@@ -51,7 +52,9 @@ public class MongoTicketRepository implements LotteryTicketRepository {
|
||||
|
||||
private MongoClient mongoClient;
|
||||
private MongoDatabase database;
|
||||
@Getter
|
||||
private MongoCollection<Document> ticketsCollection;
|
||||
@Getter
|
||||
private MongoCollection<Document> countersCollection;
|
||||
|
||||
/**
|
||||
@@ -112,24 +115,6 @@ public class MongoTicketRepository implements LotteryTicketRepository {
|
||||
return result.getInteger("seq");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tickets collection.
|
||||
*
|
||||
* @return tickets collection
|
||||
*/
|
||||
public MongoCollection<Document> getTicketsCollection() {
|
||||
return ticketsCollection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get counters collection.
|
||||
*
|
||||
* @return counters collection
|
||||
*/
|
||||
public MongoCollection<Document> getCountersCollection() {
|
||||
return countersCollection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<LotteryTicket> findById(LotteryTicketId id) {
|
||||
return ticketsCollection
|
||||
@@ -145,10 +130,10 @@ public class MongoTicketRepository implements LotteryTicketRepository {
|
||||
public Optional<LotteryTicketId> save(LotteryTicket ticket) {
|
||||
var ticketId = getNextId();
|
||||
var doc = new Document(TICKET_ID, ticketId);
|
||||
doc.put("email", ticket.getPlayerDetails().getEmail());
|
||||
doc.put("bank", ticket.getPlayerDetails().getBankAccount());
|
||||
doc.put("phone", ticket.getPlayerDetails().getPhoneNumber());
|
||||
doc.put("numbers", ticket.getLotteryNumbers().getNumbersAsString());
|
||||
doc.put("email", ticket.playerDetails().email());
|
||||
doc.put("bank", ticket.playerDetails().bankAccount());
|
||||
doc.put("phone", ticket.playerDetails().phoneNumber());
|
||||
doc.put("numbers", ticket.lotteryNumbers().getNumbersAsString());
|
||||
ticketsCollection.insertOne(doc);
|
||||
return Optional.of(new LotteryTicketId(ticketId));
|
||||
}
|
||||
@@ -160,7 +145,7 @@ public class MongoTicketRepository implements LotteryTicketRepository {
|
||||
.into(new ArrayList<>())
|
||||
.stream()
|
||||
.map(this::docToTicket)
|
||||
.collect(Collectors.toMap(LotteryTicket::getId, Function.identity()));
|
||||
.collect(Collectors.toMap(LotteryTicket::id, Function.identity()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -68,8 +68,8 @@ public class LotteryAdministration {
|
||||
var tickets = getAllSubmittedTickets();
|
||||
for (var id : tickets.keySet()) {
|
||||
var lotteryTicket = tickets.get(id);
|
||||
var playerDetails = lotteryTicket.getPlayerDetails();
|
||||
var playerAccount = playerDetails.getBankAccount();
|
||||
var playerDetails = lotteryTicket.playerDetails();
|
||||
var playerAccount = playerDetails.bankAccount();
|
||||
var result = LotteryUtils.checkTicketForPrize(repository, id, numbers).getResult();
|
||||
if (result == LotteryTicketCheckResult.CheckResult.WIN_PRIZE) {
|
||||
if (wireTransfers.transferFunds(PRIZE_AMOUNT, SERVICE_BANK_ACCOUNT, playerAccount)) {
|
||||
|
||||
@@ -57,8 +57,8 @@ public class LotteryService {
|
||||
* Submit lottery ticket to participate in the lottery.
|
||||
*/
|
||||
public Optional<LotteryTicketId> submitTicket(LotteryTicket ticket) {
|
||||
var playerDetails = ticket.getPlayerDetails();
|
||||
var playerAccount = playerDetails.getBankAccount();
|
||||
var playerDetails = ticket.playerDetails();
|
||||
var playerAccount = playerDetails.bankAccount();
|
||||
var result = wireTransfers.transferFunds(TICKET_PRIZE, playerAccount, SERVICE_BANK_ACCOUNT);
|
||||
if (!result) {
|
||||
notifications.ticketSubmitError(playerDetails);
|
||||
|
||||
@@ -24,21 +24,10 @@
|
||||
*/
|
||||
package com.iluwatar.hexagonal.domain;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* Immutable value object representing lottery ticket.
|
||||
*/
|
||||
@Getter
|
||||
@ToString
|
||||
@RequiredArgsConstructor
|
||||
public class LotteryTicket {
|
||||
|
||||
private final LotteryTicketId id;
|
||||
private final PlayerDetails playerDetails;
|
||||
private final LotteryNumbers lotteryNumbers;
|
||||
public record LotteryTicket(LotteryTicketId id, PlayerDetails playerDetails, LotteryNumbers lotteryNumbers) {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
@@ -74,5 +63,4 @@ public class LotteryTicket {
|
||||
return playerDetails.equals(other.playerDetails);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ public class LotteryUtils {
|
||||
) {
|
||||
var optional = repository.findById(id);
|
||||
if (optional.isPresent()) {
|
||||
if (optional.get().getLotteryNumbers().equals(winningNumbers)) {
|
||||
if (optional.get().lotteryNumbers().equals(winningNumbers)) {
|
||||
return new LotteryTicketCheckResult(CheckResult.WIN_PRIZE, 1000);
|
||||
} else {
|
||||
return new LotteryTicketCheckResult(CheckResult.NO_PRIZE);
|
||||
|
||||
@@ -24,22 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.hexagonal.domain;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* Immutable value object containing lottery player details.
|
||||
*/
|
||||
@EqualsAndHashCode
|
||||
@ToString
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class PlayerDetails {
|
||||
|
||||
private final String email;
|
||||
private final String bankAccount;
|
||||
private final String phoneNumber;
|
||||
|
||||
}
|
||||
public record PlayerDetails(String email, String bankAccount, String phoneNumber) {}
|
||||
|
||||
@@ -28,6 +28,7 @@ import com.iluwatar.hexagonal.domain.PlayerDetails;
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import lombok.Getter;
|
||||
import org.bson.Document;
|
||||
|
||||
/**
|
||||
@@ -41,8 +42,11 @@ public class MongoEventLog implements LotteryEventLog {
|
||||
private static final String PHONE = "phone";
|
||||
public static final String MESSAGE = "message";
|
||||
|
||||
@Getter
|
||||
private MongoClient mongoClient;
|
||||
@Getter
|
||||
private MongoDatabase database;
|
||||
@Getter
|
||||
private MongoCollection<Document> eventsCollection;
|
||||
|
||||
private final StdOutEventLog stdOutEventLog = new StdOutEventLog();
|
||||
@@ -81,39 +85,11 @@ public class MongoEventLog implements LotteryEventLog {
|
||||
eventsCollection = database.getCollection(eventsCollectionName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mongo client.
|
||||
*
|
||||
* @return mongo client
|
||||
*/
|
||||
public MongoClient getMongoClient() {
|
||||
return mongoClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mongo database.
|
||||
*
|
||||
* @return mongo database
|
||||
*/
|
||||
public MongoDatabase getMongoDatabase() {
|
||||
return database;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get events collection.
|
||||
*
|
||||
* @return events collection
|
||||
*/
|
||||
public MongoCollection<Document> getEventsCollection() {
|
||||
return eventsCollection;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void ticketSubmitted(PlayerDetails details) {
|
||||
var document = new Document(EMAIL, details.getEmail());
|
||||
document.put(PHONE, details.getPhoneNumber());
|
||||
document.put("bank", details.getBankAccount());
|
||||
var document = new Document(EMAIL, details.email());
|
||||
document.put(PHONE, details.phoneNumber());
|
||||
document.put("bank", details.bankAccount());
|
||||
document
|
||||
.put(MESSAGE, "Lottery ticket was submitted and bank account was charged for 3 credits.");
|
||||
eventsCollection.insertOne(document);
|
||||
@@ -122,9 +98,9 @@ public class MongoEventLog implements LotteryEventLog {
|
||||
|
||||
@Override
|
||||
public void ticketSubmitError(PlayerDetails details) {
|
||||
var document = new Document(EMAIL, details.getEmail());
|
||||
document.put(PHONE, details.getPhoneNumber());
|
||||
document.put("bank", details.getBankAccount());
|
||||
var document = new Document(EMAIL, details.email());
|
||||
document.put(PHONE, details.phoneNumber());
|
||||
document.put("bank", details.bankAccount());
|
||||
document.put(MESSAGE, "Lottery ticket could not be submitted because lack of funds.");
|
||||
eventsCollection.insertOne(document);
|
||||
stdOutEventLog.ticketSubmitError(details);
|
||||
@@ -132,9 +108,9 @@ public class MongoEventLog implements LotteryEventLog {
|
||||
|
||||
@Override
|
||||
public void ticketDidNotWin(PlayerDetails details) {
|
||||
var document = new Document(EMAIL, details.getEmail());
|
||||
document.put(PHONE, details.getPhoneNumber());
|
||||
document.put("bank", details.getBankAccount());
|
||||
var document = new Document(EMAIL, details.email());
|
||||
document.put(PHONE, details.phoneNumber());
|
||||
document.put("bank", details.bankAccount());
|
||||
document.put(MESSAGE, "Lottery ticket was checked and unfortunately did not win this time.");
|
||||
eventsCollection.insertOne(document);
|
||||
stdOutEventLog.ticketDidNotWin(details);
|
||||
@@ -142,9 +118,9 @@ public class MongoEventLog implements LotteryEventLog {
|
||||
|
||||
@Override
|
||||
public void ticketWon(PlayerDetails details, int prizeAmount) {
|
||||
var document = new Document(EMAIL, details.getEmail());
|
||||
document.put(PHONE, details.getPhoneNumber());
|
||||
document.put("bank", details.getBankAccount());
|
||||
var document = new Document(EMAIL, details.email());
|
||||
document.put(PHONE, details.phoneNumber());
|
||||
document.put("bank", details.bankAccount());
|
||||
document.put(MESSAGE, String
|
||||
.format("Lottery ticket won! The bank account was deposited with %d credits.",
|
||||
prizeAmount));
|
||||
@@ -154,9 +130,9 @@ public class MongoEventLog implements LotteryEventLog {
|
||||
|
||||
@Override
|
||||
public void prizeError(PlayerDetails details, int prizeAmount) {
|
||||
var document = new Document(EMAIL, details.getEmail());
|
||||
document.put(PHONE, details.getPhoneNumber());
|
||||
document.put("bank", details.getBankAccount());
|
||||
var document = new Document(EMAIL, details.email());
|
||||
document.put(PHONE, details.phoneNumber());
|
||||
document.put("bank", details.bankAccount());
|
||||
document.put(MESSAGE, String
|
||||
.format("Lottery ticket won! Unfortunately the bank credit transfer of %d failed.",
|
||||
prizeAmount));
|
||||
|
||||
@@ -36,30 +36,30 @@ public class StdOutEventLog implements LotteryEventLog {
|
||||
@Override
|
||||
public void ticketSubmitted(PlayerDetails details) {
|
||||
LOGGER.info("Lottery ticket for {} was submitted. Bank account {} was charged for 3 credits.",
|
||||
details.getEmail(), details.getBankAccount());
|
||||
details.email(), details.bankAccount());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ticketDidNotWin(PlayerDetails details) {
|
||||
LOGGER.info("Lottery ticket for {} was checked and unfortunately did not win this time.",
|
||||
details.getEmail());
|
||||
details.email());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ticketWon(PlayerDetails details, int prizeAmount) {
|
||||
LOGGER.info("Lottery ticket for {} has won! The bank account {} was deposited with {} credits.",
|
||||
details.getEmail(), details.getBankAccount(), prizeAmount);
|
||||
details.email(), details.bankAccount(), prizeAmount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prizeError(PlayerDetails details, int prizeAmount) {
|
||||
LOGGER.error("Lottery ticket for {} has won! Unfortunately the bank credit transfer of"
|
||||
+ " {} failed.", details.getEmail(), prizeAmount);
|
||||
+ " {} failed.", details.email(), prizeAmount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ticketSubmitError(PlayerDetails details) {
|
||||
LOGGER.error("Lottery ticket for {} could not be submitted because the credit transfer"
|
||||
+ " of 3 credits failed.", details.getEmail());
|
||||
+ " of 3 credits failed.", details.email());
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -26,10 +26,12 @@ package com.iluwatar.hexagonal.mongo;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.util.Properties;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Mongo connection properties loader.
|
||||
*/
|
||||
@Slf4j
|
||||
public class MongoConnectionPropertiesLoader {
|
||||
|
||||
private static final String DEFAULT_HOST = "localhost";
|
||||
@@ -50,7 +52,7 @@ public class MongoConnectionPropertiesLoader {
|
||||
port = Integer.parseInt(properties.getProperty("mongo-port"));
|
||||
} catch (Exception e) {
|
||||
// error occurred, use default properties
|
||||
e.printStackTrace();
|
||||
LOGGER.error("Error occurred: ", e);
|
||||
}
|
||||
}
|
||||
System.setProperty("mongo-host", host);
|
||||
|
||||
@@ -88,7 +88,7 @@ public class SampleData {
|
||||
);
|
||||
var wireTransfers = new InMemoryBank();
|
||||
PLAYERS.stream()
|
||||
.map(PlayerDetails::getBankAccount)
|
||||
.map(PlayerDetails::bankAccount)
|
||||
.map(e -> new SimpleEntry<>(e, RANDOM.nextInt(LotteryConstants.PLAYER_MAX_BALANCE)))
|
||||
.collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue))
|
||||
.forEach(wireTransfers::setFunds);
|
||||
|
||||
+4
-4
@@ -86,10 +86,10 @@ class MongoTicketRepositoryTest {
|
||||
var found = repository.findById(saved.get());
|
||||
assertTrue(found.isPresent());
|
||||
var ticket = found.get();
|
||||
assertEquals("foo@bar.com", ticket.getPlayerDetails().getEmail());
|
||||
assertEquals("123-123", ticket.getPlayerDetails().getBankAccount());
|
||||
assertEquals("07001234", ticket.getPlayerDetails().getPhoneNumber());
|
||||
assertEquals(original.getLotteryNumbers(), ticket.getLotteryNumbers());
|
||||
assertEquals("foo@bar.com", ticket.playerDetails().email());
|
||||
assertEquals("123-123", ticket.playerDetails().bankAccount());
|
||||
assertEquals("07001234", ticket.playerDetails().phoneNumber());
|
||||
assertEquals(original.lotteryNumbers(), ticket.lotteryNumbers());
|
||||
// clear the collection
|
||||
repository.deleteAll();
|
||||
assertEquals(0, repository.getTicketsCollection().countDocuments());
|
||||
|
||||
Reference in New Issue
Block a user