Beispiel #1
0
        public IHttpActionResult PutPerson(int id, Person person)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != person.PersonID)
            {
                return(BadRequest());
            }

            db.Entry(person).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PersonExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Beispiel #2
0
 public HttpResponseMessage Put(int id, [FromBody] Employee employee)
 {
     try
     {
         using (WebAPIDemoEntities entity = new WebAPIDemoEntities())
         {
             var ent = entity.Employee.FirstOrDefault(e => e.ID == id);
             if (ent == null)
             {
                 return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with id : " + id + " not found."));
             }
             else
             {
                 ent.FirstName = employee.FirstName;
                 ent.LastName  = employee.LastName;
                 ent.Gender    = employee.Gender;
                 ent.Salary    = employee.Salary;
                 entity.SaveChanges();
                 return(Request.CreateResponse(HttpStatusCode.OK, ent));
             }
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }
Beispiel #3
0
        public HttpResponseMessage Post([FromBody] Employee employee)
        {
            try
            {
                using (WebAPIDemoEntities entity = new WebAPIDemoEntities())
                {
                    entity.Employee.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));
            }
        }
Beispiel #4
0
 public HttpResponseMessage Delete(int id)
 {
     try
     {
         using (WebAPIDemoEntities entity = new WebAPIDemoEntities())
         {
             var ent = entity.Employee.FirstOrDefault(e => e.ID == id);
             if (ent == null)
             {
                 return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with id : " + id + " not found."));
             }
             else
             {
                 entity.Employee.Remove(ent);
                 entity.SaveChanges();
                 return(Request.CreateResponse(HttpStatusCode.OK, ent));
             }
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }