public void Add_writes_to_database() { // In-memory database only exists while the connection is open var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); try { var options = new DbContextOptionsBuilder <Project0Context>().UseSqlite(connection).Options; // Create the schema in the database using (var context = new Project0Context(options)) { context.Database.EnsureCreated(); } // Run the test against one instance of the context using (var context = new Project0Context(options)) { var orders = new OrderRepository(context); OrderImp _order = new OrderImp(1, DateTime.Now, 1, 19.99, 0.00, new StoreImp("San Francisco")); OrderImp _order2 = new OrderImp(4, DateTime.Now, 1, 19.99, 0.00, new StoreImp("San Francisco")); OrderGamesImp _orderGame = new OrderGamesImp(1, 2, 3, 2); orders.AddOrder(_order); orders.AddOrder(_order2); orders.AddOrderItem(_orderGame.OrderId, _orderGame.GameId, _orderGame.GameQuantity, _orderGame.Edition); //Tests add order Assert.Equal(_order.OrderID, context.Orders.Find(1).OrderId); Assert.Equal(_order2.OrderID, context.Orders.Find(4).OrderId); //Tests add orderItem Assert.Equal(_orderGame.OrderId, context.OrderGames.Find(1, 2).OrderId); Assert.Equal(_orderGame.GameId, context.OrderGames.Find(1, 2).GameId); } // Use a separate instance of the context to verify correct data was saved to database using (var context = new Project0Context(options)) { OrderImp _order = new OrderImp(1, DateTime.Now, 1, 19.99, 0.00, new StoreImp("San Francisco")); OrderImp _order2 = new OrderImp(4, DateTime.Now, 1, 19.99, 0.00, new StoreImp("San Francisco")); Assert.Equal(_order.OrderID, context.Orders.Find(1).OrderId); Assert.Equal(_order2.OrderID, context.Orders.Find(4).OrderId); } } finally { connection.Close(); } }
public static void ViewOrderDetails(OrderImp order, IStoreRepository storeRepository) { Console.WriteLine(); Console.WriteLine($"Order ID: {order.OrderID},\n Customer ID: {order.OrderCustomer},\n " + $"Order Cost: {order.OrderCost},\n Order Date: {order.OrderDate}" + $"\n Order Location: {storeRepository.GetStoreByLocation(order.StoreId).Location}"); //Console.WriteLine("List of games in purchase: "); //for (int i = 0; i < order.GamesInOrder.Count; i++) //{ // Console.WriteLine(order.GamesInOrder[i].Game.Name); //} }
public void Test_Find_By_Functions() { // In-memory database only exists while the connection is open var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); IEnumerable <OrderImp> returnIEnumerable(Project0Context contxt) { foreach (var item in contxt.Orders) { yield return(Mapper.Map(item)); } } try { var options = new DbContextOptionsBuilder <Project0Context>().UseSqlite(connection).Options; // Create the schema in the database using (var context = new Project0Context(options)) { context.Database.EnsureCreated(); } using (var context = new Project0Context(options)) { var orders = new OrderRepository(context); //Testing getting order by ID OrderImp _order = new OrderImp(1, DateTime.Now, 1, 29.99, 0.00, new StoreImp("San Francisco")); OrderImp _order2 = new OrderImp(4, DateTime.Now, 1, 19.99, 0.00, new StoreImp("San Francisco")); orders.AddOrder(_order); orders.AddOrder(_order2); Assert.Equal(_order, orders.GetOrderByID(_order.OrderID)); Assert.Equal(_order2, orders.GetOrderByID(_order2.OrderID)); //Tests getting order by Date List <OrderImp> testList1 = returnIEnumerable(context).ToList(); List <OrderImp> testList2 = orders.GetOrderByDate().ToList(); Assert.Equal(testList1.Count, testList2.Count); for (int i = 0; i < testList1.Count; i++) { Assert.Equal(testList1[i].OrderID, testList2[i].OrderID); Assert.Equal(testList1[i].OrderCost, testList2[i].OrderCost); Assert.Equal(testList1[i].OrderDate, testList2[i].OrderDate); } //Tests getting order by Date reversed testList2 = orders.GetOrderByDateReverse().ToList(); testList1.Reverse(); for (int i = 0; i < testList1.Count; i++) { Assert.Equal(testList1[i].OrderID, testList2[i].OrderID); Assert.Equal(testList1[i].OrderCost, testList2[i].OrderCost); Assert.Equal(testList1[i].OrderDate, testList2[i].OrderDate); } //Test getting order by Cost testList2 = orders.GetOrderByCost().ToList(); for (int i = 0; i < testList1.Count; i++) { Assert.Equal(testList1[i].OrderID, testList2[i].OrderID); Assert.Equal(testList1[i].OrderCost, testList2[i].OrderCost); Assert.Equal(testList1[i].OrderDate, testList2[i].OrderDate); } //Test getting order by Cost reverse testList2 = orders.GetOrderByCostReverse().ToList(); testList1.Reverse(); for (int i = 0; i < testList1.Count; i++) { Assert.Equal(testList1[i].OrderID, testList2[i].OrderID); Assert.Equal(testList1[i].OrderCost, testList2[i].OrderCost); Assert.Equal(testList1[i].OrderDate, testList2[i].OrderDate); } } } finally { connection.Close(); } }
static void Main(string[] args) { Console.WriteLine("Order Games"); var optionsBuilder = new DbContextOptionsBuilder <Project0Context>(); optionsBuilder.UseSqlServer(SecretConfiguration.ConnectionString); optionsBuilder.UseLoggerFactory(AppLoggerFactory); var options = optionsBuilder.Options; var dbContext = new Project0Context(options); IOrdersRepository OrdersRepository = new OrderRepository(dbContext); IStoreRepository StoreRepository = new StoreRepository(dbContext); ICustomerRepository CustomerRepository = new CustomerRepository(dbContext); IGamesRepository GamesRepository = new GamesRepository(dbContext); //Display customer screen and select customer, then pass that customer onto the selected store //CustomerScreen(CustomerRepository); int customerSelection = 0; int customerOptionSelect = 0; string name = ""; bool moreGamesToBuy = true; //Display select store screen, or go back to customer selection //StoreScreen(StoreRepository); int storeSelection; CustomerSelect: storeSelection = StoreRepository.GetStores().ToList().Count + 1; Console.WriteLine(); Console.WriteLine("Press 1 to view all customers. \nPress 2 to search for a customer by name."); Console.WriteLine($"Press 3 to view statistics."); customerOptionSelect = Convert.ToInt32(Console.ReadLine()); if (customerOptionSelect == 1) { CustomerScreen(CustomerRepository); customerSelection = Convert.ToInt32(Console.ReadLine()); if (!CustomerRepository.IsValidId(customerSelection)) { Console.WriteLine("Please input a valid customer ID"); goto CustomerSelect; } } else if (customerOptionSelect == 2) { Console.WriteLine(); Console.WriteLine("Enter a full name: "); name = Console.ReadLine(); //storeSelection = StoreRepository.GetStores().ToList().Count + 2; } else if (customerOptionSelect == 3) { int popId = OrdersRepository.GetMostPopularGame(); Console.WriteLine(); Console.WriteLine($"The most popular game is {GamesRepository.GetGameById(popId)}"); goto CustomerSelect; } else { Console.WriteLine("Please enter a valid input."); goto CustomerSelect; } StoreScreen(StoreRepository); storeSelection = Convert.ToInt32(Console.ReadLine()); CustomerImp selectedCustomer; StoreImp selectedStore;// = StoreRepository.GetStoreByLocation(storeSelection); List <CustomerImp> ListOfCustomers = CustomerRepository.GetCustomers().ToList(); if (customerOptionSelect == 1) { selectedCustomer = CustomerRepository.GetCustomerById(customerSelection); //Get the chosen customer by ID } else if (customerOptionSelect == 2) { selectedCustomer = CustomerRepository.GetCustomerByName(name); //Get the chosen customer by name } else { goto CustomerSelect; } if (storeSelection == StoreRepository.GetStores().ToList().Count + 2) //if user chooses to view all order by selected cust { if (selectedCustomer.OrdersByCustomer.Count < 1) //if there are no orders on file for customer, go to cust select { Console.WriteLine("Customer has no orders on file."); goto CustomerSelect; } List <OrderImp> OrdersByCustomer = OrdersRepository.GetAllOrdersByCustomer(selectedCustomer.Id).ToList(); for (int i = 0; i < selectedCustomer.OrdersByCustomer.Count; i++) //views all orders by customer { ViewOrderDetails(OrdersByCustomer[i], StoreRepository); } Console.WriteLine("Press 1 to return to customer select.\nPress 2 to view list of stores."); int temp = Convert.ToInt32(Console.ReadLine()); if (temp == 1) { goto CustomerSelect; } else if (temp == 2) { StoreScreen(StoreRepository); storeSelection = Convert.ToInt32(Console.ReadLine()); goto StoreSelect; } else { Console.WriteLine("Invalid input, redirecting to customer select."); goto CustomerSelect; } } else if (storeSelection == StoreRepository.GetStores().ToList().Count + 1) { goto CustomerSelect; } StoreSelect: selectedStore = StoreRepository.GetStoreByLocation(storeSelection); //Get the chosen store OptionSelect: OptionsScreen(); //View options to handle or add orders int optionSelect = Convert.ToInt32(Console.ReadLine()); int gameSelect = 0; int viewSelect = 0; if (optionSelect == 1) //view all orders at this store { ViewSelect: ViewOrdersScreen(); viewSelect = Convert.ToInt32(Console.ReadLine()); if (viewSelect == 1) { List <OrderImp> OrderList = OrdersRepository.GetOrderByDate(selectedStore).ToList(); if (OrderList.Count < 1) { Console.WriteLine(); Console.WriteLine("There are no orders at this store."); goto OptionSelect; } ViewAllOrdersInList(OrderList, StoreRepository); } else if (viewSelect == 2) { List <OrderImp> OrderList = OrdersRepository.GetOrderByDateReverse(selectedStore).ToList(); if (OrderList.Count < 1) { Console.WriteLine(); Console.WriteLine("There are no orders at this store."); goto OptionSelect; } ViewAllOrdersInList(OrderList, StoreRepository); } else if (viewSelect == 3) { List <OrderImp> OrderList = OrdersRepository.GetOrderByCost(selectedStore).ToList(); if (OrderList.Count < 1) { Console.WriteLine(); Console.WriteLine("There are no orders at this store."); goto OptionSelect; } ViewAllOrdersInList(OrderList, StoreRepository); } else if (viewSelect == 4) { List <OrderImp> OrderList = OrdersRepository.GetOrderByCostReverse(selectedStore).ToList(); if (OrderList.Count < 1) { Console.WriteLine(); Console.WriteLine("There are no orders at this store."); goto OptionSelect; } ViewAllOrdersInList(OrderList, StoreRepository); } else if (viewSelect == 5) { goto OptionSelect; } else { Console.WriteLine("Please enter a valid input."); goto ViewSelect; } } else if (optionSelect == 2) //Place an order at selected store { if (!selectedStore.CheckIfOrderIsReady(selectedCustomer)) { Console.WriteLine(); Console.WriteLine($"You have ordered from this story within the last 2 hours, " + $"please pick a different option"); goto OptionSelect; } OrderImp newOrder = new OrderImp(); OrderGamesImp newGames; while (moreGamesToBuy) { newGames = new OrderGamesImp(); GameSelection: PlaceOrderScreen(GamesRepository, selectedCustomer); gameSelect = Convert.ToInt32(Console.ReadLine()); if (GamesRepository.GetGameById(gameSelect) == null) { Console.WriteLine(); Console.WriteLine("Please enter valid ID number"); goto GameSelection; } GamesImp selectedGame = GamesRepository.GetGameById(gameSelect); selectedCustomer.LastGameBoughtId = selectedGame.Id; EditionSelect: Console.WriteLine(); Console.WriteLine("Enter the game edition: "); Console.WriteLine($"1. Standard edition: {selectedGame.Cost}"); Console.WriteLine($"2. Advanced edition: {selectedGame.AdvancedCost}"); Console.WriteLine($"3. Deluxe edition: {selectedGame.AdvancedCost + 10}"); int selectedEdition = Convert.ToInt32(Console.ReadLine()); if (selectedEdition != 1 && selectedEdition != 2 && selectedEdition != 3) { Console.WriteLine("Please enter a valid edition number."); goto GameSelection; } string editionName = ""; if (selectedEdition == 1) { editionName = "Standard"; } else if (selectedEdition == 2) { editionName = "Advanced"; } else { editionName = "Deluxe"; } Console.WriteLine(); Console.WriteLine($"Enter the number of {selectedGame.Name}, {editionName} Edition"); int quantityOfGame = Convert.ToInt32(Console.ReadLine()); StoreRepository.RemoveFromStock(quantityOfGame, selectedGame, selectedStore); if (editionName == "Deluxe") { if (selectedStore.DeluxeInStock < quantityOfGame) { Console.WriteLine("Not enough deluxe in stock, choose another edition."); goto EditionSelect; } else { StoreRepository.RemoveDeluxeFromStock(quantityOfGame, selectedStore.IDNumber); } } //Assign the game purchases' statistics newGames.Game = selectedGame; newGames.GameId = selectedGame.Id; newGames.Edition = selectedEdition; newGames.GameQuantity = quantityOfGame; newGames.OrderId = 100; newOrder.GamesInOrder.Add(newGames); AddGameChoice: Console.WriteLine("Do you want to add another game?\nPress 1 to add another game\nPress 2 to finish order."); int nextAction = Convert.ToInt32(Console.ReadLine()); if (nextAction == 1) { } else if (nextAction == 2) { moreGamesToBuy = false; } else { Console.WriteLine("Enter a valid input."); goto AddGameChoice; } //OrderImp newOrder = new OrderImp(DateTime.Now, selectedCustomer.Id, selectedStore.ShippingCosts, selectedStore); } newOrder.OrderDate = DateTime.Now; newOrder.OrderCustomer = selectedCustomer.Id; newOrder.StoreId = selectedStore.IDNumber; OrdersRepository.AddOrder(newOrder); //Adds the new order to database Console.WriteLine("newOrder's OrderId = " + newOrder.OrderID); for (int i = 0; i < newOrder.GamesInOrder.Count; i++) //adds all new OrderGames to database and assigns their OrderId { newOrder.GamesInOrder[i].OrderId = OrdersRepository.GetExactOrderByDate(newOrder.OrderDate).OrderID; OrdersRepository.AddOrderItem(newOrder.GamesInOrder[i]); } } else if (optionSelect == 3) //go back to store selection screen { StoreScreen(StoreRepository); storeSelection = Convert.ToInt32(Console.ReadLine()); goto StoreSelect; } else //invalid input { Console.WriteLine("Please enter a valid input."); goto OptionSelect; } FinalMenu(); int input = Convert.ToInt32(Console.ReadLine()); switch (input) { case 1: StoreScreen(StoreRepository); storeSelection = Convert.ToInt32(Console.ReadLine()); goto StoreSelect; case 2: goto CustomerSelect; default: break; } Console.ReadLine(); }