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.
204 lines
8.5 KiB
C#
204 lines
8.5 KiB
C#
using System.Data.SQLite;
|
|
using Ophelias.Models;
|
|
|
|
namespace Ophelias.Managers
|
|
{
|
|
internal class DatabaseManager : IDisposable
|
|
{
|
|
internal SQLiteConnection con = new SQLiteConnection("DataSource=database.sqlite3;Version=3;");
|
|
|
|
internal DatabaseManager()
|
|
{
|
|
Connect();
|
|
}
|
|
internal void Connect()
|
|
{
|
|
con.Open();
|
|
}
|
|
internal void Close()
|
|
{
|
|
con.Close();
|
|
con.Dispose();
|
|
}
|
|
internal void InitializeTables()
|
|
{
|
|
string tableCommands =
|
|
@"CREATE TABLE IF NOT EXISTS [transactions] (
|
|
[ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
|
[Rate] INTEGER NOT NULL,
|
|
[Owed] INTEGER NOT NULL,
|
|
[Penalty] INTEGER NULL,
|
|
[Multiplier] INTEGER NOT NULL,
|
|
[RefundAmount] INTEGER NOT NULL,
|
|
[PayBy] TEXT NOT NULL,
|
|
[PaidOn] TEXT NULL);
|
|
|
|
CREATE TABLE IF NOT EXISTS [reservations] (
|
|
[ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
|
[RoomID] INTEGER NOT NULL,
|
|
[GuestID] INTEGER NOT NULL,
|
|
[TransactionID] INTEGER NOT NULL,
|
|
[IsNoShow] BOOLEAN NOT NULL CHECK ([IsNoShow] IN (0,1)),
|
|
[Type] INTEGER NOT NULL CHECK ([Type] IN (0,1,2,3)),
|
|
[Status] INTEGER NOT NULL CHECK ([Status] IN (0,1,2,3)),
|
|
[CreationDate] TEXT NOT NULL,
|
|
[StartDate] TEXT NOT NULL,
|
|
[EndDate] TEXT NOT NULL,
|
|
[CheckIn] TEXT NOT NULL,
|
|
[CheckOut] TEXT NOT NULL,
|
|
[DateChanged] TEXT NOT NULL,
|
|
FOREIGN KEY ([RoomID]) REFERENCES ROOMS(ID),
|
|
FOREIGN KEY ([GuestID]) REFERENCES GUESTS(ID),
|
|
FOREIGN KEY ([TransactionID]) REFERENCES TRANSACTIONS(ID));
|
|
|
|
CREATE TABLE IF NOT EXISTS [guests] (
|
|
[ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
|
[Fname] TEXT NOT NULL,
|
|
[Lname] TEXT NOT NULL,
|
|
[Email] TEXT NOT NULL,
|
|
[CreditCard] TEXT NULL,
|
|
[Expiration] TEXT NULL,
|
|
[CCV] TEXT NULL);
|
|
|
|
CREATE TABLE IF NOT EXISTS [rooms] (
|
|
[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);";
|
|
|
|
using (SQLiteCommand cmd = con.CreateCommand())
|
|
{
|
|
cmd.CommandText = tableCommands;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
|
|
internal void InitializeRoomsTable()
|
|
{
|
|
using (SQLiteCommand cmd = con.CreateCommand())
|
|
{
|
|
for (int i = 1; i < 46; i++)
|
|
{
|
|
cmd.CommandText = $"INSERT INTO ROOMS (Occupied) VALUES (0);";
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
|
|
// Initialize Rooms
|
|
|
|
}
|
|
public void Dispose()
|
|
{
|
|
Close();
|
|
}
|
|
}
|
|
|
|
internal static class DatabaseFunctions
|
|
{
|
|
|
|
}
|
|
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,
|
|
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 (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};";
|
|
else
|
|
return null;
|
|
|
|
return query;
|
|
}
|
|
|
|
internal static string? UpdateReservation(int id,
|
|
int? roomid = null, int? guestid = null, int? transactionid = null, bool? isnoshow = null, ReservationType? type = null,
|
|
ReservationStatus? status = null, DateTime? creationdate = null, DateTime? startdate = null, DateTime? enddate = null,
|
|
DateTime? checkin = null, DateTime? checkout = null, DateTime? datechanged = null)
|
|
{
|
|
List<string> queryComponents = new List<string>();
|
|
string query = "UPDATE reservations SET";
|
|
|
|
if (roomid.HasValue)
|
|
queryComponents.Add($"RoomID = {roomid}");
|
|
if (guestid.HasValue)
|
|
queryComponents.Add($"GuestID = {guestid}");
|
|
if (transactionid.HasValue)
|
|
queryComponents.Add($"TransactionID = {transactionid}");
|
|
if (isnoshow.HasValue)
|
|
queryComponents.Add($"IsNoShow = {Convert.ToInt32(isnoshow)}");
|
|
if (type.HasValue)
|
|
queryComponents.Add($"Type = {(int)type}");
|
|
if (status.HasValue)
|
|
queryComponents.Add($"Type = {(int)status}");
|
|
if (creationdate.HasValue)
|
|
queryComponents.Add($"CreationDate = {creationdate.Value.ToString("yyyy-MM-dd")}");
|
|
if (startdate.HasValue)
|
|
queryComponents.Add($"StartDate = {startdate.Value.ToString("yyyy-MM-dd")}");
|
|
if (enddate.HasValue)
|
|
queryComponents.Add($"EndDate = {enddate.Value.ToString("yyyy-MM-dd")}");
|
|
if (checkin.HasValue)
|
|
queryComponents.Add($"CheckIn = {checkin.Value.ToString("yyyy-MM-dd")}");
|
|
if (checkout.HasValue)
|
|
queryComponents.Add($"CheckOut = {checkout.Value.ToString("yyyy-MM-dd")}");
|
|
if (datechanged.HasValue)
|
|
queryComponents.Add($"CheckOut = {datechanged.Value.ToString("yyyy-MM-dd")}");
|
|
|
|
if (queryComponents.Count == 0)
|
|
query += " " + string.Join(", ", queryComponents) + " " + $"WHERE ID = {id};";
|
|
else
|
|
return null;
|
|
|
|
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});";
|
|
}
|
|
}
|
|
}
|