Further major refactoring of code
This is another commit to mostly version the code. There have been a considerable number of changes and there is functionality that I am still determining whether it should lie within the manager or the model class itself. It makes the most sense to possibly add the "Update" or database manipulation functions on the models themselves. On the other hand, instead of creating and generating the ID in the model, the current design is to create the entry in the database first, get the last insert row ID and create a new and complete model that is returned back by the function. This allows us to leverage the autoincrement functionality of the database rather than trying to design a function and/ or make an additional call to the database. **NOTE: Code is non-functional due to some classes not having their errors resolved.
This commit is contained in:
@@ -5,12 +5,11 @@ namespace Ophelias.Managers
|
||||
{
|
||||
internal class DatabaseManager : IDisposable
|
||||
{
|
||||
internal SQLiteConnection con = new SQLiteConnection("OpheliasOasis.sqlite");
|
||||
internal SQLiteCommand? cur;
|
||||
internal SQLiteConnection con = new SQLiteConnection("DataSource=database.sqlite3;Version=3;");
|
||||
|
||||
internal DatabaseManager()
|
||||
{
|
||||
cur = new SQLiteCommand(con);
|
||||
Connect();
|
||||
}
|
||||
internal void Connect()
|
||||
{
|
||||
@@ -23,9 +22,6 @@ namespace Ophelias.Managers
|
||||
}
|
||||
internal void InitializeTables()
|
||||
{
|
||||
if (cur == null)
|
||||
return;
|
||||
|
||||
string tableCommands =
|
||||
@"CREATE TABLE IF NOT EXISTS [transactions] (
|
||||
[ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -34,8 +30,6 @@ namespace Ophelias.Managers
|
||||
[Penalty] INTEGER NULL,
|
||||
[Multiplier] INTEGER NOT NULL,
|
||||
[RefundAmount] INTEGER NOT NULL,
|
||||
[IsLate] BOOLEAN NOT NULL CHECK ([IsLate] IN (0,1)),
|
||||
[PaidOff] BOOLEAN NOT NULL CHECK ([PaidOff] IN (0,1)),
|
||||
[PayBy] TEXT NOT NULL,
|
||||
[PaidOn] TEXT NULL);
|
||||
|
||||
@@ -61,35 +55,40 @@ namespace Ophelias.Managers
|
||||
[ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
[Fname] TEXT NOT NULL,
|
||||
[Lname] TEXT NOT NULL,
|
||||
[Email] TEXT NULL,
|
||||
[Phone] TEXT NULL,
|
||||
[Email] TEXT NOT NULL,
|
||||
[CreditCard] TEXT NULL,
|
||||
[CCV] TEXT NULL,
|
||||
[CCExpiration] TEXT NULL);
|
||||
[Expiration] TEXT NULL,
|
||||
[CCV] TEXT NULL);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS [rooms] (
|
||||
[ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT);
|
||||
[ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
[Occupied] BOOLEAN NOT NULL CHECK ([Occupied] IN (0,1)));
|
||||
|
||||
CREATE TABLE IF NOT EXISTS [rates] (
|
||||
[ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
[Rate] INTEGER NOT NULL,
|
||||
[DateSet] TEXT NOT NULL);";
|
||||
|
||||
cur.CommandText = tableCommands;
|
||||
cur.ExecuteNonQuery();
|
||||
using (SQLiteCommand cmd = con.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = tableCommands;
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
internal void InitializeRoomsTable()
|
||||
{
|
||||
if (cur == null)
|
||||
return;
|
||||
using (SQLiteCommand cmd = con.CreateCommand())
|
||||
{
|
||||
for (int i = 1; i < 46; i++)
|
||||
{
|
||||
cmd.CommandText = $"INSERT INTO ROOMS (Occupied) VALUES (0);";
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize Rooms
|
||||
for (int i = 1; i < 46; i++)
|
||||
{
|
||||
cur.CommandText = $"INSERT INTO ROOMS (ID) VALUES ({i});";
|
||||
cur.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
@@ -99,66 +98,33 @@ namespace Ophelias.Managers
|
||||
|
||||
internal static class DatabaseFunctions
|
||||
{
|
||||
internal static int GetLastId(SQLiteCommand cmd, string tableName)
|
||||
{
|
||||
if (cmd == null)
|
||||
throw new NotImplementedException();
|
||||
|
||||
cmd.CommandText = $"SELECT SEQ FROM sqlite_sequence WHERE name=\"{tableName}\";";
|
||||
cmd.ExecuteNonQuery();
|
||||
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
||||
{
|
||||
reader.Read();
|
||||
return reader.GetInt32(0);
|
||||
}
|
||||
}
|
||||
internal static int GetThirtyDayOccupancy(SQLiteCommand cmd, DateTime start)
|
||||
{
|
||||
if (cmd == null)
|
||||
throw new NotImplementedException();
|
||||
|
||||
cmd.CommandText = $@"SELECT COUNT(*)
|
||||
FROM reservations
|
||||
WHERE StartDate => date({start.ToString("yyyy-MM-dd")}) AND EndDate <= date({start.AddDays(30).ToString("yyyy-MM-dd")});";
|
||||
cmd.ExecuteNonQuery();
|
||||
int thirtyDayOcc;
|
||||
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
||||
{
|
||||
reader.Read();
|
||||
thirtyDayOcc = reader.GetInt32(0);
|
||||
}
|
||||
return thirtyDayOcc;
|
||||
}
|
||||
}
|
||||
internal static class QueryBuilder
|
||||
{
|
||||
|
||||
internal static string? UpdateTransaction(int id,
|
||||
double? rate = null, double? owed = null, double? penalty = null,
|
||||
double? multiplier = null, double? refund = null, bool? isLate = null,
|
||||
bool? paidOff = null, DateTime? paidBy = null, DateTime? paidOn = null)
|
||||
double? Rate = null, double? Owed = null, double? Penalty = null,
|
||||
double? Multiplier = null, double? Refund = null,
|
||||
DateTime? PayBy = null, DateTime? PaidOn = null)
|
||||
{
|
||||
List<string> queryComponents = new List<string>();
|
||||
string query = "UPDATE transactions SET";
|
||||
|
||||
if (rate.HasValue)
|
||||
queryComponents.Add($"Rate = {rate}");
|
||||
if (owed.HasValue)
|
||||
queryComponents.Add($"Owed = {owed}");
|
||||
if (penalty.HasValue)
|
||||
queryComponents.Add($"Penalty = {penalty}");
|
||||
if (multiplier.HasValue)
|
||||
queryComponents.Add($"Multiplier = {multiplier}");
|
||||
if (refund.HasValue)
|
||||
queryComponents.Add($"Refund = {refund}");
|
||||
if (isLate.HasValue)
|
||||
queryComponents.Add($"IsLate = {Convert.ToInt32(isLate)}");
|
||||
if (paidOff.HasValue)
|
||||
queryComponents.Add($"PaidOff = {Convert.ToInt32(paidOff)}");
|
||||
if (paidBy.HasValue)
|
||||
queryComponents.Add($"PaidBy = {paidBy.Value.ToString("yyyy-MM-dd")}");
|
||||
if (paidOn.HasValue)
|
||||
queryComponents.Add($"PaidOn = {paidOn.Value.ToString("yyyy-MM-dd")}");
|
||||
if (Rate.HasValue)
|
||||
queryComponents.Add($"Rate = {Rate}");
|
||||
if (Owed.HasValue)
|
||||
queryComponents.Add($"Owed = {Owed}");
|
||||
if (Penalty.HasValue)
|
||||
queryComponents.Add($"Penalty = {Penalty}");
|
||||
if (Multiplier.HasValue)
|
||||
queryComponents.Add($"Multiplier = {Multiplier}");
|
||||
if (Refund.HasValue)
|
||||
queryComponents.Add($"Refund = {Refund}");
|
||||
if (PayBy.HasValue)
|
||||
queryComponents.Add($"PaidBy = {PayBy.Value.ToString("yyyy-MM-dd")}");
|
||||
if (PaidOn.HasValue)
|
||||
queryComponents.Add($"PaidOn = {PaidOn.Value.ToString("yyyy-MM-dd")}");
|
||||
|
||||
if (queryComponents.Count == 0)
|
||||
query += " " + string.Join(", ", queryComponents) + " " + $"WHERE ID = {id};";
|
||||
@@ -208,5 +174,30 @@ namespace Ophelias.Managers
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
internal static string CreateTransaction(double Rate, double Owed, double Penalty,
|
||||
double Multiplier, double Refund, bool isLate,
|
||||
bool PaidOff, DateTime PaidBy, DateTime? PaidOn = null)
|
||||
{
|
||||
|
||||
return @$"INSERT INTO transactions (Rate, Owed, Penalty, Multiplier, Refund, IsLate, PaidOff, PaidBy, PaidOn)
|
||||
VALUES ({Rate}, {Owed}, {Penalty}, {Multiplier}, {Refund}, {isLate}, {PaidOff}, {PaidBy}, {PaidOn});";
|
||||
}
|
||||
|
||||
internal static string CreateReservation(int roomid, int guestid, int transactionid, bool isnoshow, ReservationType type,
|
||||
ReservationStatus status, DateTime creationdate, DateTime startdate, DateTime enddate,
|
||||
DateTime checkin, DateTime checkout, DateTime datechanged)
|
||||
{
|
||||
return @$"INSERT INTO reservations (RoomID, GuestID, TransactionID, IsNoShow, Type, Status, CreationDate, StartDate, EndDate, CheckIn, CheckOut, DateChanged)
|
||||
VALUES ({roomid}, {guestid}, {transactionid}, {isnoshow}, {status}, {creationdate}, {startdate}, {enddate}, {checkin}, {checkout}, {datechanged});";
|
||||
}
|
||||
|
||||
internal static string CreateGuest(string FirstName, string LastName, string Email, string? CreditCard, string? Expiration, string? CCV)
|
||||
{
|
||||
if (CreditCard != null && Expiration != null && CCV != null)
|
||||
return $@"INSERT INTO guests (Fname, Lname, Email) VALUES ({FirstName}, {LastName}, {Email});";
|
||||
else
|
||||
return $@"INSERT INTO guests (Fname, Lname, Email, CreditCard, Expiration, CCV) VALUES ({FirstName}, {LastName}, {Email}, {CreditCard}, {Expiration}, {CCV});";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
133
OpheliasOasis/Managers/HotelManager.cs
Normal file
133
OpheliasOasis/Managers/HotelManager.cs
Normal file
@@ -0,0 +1,133 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Ophelias.Models;
|
||||
using System.Data.SQLite;
|
||||
|
||||
namespace Ophelias.Managers
|
||||
{
|
||||
internal static class HotelManager
|
||||
{
|
||||
internal static int GetLastId(string tableName)
|
||||
{
|
||||
int lastId = 0;
|
||||
using (DatabaseManager manager = new DatabaseManager())
|
||||
{
|
||||
using (SQLiteCommand cmd = manager.con.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = $"SELECT SEQ FROM sqlite_sequence WHERE name=\"{tableName}\";";
|
||||
cmd.ExecuteNonQuery();
|
||||
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
||||
{
|
||||
reader.Read();
|
||||
lastId = reader.GetInt32(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
return lastId;
|
||||
}
|
||||
internal static int GetThirtyDayOccupancy(DateTime start)
|
||||
{
|
||||
int thirtyDayOcc;
|
||||
using (DatabaseManager Manager = new DatabaseManager())
|
||||
{
|
||||
using (SQLiteCommand cmd = Manager.con.CreateCommand()) {
|
||||
cmd.CommandText = $@"SELECT COUNT(*)
|
||||
FROM reservations
|
||||
WHERE StartDate => date({start.ToString("yyyy-MM-dd")})
|
||||
AND EndDate <= date({start.AddDays(30).ToString("yyyy-MM-dd")});";
|
||||
cmd.ExecuteNonQuery();
|
||||
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
||||
{
|
||||
reader.Read();
|
||||
thirtyDayOcc = reader.GetInt32(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return thirtyDayOcc;
|
||||
}
|
||||
internal static int CreateReservation(int roomid, int guestid, int transactionid, bool isnoshow, ReservationType type,
|
||||
ReservationStatus status, DateTime creationdate, DateTime startdate, DateTime enddate,
|
||||
DateTime checkin, DateTime checkout, DateTime datechanged)
|
||||
{
|
||||
int id;
|
||||
using (DatabaseManager Manager = new DatabaseManager())
|
||||
{
|
||||
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = QueryBuilder.CreateReservation(roomid, guestid, transactionid, isnoshow, type,
|
||||
status, creationdate, startdate, enddate,
|
||||
checkin, checkout, datechanged);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
id = (int)Manager.con.LastInsertRowId;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
internal static void ChangeReservationDates(Reservation r, Transaction t, DateTime start, DateTime end)
|
||||
{
|
||||
using (DatabaseManager Manager = new DatabaseManager())
|
||||
{
|
||||
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
||||
{
|
||||
string? query = QueryBuilder.UpdateReservation(id: r.Id, startdate: start, enddate: end, datechanged: DateTime.Now);
|
||||
|
||||
if (query == null)
|
||||
throw new Exception();
|
||||
|
||||
query += QueryBuilder.UpdateTransaction(id: r.TransactionId, Multiplier: 1.1);
|
||||
cmd.CommandText = query;
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
internal static Transaction CreateTransaction(double Rate, double Owed, double Penalty,
|
||||
double Multiplier, double Refund, bool isLate,
|
||||
bool PaidOff, DateTime PaidBy, DateTime? PaidOn = null)
|
||||
{
|
||||
int Id;
|
||||
using (DatabaseManager Manager = new DatabaseManager())
|
||||
{
|
||||
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = QueryBuilder.CreateTransaction(Rate, Owed, Penalty, Multiplier, Refund, isLate, PaidOff, PaidBy, PaidOn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
Id = (int)Manager.con.LastInsertRowId;
|
||||
}
|
||||
return new Transaction(Id, Rate, Owed, Penalty, Multiplier, Refund, PaidBy, PaidOn);
|
||||
}
|
||||
internal static int CheckInGuest(DateTime CheckIn)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
internal static int CheckOutGuest(DateTime CheckOut)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
internal static Guest CreateGuest(string FirstName, string LastName, string Email, string? CreditCard = null, string? Expiration = null, string? CCV = null)
|
||||
{
|
||||
string query = QueryBuilder.CreateGuest(FirstName, LastName, Email, CreditCard, Expiration, CCV);
|
||||
|
||||
int Id;
|
||||
using (DatabaseManager Manager = new DatabaseManager())
|
||||
{
|
||||
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = QueryBuilder.CreateGuest(FirstName, LastName, Email, CreditCard, Expiration, CCV);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
Id = (int)Manager.con.LastInsertRowId;
|
||||
}
|
||||
|
||||
if (CreditCard != null && Expiration != null && CCV != null)
|
||||
return new Guest(FirstName, LastName, Email, CreditCard, Expiration, CCV);
|
||||
else
|
||||
return new Guest(FirstName, LastName, Email);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,30 +9,8 @@ using System.Data.SQLite;
|
||||
|
||||
namespace Ophelias.Managers
|
||||
{
|
||||
internal class ReservationTableManager
|
||||
internal static class ReservationTableManager
|
||||
{
|
||||
DatabaseManager dbManager;
|
||||
SQLiteCommand? cur;
|
||||
internal ReservationTableManager(DatabaseManager manager)
|
||||
{
|
||||
dbManager = manager;
|
||||
cur = manager.cur;
|
||||
}
|
||||
internal void ChangeReservationDates(Reservation r, DateTime start, DateTime end)
|
||||
{
|
||||
string? query = QueryBuilder.UpdateReservation(id: r.Id, startdate: start, enddate: end, datechanged: DateTime.Now);
|
||||
|
||||
if (query == null)
|
||||
throw new NotImplementedException();
|
||||
|
||||
query += QueryBuilder.UpdateTransaction(id: r.TransactionId, multiplier: 1.1);
|
||||
|
||||
|
||||
QueryBuilder.UpdateTransaction(r.TransactionId);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ namespace Ophelias.Managers
|
||||
internal TransactionTableManager(DatabaseManager manager)
|
||||
{
|
||||
dbManager = manager;
|
||||
cur = manager.cur;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user