//// DELETE: api/Employee/5 public HttpResponseMessage Delete(int id) { try { using (var context = new sampledbcontext()) { var employee = context.Employee.FirstOrDefault(emp => emp.id == id); context.Employee.Remove(employee); context.SaveChanges(); } return(Request.CreateResponse(HttpStatusCode.OK, "User with id= " + id + " deleted successfully")); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
// PUT: api/Employee/5 public HttpResponseMessage Put(int id, [FromBody] Employee employee) { try { using (var context = new sampledbcontext()) { var emp = context.Employee.FirstOrDefault(e => e.id == id); emp.name = employee.name; emp.salary = employee.salary; emp.designation = employee.designation; context.SaveChanges(); } return(Request.CreateResponse(HttpStatusCode.OK, employee)); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
// POST: api/Employee public HttpResponseMessage Post([FromBody] Employee employee) { try { using (var context = new sampledbcontext()) { var emp = new Employee() { name = employee.name, salary = employee.salary, designation = employee.designation }; context.Employee.Add(emp); context.SaveChanges(); } return(Request.CreateResponse(HttpStatusCode.Created, employee)); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }