Implement CRUD operations for Brewery, including service and repository layers

This commit is contained in:
Aaron Po
2026-03-29 20:08:21 -04:00
parent 56c83db207
commit 1b467ac4f1
12 changed files with 296 additions and 17 deletions

View File

@@ -0,0 +1,72 @@
using Domain.Entities;
using Infrastructure.Repository.Breweries;
using API.Core.Contracts.Breweries;
namespace Service.Breweries;
public class BreweryService(IBreweryRepository repository) : IBreweryService
{
private readonly IBreweryRepository _repository = repository;
public Task<BreweryPost?> GetByIdAsync(Guid id) => _repository.GetByIdAsync(id);
public Task<IEnumerable<BreweryPost>> GetAllAsync(int? limit = null, int? offset = null) => _repository.GetAllAsync(limit, offset);
public async Task<BreweryServiceReturn> CreateAsync(BreweryCreateDto brewery)
{
if (brewery.Location is null)
return new BreweryServiceReturn("Location must be provided");
var entity = new BreweryPost
{
BreweryPostId = Guid.NewGuid(),
PostedById = brewery.PostedById,
BreweryName = brewery.BreweryName,
Description = brewery.Description,
CreatedAt = DateTime.UtcNow,
Location = new BreweryPostLocation
{
BreweryPostLocationId = Guid.NewGuid(),
CityId = brewery.Location.CityId,
AddressLine1 = brewery.Location.AddressLine1,
AddressLine2 = brewery.Location.AddressLine2,
PostalCode = brewery.Location.PostalCode,
Coordinates = brewery.Location.Coordinates
}
};
await _repository.CreateAsync(entity);
return new BreweryServiceReturn(entity);
}
public async Task<BreweryServiceReturn> UpdateAsync(BreweryDto brewery)
{
if (brewery is null) return new BreweryServiceReturn("Brewery payload is null");
var entity = new BreweryPost
{
BreweryPostId = brewery.BreweryPostId,
PostedById = brewery.PostedById,
BreweryName = brewery.BreweryName,
Description = brewery.Description,
CreatedAt = brewery.CreatedAt,
UpdatedAt = brewery.UpdatedAt,
Timer = brewery.Timer,
Location = brewery.Location is null ? null : new BreweryPostLocation
{
BreweryPostLocationId = brewery.Location.BreweryPostLocationId,
BreweryPostId = brewery.BreweryPostId,
CityId = brewery.Location.CityId,
AddressLine1 = brewery.Location.AddressLine1,
AddressLine2 = brewery.Location.AddressLine2,
PostalCode = brewery.Location.PostalCode,
Coordinates = brewery.Location.Coordinates
}
};
await _repository.UpdateAsync(entity);
return new BreweryServiceReturn(entity);
}
public Task DeleteAsync(Guid id) => _repository.DeleteAsync(id);
}

View File

@@ -0,0 +1,12 @@
using Microsoft.Extensions.DependencyInjection;
namespace Service.Breweries.DependencyInjection;
public static class BreweryServiceCollectionExtensions
{
public static IServiceCollection AddBreweryServices(this IServiceCollection services)
{
services.AddScoped<IBreweryService, BreweryService>();
return services;
}
}

View File

@@ -0,0 +1,33 @@
using API.Core.Contracts.Breweries;
using Domain.Entities;
namespace Service.Breweries;
public interface IBreweryService
{
Task<BreweryPost?> GetByIdAsync(Guid id);
Task<IEnumerable<BreweryPost>> GetAllAsync(int? limit = null, int? offset = null);
Task<BreweryServiceReturn> CreateAsync(BreweryCreateDto brewery);
Task<BreweryServiceReturn> UpdateAsync(BreweryDto brewery);
Task DeleteAsync(Guid id);
}
public record BreweryServiceReturn
{
public bool Success { get; init; } = false;
public BreweryPost Brewery { get; init; }
public string Message { get; init; } = string.Empty;
public BreweryServiceReturn(BreweryPost brewery)
{
Success = true;
Brewery = brewery;
}
public BreweryServiceReturn(string message)
{
Success = false;
Brewery = default!;
Message = message;
}
}

View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Domain\Domain.Entities\Domain.Entities.csproj" />
<ProjectReference
Include="..\..\Infrastructure\Infrastructure.Repository\Infrastructure.Repository.csproj" />
<ProjectReference Include="..\..\API\API.Core\API.Core.csproj" />
<ProjectReference Include="..\Service.Auth\Service.Auth.csproj" />
</ItemGroup>
</Project>