public async Task <IActionResult> PutBasketCard([FromRoute] string id, [FromBody] BasketCard basketCard)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != basketCard.BasketCardId)
            {
                return(BadRequest());
            }

            _context.Entry(basketCard).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BasketCardExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 2
0
        public async Task AddToCart(long userId, long productId, int quantity)
        {
            var basketCard = context.BasketCards.FirstOrDefault(x => x.Customer.PersonId == userId && x.IsActive);

            if (basketCard == null)
            {
                basketCard = new BasketCard
                {
                    Customer = context.Customers.FirstOrDefault(p => p.PersonId == userId)
                };

                context.BasketCards.Add(basketCard);
            }
            var basketCardItem = basketCard.BasketCardItems.FirstOrDefault(x => x.Product.ProductID == productId);

            if (basketCardItem == null)
            {
                basketCardItem = new BasketCardItem
                {
                    BasketCard = basketCard,
                    Product    = context.Products.FirstOrDefault(p => p.ProductID == productId),
                    Quantity   = quantity
                };

                basketCard.BasketCardItems.Add(basketCardItem);
            }
            else
            {
                basketCardItem.Quantity = quantity;
            }

            await context.SaveChangesAsync();
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Edit(string id, [Bind("BasketCardId,CreatedOn,IsActive,ShippingAmount")] BasketCard basketCard)
        {
            if (id != basketCard.BasketCardId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(basketCard);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BasketCardExists(basketCard.BasketCardId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(basketCard));
        }
Exemplo n.º 4
0
        public async Task <BasketCard> UpdateBasketCard(BasketCard basketCard)
        {
            var updated = await _context.Redis.StringSetAsync(basketCard.Username, JsonConvert.SerializeObject(basketCard));

            if (!updated)
            {
                return(null);
            }

            return(await GetBasketCard(basketCard.Username));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Create([Bind("BasketCardId,CreatedOn,IsActive,ShippingAmount")] BasketCard basketCard)
        {
            if (ModelState.IsValid)
            {
                _context.Add(basketCard);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(basketCard));
        }
        public async Task <IActionResult> PostBasketCard([FromBody] BasketCard basketCard)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.BasketCards.Add(basketCard);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetBasketCard", new { id = basketCard.BasketCardId }, basketCard));
        }
Exemplo n.º 7
0
        public async Task <ActionResult <BasketCard> > UpdateGroup(BasketCard basketCard)
        {
            var updatedBasketCard = await _repository.UpdateBasketCard(basketCard);

            return(Ok(updatedBasketCard));
        }
Exemplo n.º 8
0
 public async Task <ActionResult <BasketCard> > UpdateBasket([FromBody] BasketCard basket)
 {
     return(Ok(await _repository.UpdateBasket(basket)));
 }
Exemplo n.º 9
0
        public void AddNullBasketItemThrowsDomainException()
        {
            var card = new BasketCard();

            Assert.Throws <DomainException>(() => { card.AddItem(null, 1); });
        }