mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-04-05 18:09:04 +00:00
Implement brewery repo, SQL procs and tests
This commit is contained in:
@@ -30,18 +30,21 @@ BEGIN
|
|||||||
THROW 50404, 'City not found.', 1;
|
THROW 50404, 'City not found.', 1;
|
||||||
|
|
||||||
DECLARE @NewBreweryID UNIQUEIDENTIFIER = NEWID();
|
DECLARE @NewBreweryID UNIQUEIDENTIFIER = NEWID();
|
||||||
|
DECLARE @NewBrewerLocationID UNIQUEIDENTIFIER = NEWID();
|
||||||
|
|
||||||
BEGIN TRANSACTION;
|
BEGIN TRANSACTION;
|
||||||
|
|
||||||
INSERT INTO dbo.BreweryPost
|
INSERT INTO dbo.BreweryPost
|
||||||
(BreweryPostID, BreweryName, Description, PostedByID)
|
(BreweryPostID, BreweryName, Description, PostedByID)
|
||||||
VALUES
|
VALUES (@NewBreweryID, @BreweryName, @Description, @PostedByID);
|
||||||
(@NewBreweryID, @BreweryName, @Description, @PostedByID);
|
|
||||||
|
|
||||||
INSERT INTO dbo.BreweryPostLocation
|
INSERT INTO dbo.BreweryPostLocation
|
||||||
(BreweryPostID, CityID, AddressLine1, AddressLine2, PostalCode, Coordinates)
|
(BreweryPostLocationID, BreweryPostID, CityID, AddressLine1, AddressLine2, PostalCode, Coordinates)
|
||||||
VALUES
|
VALUES (@NewBrewerLocationID, @NewBreweryID, @CityID, @AddressLine1, @AddressLine2, @PostalCode, @Coordinates);
|
||||||
(@NewBreweryID, @CityID, @AddressLine1, @AddressLine2, @PostalCode, @Coordinates);
|
|
||||||
|
|
||||||
COMMIT TRANSACTION;
|
COMMIT TRANSACTION;
|
||||||
|
|
||||||
|
SELECT @NewBreweryID AS BreweryPostID,
|
||||||
|
@NewBrewerLocationID AS BreweryPostLocationID;
|
||||||
|
|
||||||
END
|
END
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
CREATE OR ALTER PROCEDURE dbo.USP_GetBreweryById @BreweryPostID UNIQUEIDENTIFIER
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SELECT *
|
||||||
|
FROM BreweryPost bp
|
||||||
|
INNER JOIN BreweryPostLocation bpl
|
||||||
|
ON bp.BreweryPostID = bpl.BreweryPostID
|
||||||
|
WHERE bp.BreweryPostID = @BreweryPostID;
|
||||||
|
END
|
||||||
@@ -9,4 +9,5 @@ public class BreweryPost
|
|||||||
public DateTime CreatedAt { get; set; }
|
public DateTime CreatedAt { get; set; }
|
||||||
public DateTime? UpdatedAt { get; set; }
|
public DateTime? UpdatedAt { get; set; }
|
||||||
public byte[]? Timer { get; set; }
|
public byte[]? Timer { get; set; }
|
||||||
|
public BreweryPostLocation? Location { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,108 @@
|
|||||||
|
using Apps72.Dev.Data.DbMocker;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Infrastructure.Repository.Breweries;
|
||||||
|
using Infrastructure.Repository.Tests.Database;
|
||||||
|
using Domain.Entities;
|
||||||
|
|
||||||
|
namespace Infrastructure.Repository.Tests.Breweries;
|
||||||
|
|
||||||
|
public class BreweryRepositoryTest
|
||||||
|
{
|
||||||
|
private static BreweryRepository CreateRepo(MockDbConnection conn) =>
|
||||||
|
new(new TestConnectionFactory(conn));
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetByIdAsync_ReturnsBrewery_WhenExists()
|
||||||
|
{
|
||||||
|
var breweryId = Guid.NewGuid();
|
||||||
|
var conn = new MockDbConnection();
|
||||||
|
|
||||||
|
// Repository calls the stored procedure
|
||||||
|
const string getByIdSql = "USP_GetBreweryById";
|
||||||
|
|
||||||
|
var locationId = Guid.NewGuid();
|
||||||
|
|
||||||
|
conn.Mocks.When(cmd => cmd.CommandText == getByIdSql)
|
||||||
|
.ReturnsTable(
|
||||||
|
MockTable
|
||||||
|
.WithColumns(
|
||||||
|
("BreweryPostId", typeof(Guid)),
|
||||||
|
("PostedById", typeof(Guid)),
|
||||||
|
("BreweryName", typeof(string)),
|
||||||
|
("Description", typeof(string)),
|
||||||
|
("CreatedAt", typeof(DateTime)),
|
||||||
|
("UpdatedAt", typeof(DateTime?)),
|
||||||
|
("Timer", typeof(byte[])),
|
||||||
|
("BreweryPostLocationId", typeof(Guid)),
|
||||||
|
("CityId", typeof(Guid)),
|
||||||
|
("AddressLine1", typeof(string)),
|
||||||
|
("AddressLine2", typeof(string)),
|
||||||
|
("PostalCode", typeof(string)),
|
||||||
|
("Coordinates", typeof(byte[]))
|
||||||
|
)
|
||||||
|
.AddRow(
|
||||||
|
breweryId,
|
||||||
|
Guid.NewGuid(),
|
||||||
|
"Test Brewery",
|
||||||
|
"A test brewery description",
|
||||||
|
DateTime.UtcNow,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
locationId,
|
||||||
|
Guid.NewGuid(),
|
||||||
|
"123 Main St",
|
||||||
|
null,
|
||||||
|
"12345",
|
||||||
|
null
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
var repo = CreateRepo(conn);
|
||||||
|
var result = await repo.GetByIdAsync(breweryId);
|
||||||
|
result.Should().NotBeNull();
|
||||||
|
result!.BreweryPostId.Should().Be(breweryId);
|
||||||
|
result.Location.Should().NotBeNull();
|
||||||
|
result.Location!.BreweryPostLocationId.Should().Be(locationId);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task GetByIdAsync_ReturnsNull_WhenNotExists()
|
||||||
|
{
|
||||||
|
var conn = new MockDbConnection();
|
||||||
|
conn.Mocks.When(cmd => cmd.CommandText == "USP_GetBreweryById")
|
||||||
|
.ReturnsTable(MockTable.Empty());
|
||||||
|
var repo = CreateRepo(conn);
|
||||||
|
var result = await repo.GetByIdAsync(Guid.NewGuid());
|
||||||
|
result.Should().BeNull();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CreateAsync_ExecutesSuccessfully()
|
||||||
|
{
|
||||||
|
var conn = new MockDbConnection();
|
||||||
|
conn.Mocks.When(cmd => cmd.CommandText == "USP_CreateBrewery")
|
||||||
|
.ReturnsScalar(1);
|
||||||
|
var repo = CreateRepo(conn);
|
||||||
|
var brewery = new BreweryPost
|
||||||
|
{
|
||||||
|
BreweryPostId = Guid.NewGuid(),
|
||||||
|
PostedById = Guid.NewGuid(),
|
||||||
|
BreweryName = "Test Brewery",
|
||||||
|
Description = "A test brewery description",
|
||||||
|
CreatedAt = DateTime.UtcNow,
|
||||||
|
Location = new BreweryPostLocation
|
||||||
|
{
|
||||||
|
BreweryPostLocationId = Guid.NewGuid(),
|
||||||
|
CityId = Guid.NewGuid(),
|
||||||
|
AddressLine1 = "123 Main St",
|
||||||
|
PostalCode = "12345",
|
||||||
|
Coordinates = [0x00, 0x01]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Should not throw
|
||||||
|
var act = async () => await repo.CreateAsync(brewery);
|
||||||
|
await act.Should().NotThrowAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -35,16 +35,7 @@ public class AuthRepository(ISqlConnectionFactory connectionFactory)
|
|||||||
var result = await command.ExecuteScalarAsync();
|
var result = await command.ExecuteScalarAsync();
|
||||||
var userAccountId = result != null ? (Guid)result : Guid.Empty;
|
var userAccountId = result != null ? (Guid)result : Guid.Empty;
|
||||||
|
|
||||||
return new Domain.Entities.UserAccount
|
return await GetUserByIdAsync(userAccountId) ?? throw new Exception("Failed to retrieve newly registered user.");
|
||||||
{
|
|
||||||
UserAccountId = userAccountId,
|
|
||||||
Username = username,
|
|
||||||
FirstName = firstName,
|
|
||||||
LastName = lastName,
|
|
||||||
Email = email,
|
|
||||||
DateOfBirth = dateOfBirth,
|
|
||||||
CreatedAt = DateTime.UtcNow,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Domain.Entities.UserAccount?> GetUserByEmailAsync(
|
public async Task<Domain.Entities.UserAccount?> GetUserByEmailAsync(
|
||||||
|
|||||||
@@ -13,15 +13,31 @@ public interface IBreweryRepository
|
|||||||
Task CreateAsync(BreweryPost brewery);
|
Task CreateAsync(BreweryPost brewery);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
public class BreweryRepository : Repository<BreweryPost>, IBreweryRepository
|
||||||
: Repository<BreweryPost>(connectionFactory),
|
|
||||||
IBreweryRepository
|
|
||||||
{
|
{
|
||||||
private static ISqlConnectionFactory? _connectionFactory;
|
private readonly ISqlConnectionFactory _connectionFactory;
|
||||||
|
|
||||||
public Task<BreweryPost?> GetByIdAsync(Guid id)
|
public BreweryRepository(ISqlConnectionFactory connectionFactory)
|
||||||
|
: base(connectionFactory)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
_connectionFactory = connectionFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<BreweryPost?> GetByIdAsync(Guid id)
|
||||||
|
{
|
||||||
|
await using var connection = await CreateConnection();
|
||||||
|
await using var command = connection.CreateCommand();
|
||||||
|
command.CommandType = System.Data.CommandType.StoredProcedure;
|
||||||
|
|
||||||
|
command.CommandText = "USP_GetBreweryById";
|
||||||
|
AddParameter(command, "@BreweryPostID", id);
|
||||||
|
|
||||||
|
await using var reader = await command.ExecuteReaderAsync();
|
||||||
|
if (await reader.ReadAsync())
|
||||||
|
{
|
||||||
|
return MapToEntity(reader);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<IEnumerable<BreweryPost>> GetAllAsync(int? limit, int? offset)
|
public Task<IEnumerable<BreweryPost>> GetAllAsync(int? limit, int? offset)
|
||||||
@@ -39,29 +55,96 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
|||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task CreateAsync(BreweryPost brewery, BreweryPostLocation location)
|
public async Task CreateAsync(BreweryPost brewery)
|
||||||
{
|
{
|
||||||
await using var connection = await CreateConnection();
|
await using var connection = await CreateConnection();
|
||||||
await using var command = connection.CreateCommand();
|
await using var command = connection.CreateCommand();
|
||||||
|
|
||||||
command.CommandText = "USP_CreateBreweryPost";
|
command.CommandText = "USP_CreateBrewery";
|
||||||
command.CommandType = System.Data.CommandType.StoredProcedure;
|
command.CommandType = System.Data.CommandType.StoredProcedure;
|
||||||
|
|
||||||
|
if (brewery.Location is null)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Location must be provided when creating a brewery.");
|
||||||
|
}
|
||||||
|
|
||||||
AddParameter(command, "@BreweryName", brewery.BreweryName);
|
AddParameter(command, "@BreweryName", brewery.BreweryName);
|
||||||
AddParameter(command, "@Description", brewery.Description);
|
AddParameter(command, "@Description", brewery.Description);
|
||||||
AddParameter(command, "@PostedByID", brewery.PostedById);
|
AddParameter(command, "@PostedByID", brewery.PostedById);
|
||||||
AddParameter(command, "@CityID", location.CityId);
|
AddParameter(command, "@CityID", brewery.Location?.CityId);
|
||||||
AddParameter(command, "@AddressLine1", location.AddressLine1);
|
AddParameter(command, "@AddressLine1", brewery.Location?.AddressLine1);
|
||||||
AddParameter(command, "@AddressLine2", location.AddressLine2);
|
AddParameter(command, "@AddressLine2", brewery.Location?.AddressLine2);
|
||||||
AddParameter(command, "@PostalCode", location.PostalCode);
|
AddParameter(command, "@PostalCode", brewery.Location?.PostalCode);
|
||||||
AddParameter(command, "@Coordinates", location.Coordinates);
|
AddParameter(command, "@Coordinates", brewery.Location?.Coordinates);
|
||||||
await command.ExecuteNonQueryAsync();
|
await command.ExecuteNonQueryAsync();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override BreweryPost MapToEntity(DbDataReader reader)
|
protected override BreweryPost MapToEntity(DbDataReader reader)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
var brewery = new BreweryPost();
|
||||||
|
|
||||||
|
var ordBreweryPostId = reader.GetOrdinal("BreweryPostId");
|
||||||
|
var ordPostedById = reader.GetOrdinal("PostedById");
|
||||||
|
var ordBreweryName = reader.GetOrdinal("BreweryName");
|
||||||
|
var ordDescription = reader.GetOrdinal("Description");
|
||||||
|
var ordCreatedAt = reader.GetOrdinal("CreatedAt");
|
||||||
|
var ordUpdatedAt = reader.GetOrdinal("UpdatedAt");
|
||||||
|
var ordTimer = reader.GetOrdinal("Timer");
|
||||||
|
|
||||||
|
brewery.BreweryPostId = reader.GetGuid(ordBreweryPostId);
|
||||||
|
brewery.PostedById = reader.GetGuid(ordPostedById);
|
||||||
|
brewery.BreweryName = reader.GetString(ordBreweryName);
|
||||||
|
brewery.Description = reader.GetString(ordDescription);
|
||||||
|
brewery.CreatedAt = reader.GetDateTime(ordCreatedAt);
|
||||||
|
|
||||||
|
brewery.UpdatedAt = reader.IsDBNull(ordUpdatedAt) ? null : reader.GetDateTime(ordUpdatedAt);
|
||||||
|
|
||||||
|
// Read timer (varbinary/rowversion) robustly
|
||||||
|
if (reader.IsDBNull(ordTimer))
|
||||||
|
{
|
||||||
|
brewery.Timer = null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
brewery.Timer = reader.GetFieldValue<byte[]>(ordTimer);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
var length = reader.GetBytes(ordTimer, 0, null, 0, 0);
|
||||||
|
var buffer = new byte[length];
|
||||||
|
reader.GetBytes(ordTimer, 0, buffer, 0, (int)length);
|
||||||
|
brewery.Timer = buffer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map BreweryPostLocation if columns are present
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var ordLocationId = reader.GetOrdinal("BreweryPostLocationId");
|
||||||
|
if (!reader.IsDBNull(ordLocationId))
|
||||||
|
{
|
||||||
|
var location = new BreweryPostLocation
|
||||||
|
{
|
||||||
|
BreweryPostLocationId = reader.GetGuid(ordLocationId),
|
||||||
|
BreweryPostId = reader.GetGuid(reader.GetOrdinal("BreweryPostId")),
|
||||||
|
CityId = reader.GetGuid(reader.GetOrdinal("CityId")),
|
||||||
|
AddressLine1 = reader.GetString(reader.GetOrdinal("AddressLine1")),
|
||||||
|
AddressLine2 = reader.IsDBNull(reader.GetOrdinal("AddressLine2")) ? null : reader.GetString(reader.GetOrdinal("AddressLine2")),
|
||||||
|
PostalCode = reader.GetString(reader.GetOrdinal("PostalCode")),
|
||||||
|
Coordinates = reader.IsDBNull(reader.GetOrdinal("Coordinates")) ? null : reader.GetFieldValue<byte[]>(reader.GetOrdinal("Coordinates"))
|
||||||
|
};
|
||||||
|
brewery.Location = location;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IndexOutOfRangeException)
|
||||||
|
{
|
||||||
|
// Location columns not present, skip mapping location
|
||||||
|
}
|
||||||
|
|
||||||
|
return brewery;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void AddParameter(
|
private static void AddParameter(
|
||||||
@@ -75,9 +158,4 @@ public class BreweryRepository(ISqlConnectionFactory connectionFactory)
|
|||||||
p.Value = value ?? DBNull.Value;
|
p.Value = value ?? DBNull.Value;
|
||||||
command.Parameters.Add(p);
|
command.Parameters.Add(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task CreateAsync(BreweryPost brewery)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user