2021-01-13 01:24:27 -05:00
|
|
|
|
using Dapper;
|
|
|
|
|
using System;
|
2021-01-12 01:34:14 -05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Configuration;
|
2021-01-13 01:24:27 -05:00
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.SQLite;
|
2021-01-12 01:34:14 -05:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-01-13 01:24:27 -05:00
|
|
|
|
using GreatHomeChildcare.Models;
|
2021-01-12 01:34:14 -05:00
|
|
|
|
|
2021-01-15 00:38:58 -05:00
|
|
|
|
/* REFERENCES:
|
|
|
|
|
https://stackoverflow.com/questions/16436485/sqlite-selecting-the-maximum-corresponding-value
|
|
|
|
|
*/
|
|
|
|
|
|
2021-01-12 01:34:14 -05:00
|
|
|
|
namespace GreatHomeChildcare
|
|
|
|
|
{
|
|
|
|
|
class SqliteDataAccess
|
|
|
|
|
{
|
2021-01-23 02:16:32 -05:00
|
|
|
|
#region child
|
2021-01-15 00:38:58 -05:00
|
|
|
|
// ***************** Create *****************
|
2021-01-23 02:16:32 -05:00
|
|
|
|
|
|
|
|
|
/* Inserts a new child to the database.
|
|
|
|
|
* INPUT child
|
|
|
|
|
* OUTPUT new row to SQL database, void to program.
|
|
|
|
|
*/
|
|
|
|
|
internal void InsertNewChild(Child child)
|
|
|
|
|
{
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
2021-01-27 17:56:17 -05:00
|
|
|
|
string strQuery = "INSERT INTO Children (FirstName, LastName, DOB, address, race, gender, photo)" +
|
2021-01-23 02:16:32 -05:00
|
|
|
|
"VALUES (@FirstName, @LastName, @DOB, @address, @race, @gender, @photo);";
|
|
|
|
|
cnn.Execute(strQuery, child);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-15 00:38:58 -05:00
|
|
|
|
// ***************** Read *******************
|
2021-01-18 23:43:26 -05:00
|
|
|
|
|
2021-01-22 01:56:50 -05:00
|
|
|
|
/* Gets a single child from the sqlite database
|
|
|
|
|
* provided an id number.
|
|
|
|
|
* INPUT: integer id
|
|
|
|
|
* OUTPUT: Child object
|
|
|
|
|
*/
|
|
|
|
|
internal Child GetChildByID(int id_in)
|
|
|
|
|
{
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
|
|
|
|
string strQuery = "SELECT * FROM Children WHERE id=@id";
|
|
|
|
|
Child output = cnn.Query<Child>(strQuery, new { id = id_in }).SingleOrDefault();
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-18 23:43:26 -05:00
|
|
|
|
/* Gets all children from the sqlite database.
|
|
|
|
|
* INPUT: void
|
|
|
|
|
* OUTPUT: list of Child objects
|
|
|
|
|
*/
|
|
|
|
|
internal List<Child> GetAllChildren()
|
|
|
|
|
{
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
2021-01-20 19:11:09 -05:00
|
|
|
|
string strQuery = "SELECT * FROM Children ORDER BY LastName ASC";
|
2021-01-18 23:43:26 -05:00
|
|
|
|
var output = cnn.Query<Child>(strQuery);
|
|
|
|
|
return output.ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-15 00:38:58 -05:00
|
|
|
|
// ***************** Update *****************
|
2021-01-23 02:16:32 -05:00
|
|
|
|
|
|
|
|
|
/* Updates an individual child in the DB.
|
|
|
|
|
* Needs to do field mapping in the Execute call because
|
|
|
|
|
* the photo field can be null or not null, and
|
|
|
|
|
* the conditional operator can be used to change the query on the fly.
|
|
|
|
|
*
|
|
|
|
|
* INPUT: Child
|
|
|
|
|
* OUTPUT: Data to SQL Database, void to program
|
|
|
|
|
*/
|
|
|
|
|
internal void UpdateChild(Child child)
|
|
|
|
|
{
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
|
|
|
|
string strQuery = "UPDATE Children SET FirstName = @First_Name, LastName = @Last_Name, " +
|
|
|
|
|
"DOB = @_dob, address = @_address, race = @_race, gender = @_gender, photo = @_photo" +
|
|
|
|
|
"WHERE id = @_id;";
|
|
|
|
|
|
|
|
|
|
cnn.Execute(strQuery, new
|
|
|
|
|
{
|
|
|
|
|
_id = child.id,
|
|
|
|
|
First_Name = child.FirstName,
|
|
|
|
|
Last_Name = child.LastName,
|
|
|
|
|
_dob = child.DOB,
|
|
|
|
|
_address = child.address,
|
|
|
|
|
_race = child.race,
|
|
|
|
|
_gender = child.gender,
|
|
|
|
|
_photo = child.photo ?? null
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-15 00:38:58 -05:00
|
|
|
|
// ***************** Delete *****************
|
2021-01-12 01:34:14 -05:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region guardian
|
2021-01-15 00:38:58 -05:00
|
|
|
|
// ***************** Create *****************
|
2021-01-27 17:56:17 -05:00
|
|
|
|
internal void InsertNewGuardian(Guardian guardian_in)
|
|
|
|
|
{
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
|
|
|
|
string strQuery = "INSERT INTO Guardians (FirstName, LastName, PhoneNumber, EmailAddress, PinNumber, IsAdmin)" +
|
|
|
|
|
"VALUES (@FirstName, @LastName, @PhoneNumber, @EmailAddress, @PinNumber, @IsAdmin);";
|
|
|
|
|
cnn.Execute(strQuery, guardian_in);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-15 00:38:58 -05:00
|
|
|
|
|
2021-01-13 23:15:20 -05:00
|
|
|
|
// ***************** Read *****************
|
|
|
|
|
|
2021-01-26 00:19:14 -05:00
|
|
|
|
/* Gets a single guardian from the DB given db id.
|
|
|
|
|
* INPUT: integer
|
|
|
|
|
* OUTPUT Guardian object or null
|
|
|
|
|
*/
|
|
|
|
|
internal Guardian GetGuardianById(int id_in)
|
|
|
|
|
{
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
|
|
|
|
string sqlQuery = "SELECT * FROM Guardians WHERE id=@id;";
|
|
|
|
|
Guardian output = cnn.Query<Guardian>(sqlQuery, new { id = id_in }).SingleOrDefault();
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-25 01:49:00 -05:00
|
|
|
|
/* Gets all guardians from the DB.
|
|
|
|
|
* INPUT: void
|
|
|
|
|
* OUTPUT: list of guardian objects.
|
|
|
|
|
*/
|
|
|
|
|
internal List<Guardian> GetAllGuardians()
|
|
|
|
|
{
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
|
|
|
|
string strQuery = "SELECT * FROM Guardians ORDER BY LastName ASC;";
|
|
|
|
|
var output = cnn.Query<Guardian>(strQuery);
|
|
|
|
|
return output.ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 01:56:50 -05:00
|
|
|
|
/* Gets all guardians for a single child.
|
|
|
|
|
* INPUT: Child
|
|
|
|
|
* OUTPUT: List of guardian object.
|
|
|
|
|
*/
|
|
|
|
|
internal List<Guardian> GetGuardiansByChild(Child child_in)
|
|
|
|
|
{
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
|
|
|
|
string strQuery = "SELECT * FROM Guardians WHERE id IN (SELECT guardian_id FROM Authorized_Guardians WHERE child_id = @id)";
|
|
|
|
|
var output = cnn.Query<Guardian>(strQuery, child_in);
|
|
|
|
|
return output.ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 23:15:20 -05:00
|
|
|
|
/* Gets a Guardian by a distinct pin number. Distinctness enforced elsewhere.
|
|
|
|
|
* INPUT: integer pin#
|
|
|
|
|
* OUTPUT: Guardian object
|
|
|
|
|
*/
|
|
|
|
|
internal Guardian GetGuardianByPin(int guardian_pin_in)
|
|
|
|
|
{
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
|
|
|
|
string strQuery = "SELECT * FROM Guardians WHERE PinNumber = @pin_in";
|
|
|
|
|
Guardian guardian = cnn.Query<Guardian>(strQuery, new { pin_in = guardian_pin_in }).SingleOrDefault();
|
|
|
|
|
return guardian;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-15 00:38:58 -05:00
|
|
|
|
// ***************** Update *****************
|
2021-01-27 17:56:17 -05:00
|
|
|
|
internal void UpdateGuardian(Guardian guardian_in)
|
|
|
|
|
{
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
|
|
|
|
string strQuery = "UPDATE Guardians SET FirstName = @FirstName, LastName = @LastName, " +
|
|
|
|
|
"PhoneNumber = @PhoneNumber, EmailAddress = @EmailAddress, PinNumber = @PinNumber, " +
|
|
|
|
|
"isAdmin = @isAdmin WHERE id=@id";
|
|
|
|
|
cnn.Execute(strQuery, guardian_in);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-15 00:38:58 -05:00
|
|
|
|
// ***************** Delete *****************
|
2021-01-26 00:19:14 -05:00
|
|
|
|
internal void DeleteGuardian(Guardian guardian_in)
|
|
|
|
|
{
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
|
|
|
|
string strQuery = "DELETE FROM Guardians WHERE id = @id;";
|
|
|
|
|
cnn.Execute(strQuery, guardian_in);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-13 23:15:20 -05:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region authorized_guardians
|
2021-01-15 00:38:58 -05:00
|
|
|
|
// ***************** Create *****************
|
|
|
|
|
|
2021-01-25 01:49:00 -05:00
|
|
|
|
/* Add a guardian as an authorized_guardian of a specific child.
|
|
|
|
|
* INPUT: child, guardian
|
|
|
|
|
* OUTPUT: void to program, new row in authorized_guardian table of sql db.
|
|
|
|
|
*/
|
|
|
|
|
internal void AddNewGuardianToChild(Child child_in, Guardian guardian_in)
|
|
|
|
|
{
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
|
|
|
|
string strQuery = "INSERT INTO Authorized_Guardians (child_id, guardian_id) VALUES (@_child_id, @_guardian_id);";
|
|
|
|
|
cnn.Execute(strQuery, new
|
|
|
|
|
{
|
|
|
|
|
_child_id = child_in.id,
|
|
|
|
|
_guardian_id = guardian_in.id
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 23:15:20 -05:00
|
|
|
|
// ***************** Read *****************
|
|
|
|
|
|
2021-01-25 01:49:00 -05:00
|
|
|
|
/* Check for octomom.
|
|
|
|
|
* INPUT: Guardian
|
|
|
|
|
* OUTPUT: integer Count of children per guardian.
|
|
|
|
|
*/
|
|
|
|
|
internal int CheckForOctomom(Guardian guardian_in)
|
|
|
|
|
{
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
|
|
|
|
string strQuery = "SELECT COUNT(child_id) from Authorized_Guardians WHERE guardian_id = @id;";
|
|
|
|
|
int output = cnn.Query<int>(strQuery, guardian_in).SingleOrDefault();
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 23:15:20 -05:00
|
|
|
|
/* Gets a list of all children per a specific guardian.
|
|
|
|
|
* INPUT: Guardian
|
|
|
|
|
* OUTPUT: List of Child object.
|
|
|
|
|
*/
|
|
|
|
|
internal List<Child> GetChildrenByGuardian(Guardian guardian_in)
|
|
|
|
|
{
|
|
|
|
|
//TODO: Remove once valid login check is implemented.
|
|
|
|
|
if (guardian_in == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
|
|
|
|
string strQuery = @"
|
|
|
|
|
SELECT
|
|
|
|
|
Children.id,
|
|
|
|
|
Children.FirstName,
|
|
|
|
|
Children.LastName,
|
|
|
|
|
DOB,
|
|
|
|
|
address,
|
|
|
|
|
race,
|
|
|
|
|
gender,
|
|
|
|
|
photo
|
|
|
|
|
FROM Children
|
|
|
|
|
INNER JOIN Authorized_Guardians on Children.ID = Authorized_Guardians.child_id
|
|
|
|
|
INNER JOIN Guardians on Authorized_Guardians.guardian_id = Guardians.id
|
|
|
|
|
WHERE Guardians.id = @id
|
|
|
|
|
";
|
|
|
|
|
var output = cnn.Query<Child>(strQuery,guardian_in);
|
|
|
|
|
return output.ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-15 00:38:58 -05:00
|
|
|
|
// ***************** Update *****************
|
|
|
|
|
// ***************** Delete *****************
|
2021-01-25 01:49:00 -05:00
|
|
|
|
|
2021-01-26 00:19:14 -05:00
|
|
|
|
internal void RemoveGuardianFromAllChildren(Guardian guardian_in)
|
2021-01-25 01:49:00 -05:00
|
|
|
|
{
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
2021-01-26 00:19:14 -05:00
|
|
|
|
string strQuery = "DELETE FROM Authorized_Guardians WHERE guardian_id = @id;";
|
|
|
|
|
cnn.Execute(strQuery, guardian_in);
|
2021-01-25 01:49:00 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 01:34:14 -05:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region attendance
|
2021-01-15 00:38:58 -05:00
|
|
|
|
// ***************** Create *****************
|
|
|
|
|
|
|
|
|
|
/* Creates a new entry in the attendence table each time
|
|
|
|
|
* a child is signed in our out of the system.
|
|
|
|
|
* Does --NOT-- need an Update function as the system
|
|
|
|
|
* is designed to keep track of --all-- times a child
|
|
|
|
|
* is signed in or out, and by which guardian.
|
|
|
|
|
* INPUTS: child, guardian
|
|
|
|
|
* OUTPUT: none to program, new row to sql db.
|
|
|
|
|
*/
|
|
|
|
|
internal void SignChildInOut(Child child_in, Guardian guardian_in)
|
|
|
|
|
{
|
2021-01-17 18:19:03 -05:00
|
|
|
|
Attendance strStudentStatus = GetChildSignInOut(child_in);
|
2021-01-15 00:38:58 -05:00
|
|
|
|
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
2021-01-17 18:19:03 -05:00
|
|
|
|
string strInOut = (strStudentStatus.in_out == "in" ? "out" : "in");
|
2021-01-15 00:38:58 -05:00
|
|
|
|
string strQuery = "INSERT INTO Attendence(child_id, guardian_id,in_out) VALUES(@child_id, @guardian_id, @in_out)";
|
|
|
|
|
cnn.Execute(strQuery, new
|
|
|
|
|
{
|
|
|
|
|
child_id = child_in.id,
|
|
|
|
|
guardian_id = guardian_in.id,
|
|
|
|
|
in_out = strInOut
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ***************** Read *****************
|
|
|
|
|
|
|
|
|
|
/* gets a single child in/out status.
|
|
|
|
|
* INPUT Child
|
2021-01-17 18:19:03 -05:00
|
|
|
|
* OUTPUT string "in" or "out", plus the timestamp.
|
2021-01-15 00:38:58 -05:00
|
|
|
|
*/
|
2021-01-17 18:19:03 -05:00
|
|
|
|
internal Attendance GetChildSignInOut(Child child_in)
|
2021-01-15 00:38:58 -05:00
|
|
|
|
{
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
|
|
|
|
//Select only the latest attendence data for this student.
|
2021-01-17 18:19:03 -05:00
|
|
|
|
string strQuery = "SELECT in_out,timestamp FROM Attendence WHERE child_id=@id AND id = (SELECT MAX(id) FROM Attendence WHERE child_id = @id)";
|
2021-01-15 00:38:58 -05:00
|
|
|
|
|
2021-01-17 18:19:03 -05:00
|
|
|
|
Attendance output = cnn.Query<Attendance>(strQuery, child_in).SingleOrDefault();
|
2021-01-15 00:38:58 -05:00
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// ***************** Update *****************
|
|
|
|
|
|
|
|
|
|
// ***************** Delete *****************
|
2021-01-26 00:19:14 -05:00
|
|
|
|
internal void DeleteAttendenceForGuardian(Guardian for_guardian)
|
|
|
|
|
{
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
|
|
|
|
string strQuery = "DELETE FROM Attendence WHERE guardian_id = @id;";
|
|
|
|
|
cnn.Execute(strQuery, for_guardian);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 01:34:14 -05:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region misc
|
2021-01-15 00:38:58 -05:00
|
|
|
|
// ***************** Create *****************
|
|
|
|
|
// ***************** Read *******************
|
2021-01-12 01:34:14 -05:00
|
|
|
|
|
2021-01-13 01:24:27 -05:00
|
|
|
|
/* Checks to see if this is the first time the application has run.
|
|
|
|
|
* by counting the number of guardians in the guardian table.
|
2021-01-17 20:57:46 -05:00
|
|
|
|
* INPUT: void from program, count of guardians from db.
|
|
|
|
|
* OUTPUT: boolean true/false.
|
2021-01-13 01:24:27 -05:00
|
|
|
|
*/
|
|
|
|
|
internal bool isFirstTimeRun()
|
|
|
|
|
{
|
|
|
|
|
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
|
|
|
|
|
{
|
|
|
|
|
string strQuery = "SELECT COUNT(*) FROM Guardians";
|
|
|
|
|
int num_guardians = cnn.Query<int>(strQuery).SingleOrDefault();
|
|
|
|
|
|
|
|
|
|
if (num_guardians > 0)
|
|
|
|
|
return false;
|
|
|
|
|
else
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-15 00:38:58 -05:00
|
|
|
|
// ***************** Update *****************
|
|
|
|
|
// ***************** Delete *****************
|
|
|
|
|
#endregion
|
2021-01-13 01:24:27 -05:00
|
|
|
|
|
2021-01-12 01:34:14 -05:00
|
|
|
|
#region reports
|
2021-01-15 00:38:58 -05:00
|
|
|
|
// ***************** Create *****************
|
|
|
|
|
// ***************** Read *******************
|
|
|
|
|
// ***************** Update *****************
|
|
|
|
|
// ***************** Delete *****************
|
2021-01-12 01:34:14 -05:00
|
|
|
|
#endregion
|
|
|
|
|
|
2021-01-17 20:57:46 -05:00
|
|
|
|
// Gets the connection string to the db and returns it to the program.
|
2021-01-12 01:34:14 -05:00
|
|
|
|
private static string LoadConnectionString(string id = "Default")
|
|
|
|
|
{
|
|
|
|
|
return ConfigurationManager.ConnectionStrings[id].ConnectionString;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|