コード例 #1
0
        public async Task <IActionResult> EditBillingAddress(BillingAddressModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await GetCurrentUserAsync();

                Guid billingAddressId = Guid.NewGuid();

                // check if there are already billing address
                var billingAddressEntity = _billingAddressService.GetBillingAddressById(user.BillingAddressId);
                if (billingAddressEntity == null)
                {
                    billingAddressEntity    = _mapper.Map <BillingAddressModel, BillingAddress>(model);
                    billingAddressEntity.Id = billingAddressId;

                    // save billing address in database
                    _billingAddressService.InsertBillingAddress(billingAddressEntity);
                }
                else
                {
                    billingAddressId        = billingAddressEntity.Id;
                    billingAddressEntity    = _mapper.Map <BillingAddressModel, BillingAddress>(model);
                    billingAddressEntity.Id = billingAddressId;

                    // update billing address in database
                    _billingAddressService.UpdateBillingAddress(billingAddressEntity);
                }


                // update user billing address
                user.BillingAddressId = billingAddressEntity.Id;
                await _userManager.UpdateAsync(user);
            }

            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> Checkout(CheckoutModel model)
        {
            // get current user
            var user = await GetCurrentUserAsync();

            var totalOrderPrice = 0m;

            // create order entity
            var orderEntity = new Order
            {
                Id                     = Guid.NewGuid(),
                OrderNumber            = GenerateUniqueOrderNumber(),
                UserId                 = Guid.Parse(user.Id),
                Status                 = OrderStatus.Pending,
                OrderPlacementDateTime = DateTime.Now
            };

            var orderItemEntities = new List <OrderItem>();
            var cartItems         = new List <CartItemModel>();

            // get cart session
            if (Session.GetString(_cartItesmSessionKey) != null)
            {
                cartItems = JsonConvert.DeserializeObject <List <CartItemModel> >(Session.GetString(_cartItesmSessionKey));
            }

            foreach (var item in cartItems)
            {
                var currentItem = _productService.GetProductById(item.Id);
                if (currentItem != null)
                {
                    var newOrderItem = new OrderItem
                    {
                        Id         = Guid.NewGuid(),
                        OrderId    = orderEntity.Id,
                        ProductId  = item.Id.ToString(),
                        Name       = item.Name,
                        Quantity   = item.Quantity,
                        Price      = item.Price,
                        TotalPrice = (item.Price * item.Quantity)
                    };

                    orderItemEntities.Add(newOrderItem);
                    totalOrderPrice += newOrderItem.TotalPrice;
                }
            }

            // check if the order have item/s
            if (orderItemEntities.Count > 0)
            {
                // create billingAddress for this order
                var billingAddressEntity = _mapper.Map <CheckoutModel, BillingAddress>(model);
                _billingAddressService.InsertBillingAddress(billingAddressEntity);

                orderEntity.Items            = orderItemEntities;
                orderEntity.TotalOrderPrice  = totalOrderPrice;
                orderEntity.BillingAddressId = billingAddressEntity.Id;

                // save
                _orderService.InsertOrder(orderEntity);

                // clear cart session
                Session.Remove(_cartItesmSessionKey);
                Session.Remove(_cartItemsCountSessionKey);

                return(RedirectToAction("OrderHistoryList", "Manage"));
            }

            // something went wrong
            cartItems = new List <CartItemModel>();
            return(RedirectToAction("Index", "Cart", cartItems));
        }