Exemple #1
0
 public async Task <string> Get()
 {
     return(await _genderizeClient.GetGender(""));
 }
        public async Task <List <Person> > Run(string fileContents, SortType sortType, OrderType orderType)
        {
            var lines = fileContents.Split(Environment.NewLine);
            //Print the query output on console
            // Execute the query and write out the new file.
            var peopleService = new PersonService();
            var people        = new List <Person>();

            foreach (var line in lines)
            {
                people.Add(new Person(line));
            }
            var sortedNames = new List <Person>();


            if (sortType == SortType.firstname)
            {
                if (orderType == OrderType.ascending)
                {
                    INameSorter namesorter = new NameSorterAscending(x => x.FullName);
                    sortedNames = namesorter.Sort(people);
                }
                else if (orderType == OrderType.descending)
                {
                    INameSorter namesorter = new NameSorterDecending(x => x.FullName);
                    sortedNames = namesorter.Sort(people);
                }
            }
            else if (sortType == SortType.lastname)
            {
                if (orderType == OrderType.ascending)
                {
                    INameSorter namesorter = new NameSorterAscending(x => x.Surname);
                    sortedNames = namesorter.Sort(people);
                }
                else if (orderType == OrderType.descending)
                {
                    INameSorter namesorter = new NameSorterDecending(x => x.Surname);
                    sortedNames = namesorter.Sort(people);
                }
            }

            foreach (var person in sortedNames)
            {
                var cacheResult = _cache.Get <GenderizeResult>(person.FirstName);
                if (cacheResult == null)
                {
                    var jsonResponse = await client.GetGender(person.FirstName);

                    var genderObject = JsonConvert.DeserializeObject <GenderizeResult>(jsonResponse);
                    if (genderObject == null)
                    {
                    }

                    person.Gender = genderObject.gender;
                    var saveCache = _cache.Set(person.FirstName, genderObject);
                }
                else
                {
                    person.Gender = cacheResult.gender;
                }
            }
            var sortedLines = PersonService.GetFullNames(sortedNames);

            File.WriteAllLines(@"sorted-names-list.txt", sortedLines);
            foreach (var person in sortedNames)
            {
                var personfullnames = new Entities.Person()
                {
                    FirstName = person.FirstName,
                    Surname   = person.Surname,
                    Gender    = person.Gender,
                    FullName  = person.FirstName + " " + person.Surname,
                };

                var existingRecords = personRepository.GetByName(personfullnames.FirstName, personfullnames.Surname, personfullnames.Gender, personfullnames.FullName).ToList();
                if (existingRecords.Count > 0)
                {
                }
                else
                {
                    personRepository.Add(personfullnames);
                    personRepository.SaveAll();
                }
            }
            sortedLines.ForEach(line => logger.LogInformation(line));
            Console.WriteLine("Sorted names are written to file. Press any key to exit");

            return(sortedNames);
        }