public void InsertOrUpdate(Session session) { if (session.Id == default(int)) { // New entity context.Sessions.Add(session); } else { // Existing entity context.Entry(session).State = EntityState.Modified; } }
// POST api/Session public HttpResponseMessage PostSession(Session session) { if (ModelState.IsValid) { try { repository.InsertOrUpdate(session); repository.Save(); } catch (DbUpdateConcurrencyException) { return Request.CreateResponse(HttpStatusCode.NotFound); } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, session); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = session.Id })); return response; } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }
// PUT api/Session/5 public HttpResponseMessage PutSession(int id, Session session) { if (ModelState.IsValid && id == session.Id) { try { repository.InsertOrUpdate(session); repository.Save(); } catch (DbUpdateConcurrencyException) { return Request.CreateResponse(HttpStatusCode.NotFound); } return Request.CreateResponse(HttpStatusCode.OK); } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }