public IActionResult GetFeedPage([FromRoute] int?pageRef, int?pageSize = null, int?fundingPeriodStartYear = null, int?fundingPeriodEndYear = null, string[] fundingPeriodCodes = null, OrganisationIdentifier[] organisationGroupIdentifiers = null, OrganisationType[] organisationGroupTypes = null, OrganisationIdentifier[] organisationIdentifiers = null, OrganisationType[] organisationTypes = null, VariationReason[] variationReasons = null, string[] ukprns = null, GroupingReason[] groupingReasons = null, FundingStatus[] statuses = null, DateTime?minStatusChangeDate = null, string[] fundingStreamCodes = null, FundingLineType[] fundingLineTypes = null, string[] templateLineIds = null) { var schemeAndHost = "http://mock.web"; var fullList = new List <FeedResponseContentModel>(); var pageSizeT = pageSize ?? 10; if (fundingStreamCodes?.Any() != true || fundingStreamCodes.Contains("DSG")) { fullList.AddRange(GenerateDSGFunding.GenerateFeed(fundingPeriodStartYear, fundingPeriodEndYear, fundingPeriodCodes, organisationGroupIdentifiers, organisationGroupTypes, organisationIdentifiers, organisationTypes, variationReasons, ukprns, groupingReasons, statuses, minStatusChangeDate, fundingLineTypes, templateLineIds) ); } if (fundingStreamCodes?.Any() != true || fundingStreamCodes.Contains("PESports")) { fullList.AddRange(GeneratePESportsFunding.GenerateFeed(fundingPeriodStartYear, fundingPeriodEndYear, fundingPeriodCodes, organisationGroupIdentifiers, organisationGroupTypes, organisationIdentifiers, organisationTypes, variationReasons, ukprns, groupingReasons, statuses, minStatusChangeDate, fundingLineTypes, templateLineIds)); } fullList.Reverse(); var limitedList = fullList.Skip(((pageRef ?? 1) - 1) * pageSizeT).Take(pageSizeT).ToArray(); limitedList.Reverse(); // Reverse back var lastPageNumber = 1; var firstPageNumber = (int)Math.Ceiling(fullList.Count / (double)pageSizeT); var currentPageNumber = pageRef ?? firstPageNumber; var isFirstPage = firstPageNumber == currentPageNumber; var isLastPage = currentPageNumber == lastPageNumber; var qs = Request.QueryString.ToString(); var returnItem = new FeedResponseModel { Id = Guid.NewGuid().ToString(), Title = "MYESF / CFS shared funding model mock API", Author = new FeedResponseAuthor { Email = "*****@*****.**", Name = "Calculate Funding Service" }, Updated = DateTime.Now, Rights = "Copyright (C) 2019 Department for Education", Link = new List <FeedLink> { new FeedLink { Href = isFirstPage ? $"{schemeAndHost}/api/funding/feed" : $"{schemeAndHost}/api/funding/feed/{currentPageNumber}{qs}", Rel = "self" }, new FeedLink { Href = $"{schemeAndHost}/api/funding/feed/{firstPageNumber}{qs}", Rel = "first" }, new FeedLink { Href = $"{schemeAndHost}/api/funding/feed/{lastPageNumber}{qs}", Rel = "last" } }, AtomEntry = limitedList.ToArray() }; if (!isFirstPage) { returnItem.Link.Add(new FeedLink { Href = $"{schemeAndHost}/api/funding/feed/{currentPageNumber + 1}{qs}", Rel = "next" }); } if (!isLastPage) { returnItem.Link.Add(new FeedLink { Href = $"{schemeAndHost}/api/funding/feed/{currentPageNumber - 1}{qs}", Rel = "previous" }); } return(Ok(returnItem)); }
public HttpResponseMessage DownloadPESportsFiles() { var takeAtOnce = 50; var processedFileNames = new List <string>(); byte[] fileBytes = null; ZipArchiveEntry zipItem; using (var memoryStream = new System.IO.MemoryStream()) { using (var zip = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) { var feedResponseFull = GeneratePESportsFunding.GenerateFeed(null, null, null, null, null, null, null, null, null, null, null, null, null, null); for (var idx = 0; idx <= (int)Math.Ceiling(feedResponseFull.Length / 10.0); idx++) { var feedResponse = feedResponseFull.Skip(idx * 10).Take(10).ToArray(); foreach (var feedEntry in feedResponse) { foreach (var providerFundingId in feedEntry.Content.Funding.ProviderFundings) { var fileName = $"{providerFundingId}.txt"; if (processedFileNames.Contains(fileName)) { continue; } var funding = GeneratePESportsFunding.GenerateProviderFunding(providerFundingId); var fundingStr = JsonConvert.SerializeObject(funding); zipItem = zip.CreateEntry(fileName); processedFileNames.Add(fileName); using (var originalFileMemoryStream = new System.IO.MemoryStream(Encoding.ASCII.GetBytes(fundingStr))) { using (var entryStream = zipItem.Open()) { originalFileMemoryStream.CopyTo(entryStream); } } } } zipItem = zip.CreateEntry($"FeedResponse_PESports_{(idx * takeAtOnce) + 1}-{(idx * takeAtOnce) + feedResponse.Length}.txt"); var feedResponseStr = JsonConvert.SerializeObject(feedResponse); using (var originalFileMemoryStream = new System.IO.MemoryStream(Encoding.ASCII.GetBytes(feedResponseStr))) { using (var entryStream = zipItem.Open()) { originalFileMemoryStream.CopyTo(entryStream); } } } } fileBytes = memoryStream.ToArray(); } var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(fileBytes) }; result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "allPesports.zip" }; result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip"); return(result); }