Beispiel #1
0
        // ------------- utility methods------------------------------------------------------------



        /// <summary>
        /// Adds specified amount of unique users to the table. Very simple users with basic fields.
        /// </summary>
        /// <param name="quantity">Number of attempts of finding unique users. The names dictionary is limited, so is the
        /// ability to create unique names. </param>
        /// <returns>Report of current users</returns>
        public IActionResult AddUsersToDb(int quantity)
        {
            int createdUsers = 0;

            if (quantity > 0)
            {
                createdUsers = lifeService.GenerateCitizenUsers(quantity);
            }

            var allUsersViewModel = citizenUserRepository.GetAll().Select(u => mapper.Map <UserViewModel>(u)).ToList();

            ViewData["quantity"] = createdUsers;
            return(View("~/Views/Life/ListOfUsers.cshtml", allUsersViewModel));
        }
Beispiel #2
0
        /// <summary>
        /// Creates specified number of unique users (actually creates less than that number because of limited names dictionoary)
        /// </summary>
        /// <param name="quantity">Number of new users required</param>
        /// <returns>Actual number of users created</returns>
        public int GenerateCitizenUsers(int quantity)
        {
            var rnd            = new Random();
            var firstNamesMale = new List <string>
            {
                "Ярослав", "Викентий", "Фёдор", "Клаус", "Степан", "Остин", "Нестор", "Йозеф", "Филипп", "Леопольд",
                "Герман", "Йоган", "Эрик", "Лукьян", "Остап", "Шамиль", "Захар", "Михаил",
            };
            var firstNamesFemale = new List <string>
            {
                "Харитина", "Надежда", "Татьяна", "Клара", "Богдана", "Ульяна", "Зинаида", "Белла", "Рада", "Надежда",
                "Мария", "Генриетта", "Бронислава", "Изольда", "Шарлота", "Эмилия", "Алла", "Цара", "Зоя", "Шарлота",
            };
            var lastNamesMale = new List <string>
            {
                "Котов", "Рыбаков", "Горшков", "Меркушев", "Фокин", "Матвеев", "Цушко", "Васильев", "Афанасьев", "Барановский",
                "Красинец", "Семёнов", "Новиков", "Горбачёв", "Шамрыло", "Игнатьев", "Гриневский", "Михеев", "Ермаков", "Филиппов",
            };
            var lastNamesFemale = new List <string>
            {
                "Петрова", "Пестова", "Шарапова", "Ярова", "Моисеенко", "Иващенко", "Моисеенко", "Гончар", "Кличко", "Петренко", "Шкраба",
                "Лазарева", "Егорова", "Права", "Вишнякова", "Белоусова", "Орлова", "Плаксий", "Милославска", "Данилова",
            };
            var ListOfFullNames = citizenUserRepository.GetAll()
                                  .Select(user => user.FirstName + " " + user.LastName).ToList();
            var ListOfRoles = roleRepository.GetAll();

            // количество попыток найти уникальное сочетание имя+фамилия
            var attemptNumber = 0;
            // количество успешных попыток найти уникальное сочетание имя+фамилия
            var successfulAttemptNumber = 0;


            do
            {
                string firstName;
                string lastName;
                var    isMale = rnd.Next(0, 2);


                if (isMale == 1)
                {
                    firstName = firstNamesMale[rnd.Next(firstNamesMale.Count)];
                    lastName  = lastNamesMale[rnd.Next(lastNamesMale.Count)];
                }
                else
                {
                    firstName = firstNamesMale[rnd.Next(firstNamesMale.Count)];
                    lastName  = lastNamesMale[rnd.Next(lastNamesMale.Count)];
                }

                var FullName = firstName + " " + lastName;
                if (!ListOfFullNames.Any(s => s == FullName))
                {
                    // имя-фамилия уникально, сохраняем
                    ListOfFullNames.Add(FullName);
                    var startBirthdate = new DateTime(1950, 1, 1);
                    var range          = (new DateTime(2010, 1, 1) - startBirthdate).Days;
                    var birthdate      = startBirthdate.AddDays(rnd.Next(range));

                    var newCitizen = new CitizenUser
                    {
                        Id        = 0,
                        Login     = firstName + "_" + lastName,
                        Password  = "******",
                        FirstName = firstName,
                        LastName  = lastName,
                        BirthDate = birthdate,
                        Gender    = isMale == 1 ? Gender.Male : Gender.Female,
                        Roles     = new List <Role>(),
                    };
                    newCitizen.Roles.Add(ListOfRoles[rnd.Next(ListOfRoles.Count)]);
                    citizenUserRepository.Save(newCitizen);
                    successfulAttemptNumber++;
                }
                // имя-фамилия не уникально, ищем другие
                attemptNumber++;
            }while (attemptNumber < quantity);
            return(successfulAttemptNumber);
        }
 public List <CitizenUser> GetUsers()
 {
     return(citizenUserRepository.GetAll());
 }