// PUT api/authorapi/5 public HttpResponseMessage Put(int id, Author author) { try { var authorToUpdate = unitOfWork.AuthorRepository.GetByID(id); authorToUpdate.Name = author.Name; unitOfWork.AuthorRepository.Update(authorToUpdate); unitOfWork.Save(); return Request.CreateResponse(HttpStatusCode.OK); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message); } }
// POST api/authorapi public HttpResponseMessage Post(Author author) { try { if (ModelState.IsValid) { unitOfWork.AuthorRepository.Insert(author); return Request.CreateResponse(HttpStatusCode.OK); } return Request.CreateResponse(HttpStatusCode.InternalServerError, "Invalid Model"); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message); } }
public ActionResult Edit(Author model) { try { if (ModelState.IsValid) { EditAuthor(model); return RedirectToAction("Authors", "Admin"); } } catch (DataException) { ModelState.AddModelError("", "Не удалось сохранить изменения. Повторите попытку."); } return View(model); }
private void EditAuthor(Author model) { var authorToUpdate = unitOfWork.AuthorRepository.GetByID(model.ID); authorToUpdate.Name = model.Name; unitOfWork.AuthorRepository.Update(authorToUpdate); unitOfWork.Save(); }
private void AddAuthor(AddAuthorViewModel model) { Author author = new Author(); author.Name = model.Name; if (model.Image != null) { author.Image = FileService.SaveFile(model.Image); } bool exist = unitOfWork.AuthorRepository.Get().FirstOrDefault( x => x.Name.ToUpper() == author.Name.ToUpper()) != null; if (exist) { throw new Exception(); } unitOfWork.AuthorRepository.Insert(author); unitOfWork.Save(); }