示例#1
0
        public async Task <ActionResult> Add(int Id)
        {
            CartItemsRepository cartItemsRepository = new CartItemsRepository();
            CartItem            cartItem            = await cartItemsRepository.GetCartItemAsync(Id);

            cartItem.Amount++;
            await cartItemsRepository.SaveCartItemAsync(cartItem); //TODO: Obsluga result

            return(RedirectToAction("Index"));
        }
示例#2
0
        public async Task <ActionResult> Remove(int Id)
        {
            CartItemsRepository cartItemsRepository = new CartItemsRepository();
            CartItem            cartItem            = await cartItemsRepository.GetCartItemAsync(Id);

            cartItem.Amount--;
            if (cartItem.Amount <= 0)
            {
                await cartItemsRepository.DeleteCartItemAsync(cartItem);
            }
            else
            {
                await cartItemsRepository.SaveCartItemAsync(cartItem); //TODO: Obsluga result
            }
            return(RedirectToAction("Index"));
        }
示例#3
0
        public async Task ShouldReturn2CartItems()
        {
            //Arrange
            UseSqlServer();
            using var context = GetDbContext();
            var cart          = new Mock <CartService>();
            var cartItemsRepo = new CartItemsRepository();

            cart.Object.CartItems = cartItemsRepo.CartItems.ToList();

            //Act
            var result = await cart.Object.GetCartItemsAsync();

            //Assert
            Assert.NotEmpty(result);
            Assert.Equal(2, result.Count());
            Assert.Contains(result, r => r.Game.Title == "Test2");
            Assert.DoesNotContain(result, r => r.Game.Title == "Test60");
        }
示例#4
0
 public CartItemsServices(CartItemsRepository cartItemsRepository)
 {
     this.cartItemsRepository = cartItemsRepository;
 }
示例#5
0
        public async Task <ActionResult> AddToCart(int id)
        {
            // TODO: Code duplicate form CartController
            CartsRepository     cartRepository      = new CartsRepository();
            CartItemsRepository cartItemsRepository = new CartItemsRepository();
            string currentUserId = User.Identity.GetUserId();
            UserManager <ApplicationUser> manager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(db));
            ApplicationUser currentUser           = manager.FindById(currentUserId);

            if (currentUser is null)
            {
                return(Redirect("../../Account/Login"));
            }

            Cart cart = await db.Carts.Where(o => o.Id == currentUser.CartId).SingleOrDefaultAsync();

            if (cart == null)
            {
                cart = new Cart();
                db.Entry(cart).State = EntityState.Added;
                cart.Id                     = db.Users.Max(o => o.CartId) + 1;
                currentUser.CartId          = cart.Id;
                db.Entry(currentUser).State = EntityState.Modified;
            }
            Product  product = db.Products.Where(o => o.Id == id).Single();
            CartItem item    = await db.CartItems.Where(o => o.Product.Id == product.Id).SingleOrDefaultAsync();

            if (item != default(CartItem))
            {
                item.Amount++;
                db.Entry(item).State = EntityState.Modified;
            }
            else
            {
                item = new CartItem();
                db.Entry(item).State = EntityState.Added;
                item.Product         = product;
                item.CartId          = cart.Id;
                cart.CartItems.Add(item);
                item.Cart = cart;
                if (db.Entry(cart).State != EntityState.Added)
                {
                    db.Entry(cart).State = EntityState.Modified;
                }
            }


            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                    eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                        ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }

            return(RedirectToAction("Index"));
        }