public HttpResponseMessage Put(int id, [FromBody] Employee employee) { using (EmployeeMangementSystemEntities db = new EmployeeMangementSystemEntities()) { try { var entity = db.Employees.FirstOrDefault(e => e.Employee_ID == id); if (entity == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with id=" + id.ToString() + "not found to update")); } else { entity.Employee_Name = employee.Employee_Name; entity.Employee_Sallery = employee.Employee_Sallery; entity.Employee_Age = employee.Employee_Age; db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, entity)); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } } }
public HttpResponseMessage Delete(int id) { using (EmployeeMangementSystemEntities db = new EmployeeMangementSystemEntities()) { var entity = db.Employees.FirstOrDefault(e => e.Employee_ID == id); if (entity == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with id=" + id.ToString() + "not found to delete")); } else { db.Employees.Remove(entity); db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } } }
public HttpResponseMessage Post([FromBody] Employee employee) { try { using (EmployeeMangementSystemEntities db = new EmployeeMangementSystemEntities()) { db.Employees.Add(employee); db.SaveChanges(); var message = Request.CreateResponse(HttpStatusCode.Created, employee); message.Headers.Location = new Uri(Request.RequestUri + employee.Employee_ID.ToString()); return(message); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }