Beispiel #1
0
        public IHttpActionResult PutLibrary(int id, Library library)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != library.ID)
            {
                return(BadRequest());
            }

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
Beispiel #2
0
        public IHttpActionResult PutEmployee(int id, Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != employee.EmployeeId)
            {
                return(BadRequest());
            }

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public ActionResult Create(userinfo userinfo)
        {
            if (ModelState.IsValid)
            {
                db.userinfoes.Add(userinfo);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(userinfo));
        }
 public IHttpActionResult Put(int id, [FromBody] Employee emp)
 {
     try
     {
         using (WebApiDBEntities entity = new WebApiDBEntities())
         {
             var updateEmp = entity.Employees.FirstOrDefault(e => e.ID == id);
             if (updateEmp != null)
             {
                 updateEmp.FirstName = emp.FirstName;
                 updateEmp.LastName  = emp.LastName;
                 updateEmp.Gender    = emp.Gender;
                 updateEmp.Salary    = emp.Salary;
                 entity.SaveChanges();
                 return(Ok(updateEmp));
             }
             else
             {
                 return(NotFound());
             }
         }
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.ToString()));
     }
 }
 public IHttpActionResult Post([FromBody] Employee emp)
 {
     try
     {
         using (WebApiDBEntities entity = new WebApiDBEntities())
         {
             entity.Employees.Add(emp);
             entity.SaveChanges();
             return(Created(Request.RequestUri + "/" + emp.ID.ToString(), emp));
         }
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.ToString()));
     }
 }
Beispiel #6
0
 /// <summary>
 /// Save method.
 /// </summary>
 public void Save()
 {
     try
     {
         _context.SaveChanges();
     }
     catch (DbEntityValidationException e)
     {
         var outputLines = new List <string>();
         foreach (var eve in e.EntityValidationErrors)
         {
             outputLines.Add(string.Format(
                                 "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:", DateTime.Now,
                                 eve.Entry.Entity.GetType().Name, eve.Entry.State));
             foreach (var ve in eve.ValidationErrors)
             {
                 outputLines.Add(string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage));
             }
         }
         System.IO.File.AppendAllLines(@"C:\errors.txt", outputLines);
     }
 }
 public IHttpActionResult Delete(int id)
 {
     try
     {
         using (WebApiDBEntities entity = new WebApiDBEntities())
         {
             var emp = entity.Employees.FirstOrDefault(e => e.ID == id);
             if (emp == null)
             {
                 return(NotFound());
             }
             else
             {
                 entity.Employees.Remove(emp);
                 entity.SaveChanges();
                 return(Ok());
             }
         }
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.ToString()));
     }
 }