Exemplo n.º 1
0
        public IHttpActionResult ResetApiData()
        {
            // Delete all data:
            shoppingCartProductRepository.RemoveRange(shoppingCartProductRepository.GetAll());
            shoppingCartProductRepository.SaveChanges();

            shoppingCartRepository.RemoveRange(shoppingCartRepository.GetAll());
            shoppingCartRepository.SaveChanges();

            customerRepository.RemoveRange(customerRepository.GetAll());
            customerRepository.SaveChanges();

            productRepository.RemoveRange(productRepository.GetAll());
            productRepository.SaveChanges();

            brandRepository.RemoveRange(brandRepository.GetAll());
            brandRepository.SaveChanges();

            categoryRepository.RemoveRange(categoryRepository.GetAll());
            categoryRepository.SaveChanges();

            // Create brands:
            List <Brand> brands = new List <Brand>();

            brands.Add(new Brand()
            {
                Name = "Samsung"
            });
            brands.Add(new Brand()
            {
                Name = "Lenovo"
            });
            brands.Add(new Brand()
            {
                Name = "Dell"
            });
            brands.Add(new Brand()
            {
                Name = "Asus"
            });
            brands.Add(new Brand()
            {
                Name = "HP"
            });
            brands.Add(new Brand()
            {
                Name = "Acer"
            });
            brands.Add(new Brand()
            {
                Name = "Sony"
            });
            brands.Add(new Brand()
            {
                Name = "Microsoft"
            });
            brandRepository.AddRange(brands);
            brandRepository.SaveChanges();

            // Create categories:
            List <Category> categories = new List <Category>();

            categories.Add(new Category()
            {
                Name = "Cellphones"
            });
            categories.Add(new Category()
            {
                Name = "Computers"
            });
            categories.Add(new Category()
            {
                Name = "Tablets"
            });
            categories.Add(new Category()
            {
                Name = "Video Games"
            });
            categories.Add(new Category()
            {
                Name = "Accessories"
            });
            categoryRepository.AddRange(categories);
            categoryRepository.SaveChanges();

            // Create products:
            List <Product> products = new List <Product>();

            products.Add(new Product()
            {
                Name = "Samsung Galaxy S8", BrandId = brands[0].Id, CategoryId = categories[0].Id, Price = Convert.ToDecimal(789.99)
            });
            products.Add(new Product()
            {
                Name = "Dell XPS 13", BrandId = brands[2].Id, CategoryId = categories[1].Id, Price = Convert.ToDecimal(1144.99)
            });
            products.Add(new Product()
            {
                Name = "Samsung Galaxy Tab", BrandId = brands[0].Id, CategoryId = categories[2].Id, Price = Convert.ToDecimal(989.59)
            });
            products.Add(new Product()
            {
                Name = "Playstation 4", BrandId = brands[6].Id, CategoryId = categories[3].Id, Price = Convert.ToDecimal(299.99)
            });
            products.Add(new Product()
            {
                Name = "Microsoft Surface Mouse", BrandId = brands[7].Id, CategoryId = categories[4].Id, Price = Convert.ToDecimal(64.99)
            });
            productRepository.AddRange(products);
            productRepository.SaveChanges();

            // Create customers:
            List <Customer> customers = new List <Customer>();

            customers.Add(new Customer()
            {
                FirstName = "Adam", LastName = "Smith", EmailAddress = "*****@*****.**"
            });
            customers.Add(new Customer()
            {
                FirstName = "Arnold", LastName = "Jordan", EmailAddress = "*****@*****.**"
            });
            customers.Add(new Customer()
            {
                FirstName = "Calvin", LastName = "Williams", EmailAddress = "*****@*****.**"
            });
            customers.Add(new Customer()
            {
                FirstName = "Emma", LastName = "Jones", EmailAddress = "*****@*****.**"
            });
            customers.Add(new Customer()
            {
                FirstName = "Sophia", LastName = "Wilson", EmailAddress = "*****@*****.**"
            });
            customerRepository.AddRange(customers);
            customerRepository.SaveChanges();

            return(Ok("Api data reseted."));
        }