예제 #1
0
 // PUT api/<controller>/5
 public HttpResponseMessage Put(int id, [FromBody] Employee employee)
 {
     try
     {
         using (sampleDBEntities entity = new sampleDBEntities())
         {
             var ent = entity.Employees.FirstOrDefault(e => e.Id == id);
             if (ent == null)
             {
                 return(Request.CreateErrorResponse(HttpStatusCode.NotFound,
                                                    "Employee with ID: " + id + " is not found"));
             }
             else
             {
                 ent.Name         = employee.Name;
                 ent.Gender       = employee.Gender;
                 ent.DateOfBirth  = employee.DateOfBirth;
                 ent.AnnualSalary = employee.AnnualSalary;
                 ent.EmployeeType = employee.EmployeeType;
                 ent.HourlyPay    = employee.HourlyPay;
                 ent.HoursWorked  = employee.HoursWorked;
                 entity.SaveChanges();
                 return(Request.CreateResponse(HttpStatusCode.OK, ent));
             }
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }
예제 #2
0
 // DELETE api/<controller>/5
 public HttpResponseMessage Delete(int id)
 {
     try
     {
         using (sampleDBEntities entity = new sampleDBEntities())
         {
             var ent = entity.Employees.FirstOrDefault(e => e.Id == id);
             if (ent == null)
             {
                 return(Request.CreateErrorResponse(HttpStatusCode.NotFound,
                                                    "Employee with ID: " + id + " is not found"));
             }
             else
             {
                 entity.Employees.Remove(ent);
                 entity.SaveChanges();
                 return(Request.CreateResponse(HttpStatusCode.OK));
             }
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }
예제 #3
0
 // POST api/<controller>
 public HttpResponseMessage Post([FromBody] Employee employee)
 {
     try
     {
         using (sampleDBEntities entity = new sampleDBEntities())
         {
             entity.Employees.Add(employee);
             entity.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));
     }
 }