Files
ophelias-oasis/OpheliasOasis/Managers/DatabaseManager.cs
雲華 349589674f Manager classes added and model updates
This commit introduces a large number of changes. Namely there are a
number of additions to a new set of classes that manage the database
and/ or the models shared between the code and databse. There is
fragmented non-functional code in this commit and there may be debug/
old code that still needs to be removed. This commit is just to version
these changes as they were not commited previously. There is also some
console interface code written, but has next to no functionality
attached to any existing prompts. More details will be published per .cs
file, ie specific manager or model, once they are finished as they are
undergoing rapid and significant changes regularly.
2022-04-11 23:40:37 -04:00

213 lines
8.3 KiB
C#

using System.Data.SQLite;
using Ophelias.Models;
namespace Ophelias.Managers
{
internal class DatabaseManager : IDisposable
{
internal SQLiteConnection con = new SQLiteConnection("OpheliasOasis.sqlite");
internal SQLiteCommand? cur;
internal DatabaseManager()
{
cur = new SQLiteCommand(con);
}
internal void Connect()
{
con.Open();
}
internal void Close()
{
con.Close();
con.Dispose();
}
internal void InitializeTables()
{
if (cur == null)
return;
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,
[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);
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 NULL,
[Phone] TEXT NULL,
[CreditCard] TEXT NULL,
[CCV] TEXT NULL,
[CCExpiration] TEXT NULL);
CREATE TABLE IF NOT EXISTS [rooms] (
[ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT);
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();
}
internal void InitializeRoomsTable()
{
if (cur == null)
return;
// Initialize Rooms
for (int i = 1; i < 46; i++)
{
cur.CommandText = $"INSERT INTO ROOMS (ID) VALUES ({i});";
cur.ExecuteNonQuery();
}
}
public void Dispose()
{
Close();
}
}
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)
{
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 (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;
}
}
}