public IActionResult Create(Equipment equipment)
        {
            try {
                // If Owner doesn't exists
                if (equipment.IDCheck)
                {
                    return(Json(false));
                }

                // If Owner was chosen in dropdown
                if (equipment.Owner.ID != -1)
                {
                    equipment.OwnerID = equipment.Owner.ID;
                    equipment.Owner   = null;
                }
                else if (equipment.Owner.ID == -1)
                {
                    equipment.Owner = null;
                }

                if (ModelState.IsValid)
                {
                    equipment.LastEdited = DateTime.Now;
                    repo.Insert(equipment);

                    return(Json(true));
                }

                return(Json(false));
            }
            catch {
                return(Json(null));
            }
        }
        public static void GetJson(EquipmentHandler equipRepo, OwnerHandler ownerRepo)
        {
            List <Mac>       Macs   = new List <Mac>();
            List <Owner>     Owners = new List <Owner>();
            List <Equipment> Equip  = new List <Equipment>();

            string json = File.ReadAllText(@"C:\Users\peter\source\repos\MDMScrape\MDMScrape\bin\Debug\netcoreapp2.1\Macs.json");

            Macs = JsonConvert.DeserializeObject <List <Mac> >(json);

            for (int i = 0; i < Macs.Count; i++)
            {
                Equip.Add(Macs[i]);
                Equip[Equip.Count - 1].EquipType = Equipment.EquipmentType.Mac;
                Owners.Add(Macs[i].Owner);
            }

            var eqpTasks   = new List <Task>();
            var ownerTasks = new List <Task>();

            var owner = Owners.Distinct().ToList();

            for (int i = 0; i < Equip.Count; i++)
            {
                equipRepo.Insert(Equip[i], false);
                if (i % 20 == 0)
                {
                    equipRepo.Save();
                }
            }

            for (int i = 0; i < owner.Count; i++)
            {
                ownerRepo.Insert(owner[i], false);

                if (i % 20 == 0)
                {
                    ownerRepo.Save();
                }
            }
        }
Exemplo n.º 3
0
        public static void GetRandomTest(EquipmentHandler equipRepo, OwnerHandler ownerRepo)
        {
            var usersToAdd     = 500;
            var equipmentToAdd = 500;

            const string chars   = "abcdefghijklmnopqrstuvxyzw";
            const string numbers = "123456789";

            var random = new Random();

            var owners    = new List <Owner>();
            var equipment = new List <Equipment>();


            for (int i = 0; i < usersToAdd; i++)
            {
                var owner = new Owner();

                owner.FirstName = new string(Enumerable.Repeat(chars, 10)
                                             .Select(s => s[random.Next(s.Length)]).ToArray());

                owner.LastName = new string(Enumerable.Repeat(chars, 10)
                                            .Select(s => s[random.Next(s.Length)]).ToArray());

                owner.LastEdited = DateTime.Now;

                owners.Add(owner);
            }

            for (int i = 0; i < equipmentToAdd; i++)
            {
                var eqp = new Equipment();

                eqp.Serial = new string(Enumerable.Repeat(numbers, 15)
                                        .Select(s => s[random.Next(s.Length)]).ToArray());

                eqp.Model = new string(new string(Enumerable.Repeat(chars, 10)
                                                  .Select(s => s[random.Next(s.Length)]).ToArray()) +
                                       new string(Enumerable.Repeat(numbers, 8)
                                                  .Select(s => s[random.Next(s.Length)]).ToArray()));

                eqp.LastEdited = DateTime.Now;
                eqp.EquipType  = Equipment.EquipmentType.Laptop;

                equipment.Add(eqp);
            }


            for (int i = 0; i < equipment.Count; i++)
            {
                equipRepo.Insert(equipment[i], false);
                if (i % 20 == 0)
                {
                    equipRepo.Save();
                }
            }

            for (int i = 0; i < owners.Count; i++)
            {
                ownerRepo.Insert(owners[i], false);

                if (i % 20 == 0)
                {
                    ownerRepo.Save();
                }
            }
        }