# Module: DataAccess/Repositories/TileRepository ## Purpose Dapper-based repository for the `tiles` table. Handles CRUD operations and spatial queries for satellite tile records. ## Public Interface ### ITileRepository (interface) - `GetByIdAsync(Guid id) → Task` - `GetByTileCoordinatesAsync(int tileZoom, int tileX, int tileY) → Task`: finds tile by slippy map coordinates, returns latest version - `FindExistingTileAsync(double lat, double lon, double tileSizeMeters, int zoomLevel, int version) → Task`: fuzzy coordinate match (tolerance: 0.0001° lat/lon, 1m tile size) - `GetTilesByRegionAsync(double lat, double lon, double sizeMeters, int zoomLevel) → Task>`: spatial bounding box query with expanded range to cover tile edges - `InsertAsync(TileEntity tile) → Task`: upsert — `ON CONFLICT (latitude, longitude, tile_zoom, tile_size_meters, version) DO UPDATE` file_path, tile_x, tile_y, updated_at - `UpdateAsync(TileEntity tile) → Task` - `DeleteAsync(Guid id) → Task` ### TileRepository (implementation) Constructs a new `NpgsqlConnection` per method call (no connection pooling at the repository level; Npgsql pools connections internally). ## Internal Logic - `GetTilesByRegionAsync` calculates a bounding box by expanding the requested region by 2 × tile size to ensure edge tiles are included. Uses meters-to-degrees approximation (111,000 m/degree latitude, adjusted for longitude). - `InsertAsync` uses an upsert pattern to handle duplicate tile downloads gracefully. - `GetByTileCoordinatesAsync` orders by `version DESC` and takes the latest. ## Dependencies - NuGet: `Dapper`, `Npgsql` - `SatelliteProvider.DataAccess.Models.TileEntity` - `Microsoft.Extensions.Logging` ## Consumers - `TileService` — all read/write operations - `Program.cs` (ServeTile, GetTileByLatLon handlers) — `GetByTileCoordinatesAsync`, `InsertAsync` ## Data Models Operates on `TileEntity`. ## Configuration Receives connection string via constructor. ## External Integrations PostgreSQL — SQL queries via Dapper + Npgsql. ## Security None. ## Tests No dedicated repository tests.