mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-04-05 18:09:04 +00:00
Compare commits
2 Commits
main-2.0
...
56c83db207
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
56c83db207 | ||
|
|
7fc9ea03ef |
@@ -1,20 +1,15 @@
|
|||||||
using API.Core;
|
using API.Core;
|
||||||
using API.Core.Authentication;
|
using API.Core.Authentication;
|
||||||
using API.Core.Contracts.Common;
|
|
||||||
using Domain.Exceptions;
|
|
||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
using FluentValidation.AspNetCore;
|
using FluentValidation.AspNetCore;
|
||||||
using Infrastructure.Email;
|
using Infrastructure.Email;
|
||||||
using Infrastructure.Email.Templates;
|
|
||||||
using Infrastructure.Email.Templates.Rendering;
|
using Infrastructure.Email.Templates.Rendering;
|
||||||
using Infrastructure.Jwt;
|
using Infrastructure.Jwt;
|
||||||
using Infrastructure.PasswordHashing;
|
using Infrastructure.PasswordHashing;
|
||||||
using Infrastructure.Repository.Auth;
|
using Infrastructure.Repository.Auth;
|
||||||
using Infrastructure.Repository.Sql;
|
using Infrastructure.Repository.Sql;
|
||||||
using Infrastructure.Repository.UserAccount;
|
using Infrastructure.Repository.UserAccount;
|
||||||
using Microsoft.AspNetCore.Authentication;
|
using Infrastructure.Repository.Breweries;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using Microsoft.AspNetCore.Mvc.Filters;
|
|
||||||
using Service.Auth;
|
using Service.Auth;
|
||||||
using Service.Emails;
|
using Service.Emails;
|
||||||
using Service.UserManagement.User;
|
using Service.UserManagement.User;
|
||||||
@@ -55,6 +50,7 @@ builder.Services.AddSingleton<
|
|||||||
|
|
||||||
builder.Services.AddScoped<IUserAccountRepository, UserAccountRepository>();
|
builder.Services.AddScoped<IUserAccountRepository, UserAccountRepository>();
|
||||||
builder.Services.AddScoped<IAuthRepository, AuthRepository>();
|
builder.Services.AddScoped<IAuthRepository, AuthRepository>();
|
||||||
|
builder.Services.AddScoped<IBreweryRepository, BreweryRepository>();
|
||||||
|
|
||||||
builder.Services.AddScoped<IUserService, UserService>();
|
builder.Services.AddScoped<IUserService, UserService>();
|
||||||
builder.Services.AddScoped<ILoginService, LoginService>();
|
builder.Services.AddScoped<ILoginService, LoginService>();
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ 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;
|
||||||
|
|
||||||
@@ -38,8 +39,12 @@ BEGIN
|
|||||||
VALUES (@NewBreweryID, @BreweryName, @Description, @PostedByID);
|
VALUES (@NewBreweryID, @BreweryName, @Description, @PostedByID);
|
||||||
|
|
||||||
INSERT INTO dbo.BreweryPostLocation
|
INSERT INTO dbo.BreweryPostLocation
|
||||||
(@NewBreweryID, CityID, AddressLine1, AddressLine2, PostalCode, Coordinates)
|
(BreweryPostLocationID, BreweryPostID, CityID, AddressLine1, AddressLine2, PostalCode, Coordinates)
|
||||||
VALUES (@NewBreweryID, @CityID, @AddressLine1, @AddressLine2, @PostalCode, @Coordinates);
|
VALUES (@NewBrewerLocationID, @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
|
||||||
13
src/Core/Domain/Domain.Entities/Entities/BreweryPost.cs
Normal file
13
src/Core/Domain/Domain.Entities/Entities/BreweryPost.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
namespace Domain.Entities;
|
||||||
|
|
||||||
|
public class BreweryPost
|
||||||
|
{
|
||||||
|
public Guid BreweryPostId { get; set; }
|
||||||
|
public Guid PostedById { get; set; }
|
||||||
|
public string BreweryName { get; set; } = string.Empty;
|
||||||
|
public string Description { get; set; } = string.Empty;
|
||||||
|
public DateTime CreatedAt { get; set; }
|
||||||
|
public DateTime? UpdatedAt { get; set; }
|
||||||
|
public byte[]? Timer { get; set; }
|
||||||
|
public BreweryPostLocation? Location { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
namespace Domain.Entities;
|
||||||
|
|
||||||
|
public class BreweryPostLocation
|
||||||
|
{
|
||||||
|
public Guid BreweryPostLocationId { get; set; }
|
||||||
|
public Guid BreweryPostId { get; set; }
|
||||||
|
public string AddressLine1 { get; set; } = string.Empty;
|
||||||
|
public string? AddressLine2 { get; set; }
|
||||||
|
public string PostalCode { get; set; } = string.Empty;
|
||||||
|
public Guid CityId { get; set; }
|
||||||
|
public byte[]? Coordinates { get; set; }
|
||||||
|
public byte[]? Timer { get; set; }
|
||||||
|
}
|
||||||
@@ -7,6 +7,6 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="MailKit" Version="4.9.0" />
|
<PackageReference Include="MailKit" Version="4.15.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -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(
|
||||||
|
|||||||
@@ -0,0 +1,161 @@
|
|||||||
|
using System.Data.Common;
|
||||||
|
using Domain.Entities;
|
||||||
|
using Infrastructure.Repository.Sql;
|
||||||
|
|
||||||
|
namespace Infrastructure.Repository.Breweries;
|
||||||
|
|
||||||
|
public interface IBreweryRepository
|
||||||
|
{
|
||||||
|
Task<BreweryPost?> GetByIdAsync(Guid id);
|
||||||
|
Task<IEnumerable<BreweryPost>> GetAllAsync(int? limit, int? offset);
|
||||||
|
Task UpdateAsync(BreweryPost brewery);
|
||||||
|
Task DeleteAsync(Guid id);
|
||||||
|
Task CreateAsync(BreweryPost brewery);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class BreweryRepository : Repository<BreweryPost>, IBreweryRepository
|
||||||
|
{
|
||||||
|
private readonly ISqlConnectionFactory _connectionFactory;
|
||||||
|
|
||||||
|
public BreweryRepository(ISqlConnectionFactory connectionFactory)
|
||||||
|
: base(connectionFactory)
|
||||||
|
{
|
||||||
|
_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)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task UpdateAsync(BreweryPost brewery)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task DeleteAsync(Guid id)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task CreateAsync(BreweryPost brewery)
|
||||||
|
{
|
||||||
|
await using var connection = await CreateConnection();
|
||||||
|
await using var command = connection.CreateCommand();
|
||||||
|
|
||||||
|
command.CommandText = "USP_CreateBrewery";
|
||||||
|
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, "@Description", brewery.Description);
|
||||||
|
AddParameter(command, "@PostedByID", brewery.PostedById);
|
||||||
|
AddParameter(command, "@CityID", brewery.Location?.CityId);
|
||||||
|
AddParameter(command, "@AddressLine1", brewery.Location?.AddressLine1);
|
||||||
|
AddParameter(command, "@AddressLine2", brewery.Location?.AddressLine2);
|
||||||
|
AddParameter(command, "@PostalCode", brewery.Location?.PostalCode);
|
||||||
|
AddParameter(command, "@Coordinates", brewery.Location?.Coordinates);
|
||||||
|
await command.ExecuteNonQueryAsync();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override BreweryPost MapToEntity(DbDataReader reader)
|
||||||
|
{
|
||||||
|
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(
|
||||||
|
DbCommand command,
|
||||||
|
string name,
|
||||||
|
object? value
|
||||||
|
)
|
||||||
|
{
|
||||||
|
var p = command.CreateParameter();
|
||||||
|
p.ParameterName = name;
|
||||||
|
p.Value = value ?? DBNull.Value;
|
||||||
|
command.Parameters.Add(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user