/**************************************
         * ORDER FUNCTIONS BELOW
         * *************************************/

        ///<summary>
        ///This option takes a DB context and an order object and inserts it into the DB
        ///</summary>
        public void AddOrder(Order order)
        {
            //make an EF-Friendly Orders object
            Orders orders = new Orders();

            orders.CustomerId = order.CustomerID;
            orders.LocationId = order.LocationID;
            orders.OrderId    = order.OrderID;

            foreach (var item in order.itemsOrdered)
            {
                //make ProductsFromOrder object for each product
                ProductsFromOrder prodsInsert = new ProductsFromOrder();
                prodsInsert.OrderId   = order.OrderID;
                prodsInsert.Quantity  = item.Value;
                prodsInsert.ProductId = _dbContext.Products
                                        .Where(x => x.ProductName == item.Key)
                                        .Select(x => x.ProductId)
                                        .First();

                orders.ProductsFromOrder.Add(prodsInsert);
            }
            _dbContext.Add(orders);
            _dbContext.SaveChanges();

            var success = DecreaseInventory(order);

            if (success == false)
            {
                throw new Exception("There was an error updating the DB. Don't worry. Your order was placed successfully.");
            }
        }
        public Customer AddCustomer()
        {
            var cust1 = new Customer();

            try
            {
                Console.Write("What is your first name: ");
                cust1.FirstName = Console.ReadLine();
                Console.WriteLine();
                Console.Write("What is your last name: ");
                cust1.LastName = Console.ReadLine();
                Console.WriteLine();
                UserNamePass();
            }
            catch (FormatException)
            {
                Console.WriteLine("You didn't the connect type");
                AddCustomer();
            }

            ctx.Add(cust1);
            try
            {
                ctx.SaveChanges();
                Console.WriteLine("Added to Database.");
            }
            catch (Exception)
            {
                Console.WriteLine("Save didn't work.");
            }

            return(cust1);
        }
        public void AddCustomer(CustomerImp _customer)
        {
            var value = Mapper.Map(_customer);

            _db.Add(value);
            _db.SaveChanges();
            _customer.Id = value.CustomerId;
        }
        public void AddOrder(OrderImp order)
        {
            var value = Mapper.Map(order);

            _db.Add(value);
            _db.SaveChanges();
            order.OrderID = value.OrderId;
        }
Example #5
0
        public void AddStore(StoreImp store)
        {
            var value = Mapper.Map(store);

            _db.Add(value);
            _db.SaveChanges();
            store.IDNumber = value.StoreId;
        }
Example #6
0
        public void AddGame(GamesImp game)
        {
            var value = Mapper.Map(game);

            _db.Add(value);
            _db.SaveChanges();
            game.Id = value.GameId;
        }
        public void Create(Library.Customer customer)
        {
            var cust = new Customer()
            {
                FirstName = customer.FirstName, LastName = customer.LastName
            };

            _context.Add(cust);
            _context.SaveChanges();
        }
Example #8
0
        public void Create(Library.Location location)
        {
            var loc = new Location()
            {
                Name = location.Name
            };

            _context.Add(loc);
            _context.SaveChanges();
        }
Example #9
0
        public void Add(Library.Product product)
        {
            Product dbProduct = new Product()
            {
                Name        = product.Name,
                Description = product.Description,
                Price       = product.Price,
                OrderLimit  = product.OrderLimit
            };

            _context.Add(dbProduct);
            _context.SaveChanges();
        }
        /// <summary>
        /// adds a new Customer to the data base
        /// </summary>
        /// <param name="customer">Customer object to add to the database</param>
        public void AddCustomer(Customer customer)
        {
            IEnumerable <Customer> c = GetCustomers(firstName: customer.FirstName, lastName: customer.LastName);

            if (c.Count() != 0)
            {
                throw new ArgumentException("Customer allready exists in database");
            }

            if (customer.Id != 0)
            {
                Log.Warning("Customer allready exist in database allreay exists", customer.Id);
                throw new ArgumentException("Customer allready exists in database");
            }
            else
            {
                Customers entity = Mapper.MapCustomerToOrders(customer);
                _context.Add(entity);

                Log.Information("Added {FirstName} {LastName} to database", customer.FirstName, customer.LastName);
            }
        }
