Ejemplo n.º 1
0
        public void GetCustomerByUserNameHandlesFailuteCorrectly()
        {
            // Act
            //// Service setup
            var services = new ServiceCollection();

            services.AddMemoryCache();
            services.AddDbContext <DbContextClass>(options => options.UseInMemoryDatabase("testDb"));
            var serviceProvider = services.BuildServiceProvider();
            //// DbContext setup
            DbContextClass testContext = serviceProvider.GetService <DbContextClass>();
            //// DbContext populating
            Customer testCustomer = new Customer();
            string   testString   = "testUserName";

            testCustomer.UserName = testString;
            testContext.Add(testCustomer);
            testContext.SaveChanges();
            // Arrange
            Customer gottenCustomer = RevatureP1.UtilMethods.GetCustomerByUserName("otherName", testContext);
            var      resultVal      = gottenCustomer.UserName;
            var      expectedVal    = new Customer().UserName;

            //Assert
            Assert.Equal(expectedVal, resultVal);
        }
Ejemplo n.º 2
0
        public void ProductCountReturnsFalseWhenItShould()
        {
            // Act
            //// Service setup
            var services = new ServiceCollection();

            services.AddMemoryCache();
            services.AddDbContext <DbContextClass>(options => options.UseInMemoryDatabase("testDb"));
            var serviceProvider = services.BuildServiceProvider();
            //// DbContext setup
            DbContextClass testContext = serviceProvider.GetService <DbContextClass>();
            //// DbContext populating
            StockItem testStockItem = new StockItem();
            int       testInt       = 104;
            int       testInt2      = 204;
            int       testInt3      = 304;
            int       testint4      = 404;

            //string testString = "testVal";
            testStockItem.LocationId = testInt;
            testStockItem.ProductId  = testInt2;
            testStockItem.StockCount = testInt3;
            testContext.Add(testStockItem);
            testContext.SaveChanges();
            // Arrange
            bool enoughInStock = RevatureP1.UtilMethods.CheckProductCount(testInt, testInt2, testint4, testContext);
            var  resultVal     = enoughInStock;
            var  expectedVal   = false;

            //Assert
            Assert.Equal(expectedVal, resultVal);
        }
Ejemplo n.º 3
0
        public static void Login(DbContextClass context, ref Customer currentCust)
        {
            string userName;
            string Password;


            do
            {
                Console.WriteLine("Please enter your username, or 0 to exit.");
                userName = Console.ReadLine().Trim();
                if (userName == "0")
                {
                    Environment.Exit(0);
                }

                do
                {
                    Console.WriteLine("Please enter your password.");
                    Password = Console.ReadLine().Trim();
                } while (Password.Length == 0);
            } while (userName.Length == 0);

            var customer = context.Customers.FirstOrDefault(x => x.Username == userName && x.Password == Password);

            if (customer == null)
            {
                Console.WriteLine("Inccorrect username/password.");
                currentCust = new Customer();
            }
            else
            {
                Console.WriteLine("Congrats, you logged in.");
                currentCust = customer;
            }
        }
Ejemplo n.º 4
0
        public void BuildStockItemViewModelFromLocStockWorks()
        {
            // Act
            //// Service setup
            var services = new ServiceCollection();

            services.AddMemoryCache();
            services.AddDbContext <DbContextClass>(options => options.UseInMemoryDatabase("testDb"));
            var serviceProvider = services.BuildServiceProvider();
            //// DbContext setup
            DbContextClass testContext = serviceProvider.GetService <DbContextClass>();
            //// DbContext populating
            Location  testLocation  = new Location();
            StockItem testStockItem = new StockItem();

            testStockItem.DiscountPercent = 50;
            Product testProduct = new Product();

            testProduct.BasePrice = 6;
            testContext.Add(testProduct);
            testContext.SaveChanges();
            // Arrange
            StockItemViewModel testStockItemViewModel = RevatureP1.UtilMethods.BuildStockItemViewModelFromLocStock(
                testLocation, testStockItem, testContext);
            var resultVal   = testStockItemViewModel.SaleString;
            var expectedVal = "50% Off!";

            //Assert
            Assert.Equal(expectedVal, resultVal);
        }
