From 7fc9ea03ef04cf9c2642ff67f6d17dcce0751bfc Mon Sep 17 00:00:00 2001 From: Aaron Po Date: Sun, 29 Mar 2026 13:33:43 -0400 Subject: [PATCH] Add create brewery to brewery repository --- src/Core/API/API.Core/Program.cs | 8 +- .../05-Breweries/USP_CreateBrewery.sql | 20 +++-- .../Domain.Entities/Entities/BreweryPost.cs | 12 +++ .../Entities/BreweryPostLocation.cs | 13 +++ .../Infrastructure.Email.csproj | 2 +- .../Breweries/BreweryRepository.cs | 83 +++++++++++++++++++ 6 files changed, 122 insertions(+), 16 deletions(-) create mode 100644 src/Core/Domain/Domain.Entities/Entities/BreweryPost.cs create mode 100644 src/Core/Domain/Domain.Entities/Entities/BreweryPostLocation.cs create mode 100644 src/Core/Infrastructure/Infrastructure.Repository/Breweries/BreweryRepository.cs diff --git a/src/Core/API/API.Core/Program.cs b/src/Core/API/API.Core/Program.cs index e186dba..f6c7aee 100644 --- a/src/Core/API/API.Core/Program.cs +++ b/src/Core/API/API.Core/Program.cs @@ -1,20 +1,15 @@ using API.Core; using API.Core.Authentication; -using API.Core.Contracts.Common; -using Domain.Exceptions; using FluentValidation; using FluentValidation.AspNetCore; using Infrastructure.Email; -using Infrastructure.Email.Templates; using Infrastructure.Email.Templates.Rendering; using Infrastructure.Jwt; using Infrastructure.PasswordHashing; using Infrastructure.Repository.Auth; using Infrastructure.Repository.Sql; using Infrastructure.Repository.UserAccount; -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.Filters; +using Infrastructure.Repository.Breweries; using Service.Auth; using Service.Emails; using Service.UserManagement.User; @@ -55,6 +50,7 @@ builder.Services.AddSingleton< builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); diff --git a/src/Core/Database/Database.Migrations/scripts/03-crud/05-Breweries/USP_CreateBrewery.sql b/src/Core/Database/Database.Migrations/scripts/03-crud/05-Breweries/USP_CreateBrewery.sql index 38cdf51..b43896e 100644 --- a/src/Core/Database/Database.Migrations/scripts/03-crud/05-Breweries/USP_CreateBrewery.sql +++ b/src/Core/Database/Database.Migrations/scripts/03-crud/05-Breweries/USP_CreateBrewery.sql @@ -12,7 +12,7 @@ AS BEGIN SET NOCOUNT ON; SET XACT_ABORT ON; - + IF @BreweryName IS NULL THROW 50001, 'Brewery name cannot be null.', 1; @@ -20,13 +20,13 @@ BEGIN THROW 50002, 'Brewery description cannot be null.', 1; IF NOT EXISTS (SELECT 1 - FROM dbo.UserAccount - WHERE UserAccountID = @PostedByID) + FROM dbo.UserAccount + WHERE UserAccountID = @PostedByID) THROW 50404, 'User not found.', 1; IF NOT EXISTS (SELECT 1 - FROM dbo.City - WHERE CityID = @CityID) + FROM dbo.City + WHERE CityID = @CityID) THROW 50404, 'City not found.', 1; DECLARE @NewBreweryID UNIQUEIDENTIFIER = NEWID(); @@ -35,11 +35,13 @@ BEGIN INSERT INTO dbo.BreweryPost (BreweryPostID, BreweryName, Description, PostedByID) - VALUES (@NewBreweryID, @BreweryName, @Description, @PostedByID); + VALUES + (@NewBreweryID, @BreweryName, @Description, @PostedByID); - INSERT INTO dbo.BreweryPostLocation - (@NewBreweryID, CityID, AddressLine1, AddressLine2, PostalCode, Coordinates) - VALUES (@NewBreweryID, @CityID, @AddressLine1, @AddressLine2, @PostalCode, @Coordinates); + INSERT INTO dbo.BreweryPostLocation + (BreweryPostID, CityID, AddressLine1, AddressLine2, PostalCode, Coordinates) + VALUES + (@NewBreweryID, @CityID, @AddressLine1, @AddressLine2, @PostalCode, @Coordinates); COMMIT TRANSACTION; END diff --git a/src/Core/Domain/Domain.Entities/Entities/BreweryPost.cs b/src/Core/Domain/Domain.Entities/Entities/BreweryPost.cs new file mode 100644 index 0000000..41426f5 --- /dev/null +++ b/src/Core/Domain/Domain.Entities/Entities/BreweryPost.cs @@ -0,0 +1,12 @@ +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; } +} diff --git a/src/Core/Domain/Domain.Entities/Entities/BreweryPostLocation.cs b/src/Core/Domain/Domain.Entities/Entities/BreweryPostLocation.cs new file mode 100644 index 0000000..460e30a --- /dev/null +++ b/src/Core/Domain/Domain.Entities/Entities/BreweryPostLocation.cs @@ -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; } +} diff --git a/src/Core/Infrastructure/Infrastructure.Email/Infrastructure.Email.csproj b/src/Core/Infrastructure/Infrastructure.Email/Infrastructure.Email.csproj index b02eae8..717486f 100644 --- a/src/Core/Infrastructure/Infrastructure.Email/Infrastructure.Email.csproj +++ b/src/Core/Infrastructure/Infrastructure.Email/Infrastructure.Email.csproj @@ -7,6 +7,6 @@ - + diff --git a/src/Core/Infrastructure/Infrastructure.Repository/Breweries/BreweryRepository.cs b/src/Core/Infrastructure/Infrastructure.Repository/Breweries/BreweryRepository.cs new file mode 100644 index 0000000..f5fe035 --- /dev/null +++ b/src/Core/Infrastructure/Infrastructure.Repository/Breweries/BreweryRepository.cs @@ -0,0 +1,83 @@ +using System.Data.Common; +using Domain.Entities; +using Infrastructure.Repository.Sql; + +namespace Infrastructure.Repository.Breweries; + +public interface IBreweryRepository +{ + Task GetByIdAsync(Guid id); + Task> GetAllAsync(int? limit, int? offset); + Task UpdateAsync(BreweryPost brewery); + Task DeleteAsync(Guid id); + Task CreateAsync(BreweryPost brewery); +} + +public class BreweryRepository(ISqlConnectionFactory connectionFactory) + : Repository(connectionFactory), + IBreweryRepository +{ + private static ISqlConnectionFactory? _connectionFactory; + + public Task GetByIdAsync(Guid id) + { + throw new NotImplementedException(); + } + + public Task> 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, BreweryPostLocation location) + { + await using var connection = await CreateConnection(); + await using var command = connection.CreateCommand(); + + command.CommandText = "USP_CreateBreweryPost"; + command.CommandType = System.Data.CommandType.StoredProcedure; + + AddParameter(command, "@BreweryName", brewery.BreweryName); + AddParameter(command, "@Description", brewery.Description); + AddParameter(command, "@PostedByID", brewery.PostedById); + AddParameter(command, "@CityID", location.CityId); + AddParameter(command, "@AddressLine1", location.AddressLine1); + AddParameter(command, "@AddressLine2", location.AddressLine2); + AddParameter(command, "@PostalCode", location.PostalCode); + AddParameter(command, "@Coordinates", location.Coordinates); + await command.ExecuteNonQueryAsync(); + + } + + protected override BreweryPost MapToEntity(DbDataReader reader) + { + throw new NotImplementedException(); + } + + 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); + } + + public Task CreateAsync(BreweryPost brewery) + { + throw new NotImplementedException(); + } +}