Ejemplo n.º 1
0
        public ActionResult Create([Bind(Include = "Id,Name")] empDB empDB)
        {
            if (ModelState.IsValid)
            {
                db.empDBs.Add(empDB);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(empDB));
        }
Ejemplo n.º 2
0
        public HttpResponseMessage Delete(int id)
        {
            try
            {
                using (EmployeeDBEntities1 entities = new EmployeeDBEntities1())
                {
                    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));
            }
        }
Ejemplo n.º 3
0
 public HttpResponseMessage Put(int id, [FromBody] Employee employee)
 {
     try
     {
         using (EmployeeDBEntities1 entities = new EmployeeDBEntities1())
         {
             var entity = entities.Employees.FirstOrDefault(e => e.ID == id);
             if (entity == null)
             {
                 return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with Id = " + id.ToString() + " has no record "));
             }
             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));
     }
 }
Ejemplo n.º 4
0
 public void CreateEmployee(Employee employee)
 {
     if (employee != null)
     {
         _employeeDBEntities.Employees.Add(employee);
         _employeeDBEntities.SaveChanges();
     }
 }
Ejemplo n.º 5
0
        public IHttpActionResult DeleteCustomer(int id)
        {
            var db       = new EmployeeDBEntities1();
            var customer = new Customer {
                Id = id
            };

            db.Customers.Attach(customer);
            db.Customers.Remove(customer);
            db.SaveChanges();
            return(Ok(id));
        }
Ejemplo n.º 6
0
 public IHttpActionResult SaveCustomer(Customer customer)
 {
     if (customer.Id > 0)
     {
         var db         = new EmployeeDBEntities1();
         var dbCustomer = db.Customers.FirstOrDefault(x => x.Id == customer.Id);
         dbCustomer.Name    = customer.Name;
         dbCustomer.Address = customer.Address;
         dbCustomer.Phone   = customer.Phone;
         db.SaveChanges();
     }
     else
     {
         var db = new EmployeeDBEntities1();
         db.Customers.Add(customer);
         db.SaveChanges();
     }
     return(Ok(customer.Id));
 }
Ejemplo n.º 7
0
        public HttpResponseMessage Post([FromBody] Employee employee)
        {
            try
            {
                using (EmployeeDBEntities1 entities = new EmployeeDBEntities1())
                {
                    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));
            }
        }