Ejemplo n.º 5
0
        public void GetStockItemByIdHandlesFailureCorrectly()
        {
            // Act
            //// Service setup
            var services = new ServiceCollection();

            services.AddMemoryCache();
            services.AddDbContext <DbContextClass>(options => options.UseInMemoryDatabase("testDb"));
            var serviceProvider = services.BuildServiceProvider();
            //// DbContext setup
            DbContextClass testContext = serviceProvider.GetService <DbContextClass>();
            //// DbContext populating
            StockItem testStockItem = new StockItem();
            int       testInt       = 102;
            int       testInt2      = 202;
            int       testInt3      = 302;

            //string testString = "testVal";
            testStockItem.LocationId = testInt;
            testStockItem.ProductId  = testInt2;
            testStockItem.StockCount = testInt3;
            testContext.Add(testStockItem);
            testContext.SaveChanges();
            // Arrange
            StockItem gottenStockItem = RevatureP1.UtilMethods.GetStockItemByIds(4, 4, testContext);
            var       resultVal       = gottenStockItem.StockCount;
            var       expectedVal     = new StockItem().StockCount;

            //Assert
            Assert.Equal(expectedVal, resultVal);
        }
Ejemplo n.º 6
0
        public void GetProductByIdHandlesFailureCorrectly()
        {
            // Act
            //// Service setup
            var services = new ServiceCollection();

            services.AddMemoryCache();
            services.AddDbContext <DbContextClass>(options => options.UseInMemoryDatabase("testDb"));
            var serviceProvider = services.BuildServiceProvider();
            //// DbContext setup
            DbContextClass testContext = serviceProvider.GetService <DbContextClass>();
            //// DbContext populating
            Product testProduct = new Product();
            int     testInt     = 15;
            string  testString  = "testVal";

            testProduct.ProductId = testInt;
            testProduct.Name      = testString;
            testContext.Add(testProduct);
            testContext.SaveChanges();
            // Arrange
            Product gottenProduct = RevatureP1.UtilMethods.GetProductById(2, testContext);
            var     resultVal     = gottenProduct.Name;
            var     expectedVal   = new Product().Name;

            //Assert
            Assert.Equal(expectedVal, resultVal);
        }
