public HttpResponseMessage put(int id, [FromBody] Employee emp) { using (EmployDbEntities empEntity = new EmployDbEntities()) { try { var empObj = empEntity.Employees.FirstOrDefault(x => x.Id == id); if (empObj == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "employee with id " + id.ToString() + " not found ")); } else { empObj.FirstName = emp.FirstName; empObj.LastName = emp.LastName; empEntity.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, empObj)); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } } }
public HttpResponseMessage post([FromBody] Employee emp) { try { using (EmployDbEntities employe = new EmployDbEntities()) { employe.Employees.Add(emp); employe.SaveChanges(); var message = Request.CreateResponse(HttpStatusCode.Created, employe); message.Headers.Location = new Uri(Request.RequestUri + emp.Id.ToString()); return(message); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage Delete(int id) { try { using (EmployDbEntities emp = new EmployDbEntities()) { var objemp = emp.Employees.FirstOrDefault(x => x.Id == id); if (objemp == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "employee with id " + id.ToString() + " not found")); } else { emp.Employees.Remove(objemp); emp.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }