This commit branch has been muddled with changes that are out of scope and cover more than just the models. Specifically it includes completion of basic guest related models, database access, command line interface integration and preventing SQL injection. Total summary of changes: **IMPORTANT CHANGE: All queries are now parameterized to prevent SQL injections. Prior versions are subject to it since the strings were built. - HotelManager This class is used to provide database operations that are outside of scope from a given model. Furthermore if there is no model to work off of, such as creating a new model based off an existing entry, these functions live outside of the model since it makes no sense for models to contain functions where it would retrieve itself with no context. Realistically this could be placed in an empty constructor and address itself, however, for now this functionality has been moved off the model since it is a one use and is not guaranteed to generate a guest. Some functionality for the coming employee management functions are already in there since they share overlap with the guest module. Specifically GetBaseRate for checking and getting the base rate. - DatabaseManager This class has mostly migrated functionality out or switch to parameterized query generation to prevent SQL injections. - Guest, Reservation, and Transaction Classes Migrated functionality to create the database entry and a new entry of guest upon creation as well as update them. - Program (Terminal app) Fully integrated the guest module. Has not undergone extensive testing but does work as of this commit.
244 lines
11 KiB
C#
244 lines
11 KiB
C#
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=@Table;";
|
|
cmd.ExecuteNonQuery();
|
|
cmd.Parameters.AddWithValue("@Table", tableName);
|
|
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
reader.Read();
|
|
if (reader.HasRows)
|
|
LastId = reader.GetInt32(0);
|
|
}
|
|
}
|
|
}
|
|
return LastId;
|
|
}
|
|
internal static List<int> DailyOccupancies(DateTime Date)
|
|
{
|
|
List<int> Occupancies = new List<int>();
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
cmd.CommandText = $@"SELECT COUNT(*)
|
|
FROM reservations
|
|
WHERE DATE(@Date)
|
|
BETWEEN StartDate AND EndDate;";
|
|
cmd.ExecuteNonQuery();
|
|
cmd.Parameters.AddWithValue("@Date", Date.Date.ToString("yyyy-MM-dd"));
|
|
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
reader.Read();
|
|
if (reader.Read())
|
|
Occupancies.Add(reader.GetInt32(0));
|
|
}
|
|
}
|
|
}
|
|
return Occupancies;
|
|
}
|
|
internal static int AvgOccupancySpan(DateTime Start, DateTime End)
|
|
{
|
|
int thirtyDayOcc = 0;
|
|
int days = (int)(End.Date - Start.Date).TotalDays;
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
for (int i = 0; i < days; i++)
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
cmd.CommandText = $@"SELECT COUNT(*)
|
|
FROM reservations
|
|
WHERE DATE(@Date)
|
|
BETWEEN StartDate AND EndDate;";
|
|
cmd.ExecuteNonQuery();
|
|
cmd.Parameters.AddWithValue("@Date", Start.AddDays(i).Date.ToString("yyyy-MM-dd"));
|
|
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
reader.Read();
|
|
if(reader.HasRows)
|
|
thirtyDayOcc += reader.GetInt32(0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return thirtyDayOcc / days;
|
|
}
|
|
internal static Guest? GetGuestByEmail(string Email)
|
|
{
|
|
Guest? g = null;
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
cmd.CommandText = $"SELECT * FROM guests WHERE Email = @Email";
|
|
cmd.Parameters.AddWithValue("@Email", Email);
|
|
cmd.ExecuteNonQuery();
|
|
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
reader.Read();
|
|
if (reader.HasRows)
|
|
{
|
|
string? CreditCard = null, Expiration = null, CCV = null;
|
|
if (reader[4].GetType() != typeof(DBNull))
|
|
CreditCard = reader[4].ToString();
|
|
if (reader[5].GetType() != typeof(DBNull))
|
|
Expiration = reader[5].ToString();
|
|
if (reader[6].GetType() != typeof(DBNull))
|
|
CCV = reader[6].ToString();
|
|
g = new Guest(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), CreditCard, Expiration, CCV);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return g;
|
|
}
|
|
internal static Reservation? GetResByGuestAndDate(Guest g)
|
|
{
|
|
Reservation? r = null;
|
|
Transaction? t;
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
int? RoomNumber = null;
|
|
DateTime? CheckIn = null, CheckOut = null, DateChanged = null, LastPaid = null, PaidOn = null;
|
|
|
|
cmd.CommandText = @"SELECT * FROM reservations
|
|
INNER JOIN transactions ON reservations.TransactionID = transactions.ID
|
|
WHERE GuestID = @GuestID AND Status = @Status;";
|
|
cmd.Parameters.AddWithValue("@GuestID", g.Id);
|
|
cmd.Parameters.AddWithValue("@Status", (int)ReservationStatus.Active);
|
|
cmd.ExecuteNonQuery();
|
|
|
|
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
reader.Read();
|
|
if (reader.HasRows)
|
|
{
|
|
if (reader[20].GetType() != typeof(DBNull))
|
|
LastPaid = reader.GetDateTime(20);
|
|
if (reader[21].GetType() != typeof(DBNull))
|
|
PaidOn = reader.GetDateTime(21);
|
|
t = new Transaction(reader.GetInt32(13), reader.GetDouble(14), reader.GetDouble(15), reader.GetDouble(17), reader.GetDateTime(19),
|
|
LastPaid: LastPaid, PaidOn: PaidOn, RefundAmount: reader.GetDouble(18), Penalty: reader.GetDouble(16));
|
|
|
|
if (reader[1].GetType() != typeof(DBNull))
|
|
RoomNumber = reader.GetInt32(1);
|
|
if (reader[10].GetType() != typeof(DBNull))
|
|
CheckIn = reader.GetDateTime(10);
|
|
if (reader[11].GetType() != typeof(DBNull))
|
|
CheckOut = reader.GetDateTime(11);
|
|
if (reader[12].GetType() != typeof(DBNull))
|
|
DateChanged = reader.GetDateTime(12);
|
|
|
|
r = new Reservation(reader.GetInt32(0), g, t, (ReservationType)reader.GetInt32(5), (ReservationStatus)reader.GetInt32(6),
|
|
reader.GetDateTime(7), reader.GetDateTime(8), reader.GetDateTime(9), RoomNum: RoomNumber, IsNoShow: reader.GetBoolean(4),
|
|
CheckIn: CheckIn, CheckOut: CheckOut, DateChanged: DateChanged);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return r;
|
|
}
|
|
internal static double? GetBaseRate()
|
|
{
|
|
double? rate;
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
string query = "SELECT Rate FROM rates WHERE DefaultRate = 1;";
|
|
cmd.CommandText = query;
|
|
cmd.ExecuteNonQuery();
|
|
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
reader.Read();
|
|
if (reader.HasRows)
|
|
rate = reader.GetDouble(0);
|
|
else
|
|
rate = null;
|
|
}
|
|
}
|
|
}
|
|
return rate;
|
|
}
|
|
internal static void SetBaseRate(double Rate, DateTime DateSet)
|
|
{
|
|
double rate;
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
string query = $"INSERT INTO rates (Rate, DateSet) VALUES (@Rate, @DateSet);";
|
|
cmd.CommandText = query;
|
|
cmd.Parameters.AddWithValue("@Rate", Rate);
|
|
cmd.Parameters.AddWithValue("@DateSet", DateSet);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
internal static void CheckBaseRate()
|
|
{
|
|
using (DatabaseManager Manager = new DatabaseManager())
|
|
{
|
|
int? OldId;
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
string query = "SELECT Id FROM rates WHERE DefaultRate = 1;";
|
|
cmd.CommandText = query;
|
|
cmd.ExecuteNonQuery();
|
|
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
reader.Read();
|
|
OldId = reader.GetInt32(0);
|
|
}
|
|
}
|
|
int? Id;
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
string query = "SELECT Id FROM rates WHERE DateSet = @Date;";
|
|
cmd.CommandText = query;
|
|
cmd.Parameters.AddWithValue("@Date", DateTime.Now.Date.ToString("yyyy-MM-dd"));
|
|
cmd.ExecuteNonQuery();
|
|
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
|
{
|
|
reader.Read();
|
|
Id = reader.GetInt32(0);
|
|
}
|
|
}
|
|
if (Id != null)
|
|
{
|
|
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
|
{
|
|
cmd.CommandText = @"UPDATE rates SET DefaultRate = NULL WHERE Id = @OldID;
|
|
UPDATE rates SET DefaultRate = 1 WHERE Id = @ID";
|
|
cmd.Parameters.AddWithValue("@OldID", OldId);
|
|
cmd.Parameters.AddWithValue("@ID", Id);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
internal static int CheckInGuest(DateTime CheckIn)
|
|
{
|
|
return 1;
|
|
}
|
|
internal static int CheckOutGuest(DateTime CheckOut)
|
|
{
|
|
return 1;
|
|
}
|
|
}
|
|
}
|