49 lines
1.6 KiB
C#
49 lines
1.6 KiB
C#
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<IActionResult> Get()
|
|
{
|
|
if (!memoryCache.TryGetValue(GeoFeedCacheKey, out List<IPGeoFeed>? feed))
|
|
{
|
|
feed = await builder.GetGeoFeedData();
|
|
var cacheEntryOptions = new MemoryCacheEntryOptions()
|
|
.SetSlidingExpiration(TimeSpan.FromMinutes(15));
|
|
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"
|
|
};
|
|
}
|
|
}
|
|
}
|