/// <inheritdoc /> Task ISageApiService.PutAsync(int id, SageUpdateViewModel sage) { var arguments = new object[] { id, sage }; var func = requestBuilder.BuildRestResultFuncForMethod("PutAsync", new Type[] { typeof(int), typeof(SageUpdateViewModel) }); return((Task)func(Client, arguments)); }
// PUT api/values/5 public async Task <IHttpActionResult> Put(int id, [FromBody] SageUpdateViewModel sageViewModel) { try { var sageToUpdate = await _uow.SageRepository.GetFirstOrDefaultAsync( x => x.IdSage == id, null, q => q.Include(x => x.Books), disableTracking : false); if (sageToUpdate == null) { return(NotFound()); } sageToUpdate.Age = sageViewModel.Sage.Age; sageToUpdate.Name = sageViewModel.Sage.Name; sageToUpdate.City = sageViewModel.Sage.City; sageToUpdate.Photo = sageViewModel.Sage.Photo; var selectedBooks = new HashSet <int>(sageViewModel.SelectedBooks); sageToUpdate.Books .Where(x => !selectedBooks.Contains(x.IdBook)) .ToList() .ForEach(item => sageToUpdate.Books.Remove(item)); var existingBooks = new HashSet <int>(sageToUpdate.Books.Select(c => c.IdBook)); var books = await _uow.BookRepository.GetAllAsync(x => selectedBooks.Except(existingBooks).Contains(x.IdBook), disableTracking : false); books.ForEach(book => sageToUpdate.Books.Add(book)); await _uow.SageRepository.UpdateAsync(sageToUpdate); if (await _uow.SaveAsync()) { return(Ok()); } return(BadRequest()); } catch (Exception e) { Console.WriteLine(e); throw; } }
public async Task <ActionResult> Edit(SageViewModel sageViewModel, HttpPostedFileBase photo1) { if (ModelState.IsValid) { if (photo1 != null) { sageViewModel.Sage.Photo = new byte[photo1.ContentLength]; photo1.InputStream.Read(sageViewModel.Sage.Photo, 0, photo1.ContentLength); } var sage = new SageUpdateViewModel { Sage = sageViewModel.Sage, SelectedBooks = sageViewModel.SelectedBooks }; await _sageApiService.PutAsync(sage.Sage.IdSage, sage); return(RedirectToAction("Index")); } return(View(sageViewModel)); }