-
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.
* chore: uncomment postgres volume to reduce work to switch database * fix: split repositories to simplify having different queries per db provider * refactor: convert ConnectionString field to a property
- Loading branch information
Showing
7 changed files
with
81 additions
and
62 deletions.
There are no files selected for viewing
23 changes: 19 additions & 4 deletions
23
Common/src/Common.Infrastructure/IServiceCollectionExtension.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
35 changes: 0 additions & 35 deletions
35
Common/src/Common.Infrastructure/Persistence/Context/MetricStatusesDapperContext.cs
This file was deleted.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
Common/src/Common.Infrastructure/Persistence/Repository/MetricStatusesRepositoryOptions.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,6 @@ | ||
namespace Enmeshed.Common.Infrastructure.Persistence.Repository; | ||
|
||
public class MetricStatusesRepositoryOptions | ||
{ | ||
public string ConnectionString { get; set; } = string.Empty; | ||
} |
32 changes: 32 additions & 0 deletions
32
Common/src/Common.Infrastructure/Persistence/Repository/PostgresMetricStatusesRepository.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,32 @@ | ||
using Dapper; | ||
using Enmeshed.BuildingBlocks.Domain; | ||
using Enmeshed.DevelopmentKit.Identity.ValueObjects; | ||
using Microsoft.Extensions.Options; | ||
using Npgsql; | ||
|
||
namespace Enmeshed.Common.Infrastructure.Persistence.Repository; | ||
|
||
public class PostgresMetricStatusesRepository : IMetricStatusesRepository | ||
{ | ||
private const string QUERY = """SELECT * FROM "MetricStatuses" WHERE "Owner" = @identityAddress AND "MetricKey" = ANY(@keys)"""; | ||
|
||
private readonly MetricStatusesRepositoryOptions _options; | ||
|
||
public PostgresMetricStatusesRepository(IOptions<MetricStatusesRepositoryOptions> options) | ||
{ | ||
_options = options.Value; | ||
} | ||
|
||
public async Task<IEnumerable<MetricStatus>> GetMetricStatuses(IdentityAddress identityAddress, IEnumerable<MetricKey> keys) | ||
{ | ||
await using var connection = new NpgsqlConnection(_options.ConnectionString); | ||
|
||
var metricStatuses = await connection.QueryAsync<MetricStatus>(QUERY, new | ||
{ | ||
identityAddress = identityAddress.ToString(), | ||
keys = keys.Select(x => x.Value).ToArray() | ||
}); | ||
|
||
return metricStatuses; | ||
} | ||
} |
23 changes: 13 additions & 10 deletions
23
...ce/Repository/MetricStatusesRepository.cs → ...tory/SqlServerMetricStatusesRepository.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 |
---|---|---|
@@ -1,29 +1,32 @@ | ||
using Dapper; | ||
using Enmeshed.BuildingBlocks.Domain; | ||
using Enmeshed.DevelopmentKit.Identity.ValueObjects; | ||
using Enmeshed.Common.Infrastructure.Persistence.Context; | ||
using Dapper; | ||
using Microsoft.Data.SqlClient; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Enmeshed.Common.Infrastructure.Persistence.Repository; | ||
|
||
public class MetricStatusesRepository : IMetricStatusesRepository | ||
public class SqlServerMetricStatusesRepository : IMetricStatusesRepository | ||
{ | ||
private const string QUERY = "SELECT * FROM MetricStatuses WHERE Owner = @identityAddress and MetricKey IN @keys"; | ||
private const string QUERY = "SELECT * FROM MetricStatuses WHERE Owner = @identityAddress AND MetricKey IN @keys"; | ||
|
||
private readonly MetricStatusesRepositoryOptions _options; | ||
|
||
public MetricStatusesRepository(MetricStatusesDapperContext context) | ||
public SqlServerMetricStatusesRepository(IOptions<MetricStatusesRepositoryOptions> options) | ||
{ | ||
_context = context; | ||
_options = options.Value; | ||
} | ||
|
||
private readonly MetricStatusesDapperContext _context; | ||
|
||
public async Task<IEnumerable<MetricStatus>> GetMetricStatuses(IdentityAddress identityAddress, IEnumerable<MetricKey> keys) | ||
{ | ||
using var connection = _context.Connection; | ||
await using var connection = new SqlConnection(_options.ConnectionString); | ||
|
||
var metricStatuses = await connection.QueryAsync<MetricStatus>(QUERY, new | ||
{ | ||
identityAddress = identityAddress.ToString(), | ||
keys = keys.Select(x => x.Value) | ||
keys = keys.Select(x => x.Value).ToArray() | ||
}); | ||
|
||
return metricStatuses; | ||
} | ||
} |
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