Exemple #1
0
        //if admin clicks add then get text box information, validate it and add it to the database
        private void btnAdd_Click(object sender, EventArgs e)
        {
            string title         = txtTitle.Text;
            bool   validQuantity = Int32.TryParse(txtQuantity.Text, out var quantity);
            bool   validDate     = DateTime.TryParse(txtReleaseDate.Text, out var releaseDate);
            string console       = txtConsole.Text;
            bool   validPrice    = Double.TryParse(txtPrice.Text, out var price);
            bool   success       = false;

            //validate product input and get any error messages that may be relevant
            var  pv            = new ProductValidation();
            bool validateInput = pv.ValidationMessages(title, validQuantity, validDate, console, validPrice);

            //only insert into database if all data has been validated
            if (validateInput)
            {
                var pd = new ProductDatabase();
                success = pd.InsertGameInfo(title, quantity, releaseDate, console, price);
            }

            //if successfully inserted into the database, then let user know and reload data
            if (success)
            {
                AdminControls.AdminSetup(); //function that fills admin panel with newly updated data
                MessageBox.Show($"{quantity} of {title} added to the inventory.");
                Close();
            }
            else if (!success && validateInput)
            {
                MessageBox.Show("There was an issue adding this product."); //show user a message if there's an error.
            }
        }
        //if admin decides to edit, then validate the inputs, update in database and update data seen by admin
        private void bttEdit_Click(object sender, EventArgs e)
        {
            string[] productArr    = boxWhichProduct.Text.Split('~');
            string   gameId        = productArr[0].Trim();
            string   title         = txtTitle.Text;
            bool     validQuantity = Int32.TryParse(txtQuantity.Text, out var quantity);
            bool     validDate     = DateTime.TryParse(txtReleaseDate.Text, out var releaseDate);
            string   console       = txtConsole.Text;
            bool     validPrice    = Double.TryParse(txtPrice.Text, out var price);
            bool     success       = false;

            //validate product input and get any error messages that may be relevant
            var  pv            = new ProductValidation();
            bool validateInput = pv.ValidationMessages(title, validQuantity, validDate, console, validPrice);

            //if inputs are valid then update the database
            if (validateInput)
            {
                var pd = new ProductDatabase();
                success = pd.UpdateGameInfo(gameId, title, quantity, releaseDate, console, price);
            }

            //if the database has successfully updated then update admins data
            if (success)
            {
                AdminControls.AdminSetup();
                MessageBox.Show($"{gameId} ~ {title} was edited successfully.");
                Close();
            }
            else if (!success && validateInput)
            {
                MessageBox.Show("We are not able to update this product at this time.");
            }
        }