Ejemplo n.º 7
0
        public void GetLocationByIdHandlesFailureCorrectly()
        {
            // Act
            //// Service setup
            var services = new ServiceCollection();

            services.AddMemoryCache();
            services.AddDbContext <DbContextClass>(options => options.UseInMemoryDatabase("testDb"));
            var serviceProvider = services.BuildServiceProvider();
            //// DbContext setup
            DbContextClass testContext = serviceProvider.GetService <DbContextClass>();
            //// DbContext populating
            Location testLocation = new Location();
            int      testInt      = 12;
            string   testString   = "testVal";

            testLocation.LocationId      = testInt;
            testLocation.LocationAddress = testString;
            testContext.Add(testLocation); // Already done
            testContext.SaveChanges();
            // Arrange
            Location gottenLocation = RevatureP1.UtilMethods.GetLocationById(111, testContext);
            var      resultVal      = gottenLocation.LocationAddress;
            var      expectedVal    = new Location().LocationAddress;

            //Assert
            Assert.Equal(expectedVal, resultVal);
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> CheckoutCart()
        {
            /// <summary>
            /// Adds cart items to orderitems db then empties cart
            /// </summary>
            List <OrderItemViewModel> CurrentCart = (List <OrderItemViewModel>)_cache.Get("customerCart");

            foreach (OrderItemViewModel thisOIVM in CurrentCart)
            {
                //OrderItem(int CustomerIdIn, int LocationIdIn, int ProductIdIn, double TotalPriceWhenOrderedIn, int OrderCountIn)
                OrderItem thisOrderItem = new OrderItem(
                    thisOIVM.CustomerId,
                    thisOIVM.LocationId,
                    thisOIVM.ProductId,
                    thisOIVM.TotalPriceWhenOrdered,
                    thisOIVM.OrderCount
                    );
                _context.Add(thisOrderItem);
                _context = UtilMethods.DecrementStockItem(thisOrderItem, _context);
            }
            await _context.SaveChangesAsync();

            System.Diagnostics.Debug.WriteLine($"Orders updated");
            _cache.Set("customerCart", new List <OrderItemViewModel>());
            return(Redirect("/Home/History"));
        }
Ejemplo n.º 9
0
        public void GetCustomerByUserNameWorks()
        {
            // Act
            //// Service setup
            var services = new ServiceCollection();

            services.AddMemoryCache();
            services.AddDbContext <DbContextClass>(options => options.UseInMemoryDatabase("testDb"));
            var serviceProvider = services.BuildServiceProvider();
            //// Cache setup
            //var memoryCache = serviceProvider.GetService<IMemoryCache>();
            //IMemoryCache _cache = RevatureP1.UtilMethods.InitializeCacheIfNeeded(memoryCache);
            //// DbContext setup
            DbContextClass testContext = serviceProvider.GetService <DbContextClass>();
            //// Cache populating
            //_cache.Set("thisCustomer", testCustomer);
            //// DbContext populating
            Customer testCustomer = new Customer();
            string   testString   = "testUserNameA";

            testCustomer.UserName = testString;
            testContext.Add(testCustomer); // already done
            testContext.SaveChanges();
            // Arrange
            Customer gottenCustomer = RevatureP1.UtilMethods.GetCustomerByUserName(testString, testContext);
            var      resultVal      = gottenCustomer.UserName;
            var      expectedVal    = testString;

            //Assert
            Assert.Equal(expectedVal, resultVal);
        }
Ejemplo n.º 10
0
        public static Boolean updateShellQuantity(Product product, int quantity, DbContextClass db)
        {
            product.shell_quantity  = product.shell_quantity - quantity;
            db.Entry(product).State = EntityState.Modified;
            db.SaveChanges();

            return(true);
        }
Ejemplo n.º 11
0
        public ActionResult HistoryDetail(int id)
        {
            DbContextClass db      = new DbContextClass();
            History        history = db.Histories.Find(id);

            ViewBag.HideSlider = true;
            return(View(history));
        }
Ejemplo n.º 12
0
        public ActionResult Pakistan()
        {
            DbContextClass db      = new DbContextClass();
            List <History> history = db.Histories.ToList();

            ViewBag.HideSlider = true;
            return(View(history));
        }
Ejemplo n.º 13
0
        // GET: Home
        public ActionResult Index()
        {
            ViewBag.indexTours = ModelHelper.ToTourSummaryList(new TourHandler().GetLatestTours(6));
            DbContextClass db = new DbContextClass();

            ViewBag.indexhistory = db.Histories.Take(3).ToList();
            return(View());
        }
 public LoginController(IMemoryCache cache, DbContextClass context)
 {
     /// <summary>
     /// The constructor
     /// </summary>
     _cache   = cache;
     _context = context;
 }
Ejemplo n.º 15
0
        public Country GetCountryById(int?id)
        {
            DbContextClass db = new DbContextClass();

            using (db)
            {
                return((from c in db.Countries where c.Id == id select c).FirstOrDefault());
            }
        }
Ejemplo n.º 16
0
        public List <Country> GetCountries()
        {
            DbContextClass db = new DbContextClass();

            using (db)
            {
                return((from u in db.Countries select u).ToList());
            }
        }
Ejemplo n.º 17
0
 public HomeController(ILogger <HomeController> logger, IMemoryCache cache, DbContextClass context)
 {
     /// <summary>
     /// The constructor
     /// </summary>
     _logger  = logger;
     _cache   = UtilMethods.InitializeCacheIfNeeded(cache);
     _context = context;
 }
Ejemplo n.º 18
0
        public void AddCountry(Country country)
        {
            DbContextClass db = new DbContextClass();

            using (db)
            {
                db.Countries.Add(country);
                db.SaveChanges();
            }
        }
        protected override void Dispose(bool disposing)
        {
            DbContextClass _db = new DbContextClass();

            if (disposing)
            {
                _db.Dispose();
            }
            base.Dispose(disposing);
        }
Ejemplo n.º 20
0
        static void Main(string[] args)
        {
            using (var context = new DbContextClass())
            {
                Customer currentCust  = new Customer(); //represents the current logged in user
                Store    currentStore = new Store();    //represents the store chosen by the user
                int      locationChoice;
                int      MainChoice;

                do //loop for start up, registering, and login.
                {
                    int choice = StartUp();

                    if (choice == 1)
                    {
                        Login(context, ref currentCust);
                    }
                    else if (choice == 2)
                    {
                        currentCust = Signup(context);
                    }
                    else if (choice == 0)
                    {
                        Environment.Exit(0);
                    }
                } while (currentCust.Username == null);//done with intial login/register loop

                Cart Usercart = GetCart();

                do
                {
                    locationChoice = LocationsMenu(context);
                    currentStore   = context.Stores.Where(x => x.StoreId == locationChoice).FirstOrDefault();
                    MainChoice     = UserChoice();

                    if (MainChoice == 1)//shows products at this location
                    {
                        ShowProducts(context, locationChoice, Usercart);
                    }
                    else if (MainChoice == 2)//see the past orders from this location or of current user.
                    {
                        OrdersMenu(context, currentCust);
                    }
                    else if (MainChoice == 3)//checks cart or allows them to checkout
                    {
                        double cartTotal = ViewCart(Usercart);
                    }
                    else if (MainChoice == 4)
                    {
                        break;
                    }
                } while ();
            }
        }
Ejemplo n.º 21
0
        public List <City> GetCities(Country country)
        {
            DbContextClass db = new DbContextClass();

            using (db)
            {
                return((from u in db.Cities
                        where u.Country.Id == country.Id
                        select u).ToList());
            }
        }
Ejemplo n.º 22
0
        public void DeleteCountry(Country country)
        {
            DbContextClass db = new DbContextClass();

            using (db)
            {
                db.Entry(country).State = EntityState.Deleted;
                db.Countries.Remove(country);
                db.SaveChanges();
            }
        }
Ejemplo n.º 23
0
        public List <City> GetCitiesByCountryId(int id)
        {
            DbContextClass db = new DbContextClass();

            using (db)
            {
                return((from c in db.Cities
                        where c.Country.Id == id
                        select c).ToList());
            }
        }
Ejemplo n.º 24
0
        public void AddCity(City city)
        {
            DbContextClass db = new DbContextClass();

            using (db)
            {
                db.Entry(city.Country).State = EntityState.Unchanged;
                db.Cities.Add(city);
                db.SaveChanges();
            }
        }
        // GET: Admin
        public ActionResult AdminPanel()
        {
            DbContextClass db = new DbContextClass();
            User           u  = (User)Session[WebUtil.CURRENT_USER];

            if (!(u != null && u.IsInRole(WebUtil.ADMIN_ROLE)))
            {
                return(RedirectToAction("Login", "User", new { ctl = "Admin", act = "AdminPanel" }));
            }
            return(RedirectToAction("AddTour", "Tour"));
        }
Ejemplo n.º 26
0
        public static void OrdersMenu(DbContextClass context, Customer currentCustomer, int locationId)
        {
            int  choice1;
            bool inputInt1;

            do
            {
                Console.WriteLine($"1) View the order history for this location.\n2) View the past orders for {currentCustomer.Username}");
                string input = Console.ReadLine();
                inputInt1 = int.TryParse(input, out choice1);
            } while (!inputInt1 || choice1 <= 0 || choice1 >= 3);

            if (choice1 == 1)
            {
                var    location     = context.Stores.Find(locationId);
                string locationName = location.Location;

                Console.WriteLine($"This is a list of of products ordered from our store in {locationName}.");
                var    pastOrders  = context.Orders.Where(x => x.LocationId == locationId).AsNoTracking();
                double TotalProfit = 0;

                foreach (var i in pastOrders)
                {
                    var prod = context.Products.Find(i.ProductId);
                    Console.WriteLine($"Product: {prod.ProductTitle}\t Price: {prod.Price}\t Ordered at: {i.CheckoutTime}\t Ordered by: {i.CustomerId}");
                    TotalProfit += prod.Price;
                }
                Console.WriteLine($"The total profit from this location is ${Math.Round(TotalProfit, 2)}");
            }

            if (choice1 == 2)
            {
                if (context.Orders.ToList().Count == 0)
                {
                }
                else if (context.Orders.ToList().Count != 0)
                {
                    if (context.Orders.Where(y => y.CustomerId == currentCustomer.CustomerId).Any())
                    {
                        foreach (var x in context.Orders.Where(e => e.CustomerId == currentCustomer.CustomerId).ToList())
                        {
                            Console.WriteLine($"You have bought {x.Price} {x.ProductId} on {x.CheckoutTime} from {x.LocationId}");
                        }
                    }
                    else
                    {
                        Console.WriteLine("You haven't made any orders yet.");
                    }
                }
            }
        }
Ejemplo n.º 27
0
        //List<Player> players = new List<Player>();
        //List<Game> games = new List<Game>();
        //List<Round> rounds = new List<Round>();
        //public Rps_Game(){}


        public Rps_Game(ILogger <Rps_Game> logger, IMemoryCache cache, DbContextClass context)
        {
            _logger  = logger;
            _cache   = cache;
            _context = context;
            //if the _cache doesn't have a players list, create one.
            //if (!_cache.TryGetValue("players", out players))
            //{
            //	_cache.Set("players", new List<Player>());
            //	_cache.TryGetValue("players", out players);
            //	_cache.TryGetValue("games", out games);
            //	_cache.TryGetValue("rounds", out rounds);
            //}
        }
    static void Main(string[] args)
    {
        Parallel.For(0, 10, i =>
        {
            using (DbContextClass db = new DbContextClass()) {
                Category ct = new Category
                {
                    NameCategory = "SomeText"
                };
                db.Categories.Add(ct);
                db.SaveChanges();
            }
        });

        Console.ReadKey();
    }
Ejemplo n.º 29
0
        public void TestCustomer(string fname, string lname, string username, string password)
        {
            // ARRANGE - create the data to insert into the Db
            //create the new Person seed
            AuthModel authModel = new AuthModel()
            {
                FirstName = fname,
                LastName  = lname,
                Username  = username,
                Password  = password
            };
            Customer outCustomer = null;
            bool     success     = false;

            using (var context1 = new DbContextClass(testOptions))
            {
                context1.Database.EnsureDeleted(); //do this ONCE at hte beginning of each test
                context1.Database.EnsureCreated(); // this creates the new-for-this-test database

                //create the MemeSaverRepo instance
                ToyRepository msr = new ToyRepository(context1);

                success = msr.RegisterTest(authModel, out outCustomer);
            }

            Assert.True(success);
            Assert.Equal(outCustomer.FirstName, authModel.FirstName);
            Assert.Equal(outCustomer.LastName, authModel.LastName);
            Assert.Equal(outCustomer.CustomerUName, authModel.Username);

            // ACT - call the method that inserts into the Db
            Customer outCustomer2 = new Customer();

            using (var context2 = new DbContextClass(testOptions))
            {
                context2.Database.EnsureCreated();
                ToyRepository msr = new ToyRepository(context2);
                success = msr.LoginTest(authModel, out outCustomer2);
                // success = context2.Persons.Where(x => x.PasswordHash == testPerson.PasswordHash).FirstOrDefault();
            }

            // ASSERT - verify the the data state is as expected
            Assert.Equal(outCustomer2.FirstName, authModel.FirstName);
            Assert.Equal(outCustomer2.LastName, authModel.LastName);
            Assert.Equal(outCustomer2.CustomerUName, authModel.Username);
            Assert.True(success);
        }
Ejemplo n.º 30
0
        public static int LocationsMenu(DbContextClass context)
        {
            int  choice;
            bool inputInt;

            do
            {
                Console.WriteLine("Choose a store location from the list provided.");
                var stores = context.Stores;
                foreach (var store in stores)
                {
                    Console.WriteLine($"Store {store.StoreId} at {store.Address}");
                }

                string storeInput = Console.ReadLine();
                inputInt = int.TryParse(storeInput, out choice);
            } while (!inputInt || choice <= 0 || choice >= 5);

            return(choice);
        }