public void DeleteEntryDeletesEntry() { //Arrange: // Instantiate EntriessController so its methods can be called // Create a new entry to be deleted, and get its entry ID var entryController = new EntriesController(); var entry = new EntryModel { Name = "Zia Prostnow", EntryText = "Whippersnappers!" }; IHttpActionResult result = entryController.PostEntry(entry); CreatedAtRouteNegotiatedContentResult<EntryModel> contentResult = (CreatedAtRouteNegotiatedContentResult<EntryModel>)result; int entryIdToDelete = contentResult.Content.EntryId; //Act: Call DeleteEntry result = entryController.DeleteEntry(entryIdToDelete); //Assert: // Verify that HTTP result is OK // Verify that reading deleted entry returns result not found Assert.IsInstanceOfType(result, typeof(OkNegotiatedContentResult<Entry>)); result = entryController.GetEntry(entryIdToDelete); Assert.IsInstanceOfType(result, typeof(NotFoundResult)); }
public IHttpActionResult GetEntry(int id) { Entry dbEntry = db.Entries.Find(id); if (dbEntry == null) { return NotFound(); } // Populate new EntryModel object from Entry object EntryModel modelEntry = new EntryModel { EntryId = dbEntry.EntryId, CreatedDate = dbEntry.CreatedDate, Name = dbEntry.Name, EntryText = dbEntry.EntryText }; return Ok(modelEntry); }
public IHttpActionResult PostEntry(EntryModel entry) { // Validate the request if (!ModelState.IsValid) { return BadRequest(ModelState); } // Set up new Entry object, // and populate it with the values from // the input EntryModel object Entry dbEntry = new Entry(); dbEntry.Update(entry); // Add the new Entry object to the list of Entry objects db.Entries.Add(dbEntry); // Save the changes to the DB try { db.SaveChanges(); } catch (Exception) { throw new Exception("Unable to add the entry to the database."); } // Update the EntryModel object with the new entry ID // that was placed in the Entry object after the changes // were saved to the DB entry.EntryId = dbEntry.EntryId; return CreatedAtRoute("DefaultApi", new { id = dbEntry.EntryId }, entry); }
public IHttpActionResult PutEntry(int id, EntryModel entry) { // Validate the request if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != entry.EntryId) { return BadRequest(); } if (!EntryExists(id)) { return BadRequest(); } // Get the entry record corresponding to the entry ID, // update its properties to the values in the input EntryModel object, // and then set an indicator that the record has been modified var dbEntry = db.Entries.Find(id); dbEntry.Update(entry); db.Entry(dbEntry).State = EntityState.Modified; // Perform update by saving changes to DB try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!EntryExists(id)) { return NotFound(); } else { throw new Exception("Unable to update the entry in the database."); } } return StatusCode(HttpStatusCode.NoContent); }
public void PostEntryCreatesEntry() { //Arrange: Instantiate EntriesController so its methods can be called var entryController = new EntriesController(); //Act: // Create an EntryModel object populated with test data, // and call PostEntry var newEntry = new EntryModel { Name = "Testy", EntryText = "Welcome to my world!" }; IHttpActionResult result = entryController.PostEntry(newEntry); //Assert: // Verify that the HTTP result is CreatedAtRouteNegotiatedContentResult // Verify that the HTTP result body contains a nonzero entry ID Assert.IsInstanceOfType (result, typeof(CreatedAtRouteNegotiatedContentResult<EntryModel>)); CreatedAtRouteNegotiatedContentResult<EntryModel> contentResult = (CreatedAtRouteNegotiatedContentResult<EntryModel>)result; Assert.IsTrue(contentResult.Content.EntryId != 0); // Delete the test entry result = entryController.DeleteEntry(contentResult.Content.EntryId); }