Пример #1
0
 private string ValidateData()
 {
     if (SuburbName.Length == 0 || SuburbName == null)
     {
         return("Please enter a suburb Name");
     }
     if (SuburbList.Contains(new Suburb(SuburbName)))
     {
         return("Selected suburb already exists");
     }
     return(null);
 }
 private void UpdateBothSuburbsList()
 {
     if (SelectedContractor != null)
     {
         ContractorPreferredSuburbList = DAL.PreferredSuburbsList(SelectedContractor);
         UnselectedSuburbList          = DAL.UnselectedSuburbsList(SelectedContractor);
     }
     else
     {
         ContractorPreferredSuburbList = new SuburbList();
         UnselectedSuburbList          = DAL.GetSuburbs();
     }
 }
Пример #3
0
        /// <summary>
        /// Returns an ObservableCollection of suburbs from database.
        /// </summary>
        /// <returns></returns>
        public static SuburbList GetSuburbs()
        {
            string storedProcedureText = "usp_getSuburbs";

            MySqlDataReader result = ExecuteReader(storedProcedureText);

            SuburbList suburbList = new SuburbList();

            while (result.Read())
            {
                suburbList.Add(new Suburb(result.GetString("suburbName")));
            }

            result.Close();

            return(suburbList);
        }
Пример #4
0
        public static SuburbList UnselectedSuburbsList(Contractor contractor)
        {
            string storedProcedureName = "usp_getContractorUnselectedSuburbs";

            MySqlParameter[] parameterList = new MySqlParameter[]
            {
                new MySqlParameter("contractorIDIn", contractor.ContractorID)
            };

            MySqlDataReader result = ExecuteReader(storedProcedureName, parameterList);

            SuburbList suburbList = new SuburbList();

            while (result.Read())
            {
                suburbList.Add(new Suburb(result.GetString("SuburbName")));
            }

            result.Close();

            return(suburbList);
        }
        private void SaveButton()
        {
            string validateMessage = ValidateData();

            if (validateMessage == null)
            {
                if (SelectedContractor != null)                // Are we  in update mode?
                {
                    try
                    {
                        Contractor updatedContractor = new Contractor(
                            SelectedContractor.ContractorID,
                            ContractorFirstName,
                            ContractorLastName,
                            ContractorAddress,
                            ContractorState,
                            ContractorSuburb,
                            ContractorMobile,
                            ContractorEmail,
                            ContractorPreferredSuburbList,
                            ContractorHasSkillList
                            );
                        DAL.UpdateContractor(updatedContractor);
                        new EventLogger().Log("Updated Contractor in database");


                        // Update Preferred Suburbs
                        // Grab the list of preferred suburbs from database
                        SuburbList DBPreferredSuburbList = DAL.PreferredSuburbsList(updatedContractor);
                        // Compare each entry from contractor to DB, if it does not exist in DB, add it
                        foreach (Suburb contractorSuburb in updatedContractor.PreferredSuburbList)
                        {
                            bool found = false;
                            foreach (Suburb dbSuburb in DBPreferredSuburbList)
                            {
                                if (contractorSuburb.SuburbName == dbSuburb.SuburbName)
                                {
                                    found = true;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                DAL.AddPreferredSuburb(updatedContractor, contractorSuburb);
                            }
                        }
                        // Compare each entry from db to contractor, if it doesn't exist in contractor, remove it
                        foreach (Suburb dbSuburb in DBPreferredSuburbList)
                        {
                            bool found = false;
                            foreach (Suburb contractorSuburb in updatedContractor.PreferredSuburbList)
                            {
                                if (dbSuburb.SuburbName == contractorSuburb.SuburbName)
                                {
                                    found = true;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                DAL.RemovePreferredSuburb(updatedContractor, dbSuburb);
                            }
                        }


                        // Update Skills
                        // Get list of preferred Skills from database
                        SkillList DBSkillsList = DAL.HasSkillList(updatedContractor);
                        // Compare each entry from contractor to DB, if it does not exist in DB, add it
                        foreach (Skill contractorSkill in updatedContractor.HasSkillList)
                        {
                            bool found = false;
                            foreach (Skill dbSkill in DBSkillsList)
                            {
                                if (contractorSkill.SkillName == dbSkill.SkillName)
                                {
                                    found = true;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                DAL.AddHasSkill(updatedContractor, contractorSkill);
                            }
                        }
                        // Compare each entry from db to contractor, if it doesn't exist in contractor, remove it
                        foreach (Skill dbSkill in DBSkillsList)
                        {
                            bool found = false;
                            foreach (Skill contractorSkill in updatedContractor.HasSkillList)
                            {
                                if (dbSkill.SkillName == contractorSkill.SkillName)
                                {
                                    found = true;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                DAL.RemoveHasSkill(updatedContractor, dbSkill);
                            }
                        }
                    }
                    catch (MySqlException)
                    {
                        MessageBox.Show("Failed to save contractor, their skills or their suburbs", "Saving Failed", MessageBoxButtons.OK);
                    }
                }
                else                 // No we are in add mode
                {
                    try
                    {
                        Contractor newContractor = new Contractor(
                            ContractorFirstName,
                            ContractorLastName,
                            ContractorAddress,
                            ContractorState,
                            ContractorSuburb,
                            ContractorMobile,
                            ContractorEmail,
                            ContractorPreferredSuburbList,
                            ContractorHasSkillList
                            );
                        DAL.InsertContractor(newContractor);
                        new EventLogger().Log("Inserted Contractor into database");

                        // Insert all suburbs
                        foreach (Suburb suburb in newContractor.PreferredSuburbList)
                        {
                            DAL.AddPreferredSuburb(newContractor, suburb);
                        }

                        // Insert all skills
                        foreach (Skill skill in newContractor.HasSkillList)
                        {
                            DAL.AddHasSkill(newContractor, skill);
                        }
                    }
                    catch (MySqlException)
                    {
                        MessageBox.Show("Failed to save contractor", "Saving Failed", MessageBoxButtons.OK);
                    }
                }
                SelectedContractor = null;
                ResetDataEntry();
                UpdateContractorList();
                DataEntryAllowed      = false;
                ContractorListEnabled = true;
            }
            else
            {
                System.Windows.Forms.MessageBox.Show(validateMessage, "Saving Failed", MessageBoxButtons.OK);
            }
        }