public IHttpActionResult PostLease(LeaseModel lease) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //Set up new Lease object, // and populate it with the values from // the input LeaseModel object Lease dbLease = new Lease(); dbLease.Update(lease); // Add the new Lease object to the list of Lease objects db.Leases.Add(dbLease); try { db.SaveChanges(); } catch (Exception) { throw new Exception("Unable to add the lease to the database."); } // Update the LeaseModel object with the new lease ID // that was placed in the Lease object after the changes // were saved to the DB lease.LeaseId = dbLease.LeaseId; return(CreatedAtRoute("DefaultApi", new { id = dbLease.LeaseId }, lease)); }
public IHttpActionResult PostLease(LeaseModel lease) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var dbLease = new Lease(); dbLease.Update(lease); db.Leases.Add(dbLease); db.SaveChanges(); lease.LeaseId = dbLease.LeaseId; return(CreatedAtRoute("DefaultApi", new { id = lease.LeaseId }, lease)); }
public IHttpActionResult PutLease(int id, LeaseModel modelLease) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != modelLease.LeaseId) { return(BadRequest()); } Lease dbLease = db.Leases.FirstOrDefault(l => l.User.UserName == User.Identity.Name && l.LeaseId == id); if (dbLease == null) { return(BadRequest()); } dbLease.Update(modelLease); db.Entry(dbLease).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!LeaseExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }