コード例 #1
0
    public async Task Handle(PaymentAuthorizedEvent paymentAuthorizedEvent,
                             CancellationToken cancellationToken)
    {
        var payment = await _unitOfWork.Payments
                      .GetById(paymentAuthorizedEvent.PaymentId, cancellationToken);

        var order = await _unitOfWork.Orders
                    .GetById(payment.OrderId, cancellationToken);

        if (payment == null)
        {
            throw new ApplicationDataException("Payment not found.");
        }

        if (order == null)
        {
            throw new ApplicationDataException("Order not found.");
        }

        // Changing order status
        _orderStatusWorkflow.CalculateOrderStatus(order, payment);
        await _unitOfWork.CommitAsync(cancellationToken);

        // Broadcasting order update
        await _orderStatusBroadcaster.BroadcastOrderStatus(
            order.CustomerId,
            order.Id,
            order.Status
            );
    }
コード例 #2
0
    public override async Task <Guid> ExecuteCommand(RegisterCustomerCommand command,
                                                     CancellationToken cancellationToken)
    {
        try
        {
            var customer = await Customer.CreateNew(
                command.Email,
                command.Name,
                _uniquenessChecker
                );

            if (customer != null)
            {
                await _unitOfWork.Customers
                .Add(customer, cancellationToken);

                await CreateUserForCustomer(command);

                await _unitOfWork.CommitAsync();
            }

            return(customer.Id.Value);
        }
        catch (Exception)
        {
            throw;
        }
    }
コード例 #3
0
        public override async Task <Guid> ExecuteCommand(SaveCartCommand command, CancellationToken cancellationToken)
        {
            var customer = await _unitOfWork.CustomerRepository.GetById(command.CustomerId, cancellationToken);

            var product = await _unitOfWork.ProductRepository.GetById(command.Product.Id, cancellationToken);

            if (customer == null)
            {
                throw new InvalidDataException("Customer not found.");
            }

            if (product == null)
            {
                throw new InvalidDataException("Product not found.");
            }

            var cart = await _unitOfWork.CartRepository.GetByCustomerId(customer.Id, cancellationToken);

            var quantity = command.Product.Quantity;

            if (cart == null)
            {
                cart = new Cart(customer);
                cart.AddItem(product, quantity);
                await _unitOfWork.CartRepository.Add(cart, cancellationToken);
            }
            else
            {
                cart.ChangeCart(product, quantity);
            }

            await _unitOfWork.CommitAsync();

            return(cart.Id);
        }
コード例 #4
0
        public override async Task <Guid> ExecuteCommand(PlaceOrderCommand command, CancellationToken cancellationToken)
        {
            var cart = await _unitOfWork.CartRepository.GetById(command.CartId, cancellationToken);

            var customer = await _unitOfWork.CustomerRepository.GetById(command.CustomerId, cancellationToken);

            if (customer == null)
            {
                throw new InvalidDataException("Customer not found.");
            }

            if (cart == null)
            {
                throw new InvalidDataException("Cart not found.");
            }

            var currency = Currency.FromCode(command.Currency);
            var order    = Order.PlaceOrder(cart, currency, _currencyConverter);

            // Cleaning the cart
            cart.Clear();

            await _unitOfWork.OrderRepository.Add(order, cancellationToken);

            await _unitOfWork.CommitAsync();

            return(order.Id);
        }
