public LoggedInConsumerTest() { basketRedisService = new Mock <IBasketRedisService>(); logger = new Mock <ILogger <LoggedInConsumer> >(); consumer = new LoggedInConsumer(basketRedisService.Object, logger.Object); basketDto = new UserBasketDto(); }
public async Task <ActionResult <UserBasket> > UpdateBasket(UserBasketDto basket) { var customerBasket = _mapper.Map <UserBasketDto, UserBasket>(basket); var updatedBasket = await _basketRepository.UpdateBasketAsync(customerBasket); return(Ok(updatedBasket)); }
public AddProductCommandHandlerTest() { basketRedisService = new Mock <IBasketRedisService>(); command = new AddProductCommand { UserId = It.IsAny <string>(), Product = new BasketProduct() }; commandHandler = new AddProductCommandHandler(basketRedisService.Object); basketDto = new UserBasketDto(); }
public GetBasketQueryHandlerTest() { basketRedisService = new Mock <IBasketRedisService>(); query = new GetBasketQuery(userId); queryHandler = new GetBasketQueryHandler(basketRedisService.Object); basketDto = new UserBasketDto { Products = new List <BasketProduct> { new BasketProduct() } }; }
public async Task SaveBasket(string userId, UserBasketDto basket) { await RemoveBasket(userId); string serializeObject = JsonConvert.SerializeObject(basket); await distributedCache.SetStringAsync(userId, serializeObject, new DistributedCacheEntryOptions() { AbsoluteExpiration = DateTimeOffset.Now.AddDays(1) }); }
private async Task RemoveProductAndDecreaseBasketTotalPrice(UserBasketDto basket, RemoveProductCommand request) { var productToRemove = basket.Products.Where(x => x.Id == request.ProductId).FirstOrDefault(); if (productToRemove == null) { return; } basket.Products.Remove(productToRemove); basket.TotalPrice -= (productToRemove.Price * productToRemove.Quantity); await basketRedisService.SaveBasket(request.UserId, basket); }
private async Task AddProductToBasketAndIncreaseBasketTotalPrice(BasketProduct basketProduct, UserBasketDto basket, AddProductCommand request) { if (basketProduct != null) { basketProduct.Quantity += request.Product.Quantity; } else { basket.Products.Add(request.Product); } basket.TotalPrice += (request.Product.Price * request.Product.Quantity); await basketRedisService.SaveBasket(request.UserId, basket); }
public RemoveProductCommandHandlerTest() { basketRedisService = new Mock <IBasketRedisService>(); command = new RemoveProductCommand(userId, productId); commandHandler = new RemoveProductCommandHandler(basketRedisService.Object); basketDto = new UserBasketDto { Products = new List <BasketProduct>() { new BasketProduct { Id = productId } } }; }
public ChangeProductQuantityCommandHandlerTest() { basketRedisService = new Mock <IBasketRedisService>(); command = new ChangeProductQuantityCommand { UserId = userId, ProductId = productId, ChangeQuantityAction = action }; commandHandler = new ChangeProductQuantityCommandHandler(basketRedisService.Object); basketDto = new UserBasketDto { Products = new List <BasketProduct> { new BasketProduct { Id = productId } } }; }
public async Task GetBasketQuantityQueryHandler_Success() { //Arrange var userBasketDto = new UserBasketDto { Products = new List <BasketProduct> { new BasketProduct(), new BasketProduct() } }; basketRedisService.Setup(x => x.GetBasket(userId)).Returns(Task.FromResult(userBasketDto)); //Act var action = await queryHandler.Handle(query, It.IsAny <CancellationToken>()); //Assert Assert.Equal(userBasketDto.Products.Count, action); }
private void MergeProducts(UserBasketDto sessionBasket, UserBasketDto userBasket) { foreach (var sessionProduct in sessionBasket.Products) { var userProduct = userBasket.Products.Where(x => x.Id == sessionProduct.Id).FirstOrDefault(); if (userProduct == null) { userBasket.Products.Add(sessionProduct); } else { userProduct.Quantity += sessionProduct.Quantity; userProduct.Price += sessionProduct.Price; } userBasket.TotalPrice += sessionProduct.Price; } }
private void RemoveProductIfQuantityIsLessOrEqualZero(BasketProduct basketProduct, UserBasketDto basket) { if (basketProduct.Quantity <= 0) { basket.Products.Remove(basketProduct); } }
private async Task ChangeProductQuantityAndBasketTotalPrice(BasketProduct basketProduct, UserBasketDto basket, ChangeProductQuantityCommand request) { switch (request.ChangeQuantityAction) { case ChangeQuantityAction.Plus: basketProduct.Quantity++; basket.TotalPrice += basketProduct.Price; break; case ChangeQuantityAction.Minus: basketProduct.Quantity--; basket.TotalPrice -= basketProduct.Price; break; default: break; } RemoveProductIfQuantityIsLessOrEqualZero(basketProduct, basket); await basketRedisService.SaveBasket(request.UserId, basket); }
public async Task <ActionResult <ProductToReturnDto> > AddProductIngredient(int id, [FromForm] UserBasketDto itemDto) { var spec = new ProductsWithTypesAndBrandsSpecification(id); var product = await _unitOfWork.Repository <Product>().GetEntityWithSpec(spec); var order = await _recipeService.CreateOrderAsync(id, itemDto.Id); if (order == null) { return(BadRequest(new ApiResponse(400, "Problem creating order"))); } return(Ok(order)); }