public CreateAddressResponse SaveAddress(CreateAddressRequest addressRequest)
        {
            var createAddressResponse = new CreateAddressResponse();

            var address = _messageMapper.MapToAddress(addressRequest.Address);

            try
            {
                _addressRepository.SaveAddress(address);
                var addressDto = _messageMapper.MapToAddressDto(address);
                createAddressResponse.Address = addressDto;
                createAddressResponse.Messages.Add("Successfully saved the address");
                createAddressResponse.StatusCode = HttpStatusCode.Created;
            }
            catch (Exception e)
            {
                var error = e.ToString();
                createAddressResponse.Messages.Add(error);
                createAddressResponse.StatusCode = HttpStatusCode.InternalServerError;
            }

            return(createAddressResponse);
        }
Exemple #2
0
        public CheckoutResponse ProcessCheckout(CheckoutRequest checkoutRequest)
        {
            CheckoutResponse response = new CheckoutResponse();
            var customer = _messageMapper.MapToCustomer(checkoutRequest.Customer);

            var person = customer.Person;

            _personRepository.SavePerson(person);

            var address = _messageMapper.MapToAddress(checkoutRequest.Address);

            _addressRepository.SaveAddress(address);

            customer.PersonId = person.Id;
            customer.Person   = person;

            _customerRepository.SaveCustomer(customer);

            var cart = _cartService.GetCart();

            if (cart != null)
            {
                var     cartItems      = _cartItemRepository.FindCartItemsByCartId(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,
                    DeliveryAddress = 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 = HttpStatusCode.Created;
                response.Messages.Add("Order created");
            }
            else
            {
                response.StatusCode = HttpStatusCode.InternalServerError;
                response.Messages.Add("There is a problem creating the order");
            }

            return(response);
        }