From 1900c183890c156794e8a7285ed12a13d647ad90 Mon Sep 17 00:00:00 2001 From: kougyokugentou <41278462+kougyokugentou@users.noreply.github.com> Date: Fri, 29 Jan 2021 22:12:50 -0800 Subject: [PATCH] Revision: Change FirstTimeRun to GetNumAdmins Add: Sanity check to see if trying to remove last known admin. --- frmGuardianCrud.cs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/frmGuardianCrud.cs b/frmGuardianCrud.cs index 6c61616..93d474b 100644 --- a/frmGuardianCrud.cs +++ b/frmGuardianCrud.cs @@ -38,7 +38,7 @@ namespace GreatHomeChildcare /* If it's the first time run of this program * then setup the initial admin user. */ - if(SqliteDataAccess.isFirstTimeRun()) + if(SqliteDataAccess.GetNumAdmins() <= 0) { isAdminComboBox.SelectedItem = IsAdmin.Yes; isAdminComboBox.Enabled = false; @@ -89,8 +89,7 @@ namespace GreatHomeChildcare private void btnSaveClose_Click(object sender, EventArgs e) { Guardian checkExistingGuardian = new Guardian(); - - MessageBox.Show("Save and close clicked"); + int num_admins; this.Validate(); this.ValidateChildren(); @@ -107,13 +106,13 @@ namespace GreatHomeChildcare checkExistingGuardian = SqliteDataAccess.GetGuardianByPin(Int32.Parse(strPin)); - if(checkExistingGuardian != null) + //If this is a new guardian, check to see if that pin is in use. + if(idNumericUpDown.Value == 0 && checkExistingGuardian != null) { MessageBox.Show("Please choose a different PIN number.", "Great Home Childcare", MessageBoxButtons.OK, MessageBoxIcon.None); return; } - guardian.id = Int32.Parse(idNumericUpDown.Value.ToString()); guardian.LastName = lastNameTextBox.Text; guardian.FirstName = firstNameTextBox.Text; @@ -127,6 +126,18 @@ namespace GreatHomeChildcare } else { + //Check to see if no admins will be left over. + num_admins = SqliteDataAccess.GetNumAdmins(); + num_admins--; + + if ((IsAdmin)isAdminComboBox.SelectedItem == IsAdmin.No + && num_admins <= 0) + { + MessageBox.Show("You are removing the last known admin, that would break this program.\n\rThe IsAdmin selection has changed to YES, please re-check the form entry then resubmit.", "Great Home Childcare", MessageBoxButtons.OK, MessageBoxIcon.None); + isAdminComboBox.SelectedItem = IsAdmin.Yes; + return; + } + SqliteDataAccess.UpdateGuardian(guardian); }