Example #11
0
        public void AddCigar(Library.Models.Cigar cigar)
        {
            if (cigar.Id != 0)
            {
                _logger.LogWarning("Cigar to be added has an ID ({cigarId}) already: ignoring.", cigar.Id);
            }

            _logger.LogInformation($"Adding cigar");

            Cigar entity = Mapper.Map(cigar);

            entity.Id = 0;
            _dbContext.Add(entity);
        }
Example #12
0
 /// <summary>
 /// Add a location to the list of locations
 /// </summary>
 /// <param name="location">The location to be added</param>
 public void AddCustomer(string firstName, string lastName)
 {
     // get the context of the db
     using var context = new Project0Context(_dbContext);
     // create a new customer from the DatabaseModel
     if (firstName.Length > 0 && lastName.Length > 0)
     {
         DatabaseModels.Customer cust = new DatabaseModels.Customer()
         {
             FirstName = firstName,
             LastName  = lastName
         };
         //add customer to context and save it to DB
         context.Add(cust);
         context.SaveChanges();
     }
 }
        /// <summary>
        /// Add a location to the list of locations
        /// </summary>
        /// <param name="location">The location to be added</param>
        public void AddLocation(string name)
        {
            // get the context of the db
            using var context = new Project0Context(_dbContext);

            if (name.Length > 0)
            {
                // create the db model to add
                DatabaseModels.Location location = new DatabaseModels.Location()
                {
                    Name = name
                };

                //add location to context and save
                context.Add(location);
                context.SaveChanges();
            }
        }
Example #14
0
        public void InsertCustomer()
        {
            Console.Write("\nEnter Customer's:\nFirst Name: ");
            string FirstName = Console.ReadLine();

            Console.Write("\nLast Name: ");
            string LastName = Console.ReadLine();

            Console.Write("\nStreet Address (no city/state/zip): ");
            string StreetAddress = Console.ReadLine();

            Console.Write("\nCity: ");
            string City = Console.ReadLine();

            Console.Write("\nState:");
            string State = Console.ReadLine();

            Console.Write("\nZip Code: ");
            int Zip = Int32.Parse(Console.ReadLine());

            Console.Write("\nE-mail Address: ");
            string email = Console.ReadLine();

            Console.Write("\nPhone Number: ");
            string phone = Console.ReadLine();

            using (var context = new Project0Context(Options))
            {
                var _tempCustomer = new Customers();
                _tempCustomer.FirstName     = FirstName;
                _tempCustomer.LastName      = LastName;
                _tempCustomer.StreetAddress = StreetAddress;
                _tempCustomer.City          = City;
                _tempCustomer.State         = State;
                _tempCustomer.Zip           = Zip;
                _tempCustomer.Email         = email;
                _tempCustomer.Phone         = phone;

                context.Add(_tempCustomer);
                context.SaveChanges();
            }
            //throw new NotImplementedException();
        }
        static void Main()
        {
            using var ctx = new Project0Context();
            var current = HaveYouBeenHereBefore();

            Console.WriteLine($"Weclome {current.FirstName}");
            var start = new StartApp();

            start.MainMenu();
            var    OrderMain = new OrderHelp();
            string mainOrder = OrderMain.CustomerOrder();

            var customerTotal = ctx.Product.FirstOrDefault(m => m.ProductName == mainOrder);


            var order = new CustomerOrder
            {
                StoreId    = 2,
                OrderDate  = DateTime.Now.Date,
                Total      = customerTotal.Price,
                CustomerId = current.Id,
            };

            ctx.Add(order);
            try
            {
                ctx.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine("Your order is added.");
            }
        }
        public void Create(int customerId, int locationId, List <Library.Sale> sales)
        {
            decimal total = sales.Sum(s => s.PurchasePrice * s.SaleQuantity);
            // convert sales to db model
            var dbSales = sales.Select(s => new Sale()
            {
                ProductId     = s.ProductId,
                ProductName   = s.ProductName,
                PurchasePrice = s.PurchasePrice,
                Quantity      = s.SaleQuantity
            }).ToList();

            var dbOrder = new Order()
            {
                CustomerId = customerId,
                LocationId = locationId,
                Date       = DateTime.Now,
                OrderTotal = total,
                Sales      = dbSales
            };

            _context.Add(dbOrder);
            _context.SaveChanges();
        }
