public void PostCart_NoCartExists_ProductAdded()
        {
            const int newQuantity       = 1;
            const int userIdWithoutCart = 2;
            var       request           = new AddProductToCartRequest
            {
                ProductId                 = existingItem.Id.ProductId,
                MarketplaceSupplierId     = existingItem.Id.MarketplaceSupplierId,
                SecondHandSalesOfferId    = existingItem.Id.SecondHandSalesOfferId,
                SubscriptionItemProductId = existingItem.Id.SubscriptionItemProductId,
                Quantity = newQuantity
            };

            var result = new ShoppingCartApiController(
                LoadCart,
                CreateCart,
                LoadProductBaseData,
                LoadRetailOfferData,
                new HttpContextAccessor().WithLoggedInUser(userIdWithoutCart))
                         .AddProduct(userIdWithoutCart, validShoppingCartId, culture, request);

            Assert.That(result, Is.AssignableTo <ObjectResult>());
            var value = ((ObjectResult)result).Value;

            Assert.That(value, Is.AssignableTo <ShoppingCartResponse>());
            var shoppingCartResponse = (ShoppingCartResponse)value;

            Assert.That(shoppingCartResponse.Items[0].Quantity, Is.EqualTo(newQuantity));
        }
 public IActionResult AddProduct(
     [FromRoute] int userId,
     [FromRoute] int shoppingCartId,
     [FromQuery] string culture,
     [FromBody] AddProductToCartRequest request) =>
 ValidateModelState(ModelState)
 .AndThen(_ => ValidateShoppingCartId(shoppingCartId))
 .AndThen(_ => CheckUserId(httpContextAccessor.HttpContext, userId))
 .AndThen(GetUserCart)
 .AndThen(c => AddToCart(c, request))
 .Map(c => {
     WriteShoppingCart(c);
     return(c);
 })
 .Map(MapToResponse)
 .Match <IActionResult>(Ok, err => StatusCode((int)err));
        public void PostCart_InvalidCartId_ReturnsBadRequest(int cartId)
        {
            var request = new AddProductToCartRequest
            {
                ProductId                 = existingItem.Id.ProductId,
                MarketplaceSupplierId     = existingItem.Id.MarketplaceSupplierId,
                SecondHandSalesOfferId    = existingItem.Id.SecondHandSalesOfferId,
                SubscriptionItemProductId = existingItem.Id.SubscriptionItemProductId,
                Quantity = 0
            };

            var result = new ShoppingCartApiController(
                LoadCart,
                CreateCart,
                LoadProductBaseData,
                LoadRetailOfferData,
                new HttpContextAccessor())
                         .AddProduct(0, cartId, culture, request);

            Assert.That(result, Is.AssignableTo <StatusCodeResult>());
            var resultCode = ((StatusCodeResult)result).StatusCode;

            Assert.That(resultCode, Is.EqualTo((int)HttpStatusCode.BadRequest));
        }
 private Result <Cart, HttpStatusCode> AddToCart(Cart cart, AddProductToCartRequest request) =>
 loadProductBaseData(request.ProductId)
 .AndThen(pbd => loadRetailOfferData(request.ProductId).Map(ro => (BaseData: pbd, RetailOffer: ro)))