Пример #1
0
        public async Task <IActionResult> Edit(PetViewModel model)
        {
            if (ModelState.IsValid)
            {
                var path = string.Empty;

                if (model.ImageFile != null && model.ImageFile.Length > 0)
                {
                    path = await _imageHelper.UploadImageAsync(model.ImageFile, "Pets");
                }

                try
                {
                    model.Owner = await _ownerRepository.GetOwnerWithUserByIdAsync(model.OwnerId);

                    model.Specie = await _specieRepository.GetSpecieByIdAsync(model.SpecieId);


                    var pet = _converterHelper.ToPet(model, path, false);
                    pet.ModifiedBy = await _userHelper.GetUserByEmailAsync(User.Identity.Name);

                    await _petRepository.UpdateAsync(pet);

                    return(RedirectToAction(nameof(MyPets)));
                }
                catch (Exception exception)
                {
                    ModelState.AddModelError(string.Empty, exception.Message);
                }
            }
            model.Species = _specieRepository.GetComboSpecies();

            return(View(model));
        }
Пример #2
0
        public async Task <IActionResult> AddPet(PetViewModel model)
        {
            if (ModelState.IsValid)
            {
                var path = string.Empty;

                if (model.ImageFile != null && model.ImageFile.Length > 0)
                {
                    path = await _imageHelper.UploadImageAsync(model.ImageFile, path);
                }
                try
                {
                    model.Owner = await _ownerRepository.GetOwnerWithUserByIdAsync(model.OwnerId);

                    model.Specie = await _specieRepository.GetSpecieByIdAsync(model.SpecieId);

                    var pet = _converterHelper.ToPet(model, path, true);
                    pet.CreatedBy = await _userHelper.GetUserByEmailAsync(User.Identity.Name);

                    await _petRepository.CreateAsync(pet);

                    return(RedirectToAction($"Details/{model.OwnerId}"));
                }
                catch (Exception exception)
                {
                    ModelState.AddModelError(string.Empty, exception.Message);
                }
            }

            return(View(model));
        }