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));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> PutAnimal(int id, Animal animal)
        {
            if (id != animal.Id)
            {
                return(BadRequest());
            }

            _context.Entry(animal).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AnimalExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> PutShelter(int id, Shelter shelter)
        {
            if (id != shelter.Id)
            {
                return(BadRequest());
            }

            _context.Entry(shelter).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ShelterExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> PutCatchServiceEmployee(int id, CatchServiceEmployee catchServiceEmployee)
        {
            if (id != catchServiceEmployee.Id)
            {
                return(BadRequest());
            }

            _context.Entry(catchServiceEmployee).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CatchServiceEmployeeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 5
0
 public void Put(int id, [FromBody] Animal animal)
 {
     animal.AnimalId         = id;
     _db.Entry(animal).State = EntityState.Modified;
     _db.SaveChanges();
 }
Ejemplo n.º 6
0
 public void Put(int id, [FromBody] Cat cat)
 {
     cat.Id = id;
     _db.Entry(cat).State = EntityState.Modified;
     _db.SaveChanges();
 }
Ejemplo n.º 7
0
 public void Put(int id, [FromBody] Dog dog)
 {
     dog.Id = id;
     _db.Entry(dog).State = EntityState.Modified;
     _db.SaveChanges();
 }