public void AddToCart(AddToCartViewModel addToCartViewModel)
        {
            var cart = GetCart();

            if (cart != null)
            {
                var existingCartItem = _cartItemRepository.FindCartItemsByCartId(cart.Id)
                                       .FirstOrDefault(c => c.ProductId == addToCartViewModel.ProductId);
                if (existingCartItem != null)
                {
                    existingCartItem.Quantity++;
                    _cartItemRepository.UpdateCartItem(existingCartItem);
                }
                else
                {
                    var product = _productRepository.FindProductById(addToCartViewModel.ProductId);

                    if (product != null)
                    {
                        var cartItem = new CartItem
                        {
                            CartId    = cart.Id,
                            Cart      = cart,
                            ProductId = addToCartViewModel.ProductId,
                            Product   = product,
                            Quantity  = 1
                        };
                        _cartItemRepository.SaveCartItem(cartItem);
                    }
                }
            }
            else
            {
                var product = _productRepository.FindProductById(addToCartViewModel.ProductId);
                if (product != null)
                {
                    var newCart = new Cart
                    {
                        UniqueCartId = UniqueCartId(),
                        CartStatus   = CartStatus.Open
                    };

                    _cartRepository.SaveCart(newCart);

                    var cartItem = new CartItem
                    {
                        CartId    = newCart.Id,
                        Cart      = newCart,
                        ProductId = addToCartViewModel.ProductId,
                        Product   = product,
                        Quantity  = 1
                    };

                    _cartItemRepository.SaveCartItem(cartItem);
                }
            }
        }
        public void ProcessCheckout(CheckoutViewModel checkoutViewModel)
        {
            var person = new Person
            {
                FirstName    = checkoutViewModel.FirstName,
                MiddleName   = checkoutViewModel.MiddleName,
                LastName     = checkoutViewModel.LastName,
                EmailAddress = checkoutViewModel.EmailAddress,
                IsDeleted    = false
            };

            _personRepository.SavePerson(person);

            var address = new Address
            {
                AddressLine1 = checkoutViewModel.AddressLine1,
                AddressLine2 = checkoutViewModel.AddressLine2,
                City         = checkoutViewModel.City,
                State        = checkoutViewModel.State,
                Country      = checkoutViewModel.Country,
                ZipCode      = checkoutViewModel.ZipCode,
                IsDeleted    = false
            };

            _addressRepository.SaveAddress(address);

            var customer = new Customer
            {
                PersonId  = person.Id,
                Person    = person,
                IsDeleted = false
            };

            _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);
            }
        }
예제 #3
0
        public CheckoutResponse ProcessCheckout(CheckoutRequest request)
        {
            var response = new CheckoutResponse();
            var customer = request.Customer.MapToCustomer();

            var person = customer.Person;

            _personRepository.SavePerson(person);

            var address = request.Address.MapToAddress();

            _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);
        }
예제 #4
0
        public AddItemToCartResponse AddItemToCart(AddItemToCartRequest addItemToCartRequest)
        {
            AddItemToCartResponse response = new AddItemToCartResponse();

            var cart = GetCart();

            if (cart != null)
            {
                var existingCartItem = _cartItemRepository.FindCartItemsByCartId(cart.Id)
                                       .FirstOrDefault(c => c.ProductId == addItemToCartRequest.ProductId);
                if (existingCartItem != null)
                {
                    existingCartItem.Quantity++;
                    _cartItemRepository.UpdateCartItem(existingCartItem);

                    response.CartItem = _messageMapper.MapToCartItemDto(existingCartItem);
                }
                else
                {
                    var product = _productRepository.FindProductById(addItemToCartRequest.ProductId);

                    if (product != null)
                    {
                        var cartItem = new CartItem
                        {
                            CartId    = cart.Id,
                            Cart      = cart,
                            ProductId = addItemToCartRequest.ProductId,
                            Product   = product,
                            Quantity  = 1
                        };
                        _cartItemRepository.SaveCartItem(cartItem);
                        response.CartItem = _messageMapper.MapToCartItemDto(cartItem);
                    }
                }
            }
            else
            {
                var product = _productRepository.FindProductById(addItemToCartRequest.ProductId);
                if (product != null)
                {
                    var newCart = new Cart
                    {
                        UniqueCartId = UniqueCartId(),
                        CartStatus   = CartStatus.Open
                    };

                    _cartRepository.SaveCart(newCart);

                    var cartItem = new CartItem
                    {
                        CartId    = newCart.Id,
                        Cart      = newCart,
                        ProductId = addItemToCartRequest.ProductId,
                        Product   = product,
                        Quantity  = 1
                    };

                    _cartItemRepository.SaveCartItem(cartItem);

                    response.CartItem = _messageMapper.MapToCartItemDto(cartItem);
                }
            }
            return(response);
        }