Ejemplo n.º 1
0
        private async void btnComboSave_Click(object sender, EventArgs e)
        {
            if (ComboMode == OperationType.Add && validateCombo())
            {
                var name  = txComboName.Text;
                var price = txComboPrice.Text;

                var combo = new Combo
                {
                    Name  = name,
                    Price = decimal.Parse(price),
                };


                await _comboService.AddAsync(combo);

                MessageBox.Show("Thêm thành công");
                SetComboMode(OperationType.View);
                await LoadComboDataGridViewAsync();
            }
            if (ComboMode == OperationType.Edit && validateCombo())
            {
                var name  = txComboName.Text;
                var price = txComboPrice.Text;

                var comboId = txComboId.Text;
                var combo   = await _comboService.GetByIdAsync(comboId);

                combo.Name  = name;
                combo.Price = decimal.Parse(price);

                await _comboService.UpdateAsync(combo);

                MessageBox.Show("Cập nhật thành công");
                SetComboMode(OperationType.View);
                await LoadComboDataGridViewAsync();
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> AddToBasketAsync(string id, int amount = 1)
        {
            var currentBasket         = HttpContext.Session?.GetObjectFromJson <BasketViewModel>("basket");
            var currentBasketItemList = currentBasket?.BasketItems?.ToList();

            if (currentBasket == null)
            {
                var newBasket     = new BasketViewModel();
                var newBasketList = new List <BasketItem>();
                var newBasketItem = await this.MappingToBasketItem(id, amount);

                if (newBasketItem != null)
                {
                    newBasketList.Add(newBasketItem);
                    newBasket.BasketItems = newBasketList;
                    HttpContext.Session?.SetObjectAsJson("basket", newBasket);
                    return(Ok(newBasket));
                }
                return(StatusCode(500, "Not enough in stock"));
            }
            else
            {
                var itemFound = currentBasket.BasketItems.Where(c => c.ItemId == id).SingleOrDefault();

                if (itemFound == null)
                {
                    var newCartItem = await this.MappingToBasketItem(id, amount);

                    if (newCartItem == null)
                    {
                        return(StatusCode(500, "Not enough in stock"));
                    }

                    currentBasketItemList.Add(newCartItem);
                    currentBasket.BasketItems = currentBasketItemList;
                }
                else
                {
                    var currentItem = currentBasket.BasketItems.Where(item => item.ItemId == id).ToList().ElementAt(0);
                    var p           = await _productService.GetByIdAsync(id);

                    // If found => its a product
                    if (p != null)
                    {
                        if (p.Quantity < amount + await GetCurrentProductAmountInOrder(id))
                        {
                            return(StatusCode(500, "Not enough in stock"));
                        }
                    }

                    // If not => its a combo
                    else
                    {
                        var combo = await _comboService.GetByIdAsync(id);

                        var avail = await GetComboAvailability(id, amount);

                        if (!avail)
                        {
                            return(StatusCode(500, "Not enough in stock"));
                        }
                    }
                    itemFound.Amount += amount;
                }
                HttpContext.Session?.SetObjectAsJson("basket", currentBasket);
                return(Ok(currentBasket));
            }
        }