Esempio n. 1
0
        public async Task <ActivityCart> AddToCart(int activityId, string userEmail = null)
        {
            var cartItem = _dbContext.ActivityCart.Any(x => x.Username == userEmail) ?
                           _dbContext.ActivityCart.AsNoTracking().Where(x => x.Username == userEmail).FirstOrDefault() : null;
            List <ActivityCartDTO> dtoLst = new List <ActivityCartDTO>();

            if (cartItem != null)
            {
                if (!string.IsNullOrEmpty(cartItem.ItemDetails))
                {
                    dtoLst = JsonConvert.DeserializeObject <List <ActivityCartDTO> >(cartItem.ItemDetails);
                }
            }

            var             activityDetails = _dbContext.Activities.Where(x => x.activity_id == activityId).FirstOrDefault();
            ActivityCartDTO dto             = new ActivityCartDTO
            {
                ActivityID          = activityDetails.activity_id,
                ActivityDescription = activityDetails.activity_description,
                ActivityFee         = activityDetails.activity_fee,
                ActivityName        = activityDetails.activity_name,
                ActivityImage       = activityDetails.activity_image_path,
                Quantity            = 1
            };

            dtoLst.Add(dto);


            string itemJson = JsonConvert.SerializeObject(dtoLst);

            if (string.IsNullOrEmpty(userEmail))
            {
                return(null);
            }

            ActivityCart entity = new ActivityCart
            {
                ItemDetails = itemJson,
                Username    = userEmail
            };

            ActivityCart result = new ActivityCart();

            if (cartItem != null)
            {
                entity.Id = cartItem.Id;
                var exeResult = _dbContext.ActivityCart.Update(entity);
                result = exeResult.Entity;
            }
            else
            {
                var exeResult = await _dbContext.ActivityCart.AddAsync(entity);

                result = exeResult.Entity;
            }

            await _dbContext.SaveChangesAsync();

            return(result);
        }
        public async Task BuyNowAsync_Success()
        {
            //Arrange
            mockSession();
            ActivityCart cart = new ActivityCart()
            {
                Id          = 1,
                ItemDetails = JsonConvert.SerializeObject(new ActivityCartDTO()
                {
                    ActivityID          = 1,
                    ActivityImage       = "test//test",
                    ActivityDescription = "test",
                    ActivityName        = "test",
                    ActivityFee         = 50,
                    Quantity            = 1
                }),
                Username = "******"
            };

            _shopping.Setup(x => x.AddToCart(It.IsAny <int>(), It.IsAny <string>())).Returns(Task.FromResult(cart));

            //Act
            var result = await controller.BuyNowAsync(1) as RedirectToActionResult;

            //Assert
            Assert.Equal("GetUserDetails", result.ActionName);
        }
        public async Task AddToCart_Success()
        {
            //Arrange
            mockSession();
            List <ActivityCartDTO> lst = new List <ActivityCartDTO>()
            {
                new ActivityCartDTO()
                {
                    ActivityID          = 1,
                    ActivityImage       = "test//test",
                    ActivityDescription = "test",
                    ActivityName        = "test",
                    ActivityFee         = 50,
                    Quantity            = 1
                }
            };
            ActivityCart cart = new ActivityCart()
            {
                Id          = 1,
                ItemDetails = JsonConvert.SerializeObject(lst),
                Username    = "******"
            };

            _shopping.Setup(x => x.AddToCart(It.IsAny <int>(), It.IsAny <string>())).Returns(Task.FromResult(cart));

            //Act
            var result = await controller.AddToCart(1) as ViewResult;

            //Assert
            Assert.NotNull(result.Model);
        }
        public async Task DeleteFromCart_Success()
        {
            //Arrange
            mockSession();
            mockSession();
            List <ActivityCartDTO> lst = new List <ActivityCartDTO>()
            {
                new ActivityCartDTO()
                {
                    ActivityID          = 1,
                    ActivityImage       = "test//test",
                    ActivityDescription = "test",
                    ActivityName        = "test",
                    ActivityFee         = 50,
                    Quantity            = 1
                }
            };
            ActivityCart cart = new ActivityCart()
            {
                Id          = 1,
                ItemDetails = JsonConvert.SerializeObject(lst),
                Username    = "******"
            };

            _shopping.Setup(x => x.RemoveFromCart(It.IsAny <int>(), It.IsAny <string>())).Returns(Task.FromResult(cart));

            //Act
            var result = await controller.DeleteFromCart(1) as RedirectToActionResult;

            //Assert
            Assert.Equal("ViewShoppingCart", result.ActionName);
        }
        public static ActivityCartDTO TransformcartItem(ActivityCart cartItem, int activityID)
        {
            //cart item null check. Exception here
            var item = JsonConvert.DeserializeObject <List <ActivityCartDTO> >(cartItem.ItemDetails);

            ActivityCartDTO returnItem = item.Where(x => x.ActivityID == activityID).FirstOrDefault();

            return(returnItem);
        }