diff --git a/src/main/java/billgates/database/MySQLDatabaseGateway.java b/src/main/java/billgates/database/MySQLDatabaseGateway.java index 4a56654..d3a3531 100644 --- a/src/main/java/billgates/database/MySQLDatabaseGateway.java +++ b/src/main/java/billgates/database/MySQLDatabaseGateway.java @@ -34,6 +34,8 @@ public MySQLDatabaseGateway() { this.columnToDatabaseColumn.put("To", "to"); this.columnToDatabaseColumn.put("Location", "location"); this.columnToDatabaseColumn.put("Splitter", "splitter"); + this.columnToDatabaseColumn.put("Payee", "payee"); + this.columnToDatabaseColumn.put("Paid Back", "paid_back"); } public void initializeConnection() { @@ -78,7 +80,8 @@ public QueryUserData getUserData(String username) { user = new QueryUserData(userID, billID, username, password); } else { - throw new RuntimeException(username + " does not exist in the database!"); + // Returns null back to the caller to denote that the user wasn't found + return null; } } catch (SQLException e) { @@ -130,6 +133,34 @@ public QueryBillData getBillData(int billId) { return getBillData(billId, start, end); } + @Override + public QuerySplitBillData getSplitBillData(int splitBillId) { + List entries = new ArrayList<>(); + + try { + Statement statement = connection.createStatement(); + + String query; + + query = String.format(""" + SELECT * FROM bill_%d_%d + ORDER BY + entry_id ASC + """, this.userId, splitBillId); + + ResultSet resultSet = statement.executeQuery(query); + + while (resultSet.next()) { + entries.add(getSplitEntryData(splitBillId, resultSet.getInt("entry_id"))); + } + + } catch (SQLException e) { + throw new RuntimeException(e); + } + + return new QuerySplitBillData(splitBillId, entries); + } + @Override public QueryBillData getBillData(int billId, ZonedDateTime startDate, ZonedDateTime endDate) { List entries = new ArrayList<>(); @@ -140,23 +171,12 @@ public QueryBillData getBillData(int billId, ZonedDateTime startDate, ZonedDateT String query; - // If equal, we are getting the bill corresponding to this user - if (this.userId == billId) { - query = String.format(""" - SELECT * FROM bill_%d - WHERE date BETWEEN CAST('%s' AS DATETIME) AND CAST('%s' AS DATETIME) - ORDER BY date DESC, - entry_id ASC - """, billId, startDate.format(formatter), endDate.format(formatter)); - } else { - // If not equal, we must have that we are getting a splitterBill - query = String.format(""" - SELECT * FROM bill_%d_%d - WHERE date BETWEEN CAST('%s' AS DATETIME) AND CAST('%s' AS DATETIME) - ORDER BY date DESC, - entry_id ASC - """, this.userId, billId, startDate.format(formatter), endDate.format(formatter)); - } + query = String.format(""" + SELECT * FROM bill_%d + WHERE date BETWEEN CAST('%s' AS DATETIME) AND CAST('%s' AS DATETIME) + ORDER BY date DESC, + entry_id ASC + """, billId, startDate.format(formatter), endDate.format(formatter)); ResultSet resultSet = statement.executeQuery(query); @@ -173,34 +193,27 @@ WHERE date BETWEEN CAST('%s' AS DATETIME) AND CAST('%s' AS DATETIME) @Override public QueryEntryData getEntryData(int billId, int entryId) { - int splitBillId = -1; - double value = 0.0; - String currency = ""; - String description = ""; - String from = ""; - String to = ""; - String location = ""; - ZonedDateTime zDate = ZonedDateTime.now(); + int splitBillId; + double value; + String currency; + String description; + String from; + String to; + String location; + ZonedDateTime zDate; try { Statement statement = connection.createStatement(); String query; // If equal, we are getting the bill corresponding to this user - if (this.userId == billId) { - query = String.format(""" - SELECT * FROM bill_%d WHERE entry_id = %d - """, billId, entryId); - } else { - // If not equal, we are getting the entry from the split bill - query = String.format(""" - SELECT * FROM bill_%d_%d WHERE entry_id = %d - """, this.userId, billId, entryId); - } + query = String.format(""" + SELECT * FROM bill_%d WHERE entry_id = %d + """, billId, entryId); ResultSet resultSet = statement.executeQuery(query); - while (resultSet.next()) { + if (resultSet.next()) { // Note that, aside from the general types that we have here // All the rest objects will be parsed in a string format value = resultSet.getDouble("value"); @@ -216,6 +229,9 @@ public QueryEntryData getEntryData(int billId, int entryId) { // We can pass in the different zones we want to convert in, and we can obtain the value we want zDate = ZonedDateTime.ofInstant(i, ZoneId.systemDefault()); + } else { + // Return null to denote that we didn't find any entry with this ID in the bill + return null; } } catch (SQLException e) { @@ -233,6 +249,67 @@ public QueryEntryData getEntryData(int billId, int entryId) { splitBillId); } + @Override + public QuerySplitEntryData getSplitEntryData(int billId, int entryId) { + double value; + String currency; + String description; + String from; + String to; + String location; + ZonedDateTime zDate; + String payee; + boolean isPaidBack; + try { + Statement statement = connection.createStatement(); + + String query; + + // If equal, we are getting the bill corresponding to this user + query = String.format(""" + SELECT * FROM bill_%d_%d WHERE entry_id = %d + """, this.userId, billId, entryId); + + ResultSet resultSet = statement.executeQuery(query); + + if (resultSet.next()) { + // Note that, aside from the general types that we have here + // All the rest objects will be parsed in a string format + value = resultSet.getDouble("value"); + Timestamp date = resultSet.getTimestamp("date"); + currency = resultSet.getString("currency"); + description = resultSet.getString("description"); + from = resultSet.getString("from"); + to = resultSet.getString("to"); + location = resultSet.getString("location"); + payee = resultSet.getString("payee"); + isPaidBack = resultSet.getBoolean("paid_back"); + + Instant i = Instant.ofEpochMilli(date.getTime()); + + // We can pass in the different zones we want to convert in, and we can obtain the value we want + zDate = ZonedDateTime.ofInstant(i, ZoneId.systemDefault()); + } else { + // Return null to denote that we didn't find any entry with this ID in the bill + return null; + } + + } catch (SQLException e) { + throw new RuntimeException(e); + } + + return new QuerySplitEntryData(entryId, + zDate, + value, + currency, + description, + from, + to, + location, + payee, + isPaidBack); + } + @Override public void insertEntry(int billId, QueryEntryData entry) { try { @@ -244,78 +321,92 @@ public void insertEntry(int billId, QueryEntryData entry) { // This is the case where we don't want autoincrement if (!(entry.getId() == -1)) { - // If equal, we are inserting the entry into the user's bill - if (this.userId == billId) { - query = String.format(""" - INSERT INTO bill_%d (entry_id, value, date, currency, description, `from`, `to`, location, split_bill_id) VALUE ( - %d, %f, "%s", "%s", "%s", "%s", "%s", "%s", %d - ) - """, - billId, - entry.getId(), - entry.getValue(), - entry.getDate().format(formatter), - entry.getCurrency(), - entry.getDescription(), - entry.getFrom(), - entry.getTo(), - entry.getLocation(), - entry.getSplitBillId()); - } else { - // If not equal, we are inserting the entry into the split bill - query = String.format(""" - INSERT INTO bill_%d%d (entry_id, value, date, currency, description, `from`, `to`, location, split_bill_id) VALUE ( - %d, %f, "%s", "%s", "%s", "%s", "%s", "%s", %d - ) - """, - this.userId, - billId, - entry.getId(), - entry.getValue(), - entry.getDate().format(formatter), - entry.getCurrency(), - entry.getDescription(), - entry.getFrom(), - entry.getTo(), - entry.getLocation(), - entry.getSplitBillId()); - } + query = String.format(""" + INSERT INTO bill_%d (entry_id, value, date, currency, description, `from`, `to`, location, split_bill_id) VALUE ( + %d, %f, "%s", "%s", "%s", "%s", "%s", "%s", %d + ) + """, + billId, + entry.getId(), + entry.getValue(), + entry.getDate().format(formatter), + entry.getCurrency(), + entry.getDescription(), + entry.getFrom(), + entry.getTo(), + entry.getLocation(), + entry.getSplitBillId()); } else { // This is the case where we want auto increment + query = String.format(""" + INSERT INTO bill_%d (value, date, currency, description, `from`, `to`, location, split_bill_id) VALUE ( + %f, "%s", "%s", "%s", "%s", "%s", "%s", %d + ) + """, + billId, + entry.getValue(), + entry.getDate().format(formatter), + entry.getCurrency(), + entry.getDescription(), + entry.getFrom(), + entry.getTo(), + entry.getLocation(), + entry.getSplitBillId()); + } - if (this.userId == billId) { - query = String.format(""" - INSERT INTO bill_%d (value, date, currency, description, `from`, `to`, location, split_bill_id) VALUE ( - %f, "%s", "%s", "%s", "%s", "%s", "%s", %d - ) - """, - billId, - entry.getValue(), - entry.getDate().format(formatter), - entry.getCurrency(), - entry.getDescription(), - entry.getFrom(), - entry.getTo(), - entry.getLocation(), - entry.getSplitBillId()); - } else { - // If not equal, we are inserting the entry into the split bill - query = String.format(""" - INSERT INTO bill_%d_%d (value, date, currency, description, `from`, `to`, location, split_bill_id) VALUE ( - %f, "%s", "%s", "%s", "%s", "%s", "%s", %d - ) - """, - this.userId, - billId, - entry.getValue(), - entry.getDate().format(formatter), - entry.getCurrency(), - entry.getDescription(), - entry.getFrom(), - entry.getTo(), - entry.getLocation(), - entry.getSplitBillId()); - } + statement.execute(query); + + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + public void insertSplitEntry(int billId, QuerySplitEntryData entry) { + try { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + Statement statement = connection.createStatement(); + + String query; + + // This is the case where we don't want autoincrement + if (!(entry.getId() == -1)) { + query = String.format(""" + INSERT INTO bill_%d_%d (entry_id, value, date, currency, description, `from`, `to`, location, payee, paid_back) VALUE ( + %d, %f, "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s" + ) + """, + this.userId, + billId, + entry.getId(), + entry.getValue(), + entry.getDate().format(formatter), + entry.getCurrency(), + entry.getDescription(), + entry.getFrom(), + entry.getTo(), + entry.getLocation(), + entry.getPayee(), + entry.getIsPaidBack()); + } else { + // This is the case where we want auto increment + query = String.format(""" + INSERT INTO bill_%d_%d (value, date, currency, description, `from`, `to`, location, payee, paid_back) VALUE ( + %f, "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s" + ) + """, + this.userId, + billId, + entry.getValue(), + entry.getDate().format(formatter), + entry.getCurrency(), + entry.getDescription(), + entry.getFrom(), + entry.getTo(), + entry.getLocation(), + entry.getPayee(), + entry.getIsPaidBack()); } statement.execute(query); @@ -372,17 +463,27 @@ public void deleteEntry(int billId, int entryId) { String query; - // If equal, we are deleting the entry from the user's bill - if (this.userId == billId) { - query = String.format(""" - DELETE FROM bill_%d WHERE entry_id = %d - """, billId, entryId); - } else { - // If not equal, we are deleting the entry from the user's split bill - query = String.format(""" - DELETE FROM bill_%d_%d WHERE entry_id = %d - """, this.userId, billId, entryId); - } + query = String.format(""" + DELETE FROM bill_%d WHERE entry_id = %d + """, billId, entryId); + + statement.execute(query); + + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + public void deleteSplitEntry(int billId, int entryId) { + try { + Statement statement = connection.createStatement(); + + String query; + + query = String.format(""" + DELETE FROM bill_%d_%d WHERE entry_id = %d + """, this.userId, billId, entryId); statement.execute(query); @@ -403,27 +504,39 @@ public void modifyEntry(int billId, int entryId, String column, String newValue) String query; // If equal, we are modifying the entry from the user's bill - if (this.userId == billId) { - query = String.format(""" - UPDATE bill_%d - SET %s = "%s" - WHERE entry_id = %d - """, billId, - this.columnToDatabaseColumn.get(column), - newValue, - entryId); - } else { - // If not equal, we are modifying the entry from the user's split bill - query = String.format(""" - UPDATE bill_%d_%d - SET %s = "%s" - WHERE entry_id = %d - """, this.userId, - billId, - this.columnToDatabaseColumn.get(column), - newValue, - entryId); - } + query = String.format(""" + UPDATE bill_%d + SET %s = "%s" + WHERE entry_id = %d + """, billId, + this.columnToDatabaseColumn.get(column), + newValue, + entryId); + + statement.execute(query); + + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + public void modifySplitEntry(int billId, int entryId, String column, String newValue) { + try { + Statement statement = connection.createStatement(); + + String query; + + // If equal, we are modifying the entry from the user's bill + query = String.format(""" + UPDATE bill_%d_%d + SET %s = "%s" + WHERE entry_id = %d + """, this.userId, + billId, + this.columnToDatabaseColumn.get(column), + newValue, + entryId); statement.execute(query); @@ -441,50 +554,68 @@ public void modifyEntry(int billId, QueryEntryData entry) { String query; - // If equal, we are modifying the entry from the user's bill - if (this.userId == billId) { - query = String.format(""" - UPDATE bill_%d - SET value = %f, - date = "%s", - currency = "%s", - description = "%s", - `from` = "%s", - `to` = "%s", - location = "%s" - WHERE entry_id = %d - """, billId, - entry.getValue(), - entry.getDate().format(formatter), - entry.getCurrency(), - entry.getDescription(), - entry.getFrom(), - entry.getTo(), - entry.getLocation(), - entry.getId()); - } else { - // If not equal, we are modifying the entry from the user's split bill - query = String.format(""" - UPDATE bill_%d_%d - SET value = %f, - date = "%s", - currency = "%s", - description = "%s", - `from` = "%s", - `to` = "%s", - location = "%s" - WHERE entry_id = %d - """, this.userId, - billId, - entry.getValue(), - entry.getDate().format(formatter), - entry.getCurrency(), - entry.getDescription(), - entry.getFrom(), - entry.getTo(), - entry.getLocation(), - entry.getId()); - } + query = String.format(""" + UPDATE bill_%d + SET value = %f, + date = "%s", + currency = "%s", + description = "%s", + `from` = "%s", + `to` = "%s", + location = "%s", + split_bill_id = %d + WHERE entry_id = %d + """, billId, + entry.getValue(), + entry.getDate().format(formatter), + entry.getCurrency(), + entry.getDescription(), + entry.getFrom(), + entry.getTo(), + entry.getLocation(), + entry.getSplitBillId(), + entry.getId()); + + statement.execute(query); + + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + public void modifySplitEntry(int billId, QuerySplitEntryData entry) { + try { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + Statement statement = connection.createStatement(); + + String query; + + query = String.format(""" + UPDATE bill_%d_%d + SET value = %f, + date = "%s", + currency = "%s", + description = "%s", + `from` = "%s", + `to` = "%s", + location = "%s", + payee = "%s", + paid_back = "%s" + WHERE entry_id = %d + """, this.userId, + billId, + entry.getValue(), + entry.getDate().format(formatter), + entry.getCurrency(), + entry.getDescription(), + entry.getFrom(), + entry.getTo(), + entry.getLocation(), + entry.getPayee(), + entry.getIsPaidBack(), + entry.getId()); statement.execute(query); @@ -510,7 +641,7 @@ currency CHAR(3) NOT NULL, `from` TEXT NOT NULL, `to` TEXT NOT NULL, location TEXT NOT NULL, - split_bill_id INT NOT NULL + split_bill_id INT DEFAULT -1 NOT NULL ) """, billId); diff --git a/src/main/java/billgates/database/QueryEntry.java b/src/main/java/billgates/database/QueryEntry.java new file mode 100644 index 0000000..f1ff08f --- /dev/null +++ b/src/main/java/billgates/database/QueryEntry.java @@ -0,0 +1,125 @@ +package billgates.database; + +import billgates.entities.EntryBuilder; + +import java.time.ZonedDateTime; +import java.time.temporal.ChronoUnit; + +public class QueryEntry { + +/** + * Clean Architecture Layer: Application Business Rules + * This is the class for the general Entry + * @author Ray + */ + private int id = -1; + private final ZonedDateTime date; + private final double value; + private String currency = ""; + private String description = ""; + private String from = ""; + private String to = ""; + private String location = ""; + + public QueryEntry(int id, + ZonedDateTime date, + double value, + String currency, + String description, + String from, + String to, + String location) { + this.id = id; + this.date = date.truncatedTo(ChronoUnit.SECONDS); + this.value = value; + this.currency = currency; + this.description = description; + this.from = from; + this.to = to; + this.location = location; + } + + public QueryEntry(ZonedDateTime date, + double value, + String currency, + String description, + String from, + String to, + String location) { + this.date = date.truncatedTo(ChronoUnit.SECONDS); + this.value = value; + this.currency = currency; + this.description = description; + this.from = from; + this.to = to; + this.location = location; + } + + public QueryEntry(int id, + ZonedDateTime date, + double value) { + // Constructor for not all values provided + this.id = id; + this.date = date.truncatedTo(ChronoUnit.SECONDS); + this.value = value; + } + + public QueryEntry(ZonedDateTime date, + double value) { + // Constructor for not all values provided, with no ID provided + this.date = date.truncatedTo(ChronoUnit.SECONDS); + this.value = value; + } + + public EntryBuilder toEntryBuilder() { + return new EntryBuilder() + .setId(this.id) + .setDate(this.date) + .setValue(this.value) + .setCurrency(this.currency) + .setDescription(this.description) + .setFrom(this.from) + .setTo(this.to) + .setLocation(this.location); + } + + public int getId() { return id; } + + public ZonedDateTime getDate() { + return date; + } + + public double getValue() { + return value; + } + + public String getCurrency() { + return currency; + } + + public String getDescription() { + return description; + } + + public String getFrom() { + return from; + } + + public String getTo() { + return to; + } + + public String getLocation() { + return location; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || this.getClass() != obj.getClass()) return false; + + billgates.database.QueryEntry entry = (billgates.database.QueryEntry) obj; + return this.getId() == entry.getId(); + } + +} \ No newline at end of file diff --git a/src/main/java/billgates/database/QueryEntryData.java b/src/main/java/billgates/database/QueryEntryData.java index 1956a1a..bb55805 100644 --- a/src/main/java/billgates/database/QueryEntryData.java +++ b/src/main/java/billgates/database/QueryEntryData.java @@ -3,22 +3,13 @@ import billgates.entities.EntryBuilder; import java.time.ZonedDateTime; -import java.time.temporal.ChronoUnit; /** * Clean Architecture Layer: Application Business Rules * * @author Ray */ -public class QueryEntryData { - private int id = -1; - private final ZonedDateTime date; - private final double value; - private String currency = ""; - private String description = ""; - private String from = ""; - private String to = ""; - private String location = ""; +public class QueryEntryData extends QueryEntry{ private int splitBillId = -1; public QueryEntryData(int id, @@ -30,14 +21,7 @@ public QueryEntryData(int id, String to, String location, int splitBillId) { - this.id = id; - this.date = date.truncatedTo(ChronoUnit.SECONDS); - this.value = value; - this.currency = currency; - this.description = description; - this.from = from; - this.to = to; - this.location = location; + super(id, date, value, currency, description, from, to, location); this.splitBillId = splitBillId; } @@ -49,13 +33,7 @@ public QueryEntryData(ZonedDateTime date, String to, String location, int splitBillId) { - this.date = date.truncatedTo(ChronoUnit.SECONDS); - this.value = value; - this.currency = currency; - this.description = description; - this.from = from; - this.to = to; - this.location = location; + super(date, value, currency, description, from, to, location); this.splitBillId = splitBillId; } @@ -63,70 +41,30 @@ public QueryEntryData(int id, ZonedDateTime date, double value) { // Constructor for not all values provided - this.id = id; - this.date = date.truncatedTo(ChronoUnit.SECONDS); - this.value = value; + super(id, date, value); } public QueryEntryData(ZonedDateTime date, double value) { // Constructor for not all values provided, with no ID provided - this.date = date.truncatedTo(ChronoUnit.SECONDS); - this.value = value; + super(date, value); } public EntryBuilder toEntryBuilder() { return new EntryBuilder() - .setId(this.id) - .setDate(this.date) - .setValue(this.value) - .setCurrency(this.currency) - .setDescription(this.description) - .setFrom(this.from) - .setTo(this.to) - .setLocation(this.location); + .setId(this.getId()) + .setDate(this.getDate()) + .setValue(this.getValue()) + .setCurrency(this.getCurrency()) + .setDescription(this.getDescription()) + .setFrom(this.getFrom()) + .setTo(this.getTo()) + .setLocation(this.getLocation()) + .setSplitterBillId(this.getSplitBillId()); } - public int getId() { return id; } - - public ZonedDateTime getDate() { - return date; - } - - public double getValue() { - return value; - } - - public String getCurrency() { - return currency; - } - - public String getDescription() { - return description; - } - - public String getFrom() { - return from; - } - - public String getTo() { - return to; - } - - public String getLocation() { - return location; - } public int getSplitBillId() { return splitBillId; } - @Override - public boolean equals(Object obj) { - if (this == obj) return true; - if (obj == null || this.getClass() != obj.getClass()) return false; - - QueryEntryData entry = (QueryEntryData) obj; - return this.getId() == entry.getId(); - } - } diff --git a/src/main/java/billgates/database/QuerySplitBillData.java b/src/main/java/billgates/database/QuerySplitBillData.java new file mode 100644 index 0000000..6818685 --- /dev/null +++ b/src/main/java/billgates/database/QuerySplitBillData.java @@ -0,0 +1,26 @@ +package billgates.database; + +import java.util.List; + +/** + * Clean Architecture Layer: Application Business Rules + * + * @author Ray, Scott + */ +public class QuerySplitBillData { + private final int splitBillId; + private final List splitEntries; + + public QuerySplitBillData(int billId, List splitEntries) { + this.splitBillId = billId; + this.splitEntries = splitEntries; + } + + public int getBillId() { + return splitBillId; + } + + public List getEntries() { + return splitEntries; + } +} diff --git a/src/main/java/billgates/database/QuerySplitEntryData.java b/src/main/java/billgates/database/QuerySplitEntryData.java new file mode 100644 index 0000000..d0b2847 --- /dev/null +++ b/src/main/java/billgates/database/QuerySplitEntryData.java @@ -0,0 +1,83 @@ +package billgates.database; + +import billgates.entities.EntryBuilder; + +import java.time.ZonedDateTime; + +/** + * Clean Architecture Layer: Application Business Rules + * + * Note: The entry_id that is in this case is the entry_id of the splitter bill + * IT IS NOT THE SAME AS THE ORIGINAL ENTRY THAT WE SPLITTED ON + * @author Ray + */ +public class QuerySplitEntryData extends QueryEntry{ + private String payee = ""; + private boolean isPaidBack = false; + + public QuerySplitEntryData(int id, + ZonedDateTime date, + double value, + String currency, + String description, + String from, + String to, + String location, + String payee, + boolean isPaidBack) { + super(id, date, value, currency, description, from, to, location); + this.payee = payee; + this.isPaidBack = isPaidBack; + + } + + public QuerySplitEntryData(ZonedDateTime date, + double value, + String currency, + String description, + String from, + String to, + String location, + String payee, + boolean isPaidBack) { + super(date, value, currency, description, from, to, location); + this.payee = payee; + this.isPaidBack = isPaidBack; + } + + public QuerySplitEntryData(int id, + ZonedDateTime date, + double value) { + // Constructor for not all values provided + super(id, date, value); + } + + public QuerySplitEntryData(ZonedDateTime date, + double value) { + // Constructor for not all values provided, with no ID provided + super(date, value); + } + + public String getPayee() { + return this.payee; + } + + public boolean getIsPaidBack() { + return this.isPaidBack; + } + + public EntryBuilder toEntryBuilder() { + return new EntryBuilder() + .setId(this.getId()) + .setDate(this.getDate()) + .setValue(this.getValue()) + .setCurrency(this.getCurrency()) + .setDescription(this.getDescription()) + .setFrom(this.getFrom()) + .setTo(this.getTo()) + .setLocation(this.getLocation()) + .setPayee(this.getPayee()) + .setIsPaidBack(this.getIsPaidBack()); + } + +} diff --git a/src/main/java/billgates/interface_adapters/DatabaseGateway.java b/src/main/java/billgates/interface_adapters/DatabaseGateway.java index 269435b..db5257c 100644 --- a/src/main/java/billgates/interface_adapters/DatabaseGateway.java +++ b/src/main/java/billgates/interface_adapters/DatabaseGateway.java @@ -1,8 +1,6 @@ package billgates.interface_adapters; -import billgates.database.QueryBillData; -import billgates.database.QueryEntryData; -import billgates.database.QueryUserData; +import billgates.database.*; import java.time.ZonedDateTime; import java.util.List; @@ -26,6 +24,7 @@ public interface DatabaseGateway { * * @param username the username of the user that we wish to obtain the information from * @return a class that contains the user information queried + * returns null if the username being queried doesn't exist in users * @see QueryUserData */ QueryUserData getUserData(String username); @@ -48,6 +47,15 @@ public interface DatabaseGateway { */ QueryBillData getBillData(int billId); + /** + * Obtains information from the whole Split Bill table associated with splitBillId + * + * @param splitBillId the splitBillId that we wish to query + * @return a QuerySplitBillData that encapsulates all the QuerySplitEntryData within it + * @see QuerySplitBillData + */ + QuerySplitBillData getSplitBillData(int splitBillId); + /** * Obtains information from the whole bill table associated with the billId * with a specific date range @@ -61,26 +69,49 @@ public interface DatabaseGateway { QueryBillData getBillData(int billId, ZonedDateTime startDate, ZonedDateTime endDate); /** - * Obtains information from a specific entry (using entry_id) - * in a specific bill (using bill_id) - * the result (username, password, userId, billId) is bundled by QueryUserData class + * Obtains information from a specific entry (using entryId) + * in a specific bill (using billId) + * the result information is bundled by QueryEntryData class * * @param billId the bill ID that we are trying to obtain the entry from * @param entryId the entry ID that we are trying to obtain information from * @return a QueryEntryData that contains the information of the entry queried + * returns null if the entry_id being queried doesn't exist in the bill * @see QueryEntryData */ QueryEntryData getEntryData(int billId, int entryId); + /** + * Obtains information from a specific entry (using entryId, THE ID OF THE SPLIT ENTRY) + * in a specific SPLIT bill (using billId) + * the result entry information is bundled by QuerySplitEntryData class + * + * @param billId the bill ID that we are trying to obtain the entry from + * @param entryId the entry ID that we are trying to obtain information from + * @return a QueryEntryData that contains the information of the entry queried + * returns null if the entry_id being queried doesn't exist in the bill + * @see QuerySplitEntryData + */ + QuerySplitEntryData getSplitEntryData(int billId, int entryId); + /** * Inserts the entry into the specified bill (using billId) in the database * * @param billId the bill ID that we are trying to insert the entry into - * @param entry the QueryEntryData that we are trying to insert into + * @param entry the QueryEntryData that we are trying to insert * @see QueryEntryData */ void insertEntry(int billId, QueryEntryData entry); + /** + * Inserts the SPLIT entry into the specified SPLIT bill (using billId) in the database + * + * @param billId the SPLIT bill ID that we are trying to insert the entry into + * @param entry the QuerySplitEntryData that we are trying to insert + * @see QuerySplitEntryData + */ + void insertSplitEntry(int billId, QuerySplitEntryData entry); + /** * Inserts the user into the users table in the database * @@ -97,6 +128,14 @@ public interface DatabaseGateway { */ void deleteEntry(int billId, int entryId); + /** + * Deletes the split entry with SPLIT entryId from the specified SPLIT bill (using billId) + * + * @param billId the id of the SPLIT bill that we are trying to remove the specific entry from + * @param entryId the id of SPLIT entry that we are trying to delete + */ + void deleteSplitEntry(int billId, int entryId); + /** * Modifies the entry with entryId from the specified bill (using billId) * modifies the value in the specified column to be the newValue @@ -108,6 +147,17 @@ public interface DatabaseGateway { */ void modifyEntry(int billId, int entryId, String column, String newValue); + /** + * Modifies the SPLIT entry with SPLIT entryId from the specified bill (using billId) + * modifies the value in the specified column to be the newValue + * + * @param billId the id of the bill that we are trying to modify the specific entry from + * @param entryId the id of entry that we are trying to delete + * @param column the column in the entry that we are trying to modify + * @param newValue the new value that will be overwritten in the column + */ + void modifySplitEntry(int billId, int entryId, String column, String newValue); + /** * Overwrites the entry completely with the entry_id contained in entry * in the bill specified by billId @@ -117,6 +167,15 @@ public interface DatabaseGateway { */ void modifyEntry(int billId, QueryEntryData entry); + /** + * Overwrites the SPLIT entry completely with the SPLIT entry_id contained in entry + * in the bill specified by billId + * + * @param billId the id of the SPLIT bill that we are trying to modify the specific entry from + * @param entry a QuerySplitEntryData object that contains all the information of the new entry + */ + void modifySplitEntry(int billId, QuerySplitEntryData entry); + /** * Creates a bill with the table name "bill_{billId}" in our database * contains columns with types as specified in README.md diff --git a/src/main/resources/GenerateRayTable999.sql b/src/main/resources/GenerateRayTable999.sql new file mode 100644 index 0000000..3a07f0d --- /dev/null +++ b/src/main/resources/GenerateRayTable999.sql @@ -0,0 +1,58 @@ +-- MySQL dump 10.13 Distrib 8.0.31, for macos12.6 (arm64) +-- +-- Host: bill-gates-database.mysql.database.azure.com Database: bill +-- ------------------------------------------------------ +-- Server version 8.0.28 + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!50503 SET NAMES utf8mb4 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +-- +-- Table structure for table `bill_999` +-- + +DROP TABLE IF EXISTS `bill_999`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!50503 SET character_set_client = utf8mb4 */; +CREATE TABLE `bill_999` ( + `entry_id` int NOT NULL AUTO_INCREMENT, + `value` decimal(16,2) NOT NULL, + `date` timestamp NOT NULL, + `currency` varchar(5) NOT NULL, + `description` text NOT NULL, + `from` text NOT NULL, + `to` text NOT NULL, + `location` text NOT NULL, + `split_bill_id` int NOT NULL DEFAULT '-1', + PRIMARY KEY (`entry_id`) +) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `bill_999` +-- + +LOCK TABLES `bill_999` WRITE; +/*!40000 ALTER TABLE `bill_999` DISABLE KEYS */; +INSERT INTO `bill_999` VALUES (1,12.88,'2022-11-22 15:54:40','CAD','Starbucks w/ friends','Credit','Starbucks','College Street',-1),(2,8.24,'2022-11-22 13:04:52','CAD','Burger King - Whopper Meal','Credit','Burger King','College Spadina Intersection',-1),(3,7.73,'2022-11-22 08:22:23','CAD','McDonalds - McGirdles Offer','Credit','McDonalds','470 Yonge',-1),(4,14.80,'2022-11-20 16:37:55','CAD','KungFu Tea w/ friends','Credit','KungFu Tea','College Street',-1),(5,15.81,'2022-11-18 18:54:54','CAD','KFC Crazy Thursday 疯狂星期四','Credit','KFC','Yonge Metro 附近',-1),(6,14.58,'2022-11-17 21:49:14','CAD','Shoppers - Cookies and Chips','Credit','Shoppers','Yonge Carlton Shoppers',-1),(7,10.00,'2022-11-14 21:02:07','CAD','Charity Donation','Debit','Charity','Online',-1),(8,9.59,'2022-11-02 23:31:14','CAD','Robarts Library - Spaghetti','Credit','トロント大学','Robarts Library',-1),(9,1500.00,'2022-11-01 20:14:58','CAD','House Rent - Magical Place','Debit','Landlord','Online',-1),(10,23.79,'2022-11-03 19:00:25','CAD','The Burgernator - Fully Loaded Set ','Credit','Burgernator','Spadina-ish',-1); +/*!40000 ALTER TABLE `bill_999` ENABLE KEYS */; +UNLOCK TABLES; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2022-11-24 1:02:39