Finishes the guest part of the models and UI integration
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.
This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Ophelias.Models;
|
||||
using Ophelias.Managers;
|
||||
using System.Data.SQLite;
|
||||
|
||||
namespace Ophelias.Models
|
||||
{
|
||||
@@ -17,6 +13,44 @@ namespace Ophelias.Models
|
||||
internal string? CCV;
|
||||
internal string? Expiration;
|
||||
|
||||
internal Guest(string FirstName, string LastName, string Email, string? CreditCard = null, string? Expiration = null, string? CCV = null)
|
||||
{
|
||||
int Id;
|
||||
using (DatabaseManager Manager = new DatabaseManager())
|
||||
{
|
||||
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = "INSERT INTO guests (Fname, Lname, Email, CreditCard, Expiration, CCV) " +
|
||||
"VALUES (@Fname, @Lname, @Email, @CC, @Expiry, @CCV);";
|
||||
cmd.Parameters.AddWithValue("@Fname", FirstName);
|
||||
cmd.Parameters.AddWithValue("@Lname", LastName);
|
||||
cmd.Parameters.AddWithValue("@Email", Email);
|
||||
cmd.Parameters.AddWithValue("@CreditCard", CreditCard);
|
||||
cmd.Parameters.AddWithValue("@Expiration", Expiration);
|
||||
cmd.Parameters.AddWithValue("@CCV", CCV);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
Id = (int)Manager.con.LastInsertRowId;
|
||||
}
|
||||
|
||||
if (CreditCard != null && Expiration != null && CCV != null)
|
||||
{
|
||||
this.Id = Id;
|
||||
this.FirstName = FirstName;
|
||||
this.LastName = LastName;
|
||||
this.Email = Email;
|
||||
this.CreditCard = CreditCard;
|
||||
this.Expiration = Expiration;
|
||||
this.CCV = CCV;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Id = Id;
|
||||
this.FirstName = FirstName;
|
||||
this.LastName = LastName;
|
||||
this.Email = Email;
|
||||
}
|
||||
}
|
||||
internal Guest(int Id, string FirstName, string LastName, string Email, string? CreditCard = null, string? Expiration = null, string? CCV = null)
|
||||
{
|
||||
this.Id = Id;
|
||||
@@ -27,6 +61,28 @@ namespace Ophelias.Models
|
||||
this.Expiration = Expiration;
|
||||
this.CCV = CCV;
|
||||
}
|
||||
internal void UpdateGuest(int Id, string? FirstName = null, string? LastName = null, string? Email = null, string? CreditCard = null, string? Expiration = null, string? CCV = null)
|
||||
{
|
||||
using (DatabaseManager Manager = new DatabaseManager())
|
||||
{
|
||||
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = QueryBuilder.UpdateGuest(Id, FirstName, LastName, Email, CreditCard, Expiration, CCV);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
if (FirstName != null)
|
||||
this.FirstName = FirstName;
|
||||
if (LastName != null)
|
||||
this.LastName = LastName;
|
||||
if (FirstName != null)
|
||||
this.Email = Email;
|
||||
this.CreditCard = CreditCard;
|
||||
if (FirstName != null)
|
||||
this.Expiration = Expiration;
|
||||
if (FirstName != null)
|
||||
this.CCV = CCV;
|
||||
}
|
||||
internal void SetCreditCardInformation(string CreditCard, string Expiration, string CCV)
|
||||
{
|
||||
this.CreditCard = CreditCard;
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Ophelias.Models
|
||||
internal class Reservation
|
||||
{
|
||||
internal int Id;
|
||||
internal Room Room;
|
||||
internal int? RoomNum;
|
||||
internal Guest Guest;
|
||||
internal Transaction Transaction;
|
||||
|
||||
@@ -26,10 +26,84 @@ namespace Ophelias.Models
|
||||
internal DateTime? CheckOut;
|
||||
internal DateTime? DateChanged;
|
||||
|
||||
internal Reservation(Guest Guest, ReservationType Type,
|
||||
DateTime CreationDate, DateTime StartDate, DateTime EndDate, ReservationStatus Status = ReservationStatus.Active,
|
||||
bool IsNoShow = false, DateTime? CheckIn = null, DateTime? CheckOut = null, DateTime? DateChanged = null, int? RoomNum = null)
|
||||
{
|
||||
int Id; double Multiplier;
|
||||
switch (Type)
|
||||
{
|
||||
case ReservationType.Conventional: Multiplier = TxFunctions.ConventionalFee; break;
|
||||
case ReservationType.Prepaid: Multiplier = TxFunctions.PrepaidFee; break;
|
||||
case ReservationType.Incentive: Multiplier = TxFunctions.IncentiveFee(StartDate, EndDate); break;
|
||||
case ReservationType.SixtyDayAdvance: Multiplier = TxFunctions.SixtyDayFee; break;
|
||||
default: throw new NotImplementedException();
|
||||
}
|
||||
double? BaseRate = HotelManager.GetBaseRate();
|
||||
if (BaseRate == null) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
Transaction Transaction = new Transaction(
|
||||
Rate: (double)BaseRate,
|
||||
Owed: TxFunctions.CalculateOwed((double)BaseRate, (int)(EndDate.Date - StartDate.Date).TotalDays),
|
||||
Multiplier: Multiplier,
|
||||
PayBy: TxFunctions.GetPayByDate(Type, StartDate, EndDate)
|
||||
);
|
||||
using (DatabaseManager Manager = new DatabaseManager())
|
||||
{
|
||||
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
||||
{
|
||||
if (RoomNum != null)
|
||||
this.RoomNum = RoomNum;
|
||||
cmd.CommandText =
|
||||
"INSERT INTO reservations (RoomNum, GuestID, TransactionID, IsNoShow, Type, Status, CreationDate, StartDate, EndDate, CheckIn, CheckOut, DateChanged) " +
|
||||
"VALUES (@RoomNum, @GuestID, @TransactionID, @IsNoShow, @Type, @Status, @CreationDate, @StartDate, @EndDate, @CheckIn, @CheckOut, @DateChanged);";
|
||||
cmd.Parameters.AddWithValue("@RoomNum", RoomNum);
|
||||
cmd.Parameters.AddWithValue("@GuestID", Guest.Id);
|
||||
cmd.Parameters.AddWithValue("@TransactionID", Transaction.Id);
|
||||
cmd.Parameters.AddWithValue("@IsNoShow", Convert.ToInt32(IsNoShow));
|
||||
cmd.Parameters.AddWithValue("@Type", (int)Type);
|
||||
cmd.Parameters.AddWithValue("@Status", (int)Status);
|
||||
cmd.Parameters.AddWithValue("@CreationDate", CreationDate.ToString("yyyy-MM-dd"));
|
||||
cmd.Parameters.AddWithValue("@StartDate", StartDate.ToString("yyyy-MM-dd"));
|
||||
cmd.Parameters.AddWithValue("@EndDate", EndDate.ToString("yyyy-MM-dd"));
|
||||
if (CheckIn.HasValue)
|
||||
cmd.Parameters.AddWithValue("@CheckIn", CheckIn.Value.ToString("yyyy-MM-dd"));
|
||||
else
|
||||
cmd.Parameters.AddWithValue("@CheckIn", CheckIn);
|
||||
if (CheckOut.HasValue)
|
||||
cmd.Parameters.AddWithValue("@CheckOut", CheckOut.Value.ToString("yyyy-MM-dd"));
|
||||
else
|
||||
cmd.Parameters.AddWithValue("@CheckOut", CheckOut);
|
||||
if (DateChanged.HasValue)
|
||||
cmd.Parameters.AddWithValue("@DateChanged", DateChanged.Value.ToString("yyyy-MM-dd"));
|
||||
else
|
||||
cmd.Parameters.AddWithValue("@DateChanged", DateChanged);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
Id = (int)Manager.con.LastInsertRowId;
|
||||
}
|
||||
this.Id = Id;
|
||||
this.RoomNum = RoomNum;
|
||||
this.Guest = Guest;
|
||||
this.Transaction = Transaction;
|
||||
this.IsNoShow = IsNoShow;
|
||||
this.Type = Type;
|
||||
this.Status = Status;
|
||||
this.CreationDate = CreationDate;
|
||||
this.StartDate = StartDate;
|
||||
this.EndDate = EndDate;
|
||||
this.CheckIn = CheckIn;
|
||||
this.CheckOut = CheckOut;
|
||||
this.DateChanged = DateChanged;
|
||||
}
|
||||
internal Reservation(int Id, Guest Guest, Transaction Transaction, ReservationType Type, ReservationStatus Status,
|
||||
DateTime CreationDate, DateTime StartDate, DateTime EndDate, bool IsNoShow = false, DateTime? CheckIn = null, DateTime? CheckOut = null, DateTime? DateChanged = null)
|
||||
DateTime CreationDate, DateTime StartDate, DateTime EndDate, bool IsNoShow = false, DateTime? CheckIn = null, DateTime? CheckOut = null, DateTime? DateChanged = null, int? RoomNum = null)
|
||||
{
|
||||
this.Id = Id;
|
||||
this.RoomNum = RoomNum;
|
||||
this.Guest = Guest;
|
||||
this.Transaction = Transaction;
|
||||
this.IsNoShow = IsNoShow;
|
||||
@@ -57,23 +131,38 @@ namespace Ophelias.Models
|
||||
throw new Exception();
|
||||
|
||||
cmd.CommandText = query;
|
||||
cmd.Parameters.AddWithValue("@ID", Id);
|
||||
cmd.Parameters.AddWithValue("@Status", Status);
|
||||
cmd.Parameters.AddWithValue("@StartDate", StartDate.ToString("yyyy-MM-dd"));
|
||||
cmd.Parameters.AddWithValue("@EndDate", EndDate.ToString("yyyy-MM-dd"));
|
||||
cmd.Parameters.AddWithValue("@DateChanged", DateChanged.Value.ToString("yyyy-MM-dd"));
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
Transaction.UpdateTransactionFees(HotelManager.GetBaseRate(), TxFunctions.Changed, TxFunctions.GetPayByDate(Type, this.StartDate, this.EndDate));
|
||||
double? BaseRate = HotelManager.GetBaseRate();
|
||||
if (BaseRate == null)
|
||||
throw new ArgumentNullException(nameof(BaseRate));
|
||||
Transaction.UpdateTransactionFees((double)BaseRate, TxFunctions.Changed, TxFunctions.GetPayByDate(Type, this.StartDate, this.EndDate));
|
||||
}
|
||||
internal void CancelReservation()
|
||||
{
|
||||
DateTime _DateChanged = DateTime.Now.Date;
|
||||
using (DatabaseManager Manager = new DatabaseManager())
|
||||
{
|
||||
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
||||
{
|
||||
string? query = QueryBuilder.UpdateReservation(Id: Id, Status: ReservationStatus.Cancelled, DateChanged: DateTime.Now.Date);
|
||||
string? query = QueryBuilder.UpdateReservation(Id: Id, Status: ReservationStatus.Cancelled, DateChanged: _DateChanged);
|
||||
|
||||
if (query == null)
|
||||
throw new Exception();
|
||||
|
||||
Status = ReservationStatus.Cancelled;
|
||||
DateChanged = _DateChanged;
|
||||
|
||||
cmd.CommandText = query;
|
||||
cmd.Parameters.AddWithValue("@ID", Id);
|
||||
cmd.Parameters.AddWithValue("@Status", Status);
|
||||
cmd.Parameters.AddWithValue("@DateChanged", DateChanged.Value.ToString("yyyy-MM-dd"));
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,12 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Ophelias.Models
|
||||
{
|
||||
internal class Room
|
||||
internal class @int
|
||||
{
|
||||
internal int Id;
|
||||
internal bool Occupied;
|
||||
|
||||
internal Room(int id)
|
||||
internal @int(int id)
|
||||
{
|
||||
Id = id;
|
||||
Occupied = false;
|
||||
@@ -20,11 +20,11 @@ namespace Ophelias.Models
|
||||
|
||||
internal class RoomList
|
||||
{
|
||||
internal List<Room> Rooms;
|
||||
internal List<@int> Rooms;
|
||||
|
||||
internal RoomList()
|
||||
{
|
||||
Rooms = new List<Room>();
|
||||
Rooms = new List<@int>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,49 @@ namespace Ophelias.Models
|
||||
internal DateTime? LastPaid { get; set; } = null;
|
||||
internal DateTime? PaidOn { get; set; } = null;
|
||||
|
||||
internal Transaction(double Rate, double Owed,
|
||||
double Multiplier, DateTime PayBy, DateTime? LastPaid = null,
|
||||
DateTime? PaidOn = null, double RefundAmount = 0, double Penalty = 0)
|
||||
{
|
||||
int Id;
|
||||
using (DatabaseManager Manager = new DatabaseManager())
|
||||
{
|
||||
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = "INSERT INTO transactions (Rate, Owed, Penalty, Multiplier, RefundAmount, PayBy, LastPaid, PaidOn) " +
|
||||
"VALUES (@Rate, @Owed, @Penalty, @Multiplier, @RefundAmount, @PayBy, @LastPaid, @PaidOn)";
|
||||
cmd.Parameters.AddWithValue("@Rate", Rate);
|
||||
cmd.Parameters.AddWithValue("@Owed", Owed);
|
||||
cmd.Parameters.AddWithValue("@Multiplier", Multiplier);
|
||||
cmd.Parameters.AddWithValue("@RefundAmount", RefundAmount);
|
||||
cmd.Parameters.AddWithValue("@Penalty", Penalty);
|
||||
cmd.Parameters.AddWithValue("@PayBy", PayBy.ToString("yyyy-MM-dd"));
|
||||
cmd.Parameters.AddWithValue("@LastPaid", LastPaid);
|
||||
cmd.Parameters.AddWithValue("@PaidOn", PaidOn);
|
||||
if (LastPaid != null)
|
||||
cmd.Parameters.AddWithValue("@LastPaid", LastPaid.Value.ToString("yyyy-MM-dd"));
|
||||
else
|
||||
cmd.Parameters.AddWithValue("@LastPaid", LastPaid);
|
||||
if (PaidOn != null)
|
||||
cmd.Parameters.AddWithValue("@PaidOn", PaidOn.Value.ToString("yyyy-MM-dd"));
|
||||
else
|
||||
cmd.Parameters.AddWithValue("@PaidOn", PaidOn);
|
||||
|
||||
//cmd.CommandText = QueryBuilder.CreateTransaction(Rate, Owed, Multiplier, PayBy, Refund: RefundAmount, Penalty: Penalty, LastPaid: LastPaid, PaidOn: PaidOn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
Id = (int)Manager.con.LastInsertRowId;
|
||||
}
|
||||
this.Id = Id;
|
||||
this.Rate = Rate;
|
||||
this.Owed = Owed;
|
||||
this.Penalty = Penalty;
|
||||
this.Multiplier = Multiplier;
|
||||
this.RefundAmount = RefundAmount;
|
||||
this.PayBy = PayBy;
|
||||
this.LastPaid = LastPaid;
|
||||
this.PaidOn = PaidOn;
|
||||
}
|
||||
internal Transaction(int Id, double Rate, double Owed,
|
||||
double Multiplier, DateTime PayBy, DateTime? LastPaid = null,
|
||||
DateTime? PaidOn = null, double RefundAmount = 0, double Penalty = 0)
|
||||
@@ -51,6 +94,10 @@ namespace Ophelias.Models
|
||||
throw new Exception();
|
||||
|
||||
cmd.CommandText = query;
|
||||
cmd.Parameters.AddWithValue("@ID", Id);
|
||||
cmd.Parameters.AddWithValue("@Rate", this.Rate);
|
||||
cmd.Parameters.AddWithValue("@Multiplier", this.Multiplier);
|
||||
cmd.Parameters.AddWithValue("@PayBy", this.PayBy.ToString("yyyy-MM-dd"));
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
@@ -75,6 +122,11 @@ namespace Ophelias.Models
|
||||
throw new Exception();
|
||||
|
||||
cmd.CommandText = query;
|
||||
cmd.Parameters.AddWithValue("@ID", Id);
|
||||
cmd.Parameters.AddWithValue("@Owed", Owed);
|
||||
cmd.Parameters.AddWithValue("@RefundAmount", RefundAmount);
|
||||
cmd.Parameters.AddWithValue("@LastPaid", LastPaid);
|
||||
cmd.Parameters.AddWithValue("@PaidOn", PaidOn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
@@ -92,6 +144,8 @@ namespace Ophelias.Models
|
||||
throw new Exception();
|
||||
|
||||
cmd.CommandText = query;
|
||||
cmd.Parameters.AddWithValue("@ID", Id);
|
||||
cmd.Parameters.AddWithValue("@RefundAmount", RefundAmount);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
@@ -125,7 +179,7 @@ namespace Ophelias.Models
|
||||
}
|
||||
internal static double CalculateOwed(double Rate, int Days)
|
||||
{
|
||||
return Rate * (double)Days;
|
||||
return Rate * Days;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user