Beispiel #1
0
 /// <summary>
 /// Maps a Domain Order into an Entity Reciept
 /// </summary>
 /// <param name="ordDom">A Domain Order</param>
 /// <returns>An Entity Reciept</returns>
 public static Entities.Receipt MapOrder(dom.Order ordDom)
 {
     return(new Entities.Receipt
     {
         LocationId = ordDom.OrderLocation.StoreID,
         CustomerId = ordDom.OrderCustomer.CustID,
         ReceiptTimestamp = ordDom.OrderTimestamp
     });
 }
Beispiel #2
0
        /// <summary>
        /// Maps a Domain Order's Basket into a list of Entity Baskets
        /// </summary>
        /// <param name="ordDom">A Domain Order</param>
        /// <param name="dbId">The Id of the Domain from the Database Entity associated with it.</param>
        /// <returns>A list of Entity Baskets</returns>
        public static IEnumerable <Entities.Basket> MapBasket(dom.Order ordDom, int dbId)
        {
            var list = new List <Entities.Basket> {
            };

            foreach (KeyValuePair <dom.Product, int> item in ordDom.basket)
            {
                list.Add(new Entities.Basket {
                    ProductId = item.Key.ProductID, Quantity = item.Value, ReceiptId = dbId
                });
            }
            return(list);
        }
