public void AddItemToBasketAsync_WithNonExistentBasket_ShouldReturnFailure()
        {
            // Arrange
            _basketRepositoryMock.Setup(x => x.GetByIdAsync(It.IsAny<int>())).Returns(Task.FromResult<Basket>(null));

            // Act
            var result = _basketService.AddItemToBasketAsync(1, 1, 1, Pounds.Of(1)).Result;

            // Assert
            Assert.True(result.IsFailure);
        }
Esempio n. 2
0
        public async Task <IActionResult> AddItemToBasket(BasketItemCreateViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid model"));
            }

            BasketViewModel basket = await _basketViewModelService.GetOrCreateBasketForUserAsync(User.Identity.Name);

            var priceResult = Pounds.Create(model.PriceInPounds);

            if (priceResult.IsFailure)
            {
                return(BadRequest(priceResult.Error));
            }

            var result = await _basketService.AddItemToBasketAsync(basket.Id, model.ProductId, model.Quantity, priceResult.Value);

            if (result.IsFailure)
            {
                return(NotFound(result.Error));
            }

            BasketItemViewModel item = await _basketViewModelService.GetBasketItemForUserAsync(User.Identity.Name, result.Value);

            return(CreatedAtAction("GetItem", new { id = item.Id }, item));
        }
Esempio n. 3
0
        public async Task <IActionResult> AddToBasket(int id, int quantity = 1)
        {
            // get product details
            var product = await _basketViewModelService.GetProductDetails(id);

            // get/create user id
            string userId = GetOrCreateUserId();

            // create basket if not exists and get basket id
            int basketId;

            if (!await _basketService.BasketExistsAsync(userId))
            {
                basketId = await _basketService.CreateBasketAsync(userId);
            }
            else
            {
                basketId = await _basketService.GetBasketIdAsync(userId);
            }

            // add item to the basket
            int count = await _basketService.AddItemToBasketAsync(basketId, product.Id, product.Price, quantity);

            return(Json(new { BasketItemCount = count }));
        }
Esempio n. 4
0
        public async Task <ActionResult> AddItemToBasket(AddItemToBasketDTO dto)
        {
            var basketId = GetBasketId();

            using var uow = _unitOfWorkFactory.Create();

            var basket = await _basketService.AddItemToBasketAsync(basketId, dto.ProductId, dto.Quantity);

            uow.Commit();

            if (basketId == null)
            {
                CreateBasketCookie(basket.Id);
            }

            var basketDTO = _mapper.Map <BasketDTO>(basket);

            return(CreatedAtAction(nameof(Get), basketDTO));
        }