public string GetCustomerName()
        {
            if (CookiesService.IsPersonCookiesExist(this))
            {
                string login    = CookiesService.GetLoginCookie(this);
                string password = CookiesService.GetPasswordCookie(this);

                Person person = db.Person.Where(p => p.Email == login).FirstOrDefault();
                person.Customer.Add(db.Customer.Where(c => c.PersonId == person.Id).FirstOrDefault());
                person.Address.Add(db.Address.Where(a => a.PersonId == person.Id).FirstOrDefault());

                if (person.Customer.First() != null)
                {
                    return(person.Customer.First().Name);
                }
                else
                {
                    return(person.Email);
                }
            }
            else
            {
                return("");
            }
        }
        public void GetProductsWithSpByEfExtrasTest()
        {
            // Arrange
            var expected = new List <ViewProduct>
            {
                new ViewProduct()
                {
                    ProductId = 5, ProductName = "紅豆塔", CategoryId = 1, Price = 60, CategoryName = "餅乾類"
                }
                , new ViewProduct()
                {
                    ProductId = 12, ProductName = "紅豆奶酪", CategoryId = 3, Price = 80, CategoryName = "奶酪類"
                }
            }.ToExpectedObject();

            var categories = new List <int>()
            {
                1, 2, 3
            };
            var productName = "紅豆";

            // Act
            var obj    = new CookiesService();
            var actual = obj.GetProductsWithSpByEfExtras(categories, productName);

            // Assert
            expected.ShouldEqual(actual);
        }
        public async Task <IActionResult> Edit(Person person)
        {
            await repository.UpdatePerson(person);

            CookiesService.UpdatePersonCookies(this, person.Email, person.Password);

            UpdateViewBag("Обновление");
            return(RedirectToAction("Index", "User"));
        }
        public async Task <IActionResult> Logout(User user)
        {
            user = new User();

            CookiesService.DeletePersonCookies(this);

            CookiesService.DeleteShoppingCartCookies(this);

            UpdateViewBag("Выход");
            return(RedirectToAction("Index", "Home"));
        }
 public string GetShoppingCartCost()
 {
     if (CookiesService.IsShoppingCartCookiesExist(this))
     {
         return(CookiesService.GetShoppingCartCostCookie(this));
     }
     else
     {
         return("0");
     }
 }
        public async Task <IActionResult> Edit([FromRoute] int id)
        {
            string email    = CookiesService.GetLoginCookie(this);
            string password = CookiesService.GetPasswordCookie(this);

            Person person = await repository.GetPerson(email, password);

            if (person.Id != id)
            {
                return(BadRequest());
            }

            UpdateViewBag("Редактор профиля");
            return(View(person));
        }
        public async Task <IActionResult> AddToCart([FromRoute] int id)
        {
            var product = await repository.GetProduct(id);

            int number = 1;

            // get cart data from cookies
            Dictionary <int, int> cookiesProducts = AuthenticationService.GetCookiesCartItems(this);

            if (cookiesProducts.ContainsKey(product.Id))
            {
                cookiesProducts[product.Id] += number;
            }
            else
            {
                cookiesProducts.Add(product.Id, number);
            }

            string cartCost = (Convert.ToInt32(CookiesService.GetShoppingCartCostCookie(this)) + product.Price * number).ToString();
            string cartData = "";

            foreach (var pair in cookiesProducts)
            {
                cartData += "," + pair.Key + "." + pair.Value;
            }
            cartData = cartData.Substring(1);

            CookiesService.UpdateShoppingCartCookies(this, cartCost, cartData);

            var person = GetCurrentPerson();

            if (person != null)
            {
                CartItem cartItem = await repository.GetCartItem(person.Id, product.Id);

                if (cartItem != null)
                {
                    cartItem.Number++;
                    await repository.UpdateCartItem(cartItem);
                }
                else
                {
                    cartItem = await repository.CreateCartItem(person.Id, product.Id, number);
                }
            }

            return(RedirectToAction("Index", "Home"));
        }
        public async Task <IActionResult> Registration(Person person)
        {
            if (String.IsNullOrWhiteSpace(person.Email) || String.IsNullOrWhiteSpace(person.Password) || !IsEmailUnique(person.Email))
            {
                return(RedirectToAction("Registration"));
            }

            person.AccessLevelId = 2;

            await repository.CreatePerson(person);

            CookiesService.UpdatePersonCookies(this, person.Email, person.Password);

            UpdateViewBag("Регистрация");
            return(RedirectToAction("Index", "Home"));
        }
        public async Task <IActionResult> Info()
        {
            if (CookiesService.IsShoppingCartCookiesExist(this))
            {
                string[] values = CookiesService.GetShoppingCartDataCookie(this).Split(new char[] { ',' });

                for (int i = 0; i < values.Length; i++)
                {
                    string[] data = values[i].Split(new char[] { '.' });

                    Product product = await repository.GetProduct(Convert.ToInt32(data[0]));

                    Cart.AddToCart(product, Convert.ToInt32(data[1]));
                }
            }

            UpdateViewBag("Корзина");
            return(View(Cart));
        }
        protected Person GetCurrentPerson()
        {
            if (CookiesService.IsPersonCookiesExist(this))
            {
                string login    = CookiesService.GetLoginCookie(this);
                string password = CookiesService.GetPasswordCookie(this);

                // password ?
                Person person = db.Person.Where(p => p.Email == login).FirstOrDefault();
                person.Customer.Add(db.Customer.Where(c => c.PersonId == person.Id).FirstOrDefault());
                person.Address.Add(db.Address.Where(a => a.PersonId == person.Id).FirstOrDefault());

                return(person);
            }
            else
            {
                return(null);
            }
        }
        public void GetProductsWithSqlInTest()
        {
            // Arrange
            var expected = new List <ViewProduct>
            {
                new ViewProduct()
                {
                    ProductId = 7, ProductName = "輕乳酪蛋糕(片)", CategoryId = 2, Price = 65, CategoryName = "蛋糕類"
                }
                , new ViewProduct()
                {
                    ProductId = 8, ProductName = "重乳酪蛋糕(片)", CategoryId = 2, Price = 70, CategoryName = "蛋糕類"
                }
                , new ViewProduct()
                {
                    ProductId = 9, ProductName = "抹茶奶酪", CategoryId = 3, Price = 90, CategoryName = "奶酪類"
                }
                , new ViewProduct()
                {
                    ProductId = 10, ProductName = "草莓奶酪", CategoryId = 3, Price = 85, CategoryName = "奶酪類"
                }
                , new ViewProduct()
                {
                    ProductId = 11, ProductName = "芒果奶酪", CategoryId = 3, Price = 85, CategoryName = "奶酪類"
                }
                , new ViewProduct()
                {
                    ProductId = 12, ProductName = "紅豆奶酪", CategoryId = 3, Price = 80, CategoryName = "奶酪類"
                }
            }.ToExpectedObject();

            var categories = new List <int>()
            {
                2, 3
            };

            // Act
            var obj    = new CookiesService();
            var actual = obj.GetProductsWithSqlIn(categories);

            // Assert
            expected.ShouldEqual(actual);
        }
        public async Task <IActionResult> Login(Person person)
        {
            // if (!ModelState.IsValid())
            //    ModelState.
            Person existingPerson = await repository.GetPerson(person.Email, person.Password);

            UpdateViewBag("Аутентификация");
            if (existingPerson != null)
            {
                // успешная аутентификация
                CookiesService.UpdatePersonCookies(this, existingPerson.Email, existingPerson.Password);
                bool ready = await AuthenticationService.SynchronizeShoppingCart(repository, this, existingPerson);

                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                // поля не совпадают
                return(View(person));
            }
        }
        protected bool IsAdmin()
        {
            if (CookiesService.IsPersonCookiesExist(this))
            {
                string login    = CookiesService.GetLoginCookie(this);
                string password = CookiesService.GetPasswordCookie(this);

                Person person = db.Person.Where(p => p.Email == login).FirstOrDefault();
                if (person.AccessLevelId == 1)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
        public async Task <IActionResult> Info()
        {
            if (CookiesService.IsPersonCookiesExist(this))
            {
                string email    = CookiesService.GetLoginCookie(this);
                string password = CookiesService.GetPasswordCookie(this);
                User   user     = new User();
                Person person   = await repository.GetPerson(email, password);

                if (person == null)
                {
                    return(BadRequest());
                }
                Customer customer = await repository.GetCustomer(person.Id);

                Address address = await repository.GetAddress(person.Id);

                user = new User()
                {
                    Person   = person,
                    Customer = customer,
                    Address  = address
                };

                // tmp
                if (customer != null)
                {
                    user.ProductOrders = await repository.GetOrders(person);
                }

                UpdateViewBag();
                return(View(user));
            }
            else
            {
                UpdateViewBag("Аутентификация");
                return(RedirectToAction("Login"));
            }
        }
        public async Task <IActionResult> Create()
        {
            Person person = GetCurrentPerson();

            if (person == null)
            {
                return(RedirectToAction("Info", "User"));
            }
            Customer currentCustomer = await repository.GetCustomer(person.Id);

            if (currentCustomer == null)
            {
                return(RedirectToAction("Info", "User"));
            }
            Address currentAddress = await repository.GetAddress(person.Id);

            if (currentAddress == null)
            {
                return(RedirectToAction("Info", "User"));
            }

            // get product from ShoppingCart
            if (CookiesService.IsShoppingCartCookiesExist(this))
            {
                string[] values = CookiesService.GetShoppingCartDataCookie(this).Split(new char[] { ',' });

                ProductOrder productOrder = await repository.CreateOrder(person, values);

                CookiesService.DeleteShoppingCartCookies(this);
                UpdateViewBag();
                return(RedirectToAction("Info", "User"));
            }
            else
            {
                UpdateViewBag();
                return(RedirectToAction("Info", "ShoppingCart"));
            }
        }
Exemple #16
0
 protected BaseController()
 {
     Cookies = new CookiesService();
     Utility = new UtilityManager(new CacheService(), new AuthenticationService(), new SettingsService());
 }
Exemple #17
0
        public ICookiesService GetService()
        {
            var service = new CookiesService();

            return(service);
        }