-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add district, municipality, and state repositories
- Loading branch information
1 parent
1498672
commit a7518c4
Showing
10 changed files
with
852 additions
and
2 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/FunderMaps.Core/Interfaces/Repositories/IDistrictRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using FunderMaps.Core.Entities; | ||
|
||
namespace FunderMaps.Core.Interfaces.Repositories; | ||
|
||
/// <summary> | ||
/// District repository. | ||
/// </summary> | ||
public interface IDistrictRepository : IAsyncRepository<District, string> | ||
{ | ||
/// <summary> | ||
/// Get district by external identifier. | ||
/// </summary> | ||
/// <param name="id">External identifier.</param> | ||
/// <returns>A single district.</returns> | ||
Task<District> GetByExternalIdAsync(string id); | ||
|
||
/// <summary> | ||
/// Get district by external address id. | ||
/// </summary> | ||
/// <param name="id">External address identifier.</param> | ||
/// <returns>A single district.</returns> | ||
Task<District> GetByExternalAddressIdAsync(string id); | ||
|
||
/// <summary> | ||
/// Get district by external building id. | ||
/// </summary> | ||
/// <param name="id">External address identifier.</param> | ||
/// <returns>A single district.</returns> | ||
Task<District> GetByExternalBuildingIdAsync(string id); | ||
|
||
/// <summary> | ||
/// Get district by external neighborhood id. | ||
/// </summary> | ||
/// <param name="id">External neighborhood identifier.</param> | ||
/// <returns>A single district.</returns> | ||
Task<District> GetByExternalNeighborhoodIdAsync(string id); | ||
} |
44 changes: 44 additions & 0 deletions
44
src/FunderMaps.Core/Interfaces/Repositories/IMunicipalityRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using FunderMaps.Core.Entities; | ||
|
||
namespace FunderMaps.Core.Interfaces.Repositories; | ||
|
||
/// <summary> | ||
/// Municipality repository. | ||
/// </summary> | ||
public interface IMunicipalityRepository : IAsyncRepository<Municipality, string> | ||
{ | ||
/// <summary> | ||
/// Get municipality by external identifier. | ||
/// </summary> | ||
/// <param name="id">External identifier.</param> | ||
/// <returns>A single municipality.</returns> | ||
Task<Municipality> GetByExternalIdAsync(string id); | ||
|
||
/// <summary> | ||
/// Get municipality by external address id. | ||
/// </summary> | ||
/// <param name="id">External address identifier.</param> | ||
/// <returns>A single municipality.</returns> | ||
Task<Municipality> GetByExternalAddressIdAsync(string id); | ||
|
||
/// <summary> | ||
/// Get municipality by external building id. | ||
/// </summary> | ||
/// <param name="id">External address identifier.</param> | ||
/// <returns>A single municipality.</returns> | ||
Task<Municipality> GetByExternalBuildingIdAsync(string id); | ||
|
||
/// <summary> | ||
/// Get municipality by external neighborhood id. | ||
/// </summary> | ||
/// <param name="id">External neighborhood identifier.</param> | ||
/// <returns>A single municipality.</returns> | ||
Task<Municipality> GetByExternalNeighborhoodIdAsync(string id); | ||
|
||
/// <summary> | ||
/// Get municipality by external district id. | ||
/// </summary> | ||
/// <param name="id">External district identifier.</param> | ||
/// <returns>A single municipality.</returns> | ||
Task<Municipality> GetByExternalDistrictIdAsync(string id); | ||
} |
51 changes: 51 additions & 0 deletions
51
src/FunderMaps.Core/Interfaces/Repositories/IStateRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using FunderMaps.Core.Entities; | ||
|
||
namespace FunderMaps.Core.Interfaces.Repositories; | ||
|
||
/// <summary> | ||
/// State repository. | ||
/// </summary> | ||
public interface IStateRepository : IAsyncRepository<State, string> | ||
{ | ||
/// <summary> | ||
/// Get state by external identifier. | ||
/// </summary> | ||
/// <param name="id">External identifier.</param> | ||
/// <returns>A single state.</returns> | ||
Task<State> GetByExternalIdAsync(string id); | ||
|
||
/// <summary> | ||
/// Get state by external address id. | ||
/// </summary> | ||
/// <param name="id">External address identifier.</param> | ||
/// <returns>A single state.</returns> | ||
Task<State> GetByExternalAddressIdAsync(string id); | ||
|
||
/// <summary> | ||
/// Get state by external building id. | ||
/// </summary> | ||
/// <param name="id">External address identifier.</param> | ||
/// <returns>A single state.</returns> | ||
Task<State> GetByExternalBuildingIdAsync(string id); | ||
|
||
/// <summary> | ||
/// Get state by external neighborhood id. | ||
/// </summary> | ||
/// <param name="id">External neighborhood identifier.</param> | ||
/// <returns>A single state.</returns> | ||
Task<State> GetByExternalNeighborhoodIdAsync(string id); | ||
|
||
/// <summary> | ||
/// Get state by external district id. | ||
/// </summary> | ||
/// <param name="id">External district identifier.</param> | ||
/// <returns>A single state.</returns> | ||
Task<State> GetByExternalDistrictIdAsync(string id); | ||
|
||
/// <summary> | ||
/// Get state by external municipality id. | ||
/// </summary> | ||
/// <param name="id">External municipality identifier.</param> | ||
/// <returns>A single state.</returns> | ||
Task<State> GetByExternalMunicipalityIdAsync(string id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
using Dapper; | ||
using FunderMaps.Core; | ||
using FunderMaps.Core.Entities; | ||
using FunderMaps.Core.Exceptions; | ||
using FunderMaps.Core.Interfaces.Repositories; | ||
|
||
namespace FunderMaps.Data.Repositories; | ||
|
||
/// <summary> | ||
/// District repository. | ||
/// </summary> | ||
internal class DistrictRepository : RepositoryBase<District, string>, IDistrictRepository | ||
{ | ||
/// <summary> | ||
/// Retrieve number of entities. | ||
/// </summary> | ||
/// <returns>Number of entities.</returns> | ||
public override async Task<long> CountAsync() | ||
{ | ||
var sql = @" | ||
SELECT COUNT(*) | ||
FROM geocoder.district"; | ||
|
||
await using var connection = DbContextFactory.DbProvider.ConnectionScope(); | ||
|
||
return await connection.ExecuteScalarAsync<long>(sql); | ||
} | ||
|
||
public async Task<District> GetByExternalIdAsync(string id) | ||
{ | ||
if (TryGetEntity(id, out District? entity)) | ||
{ | ||
return entity ?? throw new InvalidOperationException(); | ||
} | ||
|
||
var sql = @" | ||
SELECT -- District | ||
d.id, | ||
d.name, | ||
d.water, | ||
d.external_id, | ||
d.municipality_id | ||
FROM geocoder.district AS d | ||
WHERE d.external_id = upper(@external_id) | ||
LIMIT 1"; | ||
|
||
await using var connection = DbContextFactory.DbProvider.ConnectionScope(); | ||
|
||
var district = await connection.QuerySingleOrDefaultAsync<District>(sql, new { external_id = id }); | ||
return district is null ? throw new EntityNotFoundException(nameof(District)) : CacheEntity(district); | ||
} | ||
|
||
public async Task<District> GetByExternalAddressIdAsync(string id) | ||
{ | ||
if (TryGetEntity(id, out District? entity)) | ||
{ | ||
return entity ?? throw new InvalidOperationException(); | ||
} | ||
|
||
var sql = @" | ||
SELECT -- District | ||
d.id, | ||
d.name, | ||
d.water, | ||
d.external_id, | ||
d.municipality_id | ||
FROM geocoder.address AS a | ||
JOIN geocoder.address_building AS ab ON ab.address_id = a.id | ||
JOIN geocoder.building_active AS ba ON ba.id = ab.building_id | ||
JOIN geocoder.neighborhood AS n ON n.id = ba.neighborhood_id | ||
JOIN geocoder.district d ON d.id = n.district_id | ||
WHERE a.external_id = upper(@external_id) | ||
LIMIT 1"; | ||
|
||
await using var connection = DbContextFactory.DbProvider.ConnectionScope(); | ||
|
||
var district = await connection.QuerySingleOrDefaultAsync<District>(sql, new { external_id = id }); | ||
return district is null ? throw new EntityNotFoundException(nameof(District)) : CacheEntity(district); | ||
} | ||
|
||
public async Task<District> GetByExternalBuildingIdAsync(string id) | ||
{ | ||
if (TryGetEntity(id, out District? entity)) | ||
{ | ||
return entity ?? throw new InvalidOperationException(); | ||
} | ||
|
||
var sql = @" | ||
SELECT -- District | ||
d.id, | ||
d.name, | ||
d.water, | ||
d.external_id, | ||
d.municipality_id | ||
FROM geocoder.building_active AS ba | ||
JOIN geocoder.neighborhood AS n on n.id = ba.neighborhood_id | ||
JOIN geocoder.district d ON d.id = n.district_id | ||
WHERE ba.external_id = upper(@external_id) | ||
LIMIT 1"; | ||
|
||
await using var connection = DbContextFactory.DbProvider.ConnectionScope(); | ||
|
||
var district = await connection.QuerySingleOrDefaultAsync<District>(sql, new { external_id = id }); | ||
return district is null ? throw new EntityNotFoundException(nameof(District)) : CacheEntity(district); | ||
} | ||
|
||
public async Task<District> GetByExternalNeighborhoodIdAsync(string id) | ||
{ | ||
if (TryGetEntity(id, out District? entity)) | ||
{ | ||
return entity ?? throw new InvalidOperationException(); | ||
} | ||
|
||
var sql = @" | ||
SELECT -- District | ||
d.id, | ||
d.name, | ||
d.water, | ||
d.external_id, | ||
d.municipality_id | ||
FROM geocoder.neighborhood AS n | ||
JOIN geocoder.district d ON d.id = n.district_id | ||
WHERE n.external_id = upper(@external_id) | ||
LIMIT 1"; | ||
|
||
await using var connection = DbContextFactory.DbProvider.ConnectionScope(); | ||
|
||
var district = await connection.QuerySingleOrDefaultAsync<District>(sql, new { external_id = id }); | ||
return district is null ? throw new EntityNotFoundException(nameof(District)) : CacheEntity(district); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Retrieve <see cref="District"/> by id. | ||
/// </summary> | ||
/// <param name="id">Unique identifier.</param> | ||
/// <returns><see cref="District"/>.</returns> | ||
public override async Task<District> GetByIdAsync(string id) | ||
{ | ||
if (TryGetEntity(id, out District? entity)) | ||
{ | ||
return entity ?? throw new InvalidOperationException(); | ||
} | ||
|
||
var sql = @" | ||
SELECT -- District | ||
d.id, | ||
d.name, | ||
d.water, | ||
d.external_id, | ||
d.municipality_id | ||
FROM geocoder.district AS d | ||
WHERE d.id = @id | ||
LIMIT 1"; | ||
|
||
await using var connection = DbContextFactory.DbProvider.ConnectionScope(); | ||
|
||
var district = await connection.QuerySingleOrDefaultAsync<District>(sql, new { id }); | ||
return district is null ? throw new EntityNotFoundException(nameof(District)) : CacheEntity(district); | ||
} | ||
|
||
/// <summary> | ||
/// Retrieve all <see cref="District"/>. | ||
/// </summary> | ||
/// <returns>List of <see cref="District"/>.</returns> | ||
public override async IAsyncEnumerable<District> ListAllAsync(Navigation navigation) | ||
{ | ||
var sql = @" | ||
SELECT -- District | ||
d.id, | ||
d.name, | ||
d.water, | ||
d.external_id, | ||
d.municipality_id | ||
FROM geocoder.district AS d | ||
OFFSET @offset | ||
LIMIT @limit"; | ||
|
||
await using var connection = DbContextFactory.DbProvider.ConnectionScope(); | ||
|
||
await foreach (var item in connection.QueryUnbufferedAsync<District>(sql, navigation)) | ||
{ | ||
yield return CacheEntity(item); | ||
} | ||
} | ||
} |
Oops, something went wrong.