public async Task <IActionResult> Edit(int id)
        {
            var user = await this.userManager.GetUserAsync(User);

            var animal = await this.animals.ByUserAsync(id, user);

            var isInAdminRole = await this.userManager.IsInRoleAsync(user, WebConstants.AdministratorRole);

            if (isInAdminRole)
            {
                animal = await this.animals.ByIdAsync(id);
            }

            if (animal == null)
            {
                return(this.AccessDenied());
            }

            var model = new AnimalFormViewModel
            {
                Name        = animal.Name,
                Age         = animal.Age,
                ImageUrl    = animal.ImageUrl,
                AnimalClass = animal.AnimalClass,
            };

            return(View(model));
        }
        public async Task <IActionResult> Edit(AnimalFormViewModel model)
        {
            var user = await this.userManager.GetUserAsync(User);

            var animal = await this.animals.ByUserAsync(model.Id, user);

            var isInAdminRole = await this.userManager.IsInRoleAsync(user, WebConstants.AdministratorRole);

            if (isInAdminRole)
            {
                animal = await this.animals.ByIdAsync(model.Id);
            }

            if (animal == null)
            {
                return(this.AccessDenied());
            }

            await this.animals.EditAsync(
                animal.Id,
                model.Name,
                model.Age,
                model.AnimalClass,
                model.ImageUrl);

            TempData.AddSuccessMessage($"Successfully edited animal {animal.Name}.");

            return(this.RedirectToCustomAction(
                       nameof(HomeController.Index),
                       nameof(HomeController),
                       new { area = string.Empty }));
        }
Example #3
0
        public IActionResult Create()
        {
            var especies      = _listEspecieService.FindAll();
            var proprietarios = _listProprietarioService.FindAll();
            var viewModel     = new AnimalFormViewModel {
                Proprietarios = proprietarios, Especies = especies
            };

            return(View(viewModel));
        }
        public async Task <IActionResult> Add(AnimalFormViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await userManager.GetUserAsync(User);

            await this.animals
            .AddAsync(model.Name, model.Age, model.ImageUrl, model.AnimalClass, user.Id);

            TempData.AddSuccessMessage("Successfully added animal to zoo.");

            return(this.RedirectToCustomAction(
                       nameof(HomeController.Index),
                       nameof(HomeController),
                       new { area = string.Empty }));
        }
Example #5
0
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var obj = await _animalService.FindByIdAsync(id.Value);

            if (obj == null)
            {
                return(NotFound());
            }

            List <Especie>      especies      = _listEspecieService.FindAll();
            List <Proprietario> proprietarios = _listProprietarioService.FindAll();
            AnimalFormViewModel viewModel     = new AnimalFormViewModel {
                Animal = obj, Proprietarios = proprietarios, Especies = especies
            };

            return(View(viewModel));
        }