Beispiel #3
0
        /// <summary>
        /// Maps an Entity Reciept and Basket into a Domain Order
        /// </summary>
        /// <param name="ordEnt">An Entity Reciept and basket</param>
        /// <returns>A Domain Order</returns>
        public static dom.Order MapOrder(Entities.Receipt ordEnt)
        {
            var custDom = MapCustomer(ordEnt.Customer);
            var locDom  = MapLocation(ordEnt.Location);
            var ordDom  = new dom.Order(custDom, locDom, ordEnt.ReceiptId, ordEnt.ReceiptTimestamp);

            foreach (Entities.Basket item in ordEnt.Basket)
            {
                var prodDom = MapProduct(item.Product);
                ordDom.basket.Add(prodDom, item.Quantity);

                //AddItemToBasket(prodDom, item.Quantity);
            }
            return(ordDom);
        }
        public ActionResult PostDetails(IFormCollection collection)
        {
            try
            {
                var custId = Convert.ToInt32(TempData["custId"]);
                var cust   = _custContext.GetCustomers(custId: custId).FirstOrDefault();
                var locId  = Convert.ToInt32(TempData["locId"]);
                var loc    = _locContext.GetLocations(locId: locId).FirstOrDefault();
                var ord    = new dom.Order(cust, loc);


                var coll = new Dictionary <int, int> {
                };                                      //prodId, Quantity to buy

                foreach (KeyValuePair <string, Microsoft.Extensions.Primitives.StringValues> collItem in collection)
                {
                    try
                    {
                        var prodId   = Convert.ToInt32(collItem.Key);
                        var quantity = Convert.ToInt32(Convert.ToString(collItem.Value));
                        coll.Add(prodId, quantity);
                    }
                    catch { }
                }

                foreach (KeyValuePair <dom.Product, int> item in loc.Inventory) //prodId, amount in stock
                {
                    foreach (KeyValuePair <int, int> collItem in coll)
                    {
                        if (item.Key.ProductID == collItem.Key)
                        {
                            ord.basket.Add(item.Key, collItem.Value);//prod, quantity to buy
                        }
                    }
                }

                foreach (KeyValuePair <dom.Product, int> item in ord.basket)
                {
                    loc.AdjustQuantity(item.Key, -1 * item.Value);
                }

                _locContext.UpdateInventory(loc);
                _locContext.Save();

                _ordContext.AddOrder(ord);
                _ordContext.Save();
                var ordId = _ordContext.GetOrdersByCustomer(ord.OrderCustomer.CustID).Last().OrderId;
                ord.OrderId       = ordId;
                TempData["ordId"] = ord.OrderId;
                _ordContext.AddBasket(ord, ordId);
                _ordContext.Save();

                ord = _ordContext.GetOrderById(ord.OrderId).First();

                return(RedirectToAction("Details", "ViewOrder", new { id = ordId }));
            }
            catch
            {
                return(Redirect(nameof(Index)));
            }
        }
        public static async Task RunUI(Project0Context dbContext)
        {
            //Establish contexts to the four domains
            var custContext = new DbRepo.CustomerRepo(dbContext);
            var prodContext = new DbRepo.ProductRepo(dbContext);
            var locContext  = new DbRepo.LocationRepo(dbContext);
            var ordContext  = new DbRepo.OrderRepo(dbContext);

            string storeName = " ____  ____  __  ____  ____  _  _   __   __ _   __    ___  ____  ____ \n" +
                               "/ ___)(_  _)/  \\(  _ \\(  __)( \\/ ) / _\\ (  ( \\ / _\\  / __)(  __)(  _ \\\n" +
                               "\\___ \\  )( (  O ))   / ) _) / \\/ \\/    \\/    //    \\( (_ \\ ) _)  )   /\n" +
                               "(____/ (__) \\__/(__\\_)(____)\\_)(_/\\_/\\_/\\_)__)\\_/\\_/ \\___/(____)(__\\_)\n";

            while (true)
            {
                Console.Clear();
                Console.WriteLine(storeName);
                Console.WriteLine();

                Console.WriteLine("1:\tAdd a customer");
                Console.WriteLine("2:\tPerform Searches and Displays");
                Console.WriteLine("3:\tPlace an order");
                Console.WriteLine("4:\tSave to file");
                Console.WriteLine("5:\tQuit");
                Console.WriteLine();
                Console.Write("Enter a valid menu option: ");
                var input = Console.ReadLine();
                if (input == "1")
                {
                    string firstName = null;
                    string lastName  = null;
                    string address   = null;

                    Console.Clear();
                    Console.WriteLine(storeName);
                    Console.WriteLine("Add a customer menu\n");

                    while (firstName == null)
                    {
                        Console.Write("Enter a first name: ");
                        firstName = Console.ReadLine();
                        if (firstName == "")
                        {
                            firstName = null;
                        }
                    }
                    while (lastName == null)
                    {
                        Console.Write("Enter a last name: ");
                        lastName = Console.ReadLine();
                        if (lastName == "")
                        {
                            lastName = null;
                        }
                    }
                    while (address == null)
                    {
                        Console.Write("Enter an address: ");
                        address = Console.ReadLine();
                        if (address == "")
                        {
                            address = null;
                        }
                    }
                    Console.WriteLine($"\nCreating a new customer with:\nFirst Name:  {firstName}\nLast Name:  {lastName}\nAddress:  {address}");

                    try
                    {
                        var newCustomer = new dom.Customer(firstName, lastName, address);
                        custContext.AddCustomer(newCustomer);
                        custContext.Save();
                        var dbCustomerId = custContext.GetCustomers(firstName, lastName).Last().CustID;
                        Console.WriteLine($"The customer has been successfully added.\nID:\t{dbCustomerId}\n");

                        Console.WriteLine("Press any key to continue.");
                        Console.ReadKey();
                    }
                    catch (ArgumentNullException ex)
                    {
                        Console.WriteLine(ex.Message);
                        s_logger.Info(ex);
                    }
                }
                else if (input == "2")
                {
                    while (true)
                    {
                        Console.Clear();
                        Console.WriteLine(storeName);
                        Console.WriteLine("Search and Display Menu\n");

                        Console.WriteLine("1:\tSearch and Display Customers by Name");
                        Console.WriteLine("2:\tDisplay Locations");
                        Console.WriteLine("3:\tDisplay all Orders for a Customer");
                        Console.WriteLine("4:\tDisplay all Orders for a Location");
                        Console.WriteLine("5:\tDisplay Details of an Order");
                        Console.WriteLine("6:\tLeave Menu");
                        Console.WriteLine();

                        Console.Write("Enter a valid menu option: ");
                        var inputMenu2 = Console.ReadLine();
                        if (inputMenu2 == "1")
                        {
                            string firstNameSearch = null;
                            string lastNameSearch  = null;

                            Console.Clear();
                            Console.WriteLine(storeName);
                            Console.WriteLine("Search and Display Customers by Name\n");

                            Console.WriteLine("Enter a First Name to search:");
                            Console.Write("\t(or leave empty): ");
                            firstNameSearch = Console.ReadLine();
                            if (firstNameSearch == "")
                            {
                                firstNameSearch = null;
                            }

                            Console.WriteLine("Enter a Last Name to search:");
                            Console.Write("\t(or leave empty): ");
                            lastNameSearch = Console.ReadLine();
                            if (lastNameSearch == "")
                            {
                                lastNameSearch = null;
                            }

                            Console.WriteLine($"\nSearching for Customers with\n\tFirst Name: {firstNameSearch}\n\tLast Name: {lastNameSearch}\n\nResults:");
                            Console.WriteLine();
                            var customersSearched = custContext.GetCustomers(firstNameSearch, lastNameSearch).ToList();
                            foreach (dom.Customer cust in customersSearched)
                            {
                                Console.WriteLine(cust.ToString() + "\n");
                            }

                            Console.WriteLine("\nPress any key to continue.");
                            Console.ReadKey();
                        }
                        else if (inputMenu2 == "2")
                        {
                            Console.Clear();
                            Console.WriteLine(storeName);
                            Console.WriteLine("Locations:\n");

                            var locations = locContext.GetLocations().ToList();
                            foreach (dom.Location item in locations)
                            {
                                Console.WriteLine(item.ToString() + "\n");
                            }
                            Console.WriteLine("\nPress any key to continue.");
                            Console.ReadKey();
                        }
                        else if (inputMenu2 == "3")
                        {
                            string inputMenu2Entry;
                            int    custId = 0;
                            bool   isInt  = false;
                            do
                            {
                                Console.Clear();
                                Console.WriteLine(storeName);
                                Console.WriteLine("Display All Orders for a Customer\n");

                                Console.Write("Enter a Customer ID: ");
                                inputMenu2Entry = Console.ReadLine();
                                isInt           = Int32.TryParse(inputMenu2Entry, out custId);
                            }while (!isInt);

                            var results = ordContext.GetOrdersByCustomer(custId: custId).ToList();
                            if (results.Count > 0)
                            {
                                foreach (dom.Order ord in results)
                                {
                                    Console.WriteLine(ord.ToString() + "\n");
                                }
                            }
                            else
                            {
                                Console.WriteLine($"No results matching CustomerID {custId}");
                            }
                            Console.WriteLine("Press any key to continue.");
                            Console.ReadKey();
                        }
                        else if (inputMenu2 == "4")
                        {
                            string inputMenu3Entry;
                            int    locId = 0;
                            bool   isInt = false;
                            do
                            {
                                Console.Clear();
                                Console.WriteLine(storeName);
                                Console.WriteLine("Display All Orders for a Location\n");

                                Console.Write("Enter a Location ID: ");
                                inputMenu3Entry = Console.ReadLine();
                                isInt           = Int32.TryParse(inputMenu3Entry, out locId);
                            }while (!isInt);

                            var results = ordContext.GetOrdersByLocation(locId).ToList();
                            if (results.Count > 0)
                            {
                                foreach (dom.Order ord in results)
                                {
                                    Console.WriteLine(ord.ToString() + "\n");
                                }
                            }
                            else
                            {
                                Console.WriteLine($"No results matching LocationID {locId}");
                            }
                            Console.WriteLine("Press any key to continue.");
                            Console.ReadKey();
                        }
                        else if (inputMenu2 == "5")
                        {
                            string inputMenu4Entry;
                            int    ordId = 0;
                            bool   isInt = false;
                            do
                            {
                                Console.Clear();
                                Console.WriteLine(storeName);
                                Console.WriteLine("Display Details of an Order:\n");

                                Console.Write("Enter a Order ID: ");
                                inputMenu4Entry = Console.ReadLine();
                                isInt           = Int32.TryParse(inputMenu4Entry, out ordId);
                            }while (!isInt);

                            var result = ordContext.GetOrderById(ordId).ToList().FirstOrDefault();
                            if (result == null)
                            {
                                Console.WriteLine($"No results matching OrderID {ordId}");
                            }
                            else
                            {
                                Console.WriteLine(result.ToString());
                                Console.WriteLine(result.BasketToString());
                            }
                            Console.WriteLine("Press any key to continue.");
                            Console.ReadKey();
                        }
                        else if (inputMenu2 == "6")
                        {
                            break;
                        }
                    }
                }
                else if (input == "3")
                {
                    string inputStr;
                    int    custId = 0;
                    int    locId  = 0;
                    bool   isInt  = false;


                    do
                    {
                        Console.Clear();
                        Console.WriteLine(storeName);
                        Console.WriteLine("Place an Order Menu\n");

                        Console.Write("Enter a Customer ID: ");
                        inputStr = Console.ReadLine();
                        isInt    = Int32.TryParse(inputStr, out custId);
                    }while (!isInt);

                    var cust = custContext.GetCustomers(custId: custId).FirstOrDefault();
                    if (cust == null)
                    {
                        Console.WriteLine($"Customer {custId} does not exist.");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Customer found:\n");
                        Console.WriteLine(cust.ToString());
                    }

                    isInt = false;
                    do
                    {
                        Console.Clear();
                        Console.WriteLine(storeName);
                        Console.WriteLine("Place an Order Menu\n");

                        Console.WriteLine($"Enter a Customer ID: {custId}");
                        Console.WriteLine("Customer found:\n");
                        Console.WriteLine(cust.ToString());

                        Console.Write("Enter a Location ID: ");
                        inputStr = Console.ReadLine();
                        isInt    = Int32.TryParse(inputStr, out locId);
                    }while (!isInt);

                    var loc = locContext.GetLocations(locId).FirstOrDefault();
                    if (loc == null)
                    {
                        Console.WriteLine($"Location {locId} does not exist.");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Location found:\n");
                        Console.WriteLine(loc.ToString());
                    }
                    bool abort = false;
                    do
                    {
                        Console.Write("Would you like to continue? (YES/NO): ");
                        string answer = Console.ReadLine();
                        if (answer.ToUpper() == "YES" || answer.ToUpper() == "Y")
                        {
                            break;
                        }
                        else if (answer.ToUpper() == "NO" || answer.ToUpper() == "N")
                        {
                            abort = true;
                        }
                    }while (!abort);

                    if (!abort)
                    {
                        try
                        {
                            var ord = new dom.Order(cust, loc, 0);
                            ordContext.AddOrder(ord);
                            ordContext.Save();
                            ord = ordContext.GetOrdersByCustomer(cust.CustID).Last();


                            int prodId = 0;

                            bool done = false;
                            do
                            {
                                do
                                {
                                    prodId = 0;

                                    Console.Clear();
                                    Console.WriteLine(storeName);
                                    Console.WriteLine("Place an Order Menu\n");

                                    /*Console.WriteLine($"Customer:\n{cust.ToString()}");
                                     * Console.WriteLine($"Location:\n{loc.ToString()}");
                                     * Console.WriteLine();*/
                                    Console.WriteLine(".____________________.");
                                    Console.WriteLine("| Location Inventory |");
                                    Console.WriteLine("|____________________|");
                                    Console.WriteLine(loc.InventoryToString());
                                    Console.WriteLine();
                                    Console.WriteLine("._____________.");
                                    Console.WriteLine("| Your basket |");
                                    Console.WriteLine("|_____________|");
                                    Console.WriteLine(ord.BasketToString());

                                    Console.Write("\n\nEnter a Product Id, or DONE if finished: ");
                                    inputStr = Console.ReadLine();
                                    if (inputStr.ToUpper() == "DONE")
                                    {
                                        done  = true;
                                        isInt = true;
                                    }
                                    else
                                    {
                                        isInt = Int32.TryParse(inputStr, out prodId);
                                    }
                                }while (!isInt);
                                if (!done)
                                {
                                    var prod = prodContext.GetProducts(prodId).FirstOrDefault();
                                    if (prod == null)
                                    {
                                        Console.WriteLine($"Product {prodId} does not exist");
                                        Console.WriteLine("\nPress any key to continue.");
                                        Console.ReadKey();
                                    }
                                    else if (!loc.FindItemById(prodId))
                                    {
                                        Console.WriteLine($"Product {prodId} is not in this location's inventory");
                                        Console.WriteLine("\nPress any key to continue.");
                                        Console.ReadKey();
                                    }
                                    else
                                    {
                                        bool addRemoveTF = true;
                                        do
                                        {
                                            Console.Write("Do you want to add or remove this Product to/from the basket? (ADD, REMOVE) ");
                                            string addOrRemove = Console.ReadLine();
                                            if (addOrRemove.ToUpper() == "ADD")
                                            {
                                                addRemoveTF = true;
                                                break;
                                            }
                                            if (addOrRemove.ToUpper() == "REMOVE")
                                            {
                                                addRemoveTF = false;
                                                break;
                                            }
                                        }while (true);

                                        bool isIntQuantity = false;
                                        int  quantity      = 0;
                                        do
                                        {
                                            Console.Write("Enter a quantity: ");
                                            inputStr      = Console.ReadLine();
                                            isIntQuantity = Int32.TryParse(inputStr, out quantity);
                                        }while (!isIntQuantity);

                                        if (addRemoveTF == true)
                                        {
                                            if (loc.AdjustQuantity(prod, -1 * quantity))
                                            {
                                                ord.basket.Add(prod, quantity);
                                                Console.WriteLine($"Added {quantity} {prod.ProductName}s to basket.");
                                                Console.WriteLine("\nPress any key to continue.");
                                                Console.ReadKey();
                                            }
                                        }
                                        else// if(addRemoveTF == false)
                                        {
                                            if (ord.AdjustQuantity(prod, -1 * quantity))
                                            {
                                                dom.Product prodInInv = null;
                                                foreach (KeyValuePair <dom.Product, int> item in loc.inventory)
                                                {
                                                    if (item.Key.ProductID == prod.ProductID)
                                                    {
                                                        prodInInv = item.Key;
                                                    }
                                                }
                                                if (prodInInv == null)
                                                {
                                                    Console.WriteLine("Something unexpected went wrong.");
                                                }
                                                else
                                                {
                                                    loc.inventory[prodInInv] += quantity;
                                                    Console.WriteLine($"Removed {quantity} {prod.ProductName}s from basket.");
                                                }

                                                Console.WriteLine("\nPress any key to continue.");
                                                Console.ReadKey();
                                            }
                                        }
                                    }
                                }
                            }while (!done);
                            ordContext.AddBasket(ord);
                            ordContext.Save();

                            locContext.UpdateInventory(loc);
                            locContext.Save();

                            Console.Clear();
                            Console.WriteLine(storeName);
                            Console.WriteLine($"Order Complete.\n");
                            Console.WriteLine(ord.ToString());
                            Console.WriteLine(ord.BasketToString());
                        }
                        catch (ArgumentNullException ex)
                        {
                            Console.WriteLine(ex.Message);
                            s_logger.Info(ex);
                        }
                    }
                    Console.WriteLine("\nPress any key to continue.");
                    Console.ReadKey();
                }
                else if (input == "4")
                {
                    try
                    {
                        var customers = custContext.GetCustomers().ToList();
                        await Serialize.JsonToFileAsync(@"C:\revature\greg-project0\json\custData.json", customers);

                        Console.WriteLine("Customers successfully saved.");

                        var products = prodContext.GetProducts().ToList();
                        await Serialize.JsonToFileAsync(@"C:\revature\greg-project0\json\prodData.json", products);

                        Console.WriteLine("Products successfully saved.");

                        var locations = locContext.GetLocations().ToList();
                        await Serialize.JsonToFileAsync(@"C:\revature\greg-project0\json\locData.json", locations);

                        Console.WriteLine("Locations successfully saved.");

                        var orders = ordContext.GetOrders().ToList();
                        await Serialize.JsonToFileAsync(@"C:\revature\greg-project0\json\ordData.json", orders);

                        Console.WriteLine("Orders successfully saved.");

                        Console.WriteLine("\nPress any key to continue.");
                        Console.ReadKey();
                    }
                    catch (IOException ex)
                    {
                        Console.WriteLine($"Error while saving: {ex.Message}");
                        s_logger.Error(ex, "Error while saving.");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Unknown Error while saving: {ex.Message}");
                        s_logger.Error(ex, "Unknown Error while saving.");
                    }
                }
                else if (input == "5")
                {
                    Console.WriteLine("Closing application...");
                    Console.WriteLine("\nPress any key to continue.");
                    Console.ReadKey();
                    break;
                }
            }
        }