Ejemplo n.º 1
0
        public bool UpdateAnimal(M.Animal animal)
        {
            bool animalUpdated = false;

            try
            {
                M.Animal matchedAnimal = _dbContext.Animals.SingleOrDefault(a => a.Id == animal.Id);
                if (matchedAnimal != null)
                {
                    // Detach first entity (there cannot be two entities with same id being searched in the DB at the same time)
                    _dbContext.Entry(matchedAnimal).State = EntityState.Detached;
                    _dbContext.Animals.Update(animal);
                    _dbContext.SaveChanges();
                    animalUpdated = true;
                }
                else
                {
                    Debug.WriteLine("Animal not found!");
                }
            }
            catch (Exception err)
            {
                Debug.WriteLine(err.Message);
            }
            return(animalUpdated);
        }
Ejemplo n.º 2
0
 public IActionResult Edit(int id)
 {
     M.Animal animal = _animalRepository.GetAnimalById(id);
     if (animal == null)
     {
         Debug.WriteLine("Animal not found!");
         return(RedirectToAction("Index"));
     }
     return(View(animal));
 }
Ejemplo n.º 3
0
 public M.Animal GetAnimalById(int id)
 {
     M.Animal animal = new M.Animal();
     try
     {
         animal = _dbContext.Animals.SingleOrDefault(a => a.Id == id);
     }
     catch (Exception err)
     {
         Debug.WriteLine(err.Message);
     }
     return(animal);
 }
Ejemplo n.º 4
0
        public bool InsertAnimal(M.Animal animal)
        {
            bool animalInserted = false;

            try
            {
                _dbContext.Animals.Add(animal);
                _dbContext.SaveChanges();
                animalInserted = true;
            }
            catch (Exception err)
            {
                Debug.WriteLine($"Error: {err.Message}");
            }
            return(animalInserted);
        }
Ejemplo n.º 5
0
        public bool RemoveAnimal(int id)
        {
            bool animalRemoved = false;

            try
            {
                M.Animal animal = _dbContext.Animals.SingleOrDefault(a => a.Id == id);
                _dbContext.Animals.Remove(animal);
                _dbContext.SaveChanges();
                animalRemoved = true;
            }
            catch (Exception err)
            {
                Debug.WriteLine(err.Message);
            }
            return(animalRemoved);
        }
Ejemplo n.º 6
0
 public IActionResult Edit(M.Animal animal)
 {
     if (ModelState.IsValid)
     {
         bool animalUpdated = _animalRepository.UpdateAnimal(animal);
         if (animalUpdated)
         {
             Debug.WriteLine("Animal Updated!");
             return(RedirectToAction("Index"));
         }
         else
         {
             Debug.WriteLine("Failed to update animal!");
             return(View(animal));
         }
     }
     return(View(animal));
 }
Ejemplo n.º 7
0
 public IActionResult Insert(ViewAnimalInsert animal)
 {
     //Request
     if (ModelState.IsValid)
     {
         M.Animal newAnimal = new M.Animal()
         {
             Name      = animal.Name, Genus = animal.Genus,
             SubSpecie = animal.SubSpecie, Specie = animal.Specie
         };
         bool animalInserted = _animalRepository.InsertAnimal(newAnimal);
         if (animalInserted)
         {
             Debug.WriteLine("Animal inserted!");
         }
         else
         {
             Debug.WriteLine("Fail to insert animal!");
         }
     }
     return(View());
 }