Add project files.
This commit is contained in:
48
AS1024.GeoFeed/Controllers/GeofeedController.cs
Normal file
48
AS1024.GeoFeed/Controllers/GeofeedController.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
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()
|
||||
.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"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user