Example #1
0
        public CheckoutResponse ProcessCheckout(CheckoutRequest checkoutRequest)
        {
            CheckoutResponse response = new CheckoutResponse();
            var customer = _messageMapper.MapToCustomer(checkoutRequest.CustomerDto);
            var person   = customer.Person;

            _personRepositery.SavePerson(person);
            var address = _messageMapper.MapTopAddress(checkoutRequest.addressDto);

            _addressRespository.SaveAddress(address);

            customer.PersonId = person.Id;
            customer.Person   = person;
            _customerRepository.SaveCustomer(customer);
            var cart = _cartService.GetCart();

            if (cart != null)
            {
                var     cartItems      = _cartItemRepository.FindCartItemsById(cart.Id);
                var     cartTotal      = _cartService.GetCartTotal();
                decimal shippingCharge = 0;
                var     orderTotal     = cartTotal + shippingCharge;
                var     order          = new Order
                {
                    OrderTotal        = orderTotal,
                    OrderItemTotal    = cartTotal,
                    ShippingCharge    = shippingCharge,
                    AddressId         = address.Id,
                    DeliveryAddresses = address,
                    CustomerId        = customer.Id,
                    Customer          = customer,
                    OrderStatus       = OrderStatus.Submitted
                };
                _orderRepository.SaveOrder(order);
                foreach (var cartItem in cartItems)
                {
                    var orderItem = new OrderItem
                    {
                        Quantity  = cartItem.Quantity,
                        Order     = order,
                        OrderId   = order.Id,
                        Product   = cartItem.product,
                        ProductId = cartItem.ProductId
                    };
                    _orderItemRepository.SaveOrderItem(orderItem);
                }
                _cartRepository.DeleteCart(cart);

                response.StatusCode = System.Net.HttpStatusCode.Created;
                response.Messages.Add(item: "Order Created");
            }
            else
            {
                response.StatusCode = System.Net.HttpStatusCode.NoContent;
                response.Messages.Add(item: "Order Emptiy");
            }


            return(response);
        }