Exemplo n.º 1
0
        public AddItemToCartResponse AddItemToCart(AddItmeToCartRequest addItmeToCartRequest)
        {
            AddItemToCartResponse response = new AddItemToCartResponse();
            var cart = GetCart();

            if (cart != null)
            {
                var existingCartItem = _cartItemRepository.FindCartItemsById(cart.Id)
                                       .FirstOrDefault(c => c.ProductId == addItmeToCartRequest.ProductId);
                if (existingCartItem != null)
                {
                    existingCartItem.Quantity++;
                    _cartItemRepository.EditCartItem(existingCartItem);
                    response.CartItem = _messageMapper.MapToCartItmeDto(existingCartItem);
                }
                else
                {
                    var product = _productRepository.FindProductById(addItmeToCartRequest.ProductId);
                    if (product != null)
                    {
                        var carItem = new CartItem //model
                        {
                            CartId    = cart.Id,
                            Cart      = cart,
                            ProductId = addItmeToCartRequest.ProductId,
                            product   = product,
                            Quantity  = 1
                        };
                        _cartItemRepository.SaveCartItem(carItem);
                        response.CartItem = _messageMapper.MapToCartItmeDto(carItem);
                    }
                }
            }
            else
            {
                var product = _productRepository.FindProductById(addItmeToCartRequest.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,
                        product   = product,
                        ProductId = addItmeToCartRequest.ProductId,
                        Quantity  = 1
                    };

                    _cartItemRepository.SaveCartItem(cartItem);
                    response.CartItem = _messageMapper.MapToCartItmeDto(cartItem);
                }
            }
            return(response);
        }
Exemplo n.º 2
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);
        }