public IHttpActionResult PostEntry(EntriesModel entry) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var dbPostEntry = new Entry(); dbPostEntry.Update(entry); db.Entries.Add(dbPostEntry); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = dbPostEntry.EntryId }, entry); }
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); }