Hexagonal pattern: Added Mongo based ticket repository and set production configuration to use that

This commit is contained in:
Ilkka Seppälä
2016-09-11 20:02:34 +03:00
parent a85463470e
commit 626c56730c
11 changed files with 339 additions and 14 deletions

View File

@ -54,7 +54,7 @@ public class ConsoleAdministration {
administartion.getAllSubmittedTickets().forEach((k,v)->System.out.println("Key: " + k + " Value: " + v));
} else if (cmd.equals("2")) {
LotteryNumbers numbers = administartion.performLottery();
System.out.println("The winning numbers: " + numbers);
System.out.println("The winning numbers: " + numbers.getNumbersAsString());
System.out.println("Time to reset the database for next round, eh?");
} else if (cmd.equals("3")) {
administartion.resetLottery();

View File

@ -0,0 +1,194 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.hexagonal.database;
import com.iluwatar.hexagonal.domain.LotteryNumbers;
import com.iluwatar.hexagonal.domain.LotteryTicket;
import com.iluwatar.hexagonal.domain.LotteryTicketId;
import com.iluwatar.hexagonal.domain.PlayerDetails;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
/**
* Mongo lottery ticket database
*/
public class MongoTicketRepository implements LotteryTicketRepository {
private static final String DEFAULT_HOST = "localhost";
private static final int DEFAULT_PORT = 27017;
private static final String DEFAULT_DB = "lotteryDB";
private static final String DEFAULT_TICKETS_COLLECTION = "lotteryTickets";
private static final String DEFAULT_COUNTERS_COLLECTION = "counters";
private MongoClient mongoClient;
private MongoDatabase database;
private MongoCollection<Document> ticketsCollection;
private MongoCollection<Document> countersCollection;
/**
* Constructor
*/
public MongoTicketRepository() {
connect();
}
/**
* Constructor accepting parameters
*/
public MongoTicketRepository(String host, int port, String dbName, String ticketsCollectionName,
String countersCollectionName) {
connect(host, port, dbName, ticketsCollectionName, countersCollectionName);
}
/**
* Connect to database with default parameters
*/
public void connect() {
connect(DEFAULT_HOST, DEFAULT_PORT, DEFAULT_DB, DEFAULT_TICKETS_COLLECTION, DEFAULT_COUNTERS_COLLECTION);
}
/**
* Connect to database with given parameters
*/
public void connect(String host, int port, String dbName, String ticketsCollectionName,
String countersCollectionName) {
if (mongoClient != null) {
mongoClient.close();
}
mongoClient = new MongoClient(host , port);
database = mongoClient.getDatabase(dbName);
ticketsCollection = database.getCollection(ticketsCollectionName);
countersCollection = database.getCollection(countersCollectionName);
if (countersCollection.count() <= 0) {
initCounters();
}
}
private void initCounters() {
Document doc = new Document("_id", "ticketId").append("seq", 1);
countersCollection.insertOne(doc);
}
/**
* @return next ticket id
*/
public int getNextId() {
Document find = new Document("_id", "ticketId");
Document increase = new Document("seq", 1);
Document update = new Document("$inc", increase);
Document result = countersCollection.findOneAndUpdate(find, update);
return result.getInteger("seq");
}
/**
* @return mongo client
*/
public MongoClient getMongoClient() {
return mongoClient;
}
/**
*
* @return mongo database
*/
public MongoDatabase getMongoDatabase() {
return database;
}
/**
*
* @return tickets collection
*/
public MongoCollection<Document> getTicketsCollection() {
return ticketsCollection;
}
/**
*
* @return counters collection
*/
public MongoCollection<Document> getCountersCollection() {
return countersCollection;
}
@Override
public Optional<LotteryTicket> findById(LotteryTicketId id) {
Document find = new Document("ticketId", id.getId());
ArrayList<Document> results = ticketsCollection.find(find).limit(1).into(new ArrayList<Document>());
if (results.size() > 0) {
LotteryTicket lotteryTicket = docToTicket(results.get(0));
return Optional.of(lotteryTicket);
} else {
return Optional.empty();
}
}
@Override
public Optional<LotteryTicketId> save(LotteryTicket ticket) {
int ticketId = getNextId();
Document doc = new Document("ticketId", ticketId);
doc.put("email", ticket.getPlayerDetails().getEmail());
doc.put("bank", ticket.getPlayerDetails().getBankAccount());
doc.put("phone", ticket.getPlayerDetails().getPhoneNumber());
doc.put("numbers", ticket.getNumbers().getNumbersAsString());
ticketsCollection.insertOne(doc);
return Optional.of(new LotteryTicketId(ticketId));
}
@Override
public Map<LotteryTicketId, LotteryTicket> findAll() {
Map<LotteryTicketId, LotteryTicket> map = new HashMap<>();
ArrayList<Document> docs = ticketsCollection.find(new Document()).into(new ArrayList<Document>());
for (Document doc: docs) {
LotteryTicket lotteryTicket = docToTicket(doc);
map.put(lotteryTicket.getId(), lotteryTicket);
}
return map;
}
@Override
public void deleteAll() {
ticketsCollection.deleteMany(new Document());
}
private LotteryTicket docToTicket(Document doc) {
PlayerDetails playerDetails = PlayerDetails.create(doc.getString("email"), doc.getString("bank"),
doc.getString("phone"));
int[] numArray = Arrays.asList(doc.getString("numbers").split(",")).stream().mapToInt(Integer::parseInt).toArray();
HashSet<Integer> numbers = new HashSet<>();
for (int num: numArray) {
numbers.add(num);
}
LotteryNumbers lotteryNumbers = LotteryNumbers.create(numbers);
return LotteryTicket.create(new LotteryTicketId(doc.getInteger("ticketId")), playerDetails, lotteryNumbers);
}
}

View File

@ -22,8 +22,10 @@
*/
package com.iluwatar.hexagonal.domain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.PrimitiveIterator;
import java.util.Random;
import java.util.Set;
@ -78,6 +80,22 @@ public class LotteryNumbers {
public Set<Integer> getNumbers() {
return Collections.unmodifiableSet(numbers);
}
/**
* @return numbers as comma separated string
*/
public String getNumbersAsString() {
List<Integer> list = new ArrayList<>();
list.addAll(numbers);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < NUM_NUMBERS; i++) {
builder.append(list.get(i));
if (i < NUM_NUMBERS - 1) {
builder.append(",");
}
}
return builder.toString();
}
/**
* Generates 4 unique random numbers between 1-20 into numbers set.

View File

@ -29,13 +29,15 @@ package com.iluwatar.hexagonal.domain;
*/
public class LotteryTicket {
private LotteryTicketId id;
private final PlayerDetails playerDetails;
private final LotteryNumbers lotteryNumbers;
/**
* Constructor.
*/
private LotteryTicket(PlayerDetails details, LotteryNumbers numbers) {
private LotteryTicket(LotteryTicketId id, PlayerDetails details, LotteryNumbers numbers) {
this.id = id;
playerDetails = details;
lotteryNumbers = numbers;
}
@ -43,8 +45,8 @@ public class LotteryTicket {
/**
* Factory for creating lottery tickets;
*/
public static LotteryTicket create(PlayerDetails details, LotteryNumbers numbers) {
return new LotteryTicket(details, numbers);
public static LotteryTicket create(LotteryTicketId id, PlayerDetails details, LotteryNumbers numbers) {
return new LotteryTicket(id, details, numbers);
}
/**
@ -61,6 +63,20 @@ public class LotteryTicket {
return lotteryNumbers;
}
/**
* @return id
*/
public LotteryTicketId getId() {
return id;
}
/**
* set id
*/
public void setId(LotteryTicketId id) {
this.id = id;
}
@Override
public String toString() {
return playerDetails.toString() + " " + lotteryNumbers.toString();

View File

@ -25,8 +25,8 @@ package com.iluwatar.hexagonal.module;
import com.google.inject.AbstractModule;
import com.iluwatar.hexagonal.banking.InMemoryBank;
import com.iluwatar.hexagonal.banking.WireTransfers;
import com.iluwatar.hexagonal.database.InMemoryTicketRepository;
import com.iluwatar.hexagonal.database.LotteryTicketRepository;
import com.iluwatar.hexagonal.database.MongoTicketRepository;
import com.iluwatar.hexagonal.notifications.LotteryNotifications;
import com.iluwatar.hexagonal.notifications.StdOutNotifications;
@ -36,7 +36,7 @@ import com.iluwatar.hexagonal.notifications.StdOutNotifications;
public class LotteryModule extends AbstractModule {
@Override
protected void configure() {
bind(LotteryTicketRepository.class).to(InMemoryTicketRepository.class);
bind(LotteryTicketRepository.class).to(MongoTicketRepository.class);
bind(LotteryNotifications.class).to(StdOutNotifications.class);
bind(WireTransfers.class).to(InMemoryBank.class);
}

View File

@ -27,6 +27,7 @@ import com.iluwatar.hexagonal.domain.LotteryConstants;
import com.iluwatar.hexagonal.domain.LotteryNumbers;
import com.iluwatar.hexagonal.domain.LotteryService;
import com.iluwatar.hexagonal.domain.LotteryTicket;
import com.iluwatar.hexagonal.domain.LotteryTicketId;
import com.iluwatar.hexagonal.domain.PlayerDetails;
import java.util.ArrayList;
@ -94,7 +95,8 @@ public class SampleData {
*/
public static void submitTickets(LotteryService lotteryService, int numTickets) {
for (int i = 0; i < numTickets; i++) {
LotteryTicket ticket = LotteryTicket.create(getRandomPlayerDetails(), LotteryNumbers.createRandom());
LotteryTicket ticket = LotteryTicket.create(new LotteryTicketId(),
getRandomPlayerDetails(), LotteryNumbers.createRandom());
lotteryService.submitTicket(ticket);
}
}

View File

@ -83,7 +83,7 @@ public class ConsoleLottery {
chosen.add(Integer.parseInt(parts[i]));
}
LotteryNumbers lotteryNumbers = LotteryNumbers.create(chosen);
LotteryTicket lotteryTicket = LotteryTicket.create(details, lotteryNumbers);
LotteryTicket lotteryTicket = LotteryTicket.create(new LotteryTicketId(), details, lotteryNumbers);
Optional<LotteryTicketId> id = service.submitTicket(lotteryTicket);
if (id.isPresent()) {
System.out.println("Submitted lottery ticket with id: " + id.get());