mirror of
https://github.com/azaion/satellite-provider.git
synced 2026-04-22 22:16:38 +00:00
90 lines
3.5 KiB
C#
90 lines
3.5 KiB
C#
using Dapper;
|
|
using Npgsql;
|
|
using SatelliteProvider.DataAccess.Models;
|
|
|
|
namespace SatelliteProvider.DataAccess.Repositories;
|
|
|
|
public class RegionRepository : IRegionRepository
|
|
{
|
|
private readonly string _connectionString;
|
|
|
|
public RegionRepository(string connectionString)
|
|
{
|
|
_connectionString = connectionString;
|
|
}
|
|
|
|
public async Task<RegionEntity?> GetByIdAsync(Guid id)
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString);
|
|
const string sql = @"
|
|
SELECT id, latitude, longitude, size_meters as SizeMeters,
|
|
zoom_level as ZoomLevel, status,
|
|
csv_file_path as CsvFilePath, summary_file_path as SummaryFilePath,
|
|
tiles_downloaded as TilesDownloaded, tiles_reused as TilesReused,
|
|
created_at as CreatedAt, updated_at as UpdatedAt
|
|
FROM regions
|
|
WHERE id = @Id";
|
|
|
|
return await connection.QuerySingleOrDefaultAsync<RegionEntity>(sql, new { Id = id });
|
|
}
|
|
|
|
public async Task<IEnumerable<RegionEntity>> GetByStatusAsync(string status)
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString);
|
|
const string sql = @"
|
|
SELECT id, latitude, longitude, size_meters as SizeMeters,
|
|
zoom_level as ZoomLevel, status,
|
|
csv_file_path as CsvFilePath, summary_file_path as SummaryFilePath,
|
|
tiles_downloaded as TilesDownloaded, tiles_reused as TilesReused,
|
|
created_at as CreatedAt, updated_at as UpdatedAt
|
|
FROM regions
|
|
WHERE status = @Status
|
|
ORDER BY created_at ASC";
|
|
|
|
return await connection.QueryAsync<RegionEntity>(sql, new { Status = status });
|
|
}
|
|
|
|
public async Task<Guid> InsertAsync(RegionEntity region)
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString);
|
|
const string sql = @"
|
|
INSERT INTO regions (id, latitude, longitude, size_meters, zoom_level,
|
|
status, csv_file_path, summary_file_path,
|
|
tiles_downloaded, tiles_reused, created_at, updated_at)
|
|
VALUES (@Id, @Latitude, @Longitude, @SizeMeters, @ZoomLevel,
|
|
@Status, @CsvFilePath, @SummaryFilePath,
|
|
@TilesDownloaded, @TilesReused, @CreatedAt, @UpdatedAt)
|
|
RETURNING id";
|
|
|
|
return await connection.ExecuteScalarAsync<Guid>(sql, region);
|
|
}
|
|
|
|
public async Task<int> UpdateAsync(RegionEntity region)
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString);
|
|
const string sql = @"
|
|
UPDATE regions
|
|
SET latitude = @Latitude,
|
|
longitude = @Longitude,
|
|
size_meters = @SizeMeters,
|
|
zoom_level = @ZoomLevel,
|
|
status = @Status,
|
|
csv_file_path = @CsvFilePath,
|
|
summary_file_path = @SummaryFilePath,
|
|
tiles_downloaded = @TilesDownloaded,
|
|
tiles_reused = @TilesReused,
|
|
updated_at = @UpdatedAt
|
|
WHERE id = @Id";
|
|
|
|
return await connection.ExecuteAsync(sql, region);
|
|
}
|
|
|
|
public async Task<int> DeleteAsync(Guid id)
|
|
{
|
|
using var connection = new NpgsqlConnection(_connectionString);
|
|
const string sql = "DELETE FROM regions WHERE id = @Id";
|
|
return await connection.ExecuteAsync(sql, new { Id = id });
|
|
}
|
|
}
|
|
|