コード例 #5
0
        public async override Task <CartDetailsViewModel> ExecuteQuery(GetCartDetailsQuery query, CancellationToken cancellationToken)
        {
            CartDetailsViewModel viewModel = new CartDetailsViewModel();

            var customer = await _unitOfWork.CustomerRepository.GetById(query.CustomerId, cancellationToken);

            if (customer == null)
            {
                throw new InvalidDataException("Customer not found.");
            }

            if (string.IsNullOrWhiteSpace(query.Currency))
            {
                throw new InvalidDataException("Currency can't be empty.");
            }

            var cart = await _unitOfWork.CartRepository.GetByCustomerId(query.CustomerId, cancellationToken);

            if (cart == null)
            {
                // Creating cart
                cart = new Cart(customer);
                await _unitOfWork.CartRepository.Add(cart, cancellationToken);

                await _unitOfWork.CommitAsync();
            }

            var currency = Currency.FromCode(query.Currency);

            viewModel.CartId = cart.Id;
            if (cart.Items.Count > 0)
            {
                var productIds = cart.Items.Select(p => p.Product.Id).ToList();
                var products   = await _unitOfWork.ProductRepository.GetByIds(productIds, cancellationToken);

                if (products == null)
                {
                    throw new InvalidDataException("Products not found");
                }

                foreach (var cartItem in cart.Items)
                {
                    var product = products.Single(p => p.Id == cartItem.Product.Id);

                    viewModel.CartItems.Add(new CartItemDetailsViewModel
                    {
                        ProductId       = cartItem.Product.Id,
                        ProductQuantity = cartItem.Quantity,
                        ProductName     = product.Name,
                        ProductPrice    = _currencyConverter.Convert(currency, cartItem.Product.Price).Value,
                        CurrencySymbol  = currency.Symbol,
                    });
                }

                viewModel.CalculateTotalOrderPrice();
            }

            return(viewModel);
        }
コード例 #6
0
        public override async Task <Guid> ExecuteCommand(UpdateCustomerCommand request, CancellationToken cancellationToken)
        {
            var customer = await _unitOfWork.CustomerRepository.GetById(request.CustomerId, cancellationToken);

            if (customer == null)
            {
                throw new InvalidDataException("Customer not found.");
            }

            customer.SetName(request.Name);
            await _unitOfWork.CommitAsync();

            return(customer.Id);
        }
コード例 #7
0
        public override async Task <Guid> ExecuteCommand(PlaceOrderCommand command,
                                                         CancellationToken cancellationToken)
        {
            var customerId   = new CustomerId(command.CustomerId);
            var productsData = new List <QuoteItemProductData>();
            var quoteId      = new QuoteId(command.QuoteId);
            var quote        = await _unitOfWork.Quotes
                               .GetById(quoteId, cancellationToken);

            var customer = await _unitOfWork.Customers
                           .GetById(customerId, cancellationToken);

            if (customer == null)
            {
                throw new ApplicationDataException("Customer not found.");
            }

            if (quote == null)
            {
                throw new ApplicationDataException("Quote not found.");
            }

            var currency = Currency.FromCode(command.Currency);

            var products = await _unitOfWork.Products
                           .GetByIds(quote.Items.Select(i => i.ProductId).ToList());

            if (products == null)
            {
                throw new ApplicationDataException("Products couldn't be loaded.");
            }

            foreach (var item in quote.Items)
            {
                var product = products
                              .Where(p => p.Id == item.ProductId)
                              .FirstOrDefault();

                productsData.Add(
                    new QuoteItemProductData(product.Id, product.Price, item.Quantity)
                    );
            }

            var order = Order.PlaceOrder(customerId, quoteId, productsData, currency, _currencyConverter);
            await _unitOfWork.Orders.Add(order);

            await _unitOfWork.CommitAsync();

            return(order.Id.Value);
        }
コード例 #8
0
        public override async Task <Guid> ExecuteCommand(RegisterCustomerCommand command, CancellationToken cancellationToken)
        {
            var customer = Customer.CreateCustomer(command.Email, command.Name, _uniquenessChecker);

            if (customer != null)
            {
                await _unitOfWork.CustomerRepository.Add(customer, cancellationToken);
                await CreateUserForCustomer(command);

                await _unitOfWork.CommitAsync();
            }

            return(customer.Id);
        }
コード例 #9
0
        public async static Task SeedData(IEcommerceUnitOfWork unitOfWork, ICurrencyConverter converter)
        {
            //Creating products
            List <Product> products = new List <Product>();
            var            rand     = new Random();

            for (char c = 'A'; c <= 'Z'; c++)
            {
                var price = new decimal(rand.NextDouble());
                products.Add(new Product($"Product {c}", Money.Of(price, converter.GetBaseCurrency().Code)));
            }

            await unitOfWork.ProductRepository.AddRange(products);

            await unitOfWork.CommitAsync();
        }
