コード例 #1
0
        public async Task <CartDto> GetAsync(Guid userId)
        {
            var cart = await _cartsRepository.GetAsync(userId);

            return(cart == null ? null : new CartDto
            {
                Id = cart.Id,
                Items = cart.Items.Select(x => new CartItemDto
                {
                    ProductId = x.ProductId,
                    Quantity = x.Quantity
                })
            });
        }
コード例 #2
0
        public async Task HandleAsync(OrderCompleted @event, ICorrelationContext context)
        {
            var cart = await _cartsRepository.GetAsync(@event.CustomerId);

            cart.Clear();
            await _cartsRepository.UpdateAsync(cart);
        }
コード例 #3
0
        public async Task HandleAsync(AddProductToCartCommand command, ICorrelationContext context)
        {
            if (command.Quantity <= 0)
            {
                throw new ASampleException(Codes.InvalidQuantity,
                                           $"Invalid quantity: '{command.Quantity}'.");
            }

            var product = await _productsRepository.GetAsync(command.ProductId);

            if (product == null)
            {
                throw new ASampleException(Codes.ProductNotFound,
                                           $"Product: '{command.ProductId}' was not found.");
            }

            if (product.Quantity < command.Quantity)
            {
                throw new ASampleException(Codes.NotEnoughProductsInStock,
                                           $"Not enough products in stock: '{command.ProductId}'.");
            }

            var cart = await _cartsRepository.GetAsync(command.CustomerId);

            cart.AddProduct(product, command.Quantity);
            await _cartsRepository.UpdateAsync(cart);

            await _busPublisher.PublishAsync(new ProductAddedToCartEvent(command.CustomerId,
                                                                         command.ProductId, command.Quantity), context);
        }
コード例 #4
0
        public async Task HandleAsync(ClearCart command, ICorrelationContext context)
        {
            var cart = await _cartsRepository.GetAsync(command.CustomerId);

            cart.Clear();
            await _cartsRepository.UpdateAsync(cart);

            await _busPublisher.PublishAsync(new CartCleared(command.CustomerId), context);
        }
コード例 #5
0
        public async Task HandleAsync(ClearCartCommand command)
        {
            var cart = await _cartRepository.GetAsync(command.CustomerId);

            cart.NullCheck(ErrorCodes.cart_not_found, command.CustomerId);

            cart.Clear();
            await _cartRepository.UpdateAsync(cart);
        }
コード例 #6
0
        public async Task HandleAsync(DeleteProductFromCartCommand command, ICorrelationContext context)
        {
            var cart = await _cartsRepository.GetAsync(command.CustomerId);

            cart.DeleteProduct(command.ProductId);
            await _cartsRepository.UpdateAsync(cart);

            await _busPublisher.PublishAsync(new ProductDeletedFromCartEvent(command.CustomerId,
                                                                             command.ProductId), context);
        }
コード例 #7
0
        public async Task HandleAsync(OrderCompletedEvent @event, ICorrelationContext context)
        {
            var cart = await _cartsRepository.GetAsync(@event.CustomerId);

            if (cart == null)
            {
                throw new ASampleException(Codes.NotFoundCartByCustomerId, $"{@event.CustomerId}");
            }
            cart.Clear();
            await _cartsRepository.UpdateAsync(cart);
        }
コード例 #8
0
        public async Task HandleAsync(DeleteProductFromCartCommand command)
        {
            var cart = await _cartsRepository.GetAsync(command.CustomerId);

            cart.NullCheck(ErrorCodes.cart_not_found, command.CustomerId);

            if (cart.Items.SingleOrDefault(p => p.ProductId == command.ProductId).Quantity > command.Quantity)
            {
                throw new MyShopException(ErrorCodes.invalid_quantity);
            }

            cart.DeleteProduct(command.ProductId, command.Quantity);
            await _cartsRepository.UpdateAsync(cart);
        }
コード例 #9
0
        public async Task <CartDto> HandleAsync(GetCart query)
        {
            var cart = await _cartsRepository.GetAsync(query.Id);

            return(cart == null ? null : new CartDto
            {
                Id = cart.Id,
                Items = cart.Items.Select(x => new CartItemDto
                {
                    ProductId = x.ProductId,
                    ProductName = x.ProductName,
                    Quantity = x.Quantity,
                    UnitPrice = x.UnitPrice
                })
            });
        }
