示例#1
0
        public UpdateQuantityOutput UpdateItem(string customerId, UpdateQuantityInput input)
        {
            var updateQuantityInput = _mapper.Map <UpdateQuantityInput>(input);
            var result = _apiService.UpdateItem(customerId, updateQuantityInput);

            return(_mapper.Map <UpdateQuantityOutput>(result));
        }
示例#2
0
        public async Task UpdateQuantidade_Invalid_ProductId()
        {
            //arrange
            var customerId = "cliente_id";
            UpdateQuantityInput updateQuantidadeInput = new UpdateQuantityInput(null, 7);

            basketServiceMock
            .Setup(c => c.UpdateItem(customerId, It.IsAny <UpdateQuantityInput>()))
            .ReturnsAsync(new UpdateQuantityOutput(new BasketItem(), new CustomerBasket()))
            .Verifiable();

            var controller = GetBasketController();

            SetControllerUser(customerId, controller);
            controller.ModelState.AddModelError("ProductId", "Required");

            //act
            var result = await controller.UpdateQuantity(updateQuantidadeInput);

            //assert
            var badRequestObjectResult = Assert.IsType <BadRequestObjectResult>(result);

            Assert.IsType <SerializableError>(badRequestObjectResult.Value);
            catalogServiceMock.Verify();
        }
        public async Task UpdateBasketAsync_success()
        {
            //arrange
            var json1 = JsonConvert.SerializeObject(new CustomerBasket("123")
            {
                Items = new List <BasketItem> {
                    new BasketItem("001", "001", "produto 001", 12.34m, 1)
                }
            });
            var json2 = JsonConvert.SerializeObject(new CustomerBasket("123")
            {
                Items = new List <BasketItem> {
                    new BasketItem("001", "001", "produto 001", 12.34m, 2)
                }
            });

            string customerId   = "123";
            var    databaseMock = new Mock <IDatabase>();

            databaseMock
            .Setup(d => d.StringSetAsync(
                       It.IsAny <RedisKey>(),
                       It.IsAny <RedisValue>(),
                       null,
                       When.Always,
                       CommandFlags.None
                       ))
            .ReturnsAsync(true)
            .Verifiable();
            databaseMock
            .SetupSequence(d => d.StringGetAsync(It.IsAny <RedisKey>(), It.IsAny <CommandFlags>()))
            .ReturnsAsync("")
            .ReturnsAsync(json1)
            .ReturnsAsync(json2);

            redisMock
            .Setup(r => r.GetDatabase(It.IsAny <int>(), It.IsAny <object>()))
            .Returns(databaseMock.Object)
            .Verifiable();

            var repository
                = new RedisBasketRepository(loggerMock.Object, redisMock.Object);

            var item = new UpdateQuantityInput("001", 2);

            //act
            var output = await repository.UpdateBasketAsync(customerId, item);

            //assert
            Assert.Equal(customerId, output.CustomerBasket.CustomerId);
            Assert.Collection(output.CustomerBasket.Items,
                              i =>
            {
                Assert.Equal("001", i.ProductId);
                Assert.Equal(2, i.Quantity);
            });

            databaseMock.Verify();
            redisMock.Verify();
        }
        public UpdateQuantityOutput UpdateBasket(string customerId, UpdateQuantityInput item)
        {
            if (item == null)
            {
                throw new ArgumentNullException();
            }

            if (string.IsNullOrWhiteSpace(item.ProductId))
            {
                throw new ArgumentException();
            }

            if (item.Quantity < 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            var        basket = GetBasket(customerId);
            BasketItem itemDB = basket.Items.Where(i => i.ProductId == item.ProductId).SingleOrDefault();

            itemDB.Quantity = item.Quantity;
            if (item.Quantity == 0)
            {
                basket.Items.Remove(itemDB);
            }
            CustomerBasket customerBasket = UpdateBasket(basket);

            return(new UpdateQuantityOutput(itemDB, customerBasket));
        }
        public UpdateQuantityOutput UpdateItem(string customerId, UpdateQuantityInput input)
        {
            if (string.IsNullOrWhiteSpace(customerId))
            {
                throw new ArgumentNullException(nameof(customerId));
            }

            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            return(_repository.UpdateBasket(customerId, input));
        }
示例#6
0
        public async Task <IActionResult> UpdateQuantity([FromBody] UpdateQuantityInput input)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            UpdateQuantityOutput value = await basketService.UpdateItem(GetUserId(), input);

            if (value == null)
            {
                return(NotFound(input));
            }

            return(base.Ok(value));
        }
        public IActionResult UpdateQuantityInCart([FromBody] UpdateQuantityInput input)
        {
            Session session      = db.Sessions.FirstOrDefault(x => x.Id == Request.Cookies["sessionId"]);
            User    guestUser    = db.Users.FirstOrDefault(x => x.Id == Request.Cookies["guestId"]);
            Cart    existingCart = null;

            if ((session != null || guestUser != null) && !(session != null && guestUser != null))
            {
                //logged in user
                if (session != null)
                {
                    existingCart = db.Carts.FirstOrDefault(x => (x.UserId == session.UserId));
                }
                //guest user
                else
                {
                    existingCart = db.Carts.FirstOrDefault(x => (x.UserId == guestUser.Id));
                }
            }
            List <CartDetail> existingCartDetails       = existingCart.CartDetails.ToList();
            CartDetail        cartDetailWithThisProduct = existingCartDetails.Find(x => x.ProductId == int.Parse(input.ProductId));

            if (input.Plus)
            {
                cartDetailWithThisProduct.Quantity = cartDetailWithThisProduct.Quantity + 1;
                db.SaveChanges();
            }
            else
            {
                if (cartDetailWithThisProduct.Quantity > 1)
                {
                    cartDetailWithThisProduct.Quantity = cartDetailWithThisProduct.Quantity - 1;
                    db.SaveChanges();
                }
            }

            double TotalPrice = 0.00;

            foreach (CartDetail cd in existingCartDetails)
            {
                TotalPrice = TotalPrice + cd.Quantity * cd.Product.Price;
            }

            string pdtprice   = $"${(cartDetailWithThisProduct.Product.Price*cartDetailWithThisProduct.Quantity):#,0.00}";
            string totalprice = $"${TotalPrice:#,0.00}";

            return(Json(new { status = "success", productId = input.ProductId, price = pdtprice, quantity = cartDetailWithThisProduct.Quantity, totalprice = totalprice }));
        }
        public async Task <ActionResult <UpdateQuantityOutput> > UpdateItem([FromBody] UpdateQuantityInput input)
        {
            string customerId = userManager.GetUserId(this.User);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var output = await basketRepository.UpdateBasketAsync(customerId, input);

                return(Ok(output));
            }
            catch (KeyNotFoundException)
            {
                return(NotFound(customerId));
            }
        }
