Esempio n. 1
0
        private void btnDeleteCrudShop_Click(object sender, EventArgs e)
        {
            ProductDAL   productDAL = new ProductDAL();
            var          product    = productDAL.GetByFilter(x => x.Id == Convert.ToInt32(dgvShop.CurrentRow.Cells[0].Value));
            DialogResult result     = MessageBox.Show("Are you sure?", "Warning", MessageBoxButtons.OKCancel);

            if (result == DialogResult.OK)
            {
                if (product.UserId == CurrentUser.Id || CurrentUser.Type == 2)
                {
                    if (CurrentUser.Type == 2)
                    {
                        LogDAL LogDAL        = new LogDAL();
                        bool   productExists = LogDAL.ProductExists(Convert.ToInt32(dgvShop.CurrentRow.Cells[0].Value));
                        Log    log           = new Log()
                        {
                            UserId       = CurrentUser.Id,
                            Description  = txbDescriptionCrudProducts.Text,
                            ProductId    = Convert.ToInt32(dgvShop.CurrentRow.Cells[0].Value),
                            Type         = Convert.ToInt32(LogTypeEnum.Delete),
                            ModifiedDate = DateTime.Now
                        };
                        if (productExists)
                        {
                            int logId = LogDAL.GetByFilter(x => x.ProductId == Convert.ToInt32(dgvShop.CurrentRow.Cells[0].Value)).Id;
                            log.Id = logId;
                        }
                        bool isValid = ValidationOperation <Log> .ValidateOperation(log);

                        if (isValid)
                        {
                            productDAL.Delete(Convert.ToInt32(product.Id));
                            LogDAL.Update(log);
                            MessageBox.Show("Successfully Deleted");
                            productDAL.GetGridData(dgvShop);
                        }
                    }
                    else
                    {
                        productDAL.Delete(Convert.ToInt32(product.Id));
                        MessageBox.Show("Successfully Deleted");
                        productDAL.GetGridData(dgvShop);
                    }
                }
                else
                {
                    MessageBox.Show("You are not allowed to delete others' products");
                }
            }
        }
Esempio n. 2
0
        private void btnUpdateCrudShop_Click(object sender, EventArgs e)
        {
            ProductDAL productDAL = new ProductDAL();
            UserDAL    userDAL    = new UserDAL();
            var        product    = productDAL.GetByFilter(x => x.Id == Convert.ToInt32(dgvShop.CurrentRow.Cells[0].Value));

            if (cmbCategoryCrudShop.Text != "" && txbCountCrudShop.Text != "" && txbPriceCrudShop.Text != "")
            {
                if (product.UserId == CurrentUser.Id || CurrentUser.Type == 2)
                {
                    Product productItem = new Product()
                    {
                        Id         = Convert.ToInt32(dgvShop.CurrentRow.Cells[0].Value),
                        Name       = this.txbNameCrudShop.Text,
                        CategoryId = Convert.ToInt32(cmbCategoryCrudShop.Text.Split('-')[0]),
                        Count      = Convert.ToInt32(this.txbCountCrudShop.Text),
                        Price      = Convert.ToDouble(this.txbPriceCrudShop.Text),
                        UserId     = product.UserId,
                        Status     = (int)ProductStatusEnum.Active,
                    };
                    if (CurrentUser.Type == 2)
                    {
                        LogDAL LogDAL        = new LogDAL();
                        bool   productExists = LogDAL.ProductExists(Convert.ToInt32(dgvShop.CurrentRow.Cells[0].Value));
                        Log    log           = new Log()
                        {
                            UserId       = CurrentUser.Id,
                            Description  = txbDescriptionCrudProducts.Text,
                            ProductId    = Convert.ToInt32(dgvShop.CurrentRow.Cells[0].Value),
                            Type         = Convert.ToInt32(LogTypeEnum.Update),
                            ModifiedDate = DateTime.Now
                        };
                        if (productExists)
                        {
                            int logId = LogDAL.GetByFilter(x => x.ProductId == Convert.ToInt32(dgvShop.CurrentRow.Cells[0].Value)).Id;
                            log.Id = logId;
                        }
                        bool isValid = ValidationOperation <Log> .ValidateOperation(log);

                        if (isValid)
                        {
                            product.UserId = userDAL.GetByFilter(x => x.Email == this.cmbUserCrudProducts.Text.Split('-')[0]).Id;
                            productDAL.Update(productItem);
                            MessageBox.Show("Successfully Updated");
                            LogDAL.Update(log);
                            productDAL.GetGridData(dgvShop);
                        }
                    }
                    else
                    {
                        productDAL.Update(productItem);
                        MessageBox.Show("Successfully Updated");
                        productDAL.GetGridData(dgvShop);
                    }
                }
                else
                {
                    MessageBox.Show("You are not allowed to update others' products");
                }
            }
            else
            {
                MessageBox.Show("The field is required");
            }
        }