Example #1
0
        public async Task <IActionResult> Create([Bind("AnimalID,Name,DateOfBirth,Disinfection,Neutered,Description,Foto,Picture,AnimalTypeFK,AnimalRaceFK,OwnerFK")] Animal animal)
        {
            if (!GetAuthorization(8, 'c'))
            {
                return(NotFound());
            }
            ViewBag.Permission = getPermissions();
            if (!checkValues(animal))
            {
                setViewBags(-1);
                return(View(animal));
            }
            if (ModelState.IsValid)
            {
                string   selectedProducts     = Request.Form["checkProduct"].ToString();
                string[] selectedProductsList = selectedProducts.Split(',');

                string   selectedGodfathers    = Request.Form["checkGodfather"].ToString();
                string[] selectedGodfatherList = selectedGodfathers.Split(',');

                Animal animalAdd = new Animal()
                {
                    Name         = animal.Name,
                    DateOfBirth  = animal.DateOfBirth,
                    Disinfection = animal.Disinfection,
                    Neutered     = animal.Neutered,
                    Description  = animal.Description,
                    AnimalTypeFK = animal.AnimalTypeFK,
                    AnimalRaceFK = animal.AnimalRaceFK,
                    OwnerFK      = animal.OwnerFK
                };
                _context.Add(animalAdd);
                _context.SaveChanges();
                if (animal.Foto != null)
                {
                    System.IO.Directory.CreateDirectory(hostingEnvironment.WebRootPath + "/images/Galeria_" + animalAdd.AnimalID);
                    var uniqueFileName = GetUniqueFileName(animal.Foto.FileName);
                    var uploads        = Path.Combine(hostingEnvironment.WebRootPath, "images/Galeria_" + animalAdd.AnimalID);
                    var filePath       = Path.Combine(uploads, uniqueFileName);

                    animal.Foto.CopyTo(new FileStream(filePath, FileMode.Create));

                    Images image = new Images()
                    {
                        AnimalFK           = animalAdd.AnimalID,
                        Name               = animal.Foto.Name,
                        Length             = animal.Foto.Length,
                        FileName           = uniqueFileName,
                        ContentType        = animal.Foto.ContentType,
                        ContentDisposition = animal.Foto.ContentDisposition
                    };
                    _context.Add(image);
                    _context.SaveChanges();
                    //to do : Save uniqueFileName  to your db table
                }

                if (selectedProductsList[0] != "")
                {
                    foreach (var temp in selectedProductsList)
                    {
                        int           prodKey = Convert.ToInt32(temp);
                        AnimalProduct aniProd = new AnimalProduct()
                        {
                            AnimalFK  = animalAdd.AnimalID,
                            ProductFK = prodKey
                        };
                        _context.Add(aniProd);
                        _context.SaveChanges();
                    }
                }

                if (selectedGodfatherList[0] != "")
                {
                    foreach (var key in selectedGodfatherList)
                    {
                        int         godkey   = Convert.ToInt32(key);
                        AnimalUsers aniUsers = new AnimalUsers()
                        {
                            AnimalFK = animalAdd.AnimalID,
                            UsersFK  = godkey
                        };
                        _context.Add(aniUsers);
                        _context.SaveChanges();
                    }
                }
                await _context.SaveChangesAsync();

                TempData["Message"] = "Animal criado com sucesso!";
                return(RedirectToAction(nameof(Index)));
            }
            setViewBags(-1);
            return(View(animal));
        }
Example #2
0
        public async Task <IActionResult> Edit(int id, [Bind("AnimalID,Name,DateOfBirth,Disinfection,Neutered,Description,Foto,Picture,AnimalTypeFK,AnimalRaceFK,OwnerFK")] Animal animal)
        {
            if (!GetAuthorization(8, 'u'))
            {
                return(NotFound());
            }
            ViewBag.Permission = getPermissions();
            if (id != animal.AnimalID)
            {
                return(NotFound());
            }
            if (!checkValues(animal))
            {
                setViewBags(id);
                return(View(animal));
            }
            if (ModelState.IsValid)
            {
                try
                {
                    string   selectedProducts     = Request.Form["checkProduct"].ToString();
                    string[] selectedProductsList = selectedProducts.Split(',');

                    string   selectedGodfathers    = Request.Form["checkGodfather"].ToString();
                    string[] selectedGodfatherList = selectedGodfathers.Split(',');

                    if (animal.Foto != null)
                    {
                        var result = _context.Images.Where(e => e.AnimalFK == animal.AnimalID);
                        if (result.Any())
                        {
                            Images old = result.First();
                            System.IO.File.Delete(hostingEnvironment.WebRootPath + "/images/Galeria_" + animal.AnimalID + "/" + old.FileName);
                            _context.Images.Remove(old);
                            _context.SaveChanges();
                        }
                        else
                        {
                            System.IO.Directory.CreateDirectory(hostingEnvironment.WebRootPath + "/images/Galeria_" + animal.AnimalID);
                        }

                        var uploads        = Path.Combine(hostingEnvironment.WebRootPath, "images/Galeria_" + animal.AnimalID);
                        var uniqueFileName = GetUniqueFileName(animal.Foto.FileName);
                        var filePath       = Path.Combine(uploads, uniqueFileName);

                        animal.Foto.CopyTo(new FileStream(filePath, FileMode.Create));

                        Images image = new Images()
                        {
                            AnimalFK           = animal.AnimalID,
                            Name               = animal.Foto.Name,
                            Length             = animal.Foto.Length,
                            FileName           = uniqueFileName,
                            ContentType        = animal.Foto.ContentType,
                            ContentDisposition = animal.Foto.ContentDisposition
                        };
                        _context.Add(image);
                        _context.SaveChanges();
                    }

                    _context.Update(animal);
                    _context.SaveChanges();

                    removeAnimalProducts(id);
                    if (selectedProductsList[0] != "")
                    {
                        foreach (var temp in selectedProductsList)
                        {
                            int           prodKey = Convert.ToInt32(temp);
                            AnimalProduct aniProd = new AnimalProduct()
                            {
                                AnimalFK  = id,
                                ProductFK = prodKey
                            };
                            _context.Add(aniProd);
                            _context.SaveChanges();
                        }
                    }
                    removeGodFathers(id);
                    if (selectedGodfatherList[0] != "")
                    {
                        foreach (var temp in selectedGodfatherList)
                        {
                            int         godkey   = Convert.ToInt32(temp);
                            AnimalUsers aniUsers = new AnimalUsers()
                            {
                                AnimalFK = id,
                                UsersFK  = godkey
                            };

                            _context.Add(aniUsers);
                            _context.SaveChanges();
                        }
                    }
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AnimalExists(animal.AnimalID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                TempData["Message"] = "Animal editado com sucesso!";
                return(RedirectToAction(nameof(Index)));
            }
            setViewBags(id);
            return(View(animal));
        }