This commit is contained in:
Aaron Po
2026-08-19 16:27:55 -04:00
parent ab265de54d
commit 2b2df0053f

View File

@@ -76,7 +76,8 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<IEnumerable<BreweryPost>> GetAllAsync(int? limit, int? offset) public async Task<IEnumerable<BreweryPost>> GetAllAsync(int? limit,
int? offset)
{ {
await using DbConnection connection = await CreateConnection(); await using DbConnection connection = await CreateConnection();
await using DbCommand command = connection.CreateCommand(); await using DbCommand command = connection.CreateCommand();
@@ -102,14 +103,20 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
public async Task<BreweryPost> UpdateAsync(BreweryPost brewery) public async Task<BreweryPost> UpdateAsync(BreweryPost brewery)
{ {
await using DbConnection connection = await CreateConnection(); await using DbConnection connection = await CreateConnection();
await using DbTransaction transaction = await connection.BeginTransactionAsync(); await using DbTransaction transaction =
await connection.BeginTransactionAsync();
try try
{ {
bool breweryExists = bool breweryExists =
await connection.ExecuteScalarAsync<int?>( await connection.ExecuteScalarAsync<int?>(
new CommandDefinition( new CommandDefinition(
"SELECT 1 FROM dbo.BreweryPost WHERE BreweryPostID = @BreweryPostId", """
SELECT 1 FROM
dbo.BreweryPost
WHERE
BreweryPostID = @BreweryPostId
""",
new { brewery.BreweryPostId }, new { brewery.BreweryPostId },
transaction transaction
) )
@@ -139,8 +146,12 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
new CommandDefinition( new CommandDefinition(
""" """
UPDATE dbo.BreweryPost UPDATE dbo.BreweryPost
SET BreweryName = @BreweryName, Description = @Description, UpdatedAt = GETDATE() SET BreweryName = @BreweryName,
WHERE BreweryPostID = @BreweryPostId AND RowVersion = @RowVersion Description = @Description,
UpdatedAt = GETDATE()
WHERE
BreweryPostID = @BreweryPostId
AND RowVersion = @RowVersion
""", """,
new new
{ {
@@ -163,7 +174,11 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
// No location supplied: clear any existing location for this brewery. // No location supplied: clear any existing location for this brewery.
await connection.ExecuteAsync( await connection.ExecuteAsync(
new CommandDefinition( new CommandDefinition(
"DELETE FROM dbo.BreweryPostLocation WHERE BreweryPostID = @BreweryPostId", """
DELETE FROM
dbo.BreweryPostLocation
WHERE BreweryPostID = @BreweryPostId
""",
new { brewery.BreweryPostId }, new { brewery.BreweryPostId },
transaction transaction
) )
@@ -174,7 +189,11 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
bool locationExists = bool locationExists =
await connection.ExecuteScalarAsync<int?>( await connection.ExecuteScalarAsync<int?>(
new CommandDefinition( new CommandDefinition(
"SELECT 1 FROM dbo.BreweryPostLocation WHERE BreweryPostID = @BreweryPostId", """
SELECT 1
FROM dbo.BreweryPostLocation
WHERE BreweryPostID = @BreweryPostId
""",
new { brewery.BreweryPostId }, new { brewery.BreweryPostId },
transaction transaction
) )
@@ -186,8 +205,11 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
new CommandDefinition( new CommandDefinition(
""" """
UPDATE dbo.BreweryPostLocation UPDATE dbo.BreweryPostLocation
SET CityID = @CityId, AddressLine1 = @AddressLine1, AddressLine2 = @AddressLine2, SET CityID = @CityId,
PostalCode = @PostalCode, Coordinates = @Coordinates AddressLine1 = @AddressLine1,
AddressLine2 = @AddressLine2,
PostalCode = @PostalCode,
Coordinates = @Coordinates
WHERE BreweryPostID = @BreweryPostId WHERE BreweryPostID = @BreweryPostId
""", """,
new new
@@ -257,7 +279,12 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
{ {
await using DbConnection connection = await CreateConnection(); await using DbConnection connection = await CreateConnection();
int rows = await connection.ExecuteAsync( int rows = await connection.ExecuteAsync(
"DELETE FROM dbo.BreweryPost WHERE BreweryPostID = @BreweryPostId", """
DELETE FROM
dbo.BreweryPost
WHERE
BreweryPostID = @BreweryPostId
""",
new { BreweryPostId = id } new { BreweryPostId = id }
); );
@@ -269,17 +296,25 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
public async Task CreateAsync(BreweryPost brewery) public async Task CreateAsync(BreweryPost brewery)
{ {
if (brewery.Location is null) if (brewery.Location is null)
throw new ArgumentException("Location must be provided when creating a brewery."); throw new ArgumentException(
"Location must be provided when creating a brewery.");
await using DbConnection connection = await CreateConnection(); await using DbConnection connection = await CreateConnection();
await using DbTransaction transaction = await connection.BeginTransactionAsync(); await using DbTransaction transaction =
await connection.BeginTransactionAsync();
try try
{ {
bool userExists = bool userExists =
await connection.ExecuteScalarAsync<int?>( await connection.ExecuteScalarAsync<int?>(
new CommandDefinition( new CommandDefinition(
"SELECT 1 FROM dbo.UserAccount WHERE UserAccountID = @PostedById", """
SELECT 1
FROM
dbo.UserAccount
WHERE
UserAccountID = @PostedById
""",
new { brewery.PostedById }, new { brewery.PostedById },
transaction transaction
) )
@@ -292,7 +327,13 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
bool cityExists = bool cityExists =
await connection.ExecuteScalarAsync<int?>( await connection.ExecuteScalarAsync<int?>(
new CommandDefinition( new CommandDefinition(
"SELECT 1 FROM dbo.City WHERE CityID = @CityId", """
SELECT 1
FROM
dbo.City
WHERE
CityID = @CityId
""",
new { brewery.Location.CityId }, new { brewery.Location.CityId },
transaction transaction
) )
@@ -306,15 +347,9 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
new CommandDefinition( new CommandDefinition(
""" """
INSERT INTO dbo.BreweryPost INSERT INTO dbo.BreweryPost
(BreweryPostID, (BreweryPostID, BreweryName, Description, PostedByID)
BreweryName,
Description,
PostedByID)
VALUES VALUES
(@BreweryPostId, (@BreweryPostId, @BreweryName, @Description, @PostedById)
@BreweryName,
@Description,
@PostedById)
""", """,
new new
{ {
@@ -331,8 +366,20 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
new CommandDefinition( new CommandDefinition(
""" """
INSERT INTO dbo.BreweryPostLocation INSERT INTO dbo.BreweryPostLocation
(BreweryPostLocationID, BreweryPostID, CityID, AddressLine1, AddressLine2, PostalCode, Coordinates) (BreweryPostLocationID,
VALUES (@BreweryPostLocationId, @BreweryPostId, @CityId, @AddressLine1, @AddressLine2, @PostalCode, @Coordinates) BreweryPostID,
CityID,
AddressLine1,
AddressLine2,
PostalCode,
Coordinates)
VALUES (@BreweryPostLocationId,
@BreweryPostId,
@CityId,
@AddressLine1,
@AddressLine2,
@PostalCode,
@Coordinates)
""", """,
new new
{ {
@@ -378,7 +425,9 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
BreweryName = reader.GetString(ordBreweryName), BreweryName = reader.GetString(ordBreweryName),
Description = reader.GetString(ordDescription), Description = reader.GetString(ordDescription),
CreatedAt = reader.GetDateTime(ordCreatedAt), CreatedAt = reader.GetDateTime(ordCreatedAt),
UpdatedAt = reader.IsDBNull(ordUpdatedAt) ? null : reader.GetDateTime(ordUpdatedAt), UpdatedAt = reader.IsDBNull(ordUpdatedAt)
? null
: reader.GetDateTime(ordUpdatedAt),
RowVersion = reader.IsDBNull(ordRowVersion) RowVersion = reader.IsDBNull(ordRowVersion)
? null ? null
: reader.GetFieldValue<byte[]>(ordRowVersion), : reader.GetFieldValue<byte[]>(ordRowVersion),
@@ -396,7 +445,8 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
BreweryPostId = brewery.BreweryPostId, BreweryPostId = brewery.BreweryPostId,
CityId = reader.GetGuid(reader.GetOrdinal("CityId")), CityId = reader.GetGuid(reader.GetOrdinal("CityId")),
City = MapCity(reader), City = MapCity(reader),
AddressLine1 = reader.GetString(reader.GetOrdinal("AddressLine1")), AddressLine1 =
reader.GetString(reader.GetOrdinal("AddressLine1")),
AddressLine2 = reader.IsDBNull(ordAddressLine2) AddressLine2 = reader.IsDBNull(ordAddressLine2)
? null ? null
: reader.GetString(ordAddressLine2), : reader.GetString(ordAddressLine2),
@@ -421,24 +471,30 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
{ {
CityId = reader.GetGuid(reader.GetOrdinal("CityId")), CityId = reader.GetGuid(reader.GetOrdinal("CityId")),
CityName = reader.GetString(reader.GetOrdinal("CityName")), CityName = reader.GetString(reader.GetOrdinal("CityName")),
StateProvinceId = reader.GetGuid(reader.GetOrdinal("StateProvinceId")), StateProvinceId =
reader.GetGuid(reader.GetOrdinal("StateProvinceId")),
StateProvince = new StateProvince StateProvince = new StateProvince
{ {
StateProvinceId = reader.GetGuid(reader.GetOrdinal("StateProvinceId")), StateProvinceId =
StateProvinceName = reader.GetString(reader.GetOrdinal("StateProvinceName")), reader.GetGuid(reader.GetOrdinal("StateProvinceId")),
StateProvinceName =
reader.GetString(reader.GetOrdinal("StateProvinceName")),
ISO3166_2 = reader.GetString(reader.GetOrdinal("ISO3166_2")), ISO3166_2 = reader.GetString(reader.GetOrdinal("ISO3166_2")),
CountryId = reader.GetGuid(reader.GetOrdinal("CountryId")), CountryId = reader.GetGuid(reader.GetOrdinal("CountryId")),
Country = new Country Country = new Country
{ {
CountryId = reader.GetGuid(reader.GetOrdinal("CountryId")), CountryId = reader.GetGuid(reader.GetOrdinal("CountryId")),
CountryName = reader.GetString(reader.GetOrdinal("CountryName")), CountryName =
ISO3166_1 = reader.GetString(reader.GetOrdinal("ISO3166_1")), reader.GetString(reader.GetOrdinal("CountryName")),
ISO3166_1 =
reader.GetString(reader.GetOrdinal("ISO3166_1")),
}, },
}, },
}; };
} }
private static void AddParameter(DbCommand command, string name, object? value) private static void AddParameter(DbCommand command, string name,
object? value)
{ {
DbParameter p = command.CreateParameter(); DbParameter p = command.CreateParameter();
p.ParameterName = name; p.ParameterName = name;