Example #17
0
        ///<summary>
        ///This option not required at this time
        ///</summary>
        //public static List<Customer> ReadAllCustomers()
        //{
        //    return customers;
        //}

        ///<summary>
        ///This option not required at this time
        ///</summary>
        //public static List<Customer> UpdateCustomer(List<Customer> Customers, Customer customer)
        //{
        //    return Customers;
        //}

        ///<summary>
        ///This option not required at this time
        ///</summary>
        //public static List<Customer> DeleteCustomer(List<Customer> Customers, Customer customer)
        //{
        //    //Customers.Find(customer);//find out how to find and delete from a list.
        //    return Customers;
        //}

        /**************************************
         * ORDER FUNCTIONS BELOW
         * *************************************/

        ///<summary>
        ///This option takes a DB context and an order object and inserts it into the DB
        ///</summary>
        public static void AddOrder(Project0Context context, Order order)
        {
            Orders orders = new Orders();

            orders.CustomerId = order.CustomerID;
            orders.LocationId = order.LocationID;
            orders.OrderId    = order.OrderID;

            foreach (var item in order.itemsOrdered)
            {
                //make ProductsFromOrder object for each product
                ProductsFromOrder prodsInsert = new ProductsFromOrder();
                prodsInsert.OrderId   = order.OrderID;
                prodsInsert.Quantity  = item.Value;
                prodsInsert.ProductId = context.Products
                                        .Where(x => x.ProductName == item.Key)
                                        .Select(x => x.ProductId)
                                        .First();

                orders.ProductsFromOrder.Add(prodsInsert);
            }
            context.Add(orders);
            context.SaveChanges();
        }
Example #18
0
 public void AddOrder(OrderImp order)
 {
     _db.Add(Mapper.Map(order));
     _db.SaveChanges();
 }
