Beispiel #1
0
        protected void AppendCsvFragment(string temporaryFilePath, IEnumerable <ExpandoObject> csvRows, bool outputHeaders)
        {
            string csv = _csvUtils.AsCsv(csvRows, outputHeaders);

            _fileSystemAccess.Append(temporaryFilePath, csv)
            .GetAwaiter()
            .GetResult();
        }
Beispiel #2
0
        private void GivenTheCsvRowTransformation(List <ProviderResult> providerResult, ExpandoObject[] transformedRows, string csv, bool outputHeaders)
        {
            _transformation
            .TransformProviderResultsIntoCsvRows(Arg.Is(providerResult), Arg.Any <IDictionary <string, TemplateMappingItem> >())
            .Returns(transformedRows);

            _csvUtils.AsCsv(Arg.Is <IEnumerable <dynamic> >(_ => _.SequenceEqual(transformedRows)), Arg.Is(outputHeaders))
            .Returns(csv);
        }
Beispiel #3
0
        private async Task <IActionResult> GetProviderDataAsCsv(PublishedProviderIdsRequest providerIds, string specificationId, string csvFileSuffix, params PublishedProviderStatus[] statuses)
        {
            ValidationResult validationResults = _validator.Validate(specificationId);

            if (!validationResults.IsValid)
            {
                return(validationResults.AsBadRequest());
            }

            if (providerIds.PublishedProviderIds.IsNullOrEmpty())
            {
                return(new BadRequestObjectResult("Provider ids must be provided"));
            }

            IEnumerable <PublishedProviderFundingCsvData> publishedProviderFundingData = await _fundingCsvDataProcessor.GetFundingData(
                providerIds.PublishedProviderIds,
                specificationId,
                statuses);

            if (publishedProviderFundingData.IsNullOrEmpty())
            {
                return(new NotFoundObjectResult("No data found for given specification and published provider ids."));
            }

            IEnumerable <dynamic> csvRows = publishedProviderFundingData.Select(x => new
            {
                UKPRN         = x.Ukprn,
                URN           = x.Urn,
                UPIN          = x.Upin,
                ProviderName  = x.ProviderName,
                FundingAmount = x.TotalFunding
            });

            string csvFileData     = _csvUtils.AsCsv(csvRows, true);
            string fundingStreamId = publishedProviderFundingData.First().FundingStreamId;
            string fundingPeriodId = publishedProviderFundingData.First().FundingPeriodId;
            string csvFileName     = $"{fundingStreamId}-{fundingPeriodId}-{csvFileSuffix}.csv";

            string blobName = $"{csvFileName}";
            string blobUrl  = string.Empty;

            await _blobClientPolicy.ExecuteAsync(async() =>
            {
                ICloudBlob blob = _blobClient.GetBlockBlobReference(blobName, BlobContainerName);
                blob.Properties.ContentDisposition = $"attachment; filename={csvFileName}";

                using (MemoryStream stream = new MemoryStream(csvFileData.AsUTF8Bytes()))
                {
                    await blob.UploadFromStreamAsync(stream);
                }

                blob.Metadata["fundingStreamId"] = fundingStreamId;
                blob.Metadata["fundingPeriodId"] = fundingPeriodId;
                blob.Metadata["specificationId"] = specificationId;
                blob.Metadata["fileName"]        = Path.GetFileNameWithoutExtension(csvFileName);
                blob.SetMetadata();

                blobUrl = _blobClient.GetBlobSasUrl(blobName, DateTimeOffset.Now.AddDays(1), SharedAccessBlobPermissions.Read, BlobContainerName);
            });

            return(new OkObjectResult(new PublishedProviderDataDownload()
            {
                Url = blobUrl
            }));
        }