Ejemplo n.º 1
0
        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();
            }
        }
Ejemplo n.º 2
0
        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();
        }
Ejemplo n.º 3
0
        public static void Main()
        {
            var optionsBuilder = new DbContextOptionsBuilder <HardwareStoreDbContext>();

            optionsBuilder.UseSqlServer(SecretConfiguration.ConnectionString);
            var dbContext = new HardwareStoreDbContext(optionsBuilder.Options);

            CustomerRepository customerRepository = new CustomerRepository(dbContext);
            OrderRepository    orderRepository    = new OrderRepository(dbContext);
            LocationRepository locationRepository = new LocationRepository(dbContext);
            ProductsRepository productsRepository = new ProductsRepository(dbContext);

            Console.WriteLine("Welcome to Francisco's Hardware Store! ");

            while (true)
            {
                Console.WriteLine("\no:\tPlace an order");
                Console.WriteLine("n:\tSearch for customer by name");
                Console.WriteLine("l:\tDisplay all orders by store location");
                Console.WriteLine("c:\tDisplay all orders by customer");
                Console.WriteLine("a\tView all orders that have been placed.");
                Console.WriteLine("q\tQuit");

                String input = Console.ReadLine();
                if (input == "o")
                {
                    customerRepository.DisplayCustomers();
                    Console.WriteLine("Which customer is the order for? input customer id:");
                    input = Console.ReadLine();
                    int val = 0;
                    if (Int32.TryParse(input, out val))
                    {
                        Library.Customer customer = customerRepository.GetCustomerById(Int32.Parse(input));
                        int customerId            = Int32.Parse(input);
                        Console.WriteLine("Your default store id is " + customer.DefaultStoreId + " location: " + locationRepository.GetLocationById(customer.DefaultStoreId).Name);
                        Console.WriteLine("Do you want to order from a different location? input y for yes");
                        input = Console.ReadLine();
                        if (input == "y")
                        {
                            locationRepository.DisplayLocations();
                            Console.WriteLine("input location id");
                            input = Console.ReadLine();
                            int val2 = 0;
                            if (Int32.TryParse(input, out val2))
                            {
                                int storeLoc  = Int32.Parse(input);    //storeloc will be ordering from
                                var inventory = locationRepository.GetInventoryByLocationId(storeLoc);
                                Console.WriteLine("Printing Inventory for location id " + storeLoc);
                                foreach (var inv in inventory)
                                {
                                    Console.WriteLine($"ProductId: {inv.ProductId} Amount in stock: {inv.AmountInStock} Product name: {productsRepository.GetProductByProductId(inv.ProductId).ProductName} Price {productsRepository.GetProductByProductId(inv.ProductId).Price}");
                                }
                                Console.WriteLine("Place your order.");
                                Console.WriteLine("input product id of the product you wish to buy.");
                                input = Console.ReadLine();
                                int productId = Int32.Parse(input);
                                Console.WriteLine("input amount of product you wish to buy");
                                input = Console.ReadLine();
                                int     productAmount = Int32.Parse(input);
                                decimal total         = productAmount * productsRepository.GetProductByProductId(productId).Price;

                                Console.WriteLine("Placing order...");
                                Order myOrder = new Order();
                                myOrder.OrderTime  = DateTime.Now;
                                myOrder.LocationId = storeLoc;
                                myOrder.CustomerId = customerId;
                                myOrder.OrderTotal = total;
                                orderRepository.AddOrder(myOrder);
                                orderRepository.Save();

                                Library.OrderItem myOrderItem = new Library.OrderItem();
                                myOrderItem.OrderId        = orderRepository.GetLastOrderAdded();
                                myOrderItem.OrderItemNum   = 1;
                                myOrderItem.QuantityBought = productAmount;
                                myOrderItem.ProductId      = productId;
                                myOrderItem.Price          = productsRepository.GetProductByProductId(productId).Price;
                                orderRepository.AddOrderItem(myOrderItem);
                                orderRepository.Save();

                                Console.WriteLine("OrderPlaced");
                            }
                            //Console.WriteLine("checking if input parsed"+input);
                        }
                        else
                        {
                            Console.WriteLine("Order will be placed from default location.");
                            int storeLoc  = customerRepository.GetCustomerById(customerId).DefaultStoreId;
                            var inventory = locationRepository.GetInventoryByLocationId(storeLoc);
                            Console.WriteLine("Printing Inventory for location id " + storeLoc);
                            foreach (var inv in inventory)
                            {
                                Console.WriteLine($"ProductId: {inv.ProductId} Amount in stock: {inv.AmountInStock} Product name: {productsRepository.GetProductByProductId(inv.ProductId).ProductName} Price {productsRepository.GetProductByProductId(inv.ProductId).Price}");
                            }
                            Console.WriteLine("Place your order.");
                            Console.WriteLine("input product id of the product you wish to buy.");
                            input = Console.ReadLine();
                            int productId = Int32.Parse(input);
                            Console.WriteLine("input amount of product you wish to buy");
                            input = Console.ReadLine();
                            int     productAmount = Int32.Parse(input);
                            decimal total         = productAmount * productsRepository.GetProductByProductId(productId).Price;

                            Console.WriteLine("Placing order...");
                            Order myOrder = new Order();
                            myOrder.OrderTime  = DateTime.Now;
                            myOrder.LocationId = storeLoc;
                            myOrder.CustomerId = customerId;
                            myOrder.OrderTotal = total;
                            orderRepository.AddOrder(myOrder);
                            orderRepository.Save();

                            Library.OrderItem myOrderItem = new Library.OrderItem();
                            myOrderItem.OrderId        = orderRepository.GetLastOrderAdded();
                            myOrderItem.OrderItemNum   = 1;
                            myOrderItem.QuantityBought = productAmount;
                            myOrderItem.ProductId      = productId;
                            myOrderItem.Price          = productsRepository.GetProductByProductId(productId).Price;
                            orderRepository.AddOrderItem(myOrderItem);
                            orderRepository.Save();

                            Console.WriteLine("OrderPlaced");
                            //order from default store
                        }
                    }
                    else
                    {
                        Console.WriteLine("No matches");
                    }
                }
                if (input == "n")
                {
                    do
                    {
                        Console.WriteLine("Enter first name:");
                        input = Console.ReadLine();
                    }while (input.Length == 0);

                    string input2;
                    do
                    {
                        Console.WriteLine("Enter last name: ");
                        input2 = Console.ReadLine();
                    }while (input2.Length == 0);
                    var  customerList = customerRepository.GetCustomerByName(input, input2);
                    bool flag         = customerList.Any();
                    if (!flag)
                    {
                        Console.WriteLine("no matches");
                    }
                    foreach (var c in customerList)
                    {
                        customerRepository.DisplayCustomer(c);
                    }
                }
                if (input == "l")
                {
                    bool flag = false;
                    Console.WriteLine("Do you want to see all of the details for each order? input y for yes");
                    string deets = Console.ReadLine();
                    if (deets == "y")
                    {
                        flag = true;
                    }

                    Console.WriteLine("Displaying all stores.");
                    locationRepository.DisplayLocations();

                    Console.WriteLine("Input a store id: ");
                    input = Console.ReadLine();
                    int val = 0;
                    if (Int32.TryParse(input, out val))
                    {
                        Console.WriteLine("Showing all orders for location " + input);
                        foreach (var x in locationRepository.GetOrderHistoryByLocation(Int32.Parse(input)))
                        {
                            if (flag)
                            {
                                orderRepository.DisplayOrderDetailsAll(x.OrderId);
                            }
                            else
                            {
                                orderRepository.DisplayOrderDetailsShort(x.OrderId);
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("no matches");
                    }
                }
                if (input == "c")
                {
                    bool flag = false;
                    Console.WriteLine("Do you want to see all of the details for each order? input y for yes");
                    string deets = Console.ReadLine();
                    if (deets == "y")
                    {
                        flag = true;
                    }

                    Console.WriteLine("Displaying all customers.");
                    customerRepository.DisplayCustomers();


                    Console.WriteLine("Input a customer id: ");
                    input = Console.ReadLine();
                    int val = 0;
                    if (Int32.TryParse(input, out val))
                    {
                        Console.WriteLine("Showing all orders for customer " + input);
                        foreach (var x in customerRepository.GetOrderHistoryByCustomer(Int32.Parse(input)))
                        {
                            if (!flag)
                            {
                                orderRepository.DisplayOrderDetailsShort(x.OrderId);
                            }
                            else
                            {
                                orderRepository.DisplayOrderDetailsAll(x.OrderId);
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("no matches");
                    }
                }
                if (input == "a")
                {
                    Console.WriteLine("Displaying all orders placed.");
                    foreach (var o in orderRepository.GetOrders())
                    {
                        Console.WriteLine("Orders" + "id: " + o.OrderId + " " + o.OrderTime + " Total: " + o.OrderTotal + " id: " + o.LocationId + o.CustomerId);
                    }
                }
                if (input == "q")
                {
                    break;
                }
            }
        }