//public HttpResponseMessage Put([FromBody]int id, [FromUri]Employee employee) //public HttpResponseMessage Put(int id, [FromUri]Employee employee) public HttpResponseMessage Put(int id, [FromBody] Employee employee) { try { using (SampleAPIEntities entities = new SampleAPIEntities()) { 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 update")); } else { entity.FirstName = employee.FirstName; entity.LastName = employee.LastName; entity.Gender = employee.Gender; entity.Salary = employee.Salary; entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, entity)); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage LoadEmployeeById(int id) { using (SampleAPIEntities entities = new SampleAPIEntities()) { var entity = entities.Employees.FirstOrDefault(e => e.ID == id); if (entity != null) { return(Request.CreateResponse(HttpStatusCode.OK, entity)); } else { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with Id " + id.ToString() + " not found")); } } }
public HttpResponseMessage Post([FromBody] Employee employee) { try { using (SampleAPIEntities entities = new SampleAPIEntities()) { entities.Employees.Add(employee); entities.SaveChanges(); var message = Request.CreateResponse(HttpStatusCode.Created, employee); message.Headers.Location = new Uri(Request.RequestUri + employee.ID.ToString()); return(message); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage Get(string gender = "All") { using (SampleAPIEntities entities = new SampleAPIEntities()) { switch (gender.ToLower()) { case "all": return(Request.CreateResponse(HttpStatusCode.OK, entities.Employees.ToList())); case "male": return(Request.CreateResponse(HttpStatusCode.OK, entities.Employees.Where(e => e.Gender.ToLower() == "male").ToList())); case "female": return(Request.CreateResponse(HttpStatusCode.OK, entities.Employees.Where(e => e.Gender.ToLower() == "female").ToList())); default: return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Value for gender must be All, Male or Female. " + gender + " is invalid.")); } } }
public HttpResponseMessage Delete(int id) { try { using (SampleAPIEntities entities = new SampleAPIEntities()) { 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.")); } else { entities.Employees.Remove(entity); entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }