using Microsoft.AspNetCore.Mvc; using AS1024.GeoFeed.Interfaces; using AS1024.GeoFeed.GeoFeedBuilder; using Microsoft.Extensions.Caching.Memory; using AS1024.GeoFeed.Models; using System.Text; namespace AS1024.GeoFeed.Controllers { [ApiController] [Route("[controller]")] [Route("[controller].csv")] public class GeofeedController : ControllerBase { private readonly IGeoFeedProvider builder; private readonly IMemoryCache memoryCache; private const string GeoFeedCacheKey = "GeoFeedData"; public GeofeedController(IGeoFeedProvider builder, IMemoryCache memoryCache) { this.builder = builder; this.memoryCache = memoryCache; } [HttpGet] [Route("")] public async Task Get() { if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List? feed)) { feed = await builder.GetGeoFeedData(); var cacheEntryOptions = new MemoryCacheEntryOptions() .SetAbsoluteExpiration(TimeSpan.FromHours(1)); memoryCache.Set(GeoFeedCacheKey, feed, cacheEntryOptions); } var csvContent = feed.ToGeoFeedCsv(); // Assuming ToGeoFeedCsv() returns a string in CSV format. var contentBytes = Encoding.UTF8.GetBytes(csvContent); var contentType = "text/csv"; return new FileContentResult(contentBytes, contentType) { FileDownloadName = "geofeed.csv" }; } } }