Files
雲華 0a5fa3e402 Updateed some formatting and bug fixes
Fixed inconsistencies in queries that would result in errors because
the parameters were missing. Updating a guest is an example that had
this issue. After checking parameters should all be supplied where
needed.
2022-04-17 18:55:38 -04:00

123 lines
5.3 KiB
C#

using Ophelias.Managers;
using System.Data.SQLite;
namespace Ophelias.Models
{
internal class Guest
{
/*
* This class represents a guest in the real world.
* A guest in the eyes of the system is comprised of
* a unique ID, a first and last name, an email, and
* payment information. The payment information is optional
* as they may not specify it at the time or have none readily
* available.
*/
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)
{
/*
* This creates a new guest under the assumption that they never existed previously.
* As a result, this function will create a new entry in the database and generate
* a unique ID for that customer. These details are then assigned to create a new instance
* of the guest after the database query is executed. For constraints on Guests, see the Database
* class.
*/
int Id;
using (Database Manager = new()) // Opens a new connection to the database
{
using (SQLiteCommand cmd = Manager.con.CreateCommand()) // Creates a new command that will be executed in the database
{
cmd.CommandText = "INSERT INTO guests (Fname, Lname, Email, CreditCard, Expiration, CCV) " +
"VALUES (@Fname, @Lname, @Email, @CreditCard, @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("@Expiry", Expiration);
cmd.Parameters.AddWithValue("@CCV", CCV);
cmd.ExecuteNonQuery();
}
Id = (int)Manager.con.LastInsertRowId; // Gets the auto incremented ID of the guest that was just created and assigns it to the guest
}
this.Id = Id;
this.FirstName = FirstName;
this.LastName = LastName;
this.Email = Email;
this.CreditCard = CreditCard;
this.Expiration = Expiration;
this.CCV = CCV;
}
internal Guest(int Id, string FirstName, string LastName, string Email, string? CreditCard = null, string? Expiration = null, string? CCV = null)
{
/*
* This creates an instance of a guest assuming they already exist as an ID is required.
*/
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)
{
/*
* UpdateGuest provides functionality that enables guests to update their account information.
* This information is first passed off to the database so the changes can accurately be reflected
* and then the changes are copied to the existing guest if they are not null. Null changes are considered
* to be no changes and not an erasure of data.
*/
using (Database Manager = new()) // Opens a new connection to the database
{
using SQLiteCommand cmd = Manager.con.CreateCommand(); // Creates a new command that will be executed in the database
cmd.CommandText = QueryBuilder.UpdateGuest(Id, FirstName, LastName, Email, CreditCard, Expiration, CCV);
cmd.Parameters.AddWithValue("@Id", Id);
cmd.Parameters.AddWithValue("@Fname", FirstName);
cmd.Parameters.AddWithValue("@Lname", LastName);
cmd.Parameters.AddWithValue("@Email", Email);
cmd.Parameters.AddWithValue("@CreditCard", CreditCard);
cmd.Parameters.AddWithValue("@Expiry", Expiration);
cmd.Parameters.AddWithValue("@CCV", CCV);
cmd.ExecuteNonQuery();
}
if (FirstName != null)
{
this.FirstName = FirstName;
}
if (LastName != null)
{
this.LastName = LastName;
}
if (Email != null)
{
this.Email = Email;
}
if (CreditCard != null)
{
this.CreditCard = CreditCard;
}
if (Expiration != null)
{
this.Expiration = Expiration;
}
if (CCV != null)
{
this.CCV = CCV;
}
}
}
}