/// <summary> /// Determines child type of IMaphawksDatabaseTable /// </summary> /// /// <param name="record">Represents a row in a table</param> /// /// <returns>Table enum type corresponding to a database table, if table type is invalid, returns Table.None</returns> public Table GetTable(IMaphawksDatabaseTable record) { if (!(record as Locations is null)) { return(Table.Locations); } if (!(record as Contacts is null)) { return(Table.Contacts); } if (!(record as SpecialQualities is null)) { return(Table.Special_Qualities); } if (!(record as DailyHours is null)) { return(Table.Daily_Hours); } return(Table.None); }
/// <summary> /// Alters location record information, whether it be through: /// CREATING a new record, /// UPDATING a prexisting record, /// DELETING a preexisting record, or /// RECOVERING a delete Locations record /// </summary> /// /// <param name="context"> MaphawksContext class object </param> /// <param name="action"> AlterRecordInfoEnum type. Create, Update, Delete or Recover</param> /// <param name="table"> Maphawks Database table (model class) </param> /// /// <returns> Returns true on success, otherwise returns false </returns> public virtual bool AlterRecordInfo(AlterRecordInfoEnum action, IMaphawksDatabaseTable table) { if (table is null) { return(false); } var table_type = GetTable(table); switch (table_type) { case Table.Locations: return(AlterLocations(action, table)); case Table.Contacts: return(AlterContacts(action, table)); case Table.Special_Qualities: return(AlterSpecialQualities(action, table)); case Table.Daily_Hours: return(AlterDailyHours(action, table)); default: return(false); } }
/// <summary> /// Performs Create, Update, and Delete actions on SpecialQualities table /// </summary> /// /// <param name="action"> AlterRecordInfoEnum type. Create, Update, Delete or Recover</param> /// <param name="table"> Maphawks Database table (model class) </param> /// /// <returns>False if error, true otherwise</returns> private bool AlterSpecialQualities(AlterRecordInfoEnum action, IMaphawksDatabaseTable table) { var special_qualities_record = (SpecialQualities)table; if (action is AlterRecordInfoEnum.Create) { try { context.Add(special_qualities_record); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } if (action is AlterRecordInfoEnum.Update) { try { context.Update(special_qualities_record); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } if (action is AlterRecordInfoEnum.Delete) { try { context.Remove(context.SpecialQualities.Single(c => c.LocationId.Equals(special_qualities_record.LocationId))); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } return(false); }
/// <summary> /// Performs Create, Update and Delete actions on DailyHours table /// </summary> /// /// <param name="action"> AlterRecordInfoEnum type. Create, Update, Delete or Recover</param> /// <param name="table"> Maphawks Database table (model class) </param> /// /// <returns>False if error, true otherwise</returns> private bool AlterDailyHours(AlterRecordInfoEnum action, IMaphawksDatabaseTable table) { var daily_hours_record = (DailyHours)table; if (action is AlterRecordInfoEnum.Create) { try { context.Add(daily_hours_record); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } if (action is AlterRecordInfoEnum.Update) { try { context.Update(daily_hours_record); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } if (action is AlterRecordInfoEnum.Delete) { try { context.Remove(context.DailyHours.Single(c => c.LocationId.Equals(daily_hours_record.LocationId))); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } return(false); }
/// <summary> /// Adds a row in Contacts, SpecialQualities, or DailyHours table if one didn't exist before an edit. /// Deletes a row in Contacts, SpecialQualities, or DailyHours table if after an edit an entire row's fields are null. /// </summary> /// /// <param name="referenceRow"> The row before the edit </param> /// <param name="editedRow"> The row after the edit </param> public virtual void _AddDeleteRow(IMaphawksDatabaseTable referenceRow, IMaphawksDatabaseTable editedRow) { if (referenceRow is null && editedRow is null || // Don't need to add a new row !(referenceRow is null) && !(editedRow is null)) { return; } // Add row if (referenceRow is null && !(editedRow is null)) { var table = GetTable(editedRow); switch (table) { case (Table.Contacts): AlterRecordInfo(AlterRecordInfoEnum.Create, (Contacts)editedRow); return; case (Table.Special_Qualities): AlterRecordInfo(AlterRecordInfoEnum.Create, (SpecialQualities)editedRow); return; case (Table.Daily_Hours): AlterRecordInfo(AlterRecordInfoEnum.Create, (DailyHours)editedRow); return; default: return; } } // Delete row if (!(referenceRow is null) && editedRow is null) { var table = GetTable(referenceRow); switch (table) { case (Table.Contacts): AlterRecordInfo(AlterRecordInfoEnum.Delete, (Contacts)referenceRow); return; case (Table.Special_Qualities): AlterRecordInfo(AlterRecordInfoEnum.Delete, (SpecialQualities)referenceRow); return; case (Table.Daily_Hours): AlterRecordInfo(AlterRecordInfoEnum.Delete, (DailyHours)referenceRow); return; default: return; } } }
/// <summary> /// Performs Create, Update, and Delete actions on Locations table /// </summary> /// /// <param name="action"> AlterRecordInfoEnum type. Create, Update, Delete or Recover</param> /// <param name="table"> Maphawks Database table (model class) </param> /// /// <returns>Fals if error, true otherwise.</returns> private bool AlterLocations(AlterRecordInfoEnum action, IMaphawksDatabaseTable table) { var locations_record = (Locations)table; if (action is AlterRecordInfoEnum.Create) { try { context.Add(locations_record); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } if (action is AlterRecordInfoEnum.Update) { try { context.Update(locations_record); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } if (action is AlterRecordInfoEnum.Delete) { try { locations_record.SoftDelete = true; context.Update(locations_record); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } if (action is AlterRecordInfoEnum.Recover) { try { locations_record.SoftDelete = false; context.Update(locations_record); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } return(false); }
/// <summary> /// Alters location record information, whether it be through: /// CREATING a new record, /// UPDATING a prexisting record, /// DELETING a preexisting record, or /// RECOVERING a delete Locations record /// </summary> /// /// /// <param name="context"> MaphawksContext class object </param> /// <param name="action"> AlterRecordInfoEnum type. Create, Update, Delete or Recover</param> /// <param name="table"> Maphawks Database table (model class) </param> /// /// <returns> Returns true on success, otherwise returns false </returns> public virtual bool AlterRecordInfo(AlterRecordInfoEnum action, IMaphawksDatabaseTable table) { if (table is null) { return(false); } var table_type = GetTable(table); switch (table_type) { #region Locations case Table.Locations: var locations_record = (Locations)table; if (action is AlterRecordInfoEnum.Create) { try { context.Add(locations_record); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } if (action is AlterRecordInfoEnum.Update) { try { context.Update(locations_record); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } if (action is AlterRecordInfoEnum.Delete) { try { locations_record.SoftDelete = true; context.Update(locations_record); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } if (action is AlterRecordInfoEnum.Recover) { try { locations_record.SoftDelete = false; context.Update(locations_record); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } return(false); #endregion #region Contact case Table.Contacts: var contact_record = (Contacts)table; if (action is AlterRecordInfoEnum.Create) { try { context.Add(contact_record); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } if (action is AlterRecordInfoEnum.Update) { try { context.Update(contact_record); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } if (action is AlterRecordInfoEnum.Delete) { try { context.Remove(context.Contacts.Single(c => c.LocationId.Equals(contact_record.LocationId))); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } return(false); #endregion #region Special Qualities case Table.Special_Qualities: var special_qualities_record = (SpecialQualities)table; if (action is AlterRecordInfoEnum.Create) { try { context.Add(special_qualities_record); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } if (action is AlterRecordInfoEnum.Update) { try { context.Update(special_qualities_record); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } if (action is AlterRecordInfoEnum.Delete) { try { context.Remove(context.SpecialQualities.Single(c => c.LocationId.Equals(special_qualities_record.LocationId))); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } return(false); #endregion #region Daily Hours case Table.Daily_Hours: var daily_hours_record = (DailyHours)table; if (action is AlterRecordInfoEnum.Create) { try { context.Add(daily_hours_record); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } if (action is AlterRecordInfoEnum.Update) { try { context.Update(daily_hours_record); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } if (action is AlterRecordInfoEnum.Delete) { try { context.Remove(context.DailyHours.Single(c => c.LocationId.Equals(daily_hours_record.LocationId))); context.SaveChanges(); return(true); } catch (DbUpdateException) { return(false); } } return(false); #endregion default: return(false); } }