示例#9
0
        public async Task UpdateQuantidade_ProductId_NotFound()
        {
            //arrange
            var customerId = "cliente_id";
            UpdateQuantityInput updateQuantidadeInput = new UpdateQuantityInput("001", 7);

            basketServiceMock
            .Setup(c => c.UpdateItem(customerId, It.IsAny <UpdateQuantityInput>()))
            .ReturnsAsync((UpdateQuantityOutput)null)
            .Verifiable();

            var controller = GetBasketController();

            SetControllerUser(customerId, controller);

            //act
            var result = await controller.UpdateQuantity(updateQuantidadeInput);

            //assert
            var notFoundObjectResult = Assert.IsType <NotFoundObjectResult>(result);

            Assert.Equal(updateQuantidadeInput, notFoundObjectResult.Value);
            catalogServiceMock.Verify();
        }
示例#10
0
        public async Task UpdateQuantidade_Success()
        {
            //arrange
            var customerId = "cliente_id";
            var controller = GetBasketController();

            SetControllerUser(customerId, controller);
            var itemBasket = GetFakeItemBasket();
            UpdateQuantityInput updateQuantidadeInput = new UpdateQuantityInput("001", 7);

            basketServiceMock
            .Setup(c => c.UpdateItem(customerId, It.IsAny <UpdateQuantityInput>()))
            .ReturnsAsync(new UpdateQuantityOutput(itemBasket, new CustomerBasket()))
            .Verifiable();

            //act
            var result = await controller.UpdateQuantity(updateQuantidadeInput);

            //assert
            var okObjectResult = Assert.IsType <OkObjectResult>(result);

            Assert.IsType <UpdateQuantityOutput>(okObjectResult.Value);
            catalogServiceMock.Verify();
        }
示例#11
0
        public async Task <ActionResult <UpdateQuantityOutput> > UpdateItem(string customerId, [FromBody] UpdateQuantityInput input)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var output = await _repository.UpdateBasketAsync(customerId, input);

                await this._connection
                .InvokeAsync("UpdateUserBasketCount", $"{customerId}", output.CustomerBasket.Items.Count);

                return(Ok(output));
            }
            catch (KeyNotFoundException)
            {
                return(NotFound(customerId));
            }
        }
        public async Task <UpdateQuantityOutput> UpdateItem(string customerId, UpdateQuantityInput input)
        {
            var uri = $"{BasketUris.UpdateItem}/{customerId}";

            return(await PutAsync <UpdateQuantityOutput>(uri, input));
        }