Exemple #1
0
        public HttpResponseMessage Put(int id, [FromBody] Employee employee)
        {
            try
            {
                using (API018Entities entities = new API018Entities())
                {
                    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));
            }
        }
Exemple #2
0
        public HttpResponseMessage Post([FromBody] Employee employee)
        {
            try
            {
                using (API018Entities entities = new API018Entities())
                {
                    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));
            }
        }
Exemple #3
0
 public HttpResponseMessage Delete(int id)
 {
     try
     {
         using (API018Entities entities = new API018Entities())
         {
             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));
     }
 }