public async Task <IActionResult> Create([Bind("DonationID,DateOfDonation,UsersFK")] DonationIndexViewModel donationIVM)
        {
            if (!GetAuthorization(5, 'c'))
            {
                return(NotFound());
            }
            ViewBag.Permission = getPermissions();
            if (!checkValues(donationIVM))
            {
                return(Redirect(nameof(Create)));
            }

            if (ModelState.IsValid)
            {
                string   selected     = Request.Form["checkProduct"].ToString();
                string[] selectedList = selected.Split(',');
                Donation donation     = new Donation
                {
                    DateOfDonation = donationIVM.DateOfDonation,
                    UsersFK        = donationIVM.UsersFK
                };
                _context.Add(donation);
                _context.SaveChanges();

                if (selectedList[0] != "")
                {
                    foreach (var temp in selectedList)
                    {
                        int prodKey  = Convert.ToInt32(temp);
                        int newQuant = 0;
                        if (Request.Form["quantityProduct " + Convert.ToInt32(temp)] != "")
                        {
                            newQuant = Convert.ToInt32(Request.Form["quantityProduct " + Convert.ToInt32(temp)].ToString());
                        }
                        DonationProduct donationProduct = new DonationProduct
                        {
                            DonationFK = donation.DonationID,
                            ProductFK  = prodKey,
                            Quantity   = newQuant
                        };
                        var prod = new Product
                        {
                            ProductID = prodKey
                        };
                        prod.Quantity = _context.Products.Where(e => e.ProductID == prodKey).FirstOrDefault().Quantity + newQuant;
                        _context.Entry(prod).Property("Quantity").IsModified = true;
                        _context.SaveChanges();
                        _context.Add(donationProduct);
                    }
                }

                await _context.SaveChangesAsync();

                TempData["Message"] = "Doacao criada com sucesso!";
                return(RedirectToAction(nameof(Index)));
            }
            return(View(donationIVM));
        }
        public async Task <IActionResult> Edit(int id, [Bind("DonationID,DateOfDonation,UsersFK")] DonationIndexViewModel donationIVM)
        {
            /*if (id != donationIVM.EditDonation.DonationID)
             * {
             *  return NotFound();
             * }*/
            if (!GetAuthorization(5, 'u'))
            {
                return(NotFound());
            }
            ViewBag.Permission = getPermissions();
            if (!checkValues(donationIVM))
            {
                return(Redirect(nameof(Edit)));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    string   selected     = Request.Form["checkProduct"].ToString();
                    string[] selectedList = selected.Split(',');

                    Donation toEdit = _context.Donation.FirstOrDefault(e => e.DonationID == id);
                    toEdit.DateOfDonation = donationIVM.DateOfDonation;
                    toEdit.UsersFK        = donationIVM.UsersFK;
                    _context.Update(toEdit);
                    _context.SaveChanges();

                    var x           = 0;
                    var allToRemove = _context.DonationProduct.Where(p => p.DonationFK == id);

                    foreach (DonationProduct don in allToRemove)
                    {
                        var result3 = _context.Products.SingleOrDefault(p => p.ProductID == don.ProductFK);

                        if (result3 != null)
                        {
                            result3.Quantity -= don.Quantity;
                            _context.DonationProduct.Remove(don);
                        }
                    }

                    if (selectedList[0] != "")
                    {
                        foreach (var temp in selectedList)
                        {
                            int prodKey  = Convert.ToInt32(temp);
                            int newQuant = 0;
                            if (Request.Form["quantityProduct " + Convert.ToInt32(temp)] != "")
                            {
                                newQuant = Convert.ToInt32(Request.Form["quantityProduct " + Convert.ToInt32(temp)].ToString());
                            }
                            DonationProduct donationProduct = new DonationProduct
                            {
                                DonationFK = id,
                                ProductFK  = prodKey,
                                Quantity   = newQuant
                            };
                            x++;
                            var quantityToRemove = allToRemove.Where(p => p.ProductFK == prodKey && p.DonationFK == id).ToList();
                            var result           = _context.Products.SingleOrDefault(p => p.ProductID == prodKey);
                            if (result != null)
                            {
                                result.Quantity += newQuant;
                                _context.SaveChanges();
                            }

                            _context.SaveChanges();
                            _context.Add(donationProduct);
                        }
                    }
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!DonationExists(donationIVM.EditDonation.DonationID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                TempData["Message"] = "Doacao editada com sucesso!";
                return(RedirectToAction(nameof(Index)));
            }
            return(View(donationIVM));
        }