public IActionResult Dashboard()
        {
            int? activeId = HttpContext.Session.GetInt32("activeUser");
            bool isValid  = SecurityCheck.CheckForActiveUser(activeId);

            if (isValid == false)
            {
                return(RedirectToAction("Index"));
            }
            PetValidation newPetModel = new PetValidation();

            newPetModel.AllBreeds = _context.breed.OrderByDescending(b => b.Name).ToList();
            ViewBag.NewPetModel   = newPetModel;//Model placed in ViewBag to allow for form to appear on rendered partial
            PetOwner activeUser = _context.petowner.Include(o => o.CountryOfResidence).Include(o => o.OwnedPets).ThenInclude(p => p.Breed).Single(o => o.Id == (int)activeId);

            return(View(activeUser));
        }
Esempio n. 2
0
        //Route to process new pet and maybe breed creation
        public IActionResult NewPet(PetValidation newPotentialPet)
        {
            int? activeId = HttpContext.Session.GetInt32("activeUser");
            bool isValid  = SecurityCheck.CheckForActiveUser(activeId);

            if (isValid == false)
            {
                return(RedirectToAction("Index", "PetOwner"));
            }
            if (ModelState.IsValid)
            {
                if (newPotentialPet.BreedId == 0)
                {
                    var shouldBeNull = _context.breed.SingleOrDefault(b => b.Name == newPotentialPet.NewBreedName);
                    if (shouldBeNull != null)
                    {
                        //Logic for this did not seem to work as a custom validation, //TODO//
                        ViewBag.Error = "There is already a breed by that name";
                        return(View("Dashboard"));
                    }
                    Breed newBreed = new Breed {
                        Name = newPotentialPet.NewBreedName
                    };
                    _context.Add(newBreed);
                    _context.SaveChanges();
                    int newBreedId = _context.breed.Single(b => b.Name == newPotentialPet.NewBreedName).Id;
                    newPotentialPet.BreedId = newBreedId;
                }
                Pet newPet = new Pet {//Transfer to DB Model
                    Name        = newPotentialPet.PetName,
                    BreedId     = newPotentialPet.BreedId,
                    PetOwnerId  = (int)activeId,
                    DateOfBirth = newPotentialPet.DateOfBirth,
                    Active      = true
                };
                _context.Add(newPet);
                _context.SaveChanges();
                return(RedirectToAction("Dashboard", "PetOwner"));
            }
            return(View("Dashboard"));
        }