コード例 #10
0
        public async Task HandleAsync(OrderCanceled @event, ICorrelationContext context)
        {
            var cart = await _cartsRepository.GetAsync(@event.CustomerId);

            foreach (var cartItem in cart.Items)
            {
                var product = await _productsRepository.GetAsync(cartItem.ProductId);

                if (product == null)
                {
                    continue;
                }
                product.SetQuantity(product.Quantity + cartItem.Quantity);
                await _productsRepository.UpdateAsync(product);
            }
        }
コード例 #11
0
        public async Task <CartDto> HandleAsync(GetCartQuery query)
        {
            var cart = await _cartsRepository.GetAsync(query.Id);

            cart.NullCheck(ErrorCodes.cart_not_found, query.Id);

            return(new CartDto()
            {
                Id = cart.Id,
                Items = cart.Items.Select(x => new CartItemDto()
                {
                    ProductId = x.ProductId,
                    ProductName = x.ProductName,
                    UnitPrice = x.UnitPrice,
                    Quantity = x.Quantity,
                })
            });
        }
コード例 #12
0
        public async Task HandleAsync(CreateOrderCommand command)
        {
            // TODO: zmienić logikę, bo teraz klient może mimeć tylko jedno aktywne zamówineie. Zrobi unikatowe id koszyka.
            // TODO: żeby złożyć zamównienie to klient musi uzupełnić swoje dane.
            if (await _ordersRepository.HasPendingOrderAsync(command.CustomerId))
            {
                throw new MyShopException("customer_has_pending_order",
                                          $"Customer with id: '{command.CustomerId}' has already a pending order.");
            }

            var cart = await _cartsRepository.GetAsync(command.CustomerId);

            var order = new Order(command.Id, command.CustomerId, cart);

            await _ordersRepository.AddAsync(order);

            cart.Clear();
        }
コード例 #13
0
        public async Task HandleAsync(OrderCancelEvent @event, ICorrelationContext context)
        {
            var cart = await _cartsRepository.GetAsync(@event.CustomerId);

            if (cart == null)
            {
                throw new ASampleException(Codes.NotFoundCartByCustomerId, $"{@event.CustomerId}");
            }

            foreach (var cartItem in cart.Items)
            {
                var product = await _productsRepository.GetAsync(cartItem.ProductId);

                if (product == null)
                {
                    continue;
                }
                product.SetQuantity(product.Quantity + cartItem.Quantity);
                await _productsRepository.UpdateAsync(product);
            }
        }
コード例 #14
0
        public async Task HandleAsync(ChangeOrderStatusCommand command)
        {
            var order = await _ordersRepository.GetAsync(command.Id);

            order.NullCheck(ErrorCodes.order_not_found, command.Id);

            switch (command.Status)
            {
            case OrderStatus.Approved:
                // TODO: ten status w momencie, gdy koszyk zostanie opłacony.
                order.Approve();
                await ReserveProductsAsync(order.Cart.Items);

                break;

            case OrderStatus.Completed:
                // TODO: ten status w momencie, gdy zamówienie zostanie wysłane
                order.Complete();
                var cart = await _cartsRepository.GetAsync(order.Cart.Id);

                cart.Clear();
                await _cartsRepository.UpdateAsync(cart);

                break;

            case OrderStatus.Revoked:
                order.Revoke();
                // TODO: to samo dać dla cancel.
                await ReleaseProductsAsync(order.Cart.Items);

                break;

            default:
                throw new MyShopException(ErrorCodes.bad_order_status);
            }

            await _ordersRepository.UpdateAsync(order);
        }
コード例 #15
0
        public async Task HandleAsync(AddProductToCartCommand command)
        {
            if (command.Quantity <= 0)
            {
                throw new MyShopException(ErrorCodes.negative_quantity);
            }

            var product = await _productsRepository.GetAsync(command.ProductId);

            product.NullCheck(ErrorCodes.product_not_found, command.ProductId);

            if (product.Quantity < command.Quantity)
            {
                throw new MyShopException(ErrorCodes.not_enough_products_in_stock);
            }

            var cart = await _cartsRepository.GetAsync(command.CustomerId);

            cart.NullCheck(ErrorCodes.cart_not_found, command.CustomerId);

            cart.AddProduct(product, command.Quantity);
            await _cartsRepository.UpdateAsync(cart);
        }