Exemplo n.º 1
0
        /// <summary>
        /// Fetches a list of github users from either the api or in the memory cache
        /// </summary>
        /// <param name="model">list of string username</param>
        /// <returns>response object</returns>
        public async Task <ResponseObject> GetGitHubUsers(string model)
        {
            var           response        = new ResponseObject(ResponseType.Success, string.Empty);
            List <string> stringListModel = DataTypeConverter.ConvertStringToList(model);
            var           gitHubUserList  = new List <GitHubUsersDto>();

            stringListModel = StringSanitizer.SanitizeStringListByGitHubPolicy(stringListModel);
            var modelValuesCount = ValidateArrayString(stringListModel);

            if (!modelValuesCount.IsSuccess)
            {
                return(modelValuesCount);
            }
            int numberOfCachedRecords   = 0;
            int numberOfRecordsNotFound = 0;

            foreach (var item in stringListModel)
            {
                if (stringListModel.Any())
                {
                    var sanitizedString     = StringSanitizer.ToLowerAndTrim(item);
                    var gitHubUserDto       = new GitHubUsersDto();
                    var fetchDataFromGithub = await FetchDataByUserName(sanitizedString);

                    if (fetchDataFromGithub.IsSuccess)
                    {
                        gitHubUserDto = _mapper.Map <GitHubUsersDto>(fetchDataFromGithub.Data);
                        _inMemoryWorkerService.StoreDataInMemory(gitHubUserDto, StringSanitizer.ToLowerAndTrim(gitHubUserDto.Login));
                        gitHubUserList.Add(gitHubUserDto);
                    }
                    else if (fetchDataFromGithub.IsCached)
                    {
                        gitHubUserDto = _mapper.Map <GitHubUsersDto>(fetchDataFromGithub.Data);
                        numberOfCachedRecords++;
                        gitHubUserList.Add(gitHubUserDto);
                    }
                    else
                    {
                        numberOfRecordsNotFound++;
                    }
                }
            }
            response.Data    = gitHubUserList.OrderBy(x => x.Name);
            response.Message = numberOfCachedRecords + " out of " + modelValuesCount.Data + " is cached and " + numberOfRecordsNotFound + " record(s) were not found. Username that violates github policy were removed.";
            return(response);
        }