[AZ-310] [AZ-311] Route tile endpoints through ITileService

Move cache+DB+download logic for /tiles/{z}/{x}/{y} and
/api/satellite/tiles/latlon out of Program.cs into TileService.
Endpoints now inject only ITileService + ILogger. Service owns
IMemoryCache (1h absolute / 30min sliding preserved). Added
TileBytes DTO; ITileService gains GetOrDownloadTileAsync and
DownloadAndStoreSingleTileAsync. 5 new unit tests cover cache
hit, repo hit, downloader fallback, and AZ-311 happy + error.

Build clean (0/0), unit suite 40/40. Resolves architecture
baseline F3 in code (docs handled by AZ-315).

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Oleksandr Bezdieniezhnykh
2026-05-10 06:06:11 +03:00
parent 220277b9c7
commit 12b582deac
12 changed files with 387 additions and 121 deletions
+19 -96
View File
@@ -1,15 +1,12 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using SatelliteProvider.DataAccess;
using SatelliteProvider.DataAccess.Models;
using SatelliteProvider.DataAccess.Repositories;
using SatelliteProvider.Common.Configs;
using SatelliteProvider.Common.DTO;
using SatelliteProvider.Common.Interfaces;
using SatelliteProvider.Common.Utils;
using SatelliteProvider.Services;
using Serilog;
@@ -138,63 +135,14 @@ app.MapGet("/api/satellite/route/{id:guid}", GetRoute)
app.Run();
async Task<IResult> ServeTile(int z, int x, int y, HttpContext httpContext, ITileRepository tileRepository, ISatelliteDownloader downloader, IMemoryCache cache, ILogger<Program> logger)
async Task<IResult> ServeTile(int z, int x, int y, HttpContext httpContext, ITileService tileService, ILogger<Program> logger)
{
var cacheKey = $"tile_{z}_{x}_{y}";
try
{
if (cache.TryGetValue(cacheKey, out byte[]? cachedBytes) && cachedBytes != null)
{
httpContext.Response.Headers.CacheControl = "public, max-age=86400";
httpContext.Response.Headers.ETag = $"\"{z}_{x}_{y}\"";
return Results.Bytes(cachedBytes, "image/jpeg");
}
string? filePath = null;
var tile = await tileRepository.GetByTileCoordinatesAsync(z, x, y);
if (tile != null && File.Exists(tile.FilePath))
{
filePath = tile.FilePath;
}
else
{
var tileCenter = GeoUtils.TileToWorldPos(x, y, z);
var downloadedTile = await downloader.DownloadSingleTileAsync(tileCenter.Lat, tileCenter.Lon, z);
var now = DateTime.UtcNow;
var tileEntity = new TileEntity
{
Id = Guid.NewGuid(),
TileZoom = z,
TileX = downloadedTile.X,
TileY = downloadedTile.Y,
Latitude = downloadedTile.CenterLatitude,
Longitude = downloadedTile.CenterLongitude,
TileSizeMeters = downloadedTile.TileSizeMeters,
TileSizePixels = 256,
ImageType = "jpg",
MapsVersion = $"downloaded_{now:yyyy-MM-dd}",
Version = now.Year,
FilePath = downloadedTile.FilePath,
CreatedAt = now,
UpdatedAt = now
};
await tileRepository.InsertAsync(tileEntity);
filePath = tileEntity.FilePath;
}
var bytes = await File.ReadAllBytesAsync(filePath);
cache.Set(cacheKey, bytes, new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(1),
SlidingExpiration = TimeSpan.FromMinutes(30)
});
httpContext.Response.Headers.CacheControl = "public, max-age=86400";
httpContext.Response.Headers.ETag = $"\"{z}_{x}_{y}\"";
return Results.Bytes(bytes, "image/jpeg");
var tile = await tileService.GetOrDownloadTileAsync(z, x, y, httpContext.RequestAborted);
httpContext.Response.Headers.CacheControl = $"public, max-age={(long)tile.MaxAge.TotalSeconds}";
httpContext.Response.Headers.ETag = tile.ETag;
return Results.Bytes(tile.Bytes, tile.ContentType);
}
catch (Exception ex)
{
@@ -203,51 +151,26 @@ async Task<IResult> ServeTile(int z, int x, int y, HttpContext httpContext, ITil
}
}
async Task<IResult> GetTileByLatLon([FromQuery] double Latitude, [FromQuery] double Longitude, [FromQuery] int ZoomLevel, ISatelliteDownloader downloader, ITileRepository tileRepository, ILogger<Program> logger)
async Task<IResult> GetTileByLatLon([FromQuery] double Latitude, [FromQuery] double Longitude, [FromQuery] int ZoomLevel, ITileService tileService, ILogger<Program> logger)
{
try
{
var downloadedTile = await downloader.DownloadSingleTileAsync(
Latitude,
Longitude,
ZoomLevel);
var now = DateTime.UtcNow;
var currentVersion = now.Year;
var tileEntity = new TileEntity
{
Id = Guid.NewGuid(),
TileZoom = downloadedTile.ZoomLevel,
TileX = downloadedTile.X,
TileY = downloadedTile.Y,
Latitude = downloadedTile.CenterLatitude,
Longitude = downloadedTile.CenterLongitude,
TileSizeMeters = downloadedTile.TileSizeMeters,
TileSizePixels = 256,
ImageType = "jpg",
MapsVersion = $"downloaded_{now:yyyy-MM-dd}",
Version = currentVersion,
FilePath = downloadedTile.FilePath,
CreatedAt = now,
UpdatedAt = now
};
await tileRepository.InsertAsync(tileEntity);
var tile = await tileService.DownloadAndStoreSingleTileAsync(Latitude, Longitude, ZoomLevel);
var response = new DownloadTileResponse
{
Id = tileEntity.Id,
ZoomLevel = tileEntity.TileZoom,
Latitude = tileEntity.Latitude,
Longitude = tileEntity.Longitude,
TileSizeMeters = tileEntity.TileSizeMeters,
TileSizePixels = tileEntity.TileSizePixels,
ImageType = tileEntity.ImageType,
MapsVersion = tileEntity.MapsVersion,
Version = currentVersion,
FilePath = tileEntity.FilePath,
CreatedAt = tileEntity.CreatedAt,
UpdatedAt = tileEntity.UpdatedAt
Id = tile.Id,
ZoomLevel = tile.TileZoom,
Latitude = tile.Latitude,
Longitude = tile.Longitude,
TileSizeMeters = tile.TileSizeMeters,
TileSizePixels = tile.TileSizePixels,
ImageType = tile.ImageType,
MapsVersion = tile.MapsVersion,
Version = tile.Version,
FilePath = tile.FilePath,
CreatedAt = tile.CreatedAt,
UpdatedAt = tile.UpdatedAt
};
return Results.Ok(response);