Exemplo n.º 1
0
        /**
         * @desc This method will save the object into the database
         * @return [bool] Returns true in case of success, false if there was a problem
         */
        public bool SaveMember()
        {
            // Convert date into mysql format
            string mysqlDate = Utils.sGetMysqlDate(this.Birthdate);
            string query;

            // Check Birthdate format
            if (mysqlDate == "0000-00-00")
            {
                MessageBox.Show("The Date of Birth is in incorrect format!");
            }
            // Check e-mail format
            else if (Utils.bValidateEmail(this.Email) == false)
            {
                MessageBox.Show("The E-Mail address is incorrect!");
            }
            else
            {
                // First the user object is filled
                clUser.IsActive = (this.IsActive) ? true : false;
                clUser.Login = this.Email;
                clUser.Password = mysqlDate;
                clUser.Profile = "member";
                // Create mysql connection
                mySqlConn conn = new mySqlConn();
                conn.connect();

                // If the User details were correctly saved
                if (clUser.SaveUser())
                {
                    // Check if there is a new picture to save
                    if ((this.FilePath != null) && (this.FilePath.Length > 1))
                    {
                        this.Id_file = conn.uploadFileToDB(this.FilePath, this.FileName);
                    }

                    // The insert query is launched in case of existing members only, not new members
                    if (this.Id_member == -1)
                    {
                        // Create insert query
                        query = "insert into `gym`.`members` (`id_member`, `firstName`, `lastName`, `birthdate`, `address_1`, `city`, `county`, `postalcode`, `type`, `id_user`, `is_active`, `address_2`, `emerg_contact_name`, `emerg_contact_relation`, `emerg_contact_phone`, `emerg_contact_mobile`, `medical_allergies`, `medical_notes`, `id_file`, `medical_doctor_name`, `medical_phone`, `email`, `member_number`, `phone`,`mobile`,`gender`) values " +
                                 "(NULL, '" + this.FirstName + "', '" + this.LastName + "', '" + mysqlDate + "', '" + this.Address_1 + "', '" + this.City + "', '" + this.County + "', '" + this.PostalCode + "', '" + this.Type + "', '" + clUser.Id_user + "', '" + ((this.IsActive) ? "1" : "0") + "', '" + this.Address_2 + "', '" + this.EmergContactName + "', '" + this.EmergContactRelation + "', '" + this.EmergContactPhone + "', '" + this.EmergContactMobile + "', '" + this.MedicalAllergies + "', '" + this.MedicalNotes + "', '" + this.Id_file + "', '" + this.MedicalDoctorName + "', '" + this.MedicalPhone + "', '" + this.Email + "', '" + this.MemberNumber + "','" + this.Phone + "','" + this.Mobile + "','" + this.Gender + "')";

                        // Launch insert query
                        int id_member = conn.InsertToDB(query);
                        // Check if the insert was successful
                        if (id_member != -1)
                        {
                            this.Id_member = id_member;
                            MessageBox.Show("The new member has been added to the databse succesfully!");
                            return true;
                        }
                        else
                        {
                            MessageBox.Show("There was a problem adding the new user, please check your data!");
                            clUser.DeleteUser();
                            return false;
                        }
                    }
                    // This is a member update
                    else
                    {
                        // Create update query
                        query = "UPDATE members SET firstName = '" + this.FirstName + "', lastName = '" + this.LastName + "', birthdate = '" + mysqlDate + "', address_1 = '" + this.Address_1 + "', city = '" + this.City + "', county = '" + this.County + "', postalcode = '" + this.PostalCode + "', type = '" + this.Type + "', is_active = " + ((this.IsActive) ? "1" : "0") + ", address_2 = '" + this.Address_2 + "', emerg_contact_name = '" + this.EmergContactName + "', emerg_contact_relation = '" + this.EmergContactRelation + "', emerg_contact_phone = '" + this.EmergContactPhone + "', emerg_contact_mobile = '" + this.EmergContactMobile + "', medical_allergies = '" + this.MedicalAllergies + "', medical_notes = '" + this.MedicalNotes + "', id_file = '" + this.Id_file + "', medical_doctor_name = '" + this.MedicalDoctorName + "', medical_phone = '" + this.MedicalPhone + "', email = '" + this.Email + "', phone = '" + this.Phone + "', mobile = '" + this.Mobile +
                            "', gender = '" + this.Gender + "' " + " WHERE id_member = '"+this.Id_member+"'";
                        // Launch update query
                        int result = conn.DeleteOrUpdate(query);
                        // Check if the update was successful
                        if (result > 0)
                        {
                            MessageBox.Show("The member data has been updated succesfully!");
                            return true;
                        }
                        else
                        {
                            MessageBox.Show("There was a problem updating the user information, please check your data!");
                            clUser.DeleteUser();
                            return false;
                        }
                    }
                }
                // If the user saving was false, then it was becuase of duplicate e-mail at this point
                else
                {
                    MessageBox.Show("The e-mail already exists in the database! Please choose another one.");
                    return false;
                }

            }
             return false;
        }