Ejemplo n.º 1
0
        public IHttpActionResult PutUser(int id, User user)
        {
            db.Configuration.ProxyCreationEnabled = false;
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != user.UserId)
            {
                return(BadRequest());
            }

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
Ejemplo n.º 2
0
        public static Response NewOrder(NewOrderView view, string userName)
        {
            using (var transacction = db.Database.BeginTransaction())
            {
                try
                {
                    var user  = db.Users.Where(u => u.UserName == userName).FirstOrDefault();
                    var order = new Order
                    {
                        CompanyId  = user.CompanyId,
                        CustomerId = view.CustomerId,
                        Date       = view.Date,
                        Remarks    = view.Remarks,
                        StateId    = DBHelper.GetState("Created", db),
                    };
                    db.Orders.Add(order);
                    db.SaveChanges();
                    var details = db.OrderDetailTmps.Where(odt => odt.UserName == userName).ToList();
                    foreach (var detail in details)
                    {
                        var orderDetail = new OrderDetail
                        {
                            Description = detail.Description,
                            OrderId     = order.OrderId,
                            Price       = detail.Price,
                            ProductId   = detail.ProductId,
                            Quantity    = detail.Quantity,
                            TaxRate     = detail.TaxRate,
                        };
                        db.OrderDetails.Add(orderDetail);
                        db.OrderDetailTmps.Remove(detail);
                    }

                    db.SaveChanges();
                    transacction.Commit();
                    return(new Response {
                        Succeded = true
                    });
                }
                catch (Exception ex)
                {
                    transacction.Rollback();
                    return(new Response {
                        Succeded = false, Message = ex.Message
                    });
                }
            }
        }
Ejemplo n.º 3
0
 public static Response SaveChanges(EcomerceDataContext db)
 {
     try
     {
         db.SaveChanges();
         return(new Response {
             Succeded = true,
         });
     }
     catch (Exception ex)
     {
         var response = new Response {
             Succeded = false,
         };
         if (ex.InnerException != null &&
             ex.InnerException.InnerException != null &&
             ex.InnerException.InnerException.Message.Contains("_Index"))
         {
             response.Message = "There is a record with the same value";
         }
         else if (ex.InnerException != null &&
                  ex.InnerException.InnerException != null &&
                  ex.InnerException.InnerException.Message.Contains("REFERENCE"))
         {
             response.Message = "The record can't be delete because it has related records";
         }
         else
         {
             response.Message = ex.Message;
         }
         return(response);
     }
 }
Ejemplo n.º 4
0
        public ActionResult DeleteProduct(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var orderDetailTmp = db.OrderDetailTmps.Where(odt => odt.UserName == User.Identity.Name && odt.ProductId == id).FirstOrDefault();

            if (orderDetailTmp == null)
            {
                return(HttpNotFound());
            }
            db.OrderDetailTmps.Remove(orderDetailTmp);
            db.SaveChanges();
            return(RedirectToAction("Create"));
        }
Ejemplo n.º 5
0
        public static int GetState(string description, EcomerceDataContext db)
        {
            var state = db.States.Where(s => s.Description == description).FirstOrDefault();

            if (state == null)
            {
                state = new State {
                    Description = description,
                };
                db.States.Add(state);
                db.SaveChanges();
            }
            return(state.StateId);
        }
Ejemplo n.º 6
0
        public ActionResult Edit(User user)
        {
            if (ModelState.IsValid)
            {
                var pic    = user.Photo;
                var folder = "~/Content/Users";

                if (user.PhotoFile != null)
                {
                    if (pic == null || pic == string.Empty)
                    {
                        pic = FilesHelper.GetNamePhoto(user.UserId);
                    }
                    else
                    {
                        pic = pic.Substring(16);
                    }


                    if (pic != null)
                    {
                        pic = FilesHelper.UploadPhoto(user.PhotoFile, pic, folder);
                        pic = string.Format("{0}/{1}", folder, pic);
                    }
                }
                user.Photo = pic;
                var db2         = new EcomerceDataContext();
                var currenrUser = db2.Users.Find(user.UserId);
                if (currenrUser.UserName != user.UserName)
                {
                    UsersHelper.UpdateUserName(currenrUser.UserName, user.UserName);
                }
                db2.Dispose();
                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();

                var respons = DBHelper.SaveChanges(db);

                if (!respons.Succeded)
                {
                    ModelState.AddModelError(string.Empty, respons.Message);
                    return(View(user));
                }
                return(RedirectToAction("Index"));
            }
            ViewBag.CityId       = new SelectList(CombosHelper.GetCities(user.DepartmentId), "CityId", "Name", user.CityId);
            ViewBag.CompanyId    = new SelectList(CombosHelper.GetCompanies(), "CompanyId", "Name", user.CompanyId);
            ViewBag.DepartmentId = new SelectList(CombosHelper.GetDepartments(), "DepartmentId", "Name", user.DepartmentId);
            return(View(user));
        }
Ejemplo n.º 7
0
        public ActionResult Create(Customer customer)
        {
            if (ModelState.IsValid)
            {
                using (var transaction = db.Database.BeginTransaction())
                {
                    try
                    {
                        db.Customers.Add(customer);
                        var response = DBHelper.SaveChanges(db);
                        if (!response.Succeded)
                        {
                            ModelState.AddModelError(string.Empty, response.Message);
                            transaction.Rollback();
                            ViewBag.CityId       = new SelectList(CombosHelper.GetCities(customer.DepartmentId), "CityId", "Name", customer.CityId);
                            ViewBag.DepartmentId = new SelectList(CombosHelper.GetDepartments(), "DepartmentId", "Name", customer.DepartmentId);
                            return(View(customer));
                        }
                        UsersHelper.CreateUserASP(customer.UserName, "Customer");
                        var user            = db.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();
                        var companyCustomer = new CompanyCustomer {
                            CompanyId = user.CompanyId, CustomerId = customer.CustomerId,
                        };
                        db.CompanyCustomers.Add(companyCustomer);
                        db.SaveChanges();
                        transaction.Commit();
                        return(RedirectToAction("Index"));
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        ModelState.AddModelError(string.Empty, ex.Message);
                    }
                }
            }

            ViewBag.CityId       = new SelectList(CombosHelper.GetCities(customer.DepartmentId), "CityId", "Name", customer.CityId);
            ViewBag.DepartmentId = new SelectList(CombosHelper.GetDepartments(), "DepartmentId", "Name", customer.DepartmentId);
            return(View(customer));
        }