Exemplo n.º 1
0
 public IReadOnlyList <PricedProductItem> Calculate(params ProductItem[] productItems)
 {
     return(productItems
            .Select(pi =>
                    PricedProductItem.Create(pi, FakePrice))
            .ToList());
 }
    public async Task <IActionResult> RemoveProduct(
        [FromServices] Func <RemoveProductItemFromShoppingCart, CancellationToken, ValueTask> handle,
        Guid id,
        [FromBody] RemoveProductRequest request,
        CancellationToken ct
        )
    {
        if (request == null)
        {
            throw new ArgumentNullException(nameof(request));
        }

        var command = RemoveProductItemFromShoppingCart.From(
            id,
            PricedProductItem.From(
                ProductItem.From(
                    request.ProductItem?.ProductId,
                    request.ProductItem?.Quantity
                    ),
                request.ProductItem?.UnitPrice
                ),
            request.Version
            );

        await handle(command, ct);

        return(Ok());
    }
Exemplo n.º 3
0
        public static ProductAdded Create(Guid cartId, PricedProductItem productItem)
        {
            Guard.Against.Default(cartId, nameof(cartId));
            Guard.Against.Null(productItem, nameof(productItem));

            return(new ProductAdded(cartId, productItem));
        }
Exemplo n.º 4
0
        public void RemoveProduct(
            PricedProductItem productItemToBeRemoved)
        {
            Guard.Against.Null(productItemToBeRemoved, nameof(productItemToBeRemoved));

            if (Status != CartStatus.Pending)
            {
                throw new InvalidOperationException($"Removing product from the cart in '{Status}' status is not allowed.");
            }

            var existingProductItem = FindProductItemMatchingWith(productItemToBeRemoved);

            if (existingProductItem is null)
            {
                throw new InvalidOperationException($"Product with id `{productItemToBeRemoved.ProductId}` and price '{productItemToBeRemoved.UnitPrice}' was not found in cart.");
            }

            if (existingProductItem.HasEnough(productItemToBeRemoved.Quantity))
            {
                throw new InvalidOperationException($"Cannot remove {productItemToBeRemoved.Quantity} items of Product with id `{productItemToBeRemoved.ProductId}` as there are only ${existingProductItem.Quantity} items in card");
            }

            var @event = ProductRemoved.Create(Id, productItemToBeRemoved);

            Enqueue(@event);
            Apply(@event);
        }
    public static ProductRemoved Create(Guid cartId, PricedProductItem productItem)
    {
        if (cartId == Guid.Empty)
        {
            throw new ArgumentOutOfRangeException(nameof(cartId));
        }

        return(new ProductRemoved(cartId, productItem));
    }
Exemplo n.º 6
0
        public IReadOnlyList <PricedProductItem> Calculate(params ProductItem[] productItems)
        {
            Guard.Against.Zero(productItems.Length, "Product items count");

            var random = new Random();

            return(productItems
                   .Select(pi =>
                           PricedProductItem.Create(pi, (decimal)random.NextDouble() * 100))
                   .ToList());
        }
    public IReadOnlyList <PricedProductItem> Calculate(params ProductItem[] productItems)
    {
        if (productItems.Length == 0)
        {
            throw new ArgumentOutOfRangeException(nameof(productItems.Length));
        }

        var random = new Random();

        return(productItems
               .Select(pi =>
                       PricedProductItem.Create(pi, (decimal)random.NextDouble() * 100))
               .ToList());
    }
    public async Task <IActionResult> RemoveProduct(Guid id, [FromBody] RemoveProductRequest request)
    {
        var command = Carts.RemovingProduct.RemoveProduct.Create(
            id,
            PricedProductItem.Create(
                request?.ProductItem?.ProductId,
                request?.ProductItem?.Quantity,
                request?.ProductItem?.UnitPrice
                )
            );

        await commandBus.Send(command);

        return(Ok());
    }
    public ProductItemsList Remove(PricedProductItem productItem)
    {
        var clone = new List <PricedProductItem>(items);

        var currentProductItem = Find(productItem);

        if (currentProductItem == null)
        {
            throw new InvalidOperationException("Product item wasn't found");
        }

        clone.Remove(currentProductItem);

        return(new ProductItemsList(clone));
    }
    public async Task <IActionResult> InitOrder([FromBody] InitOrderRequest?request)
    {
        var orderId = idGenerator.New();

        var command = InitializeOrder.Create(
            orderId,
            request?.ClientId,
            request?.ProductItems?.Select(
                pi => PricedProductItem.Create(pi.ProductId, pi.Quantity, pi.UnitPrice)).ToList(),
            request?.TotalPrice
            );

        await commandBus.Send(command);

        return(Created("api/Orders", orderId));
    }
    public ProductItemsList Add(PricedProductItem productItem)
    {
        var clone = new List <PricedProductItem>(items);

        var currentProductItem = Find(productItem);

        if (currentProductItem == null)
        {
            clone.Add(productItem);
        }
        else
        {
            clone[clone.IndexOf(currentProductItem)] = currentProductItem.MergeWith(productItem);
        }

        return(new ProductItemsList(clone));
    }
Exemplo n.º 12
0
        public async Task <IActionResult> InitOrder([FromBody] InitOrderRequest request)
        {
            Guard.Against.Null(request, nameof(request));

            var orderId = idGenerator.New();

            var command = Commands.InitOrder.Create(
                orderId,
                request.ClientId,
                request.ProductItems?.Select(
                    pi => PricedProductItem.Create(pi.ProductId, pi.Quantity, pi.UnitPrice)).ToList(),
                request.TotalPrice
                );

            await commandBus.Send(command);

            return(Created("api/Orders", orderId));
        }
Exemplo n.º 13
0
        public async Task <IActionResult> RemoveProduct(Guid id, [FromBody] RemoveProductRequest request)
        {
            Guard.Against.Null(request, nameof(request));
            Guard.Against.Null(request.ProductItem, nameof(request));

            var command = Commands.RemoveProduct.Create(
                id,
                PricedProductItem.Create(
                    request.ProductItem.ProductId,
                    request.ProductItem.Quantity,
                    request.ProductItem.UnitPrice
                    )
                );

            await commandBus.Send(command);

            return(Ok());
        }
Exemplo n.º 14
0
 private PricedProductItem FindProductItemMatchingWith(PricedProductItem productItem)
 {
     return(ProductItems
            .SingleOrDefault(pi => pi.MatchesProductAndPrice(productItem)));
 }
Exemplo n.º 15
0
 public ProductRemoved(Guid cartId, PricedProductItem productItem)
 {
     CartId      = cartId;
     ProductItem = productItem;
 }
Exemplo n.º 16
0
 private ProductAdded(Guid cartId, PricedProductItem productItem)
 {
     CartId      = cartId;
     ProductItem = productItem;
 }
Exemplo n.º 17
0
 public static RemoveProduct Create(Guid cardId, PricedProductItem productItem)
 {
     return(new(cardId, productItem));
 }
Exemplo n.º 18
0
 private RemoveProduct(Guid cardId, PricedProductItem productItem)
 {
     CartId      = cardId;
     ProductItem = productItem;
 }
 public PricedProductItem?Find(PricedProductItem productItem) =>
 items.SingleOrDefault(
     pi => pi.MatchesProductAndUnitPrice(productItem)
     );