public IEnumerable <Employee> Get() { using (EmployeeDBOWINEntities entities = new EmployeeDBOWINEntities()) { return(entities.Employees.ToList()); } }
public HttpResponseMessage Get(int id) { using (EmployeeDBOWINEntities entities = new EmployeeDBOWINEntities()) { var entity = entities.Employees.FirstOrDefault(e => e.ID == id); if (entity != null) { return(Request.CreateResponse(HttpStatusCode.OK, entity)); } return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with Id = " + id.ToString() + " not found"));; } }
public HttpResponseMessage Post([FromBody] Employee employee) { try { using (EmployeeDBOWINEntities entities = new EmployeeDBOWINEntities()) { entities.Employees.Add(employee); entities.SaveChanges(); var message = Request.CreateResponse(HttpStatusCode.OK, employee); message.Headers.Location = new Uri(Request.RequestUri + employee.ID.ToString()); return(message); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage Delete(int id) { try { using (EmployeeDBOWINEntities entities = new EmployeeDBOWINEntities()) { var entity = entities.Employees.FirstOrDefault(e => e.ID == id); if (entity == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with Id = " + id.ToString() + " not found to delete")); } entities.Employees.Remove(entity); return(Request.CreateResponse(HttpStatusCode.OK, entity)); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }