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)); }
/// <summary> /// creates a pet from that owner /// </summary> /// <param name="id">user id</param> /// <returns>view create</returns> public async Task <IActionResult> AddPet(int?id) { if (id == null) { return(new NotFoundViewResult("PetNotFound")); } var owner = await _ownerRepository.GetByIdAsync(id.Value); if (owner == null) { return(new NotFoundViewResult("PetNotFound")); } var model = new PetViewModel { DateOfBirth = DateTime.Today, OwnerId = owner.Id, Species = _specieRepository.GetComboSpecies() }; return(View(model)); }
public PetViewModel ToPetViewModel(Pet pet) { return(new PetViewModel { Id = pet.Id, Name = pet.Name, Breed = pet.Breed, Gender = pet.Gender, Weight = pet.Weight, ImageUrl = pet.ImageUrl, Sterilization = pet.Sterilization, DateOfBirth = pet.DateOfBirth, Appointments = pet.Appointments, OwnerId = pet.Owner.Id, SpecieId = pet.Specie.Id, Species = _specieRepository.GetComboSpecies(), ModifiedBy = pet.ModifiedBy, CreateDate = pet.CreateDate, UpdateDate = DateTime.Now, OwnerFullName = pet.Owner.User.FullName, }); }