Further major refactoring of code
This is another commit to mostly version the code. There have been a considerable number of changes and there is functionality that I am still determining whether it should lie within the manager or the model class itself. It makes the most sense to possibly add the "Update" or database manipulation functions on the models themselves. On the other hand, instead of creating and generating the ID in the model, the current design is to create the entry in the database first, get the last insert row ID and create a new and complete model that is returned back by the function. This allows us to leverage the autoincrement functionality of the database rather than trying to design a function and/ or make an additional call to the database. **NOTE: Code is non-functional due to some classes not having their errors resolved.
This commit is contained in:
133
OpheliasOasis/Managers/HotelManager.cs
Normal file
133
OpheliasOasis/Managers/HotelManager.cs
Normal file
@@ -0,0 +1,133 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
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=\"{tableName}\";";
|
||||
cmd.ExecuteNonQuery();
|
||||
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
||||
{
|
||||
reader.Read();
|
||||
lastId = reader.GetInt32(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
return lastId;
|
||||
}
|
||||
internal static int GetThirtyDayOccupancy(DateTime start)
|
||||
{
|
||||
int thirtyDayOcc;
|
||||
using (DatabaseManager Manager = new DatabaseManager())
|
||||
{
|
||||
using (SQLiteCommand cmd = Manager.con.CreateCommand()) {
|
||||
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();
|
||||
using (SQLiteDataReader reader = cmd.ExecuteReader())
|
||||
{
|
||||
reader.Read();
|
||||
thirtyDayOcc = reader.GetInt32(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return thirtyDayOcc;
|
||||
}
|
||||
internal static int CreateReservation(int roomid, int guestid, int transactionid, bool isnoshow, ReservationType type,
|
||||
ReservationStatus status, DateTime creationdate, DateTime startdate, DateTime enddate,
|
||||
DateTime checkin, DateTime checkout, DateTime datechanged)
|
||||
{
|
||||
int id;
|
||||
using (DatabaseManager Manager = new DatabaseManager())
|
||||
{
|
||||
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = QueryBuilder.CreateReservation(roomid, guestid, transactionid, isnoshow, type,
|
||||
status, creationdate, startdate, enddate,
|
||||
checkin, checkout, datechanged);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
id = (int)Manager.con.LastInsertRowId;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
internal static void ChangeReservationDates(Reservation r, Transaction t, DateTime start, DateTime end)
|
||||
{
|
||||
using (DatabaseManager Manager = new DatabaseManager())
|
||||
{
|
||||
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
||||
{
|
||||
string? query = QueryBuilder.UpdateReservation(id: r.Id, startdate: start, enddate: end, datechanged: DateTime.Now);
|
||||
|
||||
if (query == null)
|
||||
throw new Exception();
|
||||
|
||||
query += QueryBuilder.UpdateTransaction(id: r.TransactionId, Multiplier: 1.1);
|
||||
cmd.CommandText = query;
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
internal static Transaction CreateTransaction(double Rate, double Owed, double Penalty,
|
||||
double Multiplier, double Refund, bool isLate,
|
||||
bool PaidOff, DateTime PaidBy, DateTime? PaidOn = null)
|
||||
{
|
||||
int Id;
|
||||
using (DatabaseManager Manager = new DatabaseManager())
|
||||
{
|
||||
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = QueryBuilder.CreateTransaction(Rate, Owed, Penalty, Multiplier, Refund, isLate, PaidOff, PaidBy, PaidOn);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
Id = (int)Manager.con.LastInsertRowId;
|
||||
}
|
||||
return new Transaction(Id, Rate, Owed, Penalty, Multiplier, Refund, PaidBy, PaidOn);
|
||||
}
|
||||
internal static int CheckInGuest(DateTime CheckIn)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
internal static int CheckOutGuest(DateTime CheckOut)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
internal static Guest CreateGuest(string FirstName, string LastName, string Email, string? CreditCard = null, string? Expiration = null, string? CCV = null)
|
||||
{
|
||||
string query = QueryBuilder.CreateGuest(FirstName, LastName, Email, CreditCard, Expiration, CCV);
|
||||
|
||||
int Id;
|
||||
using (DatabaseManager Manager = new DatabaseManager())
|
||||
{
|
||||
using (SQLiteCommand cmd = Manager.con.CreateCommand())
|
||||
{
|
||||
cmd.CommandText = QueryBuilder.CreateGuest(FirstName, LastName, Email, CreditCard, Expiration, CCV);
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
Id = (int)Manager.con.LastInsertRowId;
|
||||
}
|
||||
|
||||
if (CreditCard != null && Expiration != null && CCV != null)
|
||||
return new Guest(FirstName, LastName, Email, CreditCard, Expiration, CCV);
|
||||
else
|
||||
return new Guest(FirstName, LastName, Email);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user