Files
ophelias-oasis/OpheliasOasis/Models/Guest.cs
雲華 34bebca414 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.
2022-04-15 02:29:55 -04:00

103 lines
3.8 KiB
C#

using Ophelias.Managers;
using System.Data.SQLite;
namespace Ophelias.Models
{
internal class Guest
{
internal int Id;
internal string FirstName;
internal string LastName;
internal string Email;
internal string? CreditCard;
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;
this.FirstName = FirstName;
this.LastName = LastName;
this.Email = Email;
this.CreditCard = CreditCard;
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;
this.Expiration = Expiration;
this.CCV = CCV;
}
}
internal class GuestList
{
internal List<Guest> Guests;
internal GuestList()
{
Guests = new List<Guest>();
}
}
}