public async Task <IEnumerable <string> > GetUrlsFromCsv(Stream stream) { var urls = new List <string>(); using (var streamReader = new StreamReader(stream)) using (var csvReader = new CsvReader(streamReader, CultureInfo.CurrentUICulture)) { var checkingHeader = true; while (await csvReader.ReadAsync()) { var url = csvReader.GetField <string>(0); if (checkingHeader) { checkingHeader = false; var isHeaderRow = _urlAdapter.TryGetUrlHash(url) == null; if (isHeaderRow) { continue; } } urls.Add(url); } } return(urls); }
private async Task <IEnumerable <HomeUrlMappingViewModel> > TryCreateShortUrls(string websiteUrl, IEnumerable <string> urls) { var mappings = new List <(string url, string urlHash)>(); foreach (var url in urls) { var urlWithProtocol = _urlUtil.GetUrlWithProtocol(url); var urlHash = _urlAdapter.TryGetUrlHash(urlWithProtocol); if (urlHash != null) { mappings.Add((urlWithProtocol, urlHash)); } } if (await _urlRepository.TryCreateShortUrls(mappings)) { var viewModels = mappings.Select(m => new HomeUrlMappingViewModel(websiteUrl, m.url, m.urlHash)); return(viewModels); } return(null); }
public void UrlAdapterStringToHex_TryGetUrlHash_ShouldReturnNullFor_InvalidUrl(string input) { Assert.Null(_urlAdapter.TryGetUrlHash(input)); }