Esempio n. 1
0
        /// <summary>
        /// Sets the current client's theme, either to the database (if user is registered) or the cookie.
        /// </summary>
        /// <param name="themeId">The id of the supported theme to set.</param>
        public async Task SetAsync(string themeId)
        {
            if (string.IsNullOrEmpty(themeId))
            {
                throw new ArgumentNullException(nameof(themeId), $"{nameof(themeId)} cannot be null.");
            }

            // Ensures a supported theme:
            Theme theme = this.GetSupportedThemeOrNull(themeId);

            if (theme == null)
            {
                theme = this.Options.SupportedThemes.FirstOrDefault(t => t.ID.Equals(this.Options.DefaultTheme));
            }

            // Sets the theme:
            // Checks if the user is authenticated, to assign the theme to the database:
            if (_userIdentity.IsAuthenticated())
            {
                await this.SetUserThemeToDatabase(theme);
            }

            // Assigns the theme to the cookie:
            this.SetUserThemeToCookie(theme);

            // Assigns the theme to the current scope:
            this.Theme = theme;
        }
Esempio n. 2
0
        /// <summary>
        /// Removes the specified product from the cart.
        /// </summary>
        /// <param name="productId">The id of the product to remove.</param>
        public async Task RemoveProductAsync(int productId)
        {
            // First, checks there are items in the cart:
            if (this.IsEmpty())
            {
                return;
            }

            ClientCartProduct cartProduct = this.Cart.Products.FirstOrDefault(i => i.ProductId == productId);

            if (cartProduct != null)
            {
                _dbContext.ClientCartProducts.Remove(cartProduct);
                this.Cart.Products.Remove(cartProduct);
                if (this.Cart.Products.Count < 1)
                {
                    _dbContext.ClientCarts.Remove(this.Cart);
                }
                await _dbContext.SaveChangesAsync();
            }

            // If empty - deletes the cart id cookie and removes from the cache:
            if (this.IsEmpty())
            {
                if (_userIdentity.IsAuthenticated())
                {
                    User user = await _userIdentity.GetCurrentAsync();

                    user.ClientCartId = null;
                    user.ClientCart   = null;
                    _userCache.Set(user);
                    this.RemoveCacheAuthUserCart(user.Id);
                }
                else
                {
                    _httpContextAccessor.HttpContext.Response.Cookies.Delete(CART_COOKIE);
                    this.RemoveCacheClientCart(this.Cart.Id);
                }
            }
        }
Esempio n. 3
0
        public async Task <IActionResult> Checkout()
        {
            if (_clientCart.IsEmpty())
            {
                return(NotFound());
            }

            CheckoutVM model = new CheckoutVM()
            {
                ShippingDetails = new CheckoutVM.ShippingDetailsModel()
                {
                    ShippingSameAsBilling = true
                }
            };

            if (_userIdentity.IsAuthenticated())
            {
                // Checks to set already known information about the user in the checkout:
                User user = await _userIdentity.GetCurrentAsync();

                model.BillingDetails = new CheckoutVM.BillingDetailsModel()
                {
                    Name  = $"{user.FirstName} {user.LastName}",
                    Phone = user.PhoneNumber
                };
                if (user.Address != null)
                {
                    model.BillingDetails.Street     = user.Address.Street;
                    model.BillingDetails.PostalCode = user.Address.PostalCode;
                    model.BillingDetails.City       = user.Address.City;
                    model.BillingDetails.Country    = user.Address.Country;
                }
            }
            else
            {
                model.SignMethod = SignMethod.Login;
                model.LoginVM    = new CheckoutVM.ConditionalLoginVM()
                {
                    RememberMe = true
                };
                model.RegisterVM = new CheckoutVM.ConditionalRegisterVM();
            }

            // TODO: for testing:
            //model.BillingDetails = new CheckoutVM.BillingDetailsModel()
            //{
            //    Name = "A B",
            //    Phone = "0541234567",
            //    Street = "ABC",
            //    PostalCode = "12345",
            //    City = "New York",
            //    Country = "USA"
            //};
            //model.CreditCard = new CheckoutVM.CreditCardInfo()
            //{
            //    CardNumber = "123456789",
            //    CardExpiryDate = "04/23",
            //    CardHolderName = "A B",
            //    CardSecurityCode = "444"
            //};
            //model.DeliveryMethodId = 4;

            ViewData["DeliveryMethods"] = await _dbContext.DeliveryMethods.OrderBy(dm => dm.Price).ToListAsync();

            return(View(model));
        }