Example #19
0
        #pragma warning restore CS0618 // Type or member is obsolete
        static void Main(string[] args)
        {
            var      Repo      = new FrameworkRepo();
            string   curr_name = "";
            string   curr_email;
            int      curr_cart;
            DateTime newt = DateTime.Now;


            var optionsBuilder = new DbContextOptionsBuilder <Project0Context>();

            optionsBuilder.UseSqlServer(Secret.ConnectionString);
            //optionsBuilder.UseLoggerFactory(AppLoggerFactory); This works but spams the console with data.
            var options = optionsBuilder.Options;

            Console.WriteLine("Welcome to Comic League United the 7th largest comic supply store in the tri-state area.");
            Console.WriteLine("Please login in order make a new customer. ");
            Console.WriteLine("Please enter Customer name now. ");
            curr_name = Console.ReadLine();
            Console.WriteLine("Please enter Customer email now. ");
            curr_email = Console.ReadLine();

            using (var dbContext = new Project0Context(options))
            {
                var store = dbContext.Customer.FirstOrDefault(x => x.Name == curr_name || x.Email == curr_email);
                if (store == null)
                {
                    Console.WriteLine("Please enter your store location now. ");
                    string temp = Console.ReadLine();
                    Repo.AddCustomer(dbContext, curr_name, curr_email, temp);
                    dbContext.SaveChanges();
                    Console.WriteLine("Welcome New Customer. ");
                }
                else
                {
                    Console.WriteLine("Welcome Back " + curr_name);
                }
                var cart = new Orders();
                cart.CustomerId = store.CustomerId;
                cart.OrderTime  = newt;
                dbContext.Add(cart);
                dbContext.SaveChanges();
                curr_cart = cart.OrdersId;
                Console.ReadKey();
            }

            while (true)
            {
                Repo.MainMenu();
                string choice = "11";
                string temp   = "";


                try
                {
                    choice = Console.ReadLine();
                    Console.Clear();
                    if (choice == "0")
                    {
                        break;
                    }
                    else if (choice == "1")
                    {
                        choice = "11";
                        Console.WriteLine("1: Add a Store");
                        Console.WriteLine("2: Delete a Store");
                        Console.WriteLine("3: Update a Store");
                        choice = Console.ReadLine();
                        Console.Clear();
                        if (choice == "1")
                        {
                            Console.WriteLine("Please enter a Store Name to add");
                            temp = Console.ReadLine();
                            using (var dbContext = new Project0Context(options))
                            {
                                Repo.AddStore(dbContext, temp);
                            }
                        }
                        else if (choice == "2")
                        {
                            Console.WriteLine("Please enter a Store Name to delete");
                            temp = Console.ReadLine();
                            using (var dbContext = new Project0Context(options))
                            {
                                Repo.DeleteStore(dbContext, temp);
                            }
                        }
                        else if (choice == "3")
                        {
                            Console.WriteLine("Please enter a Store Name to update");
                            temp = Console.ReadLine();
                            Console.WriteLine("Please enter the new name. ");
                            string temp2 = Console.ReadLine();
                            using (var dbContext = new Project0Context(options))
                            {
                                Repo.UpdateStore(dbContext, temp, temp2);
                            }
                        }
                        else
                        {
                            throw new ArgumentException("Please pick a valid option. ");
                        }
                    }
                    else if (choice == "2")
                    {
                        choice = "11";
                        Console.WriteLine("1: Show All Stores. ");
                        Console.WriteLine("2: Show One Store. ");
                        choice = Console.ReadLine();
                        Console.Clear();
                        if (choice == "1")
                        {
                            using (var dbContext = new Project0Context(options))
                            {
                                Repo.ShowStores(dbContext);
                                Console.ReadKey();
                            }
                        }
                        else if (choice == "2")
                        {
                            Console.WriteLine("Please enter the store name. ");
                            temp = Console.ReadLine();
                            using (var dbContext = new Project0Context(options))
                            {
                                Repo.ShowStores(dbContext, temp);
                                Console.ReadKey();
                            }
                        }
                        else
                        {
                            throw new ArgumentException("Please pick a valid option. ");
                        }
                    }
                    else if (choice == "3")
                    {
                        choice = "11";
                        Console.WriteLine("1: Add a Product");
                        Console.WriteLine("2: Delete a Product");
                        Console.WriteLine("3: Update a Product");
                        choice = Console.ReadLine();
                        Console.Clear();
                        if (choice == "1")
                        {
                            string  placeholder;
                            int     inv   = 1;
                            decimal price = 5.00m;
                            int     id;
                            Console.WriteLine("Please enter a Product Name.");
                            placeholder = Console.ReadLine();
                            Console.WriteLine("Please enter the number of products in inventory.");
                            temp = Console.ReadLine();
                            int.TryParse(temp, out inv);
                            Console.WriteLine("Please enter the price of the product.");
                            temp = Console.ReadLine();
                            decimal.TryParse(temp, out price);
                            Console.WriteLine("Please enter a Store ID to add the Product.");
                            temp = Console.ReadLine();
                            int.TryParse(temp, out id);
                            if (id == 2)
                            {
                                id = 6;
                            }
                            if (id == 3)
                            {
                                id = 11;
                            }

                            using (var dbContext = new Project0Context(options))
                            {
                                Repo.AddProduct(dbContext, placeholder, price, inv, id);
                            }
                        }
                        else if (choice == "2")
                        {
                            Console.WriteLine("Please enter a product Name to delete");
                            temp = Console.ReadLine();
                            using (var dbContext = new Project0Context(options))
                            {
                                Repo.DeleteProduct(dbContext, temp);
                            }
                        }
                        else if (choice == "3")
                        {
                            string  placeholder;
                            string  old;
                            int     inv   = 1;
                            decimal price = 5.00m;
                            int     id;
                            Console.WriteLine("Please enter the old Product Name.");
                            old = Console.ReadLine();
                            Console.WriteLine("Please enter the new Product Name.");
                            placeholder = Console.ReadLine();
                            Console.WriteLine("Please enter the number of products in inventory.");
                            temp = Console.ReadLine();
                            int.TryParse(temp, out inv);
                            Console.WriteLine("Please enter the price of the product.");
                            temp = Console.ReadLine();
                            decimal.TryParse(temp, out price);
                            Console.WriteLine("Please enter a Store ID to add the Product.");
                            temp = Console.ReadLine();
                            int.TryParse(temp, out id);


                            using (var dbContext = new Project0Context(options))
                            {
                                Repo.UpdateProduct(dbContext, placeholder, price, inv, id, old);
                            }
                        }

                        else
                        {
                            throw new ArgumentException("Please pick a valid option. ");
                        }
                    }
                    else if (choice == "4")
                    {
                        choice = "11";
                        Console.WriteLine("1: Show All Products");
                        Console.WriteLine("2: Show A Product");
                        choice = Console.ReadLine();
                        Console.Clear();
                        if (choice == "1")
                        {
                            using (var dbContext = new Project0Context(options))
                            {
                                Repo.ShowProducts(dbContext);
                                Console.ReadKey();
                            }
                        }
                        else if (choice == "2")
                        {
                            Console.WriteLine("Please enter the product name. ");
                            temp = Console.ReadLine();
                            using (var dbContext = new Project0Context(options))
                            {
                                Repo.ShowProducts(dbContext, temp);
                                Console.ReadKey();
                            }
                        }
                        else
                        {
                            throw new ArgumentException("Please pick a valid option. ");
                        }
                    }
                    else if (choice == "5")
                    {
                        choice = "11";
                        Console.WriteLine("1: Add a Customer");
                        Console.WriteLine("2: Delete a Customer");
                        Console.WriteLine("3: Update a Customer");
                        choice = Console.ReadLine();
                        Console.Clear();
                        if (choice == "1")
                        {
                            string email;
                            string id;
                            Console.WriteLine("Please enter the name of the customer. ");
                            temp = Console.ReadLine();
                            Console.WriteLine("Please enter the customers email. ");
                            email = Console.ReadLine();
                            Console.WriteLine("Please enter the customers store name. ");
                            id = Console.ReadLine();
                            using (var dbContext = new Project0Context(options))
                            {
                                Repo.AddCustomer(dbContext, temp, email, id);
                            }
                        }
                        else if (choice == "2")
                        {
                            string email;
                            Console.WriteLine("Please enter the name of the customer. ");
                            temp = Console.ReadLine();
                            Console.WriteLine("Please enter the customers email. ");
                            email = Console.ReadLine();
                            using (var dbContext = new Project0Context(options))
                            {
                                Repo.DeleteCustomer(dbContext, temp, email);
                            }
                        }
                        else if (choice == "3")
                        {
                            string email, oldn, olde;
                            Console.WriteLine("Please enter the name of the customer to edit. ");
                            oldn = Console.ReadLine();
                            Console.WriteLine("Please enter the old customers email. ");
                            olde = Console.ReadLine();
                            Console.WriteLine("Please enter the new name of the customer. ");
                            temp = Console.ReadLine();
                            Console.WriteLine("Please enter the new customers email. ");
                            email = Console.ReadLine();
                            using (var dbContext = new Project0Context(options))
                            {
                                Repo.UpdateCustomer(dbContext, temp, email, oldn, olde);
                            }
                        }
                        else
                        {
                            throw new ArgumentException("Please pick a valid option. ");
                        }
                    }
                    else if (choice == "6")
                    {
                        Console.Clear();
                        using (var dbContext = new Project0Context(options))
                        {
                            Repo.ShowCustomers(dbContext);
                            Console.ReadKey();
                        }
                    }
                    else if (choice == "7")
                    {
                        choice = "11";
                        Console.WriteLine("1: Add a product to your cart");
                        Console.WriteLine("2: Delete a product from your cart");
                        Console.WriteLine("3: Checkout");
                        choice = Console.ReadLine();
                        Console.Clear();
                        if (choice == "1")
                        {
                            int    inv = 1;
                            string name;
                            Console.WriteLine("Please enter the name of the product you'd like to add. ");
                            name = Console.ReadLine();
                            Console.WriteLine("How many would you like to add. ");
                            temp = Console.ReadLine();
                            int.TryParse(temp, out inv);
                            using (var dbContext = new Project0Context(options))
                            {
                                if (name.Substring(name.Length - 4, 3) == "Set")
                                {
                                    Repo.AddSet(dbContext, name, inv, curr_cart);
                                }
                                else
                                {
                                    Repo.AddCart(dbContext, name, inv, curr_cart);
                                }
                            }
                        }
                        else if (choice == "2")
                        {
                            int inv = 1;
                            Console.WriteLine("Please enter the name of the product you'd like to remove. ");
                            temp = Console.ReadLine();
                            Console.WriteLine("How many would you like to remove. ");
                            string temper = Console.ReadLine();
                            int.TryParse(temper, out inv);
                            using (var dbContext = new Project0Context(options))
                            {
                                Repo.DeleteCart(dbContext, temp, inv, curr_cart);
                            }
                        }
                        else if (choice == "3")
                        {
                            Console.Clear();
                            decimal total = 0;

                            using (var dbContext = new Project0Context(options))
                            {
                                if (Repo.CheckCartTime(dbContext, curr_name, curr_cart, newt))
                                {
                                    var ordertotal = dbContext.Orders.First(x => x.OrdersId == curr_cart);
                                    Repo.CheckOut(dbContext, curr_name, curr_cart, out total);
                                    Console.WriteLine("Total: " + total);
                                    Console.WriteLine("Thank you for shopping with us come back soon. ");
                                    ordertotal.Total = total;
                                    Console.ReadKey();
                                    break;
                                }
                                else
                                {
                                    Console.WriteLine("Cannot checkout at this time.");
                                    Console.ReadKey();
                                }
                            }
                        }
                        else
                        {
                            throw new ArgumentException("Please pick a valid option. ");
                        }
                    }
                    else if (choice == "8")
                    {
                        Console.Clear();
                        using (var dbContext = new Project0Context(options))
                        {
                            Repo.ShowCart(dbContext, curr_name, curr_cart);
                            Console.WriteLine("Please press any key to return. ");
                            Console.ReadKey();
                        }
                    }
                    else if (choice == "9")
                    {
                        choice = "11";
                        Console.WriteLine("1: Sort by earliest");
                        Console.WriteLine("2: Sort by latest");
                        Console.WriteLine("3: Sort by cheapest");
                        Console.WriteLine("4: Sort by most expensive");
                        Console.WriteLine("5: Show Order Statistics.");
                        choice = Console.ReadLine();
                        Console.Clear();

                        using (var dbContext = new Project0Context(options))
                        {
                            if (choice == "1" || choice == "2" || choice == "3" || choice == "4")
                            {
                                Repo.ShowHistory(dbContext, curr_name, choice);
                                Console.ReadKey();
                            }
                            else if (choice == "5")
                            {
                                Repo.ShowStatistics(dbContext);
                                Console.ReadKey();
                            }
                            else
                            {
                                Console.WriteLine("Please only enter a valid option ");
                            }
                        }
                    }
                    else
                    {
                        throw new ArgumentException("Please pick a valid option. ");
                    }
                }
                catch (ArgumentException e)
                {
                    Console.Clear();
                    Console.WriteLine(e);
                    Console.ReadKey();
                }


                choice = "11";
            }
        }