public static async Task WorkingWithCombinedAlerts()
        {
            DbContextOptionsBuilder <ManufacturingContext> optionsBuilder = new DbContextOptionsBuilder <ManufacturingContext>();

            optionsBuilder.UseSqlServer("server=172.27.192.1;database=manufacturing_inventory;user=aelmendorf;password=Drizzle123!;MultipleActiveResultSets=true");
            using var context = new ManufacturingContext(optionsBuilder.Options);

            var user = context.Users
                       .Include(e => e.Sessions)
                       .ThenInclude(e => e.Transactions)
                       .Include(e => e.Permission)
                       .FirstOrDefault(e => e.FirstName == "Andrew");
            UserService userService = new UserService();

            if (user != null)
            {
                Session session = new Session(user);
                await context.Sessions.AddAsync(session);

                await context.SaveChangesAsync();

                userService.CurrentUserName    = user.UserName;
                userService.CurrentSessionId   = session.Id;
                userService.UserPermissionName = user.Permission.Name;
            }
            var stockType = await context.Categories.OfType <StockType>().Include(e => e.PartInstances).ThenInclude(e => e.BubblerParameter).FirstOrDefaultAsync(e => e.Id == 16);

            if (stockType != null)
            {
                CombinedAlert tmaAlert  = new CombinedAlert();
                UserAlert     userAlert = new UserAlert();
                userAlert.IsEnabled  = true;
                tmaAlert.StockHolder = stockType;
                userAlert.Alert      = tmaAlert;
                userAlert.User       = user;
                var added = await context.AddAsync(userAlert);

                if (added != null)
                {
                    await context.SaveChangesAsync();

                    Console.WriteLine("Should be saved");
                }
                else
                {
                    Console.WriteLine("Could Not Save UserAlert");
                }
            }
            else
            {
                Console.WriteLine("StockType is null");
            }
        }
        public static async Task WorkingWithIndividualAlerts()
        {
            DbContextOptionsBuilder <ManufacturingContext> optionsBuilder = new DbContextOptionsBuilder <ManufacturingContext>();

            optionsBuilder.UseSqlServer("server=172.27.192.1;database=manufacturing_inventory;user=aelmendorf;password=Drizzle123!;MultipleActiveResultSets=true");
            using var context = new ManufacturingContext(optionsBuilder.Options);

            var user = context.Users
                       .Include(e => e.Sessions)
                       .ThenInclude(e => e.Transactions)
                       .Include(e => e.Permission)
                       .FirstOrDefault(e => e.FirstName == "Andrew");
            UserService userService = new UserService();

            if (user != null)
            {
                Session session = new Session(user);
                await context.Sessions.AddAsync(session);

                await context.SaveChangesAsync();

                userService.CurrentUserName    = user.UserName;
                userService.CurrentSessionId   = session.Id;
                userService.UserPermissionName = user.Permission.Name;
            }
            var partInstance = await context.PartInstances.Include(e => e.BubblerParameter).Include(e => e.IndividualAlert).FirstOrDefaultAsync(e => e.Id == 1);

            if (partInstance != null)
            {
                IndividualAlert alert = new IndividualAlert();
                alert.PartInstance = partInstance;
                UserAlert userAlert = new UserAlert();
                userAlert.Alert = alert;
                userAlert.User  = user;
                var added = await context.AddAsync(userAlert);

                if (added != null)
                {
                    await context.SaveChangesAsync();

                    Console.WriteLine("Should be saved");
                }
                else
                {
                    Console.WriteLine("Failed to add Alert");
                }
            }
            else
            {
                Console.WriteLine("PartInstance Not Found");
            }
        }
        public static async Task CreatingUsers()
        {
            DbContextOptionsBuilder <ManufacturingContext> optionsBuilder = new DbContextOptionsBuilder <ManufacturingContext>();

            optionsBuilder.UseSqlServer("server=192.168.0.5;database=manufacturing_inventory_dev;user=aelmendorf;password=Drizzle123!;MultipleActiveResultSets=true");
            using var context = new ManufacturingContext(optionsBuilder.Options);
            Console.WriteLine("Creating Users");
            User user1 = new User();

            user1.UserName     = "******";
            user1.PermissionId = 1;

            User user2 = new User();

            user2.UserName     = "******";
            user2.PermissionId = 1;

            User user3 = new User();

            user3.UserName     = "******";
            user3.PermissionId = 1;

            context.Users.Add(user1);
            context.Users.Add(user2);
            context.Users.Add(user3);
            await context.SaveChangesAsync();

            Console.WriteLine("Should be created");
        }
        public static async Task DeleteAll()
        {
            DbContextOptionsBuilder <ManufacturingContext> optionsBuilder = new DbContextOptionsBuilder <ManufacturingContext>();

            optionsBuilder.UseSqlServer("server=172.20.4.20;database=manufacturing_inventory_dev;user=aelmendorf;password=Drizzle123!;MultipleActiveResultSets=true");
            using var context = new ManufacturingContext(optionsBuilder.Options);
            IRepository <PartInstance> instanceRepository = new PartInstanceRepository(context);
            IRepository <Part>         partRepository     = new PartRepository(context);

            await partRepository.LoadAsync();

            var parts = await context.Parts.Include(e => e.PartInstances).Where(e => e.Id > 1).ToListAsync();

            List <int> instanceIds = new List <int>();

            foreach (var part in parts)
            {
                var ids = part.PartInstances.Select(e => e.Id);
                instanceIds.AddRange(ids);
            }
            await instanceRepository.LoadAsync();

            Console.WriteLine("Attempting to delete id list, see log below");
            foreach (var id in instanceIds)
            {
                var partInstance = await instanceRepository.GetEntityAsync(e => e.Id == id);

                if (partInstance != null)
                {
                    var output = await instanceRepository.DeleteAsync(partInstance);

                    if (output != null)
                    {
                        var count = await context.SaveChangesAsync();

                        if (count > 0)
                        {
                            Console.WriteLine("Successfully Deleted: Id:{0} Name:{1}", partInstance.Id, partInstance.Name);
                        }
                        else
                        {
                            Console.WriteLine("Error Saving Id:{0} Name:{1}", partInstance.Id, partInstance.Name);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Error Deleting: Id:{0} Name:{1}", partInstance.Id, partInstance.Name);
                    }
                }
                else
                {
                    Console.WriteLine("Could Not Find:  Id:{0} ", id);
                }
            }
        }
        public static async Task DeletingAlerts()
        {
            DbContextOptionsBuilder <ManufacturingContext> optionsBuilder = new DbContextOptionsBuilder <ManufacturingContext>();

            optionsBuilder.UseSqlServer("server=172.27.192.1;database=manufacturing_inventory;user=aelmendorf;password=Drizzle123!;MultipleActiveResultSets=true");
            using var context = new ManufacturingContext(optionsBuilder.Options);
            //var alert =await context.Alerts.OfType<IndividualAlert>().Include(e=>e.UserAlerts).ThenInclude(e=>e.User).Include(e=>e.PartInstance).FirstOrDefaultAsync(e => e.Id == 3);

            var user = context.Users
                       .Include(e => e.Sessions)
                       .ThenInclude(e => e.Transactions)
                       .Include(e => e.Permission)
                       .Include(e => e.UserAlerts)
                       .ThenInclude(e => e.Alert)
                       .FirstOrDefault(e => e.FirstName == "Andrew");
            UserService userService = new UserService();

            if (user != null)
            {
                Session session = new Session(user);
                await context.Sessions.AddAsync(session);

                await context.SaveChangesAsync();

                userService.CurrentUserName    = user.UserName;
                userService.CurrentSessionId   = session.Id;
                userService.UserPermissionName = user.Permission.Name;
                Console.WriteLine("User found and Object created");
                var alert = await context.UserAlerts.FirstOrDefaultAsync(e => e.UserId == user.Id && e.AlertId == 3);

                if (alert != null)
                {
                    await context.SaveChangesAsync();

                    Console.WriteLine("Done?");
                }
                else
                {
                    Console.WriteLine("Could not find alert");
                }
            }
        }
        public static async Task DeleteUserAlerts()
        {
            DbContextOptionsBuilder <ManufacturingContext> optionsBuilder = new DbContextOptionsBuilder <ManufacturingContext>();

            optionsBuilder.UseSqlServer("server=192.168.0.5;database=manufacturing_inventory_dev;user=aelmendorf;password=Drizzle123!;MultipleActiveResultSets=true");
            using var context = new ManufacturingContext(optionsBuilder.Options);
            Console.WriteLine("Deleting User Alerts, Please Wait...");

            var userAlerts = context.UserAlerts.Include(e => e.User).Include(e => e.Alert).Where(e => e.UserId == 1);

            foreach (var userAlert in userAlerts)
            {
                Console.WriteLine("User: {0} Alert: {1}", userAlert.User.UserName, userAlert.AlertId);
            }

            context.RemoveRange(userAlerts);
            await context.SaveChangesAsync();

            Console.WriteLine("Should be cleared");
        }
        public static async Task ResetStockTypeQuantity()
        {
            DbContextOptionsBuilder <ManufacturingContext> optionsBuilder = new DbContextOptionsBuilder <ManufacturingContext>();

            optionsBuilder.UseSqlServer("server=172.27.192.1;database=manufacturing_inventory;user=aelmendorf;password=Drizzle123!;MultipleActiveResultSets=true");
            using var context = new ManufacturingContext(optionsBuilder.Options);
            var tma = await context.Categories.OfType <StockType>().Include(e => e.PartInstances).ThenInclude(e => e.BubblerParameter).FirstOrDefaultAsync(e => e.Id == 17);

            foreach (var instance in tma.PartInstances)
            {
                Console.WriteLine("PartInstance: {0} Quantity", instance.BubblerParameter.Weight);
            }
            tma.Quantity  = 0;
            tma.Quantity += (int)tma.PartInstances.Sum(instance => instance.BubblerParameter.Weight);
            Console.WriteLine();
            Console.WriteLine("New Quantity: {0}", tma.Quantity);
            context.Update(tma);
            await context.SaveChangesAsync();

            Console.WriteLine("Should be done");
        }
        public static async Task DeleteOldAlerts()
        {
            DbContextOptionsBuilder <ManufacturingContext> optionsBuilder = new DbContextOptionsBuilder <ManufacturingContext>();

            optionsBuilder.UseSqlServer("server=172.20.4.20;database=manufacturing_inventory_dev;user=aelmendorf;password=Drizzle123!;MultipleActiveResultSets=true");
            using var context = new ManufacturingContext(optionsBuilder.Options);
            await context.Alerts.OfType <IndividualAlert>().Include(e => e.PartInstance).ForEachAsync(alert => {
                if (alert.PartInstance != null)
                {
                    Console.WriteLine("Removing {0} alert id: {1}", alert.PartInstance.Name, alert.Id);
                }
                else
                {
                    Console.WriteLine("Should be combined, trying to delete id: {0}", alert.Id);
                }
                context.Remove(alert);
            });

            await context.SaveChangesAsync();

            Console.WriteLine("Should be done");
        }
        public static async Task TestingUserAlerts(int userId, int alertId)
        {
            DbContextOptionsBuilder <ManufacturingContext> optionsBuilder = new DbContextOptionsBuilder <ManufacturingContext>();

            optionsBuilder.UseSqlServer("server=192.168.0.5;database=manufacturing_inventory_dev;user=aelmendorf;password=Drizzle123!;MultipleActiveResultSets=true");
            using var context = new ManufacturingContext(optionsBuilder.Options);
            Console.WriteLine("Creating User Alerts, Please Wait...");
            var user1 = await context.Users.AsNoTracking().Include(e => e.UserAlerts).FirstOrDefaultAsync(e => e.Id == userId);

            var alert2 = await context.Alerts.AsNoTracking().OfType <CombinedAlert>().FirstOrDefaultAsync(e => e.Id == alertId);

            UserAlert userAlert1 = new UserAlert();

            userAlert1.AlertId   = alert2.Id;
            userAlert1.UserId    = user1.Id;
            userAlert1.IsEnabled = true;

            context.UserAlerts.Add(userAlert1);

            await context.SaveChangesAsync();

            Console.WriteLine("UserAlert {0},{1} Created", userId, alertId);
        }
        public static async Task ChangeStockTypeThenAlert()
        {
            DbContextOptionsBuilder <ManufacturingContext> optionsBuilder = new DbContextOptionsBuilder <ManufacturingContext>();

            optionsBuilder.UseSqlServer("server=172.27.192.1;database=manufacturing_inventory;user=aelmendorf;password=Drizzle123!;MultipleActiveResultSets=true");
            using var context = new ManufacturingContext(optionsBuilder.Options);
            var user = context.Users
                       .Include(e => e.Sessions)
                       .ThenInclude(e => e.Transactions)
                       .Include(e => e.Permission)
                       .FirstOrDefault(e => e.FirstName == "Andrew");
            UserService userService = new UserService();

            if (user != null)
            {
                Session session = new Session(user);
                context.Sessions.Add(session);
                context.SaveChanges();
                userService.CurrentUserName    = user.UserName;
                userService.CurrentSessionId   = session.Id;
                userService.UserPermissionName = user.Permission.Name;
            }
            var partInstance = await context.PartInstances.Include(e => e.IndividualAlert).Include(e => e.StockType).ThenInclude(e => e.CombinedAlert).FirstOrDefaultAsync(e => e.Id == 66);

            var newStockType = await context.Categories.OfType <StockType>().Include(e => e.CombinedAlert).ThenInclude(e => e.UserAlerts).FirstOrDefaultAsync(e => e.Id == 14);

            if (partInstance != null && newStockType != null)
            {
                if (newStockType.IsDefault)
                {
                    if (!partInstance.StockType.IsDefault)
                    {
                        //from combinded to individual
                        Console.WriteLine("Combined to Individual");
                        IndividualAlert alert = new IndividualAlert();
                        alert.PartInstance           = partInstance;
                        partInstance.IndividualAlert = alert;
                        context.Add(alert);
                        partInstance.StockType = newStockType;
                        context.Update(partInstance);
                        await context.SaveChangesAsync();

                        Console.WriteLine("Case one should be done");
                    }
                    else
                    {
                        //from individual to individual.  Should never be here
                        Console.WriteLine("You should not be here");
                    }
                }
                else
                {
                    if (partInstance.StockType.IsDefault)
                    {
                        if (partInstance.IndividualAlert != null)
                        {
                            //from individual to combined
                            var userAlerts = context.UserAlerts.Where(e => e.AlertId == partInstance.IndividualAlertId);
                            context.RemoveRange(userAlerts);
                            var deleted = context.Alerts.Remove(partInstance.IndividualAlert);
                            partInstance.IndividualAlert = null;
                            partInstance.StockType       = newStockType;
                            newStockType.Quantity       += partInstance.Quantity;
                            context.Update(newStockType);
                            await context.SaveChangesAsync();

                            Console.WriteLine("Should be done");
                        }
                        else
                        {
                            Console.WriteLine("You should not be here");
                        }
                    }
                    else
                    {
                        //from combined to another combined
                        Console.WriteLine("Combined to Combined");
                        var oldStock = context.Entry <StockType>(partInstance.StockType).Entity;
                        oldStock.Quantity -= partInstance.Quantity;
                        var okay = oldStock.PartInstances.Remove(partInstance);
                        partInstance.StockType = newStockType;
                        newStockType.PartInstances.Add(partInstance);
                        newStockType.Quantity += partInstance.Quantity;

                        context.Update(newStockType);
                        context.Update(oldStock);
                        context.Update(partInstance);
                        await context.SaveChangesAsync();

                        Console.WriteLine("Should be finished");
                    }
                }
                Console.WriteLine("Done, Press any key to exit");
            }
            else
            {
                Console.WriteLine("PartInstance not found");
            }
        }