public ActionResult Lawyer_Update([DataSourceRequest] DataSourceRequest request, LawyerViewModel db) { if (db != null && ModelState.IsValid) { LawyerService.Update(db); } return(Json(new[] { db }.ToDataSourceResult(request, ModelState))); }
// by convention routing: PUT/api.lawyers public HttpResponseMessage Put(int id, Lawyer body) { using (LawyerService lawyerService = new LawyerService(new DataContext())) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } try { lawyerService.Update(id, body); return(new HttpResponseMessage(HttpStatusCode.OK)); } catch (NullReferenceException ex) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message)); } catch (HttpRequestValidationException ex) { return(Request.CreateErrorResponse(HttpStatusCode.Forbidden, ex.Message)); } } }
public void UpdateALawyer() { // setting up the expectations // current lowyer is the one to be updated. Lawyer currentLawyer = Lawyers.ElementAt(1); // create the updated lawyer. This will become the new "Active" instace. Lawyer updatedLawyer = new Lawyer { Name = "Updated", Surname = "Surneos", Initials = "US", DateOfBirth = new DateTime(2000, 1, 1), Email = "*****@*****.**", GenderRefId = 1, TitleRefId = 1 }; // before any change we need to make sure the DB contains the current instance // and does not contain the updated (new) one. CollectionAssert.DoesNotContain(Lawyers, updatedLawyer); CollectionAssert.Contains(Lawyers, currentLawyer); using (LawyerService lawyerService = new LawyerService(MockContext.Object)) { // update the lawyer lawyerService.Update(currentLawyer.Id, updatedLawyer); // validate .Add is called only once MockLawyersSet.Verify(m => m.Add(It.IsAny <Lawyer>()), Times.Once); // make sure both instances are now in DB CollectionAssert.Contains(Lawyers, updatedLawyer); CollectionAssert.Contains(Lawyers, currentLawyer); // validate old instance is not Active Assert.AreEqual(false, currentLawyer.Active); // verify the new instance is Active Assert.AreEqual(true, updatedLawyer.Active); // we recall the new lawyer instace with his new surname Lawyer fetched2 = lawyerService.GetAll(Doubles.GetParameters(Surname: updatedLawyer.Surname)).FirstOrDefault(); Assert.AreEqual(updatedLawyer, fetched2); // verify that we cannot find the lawyer with his old surname Lawyer oldLawyer = lawyerService.GetAll(Doubles.GetParameters(Surname: currentLawyer.Surname)).FirstOrDefault(); Assert.IsNull(oldLawyer); // verify that we cannot update the inactive instace try { lawyerService.Update(currentLawyer.Id, new Lawyer()); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(HttpRequestValidationException)); Assert.AreEqual(string.Format("the lawyer record with id: '{0}' is archived.", currentLawyer.Id), ex.Message); } // verify we cannot update a non existing lawyer try { lawyerService.Update(404, new Lawyer()); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NullReferenceException)); Assert.AreEqual("Lawyer not found.", ex.Message); } } }