コード例 #10
0
    public async Task Handle(PaymentCreatedEvent paymentCreatedEvent
                             , CancellationToken cancellationToken)
    {
        var payment = await _unitOfWork.Payments
                      .GetById(paymentCreatedEvent.PaymentId, cancellationToken);

        var customer = await _unitOfWork.Customers
                       .GetById(payment.CustomerId, cancellationToken);

        var order = await _unitOfWork.Orders
                    .GetById(payment.OrderId, cancellationToken);

        if (payment == null)
        {
            throw new ApplicationDataException("Payment not found.");
        }

        if (customer == null)
        {
            throw new ApplicationDataException("Customer not found.");
        }

        if (order == null)
        {
            throw new ApplicationDataException("order not found.");
        }

        // Changing order status
        _orderStatusWorkflow.CalculateOrderStatus(order, payment);
        await _unitOfWork.CommitAsync(cancellationToken);

        // Attempting to pay
        MakePaymentCommand command = new MakePaymentCommand(paymentCreatedEvent.PaymentId.Value);
        await _mediator.Send(command, cancellationToken);

        // Broadcasting order update
        await _orderStatusBroadcaster.BroadcastOrderStatus(
            customer.Id,
            order.Id,
            order.Status
            );
    }
コード例 #11
0
        public async Task ProcessMessages(int batchSize, CancellationToken cancellationToken)
        {
            var messages = await _unitOfWork.MessageRepository.FetchUnprocessed(batchSize, cancellationToken);
            foreach (var message in messages)
            {
                try
                {
                    //Setting processed date
                    message.SetProcessedAt(DateTime.Now);
                    await _publisher.Publish(message, cancellationToken);

                    _unitOfWork.MessageRepository.UpdateProcessedAt(message);
                    await _unitOfWork.CommitAsync();
                }
                catch (Exception ex)
                {                    
                    _logger.LogError($"\n-------- An error has occurred while processing message {message.Id}: {ex.Message}\n");
                }
            }
        }
コード例 #12
0
        public async Task Handle(OrderPlacedEvent orderPlacedEvent,
                                 CancellationToken cancellationToken)
        {
            var order = await _unitOfWork.Orders
                        .GetById(orderPlacedEvent.OrderId, cancellationToken);

            if (order == null)
            {
                throw new ApplicationDataException("Order not found.");
            }

            // Creating a payment
            var payment = Payment
                          .CreateNew(order.CustomerId, order.Id);

            await _unitOfWork.Payments
            .Add(payment);

            await _unitOfWork.CommitAsync();
        }
コード例 #13
0
    public override async Task <Guid> ExecuteCommand(CreateQuoteCommand command,
                                                     CancellationToken cancellationToken)
    {
        var customerId = new CustomerId(command.CustomerId);
        var customer   = await _unitOfWork.Customers
                         .GetById(customerId, cancellationToken);

        var productId = new ProductId(command.Product.Id);
        var product   = await _unitOfWork.Products
                        .GetById(productId, cancellationToken);

        if (customer == null)
        {
            throw new ApplicationDataException("Customer not found.");
        }

        if (product == null)
        {
            throw new ApplicationDataException("Product not found.");
        }

        var quantity            = command.Product.Quantity;
        var quotetemProductData = new QuoteItemProductData(
            product.Id,
            product.Price, quantity
            );

        var quote = Quote.CreateNew(customerId);

        quote.AddItem(quotetemProductData);

        await _unitOfWork.Quotes
        .Add(quote, cancellationToken);

        await _unitOfWork.CommitAsync();

        return(quote.Id.Value);
    }
コード例 #14
0
        public override async Task <Guid> ExecuteCommand(MakePaymentCommand command, CancellationToken cancellationToken)
        {
            var payment = await _unitOfWork.PaymentRepository.GetById(command.PaymentId, cancellationToken);

            if (payment == null)
            {
                throw new InvalidDataException("Payment not found.");
            }

            if (payment.Order == null)
            {
                throw new InvalidDataException("Order not found.");
            }

            // Making a fake payment (here we could call a financial institution service and use the customer billing info)
            payment.MarkAsPaid();

            // Changing order status
            _orderStatusWorkflowManager.CalculateOrderStatus(payment.Order, payment);
            await _unitOfWork.CommitAsync();

            return(payment.Id);
        }