public string updateTest(Accessory row)
 {
     if (accessoryDAO.Update(Database.getAccessoryName(), row))
     {
         return("SUCCESS");
     }
     return("FAIL");
 }
        public IActionResult UpdateAcc([FromBody] DTO.AccessoryJSON acc)
        {
            AccessoryDAO dao = new AccessoryDAO(_context);
            Accessory    dto = new Accessory
            {
                AccId             = acc.AccId,
                AccName           = acc.AccName,
                AccCatId          = acc.AccCatId,
                Brand             = acc.Brand,
                Description       = acc.Description,
                Price             = Double.Parse(acc.Price),
                StartSellingDate  = DateTime.Parse(acc.StartSellingDate),
                Image             = acc.Image,
                AvailableQuantity = int.Parse(acc.AvailableQuantity),
                SalePercent       = double.Parse(acc.SalePercent),
                IsDelete          = acc.IsDelete.Equals("true") ? true : false
            };
            bool result = dao.Update(dto);

            return(new JsonResult(result));
        }
        public IActionResult CheckOut([Bind("DeliveryAddress", "DeliveryPhone")] DTO.Transaction tran)
        {
            string generateAccTranID()
            {
                string         id;
                Random         rd  = new Random();
                TransactionDAO dao = new TransactionDAO(_context);

                do
                {
                    id = "AT";
                    for (int i = 0; i < 4; i++)
                    {
                        id += rd.Next(10);
                    }
                } while (dao.CheckExisted(id));
                return(id);
            }

            AccessoryDAO accDAO   = new AccessoryDAO(_context);
            string       userName = HttpContext.Session.GetString("USER");
            Dictionary <string, DTO.Accessory> shoppingCart = HttpContext.Session.GetCollectionFromJson <Dictionary <string, DTO.Accessory> >("cart");

            foreach (var item in shoppingCart)
            {
                string        id         = item.Key;
                DTO.Accessory dtoSession = item.Value;
                DTO.Accessory accDTO     = accDAO.FindByPrimaryKey(id);
                if (accDTO.IsDelete || accDTO.AvailableQuantity == 0 || accDTO.AvailableQuantity < dtoSession.Quantity)
                {
                    return(RedirectToAction("Index", "ShoppingCart"));
                }
            }
            TransactionDAO tranDAO   = new TransactionDAO(_context);
            string         accTranID = generateAccTranID();

            DTO.Transaction tranDTO = new DTO.Transaction(accTranID, userName, tran.DeliveryPhone, tran.DeliveryAddress, 0);
            tranDAO.Insert(tranDTO);
            double total = 0;
            List <DTO.Accessory> list = new List <DTO.Accessory>();

            foreach (var item in shoppingCart)
            {
                string             id         = item.Key;
                DTO.Accessory      dtoSession = item.Value;
                DTO.Accessory      accDTO     = accDAO.FindByPrimaryKey(id);
                DTO.TransactionRel tranRelDTO = new DTO.TransactionRel(id, accTranID, accDTO.AccName, accDTO.AccCatId, accDTO.Brand, accDTO.Description, accDTO.Image, accDTO.Price, accDTO.SalePercent, accDTO.StartSellingDate, dtoSession.Quantity, accDTO.AccCatName);
                TransactionRelDAO  tranRelDAO = new TransactionRelDAO(_context);
                tranRelDAO.Insert(tranRelDTO);
                accDTO.AvailableQuantity -= dtoSession.Quantity;
                total += dtoSession.Quantity * accDTO.Price * (1 - accDTO.SalePercent);
                list.Add(accDTO);
            }
            accDAO.Update(list);
            total = (double)Math.Round(total * 100) / 100;
            tranDTO.TotalPrice = total;
            tranDAO.UpdateTotalPrice(total, accTranID);
            shoppingCart.Clear();
            HttpContext.Session.SetCollectionAsJson("cart", shoppingCart);
            return(RedirectToAction("UserDetails", "Principal"));
        }