mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-04-05 18:09:04 +00:00
Implement CRUD operations for Brewery, including service and repository layers
This commit is contained in:
72
src/Core/Service/Service.Breweries/BreweryService.cs
Normal file
72
src/Core/Service/Service.Breweries/BreweryService.cs
Normal 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);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
33
src/Core/Service/Service.Breweries/IBreweryService.cs
Normal file
33
src/Core/Service/Service.Breweries/IBreweryService.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
15
src/Core/Service/Service.Breweries/Service.Breweries.csproj
Normal file
15
src/Core/Service/Service.Breweries/Service.Breweries.csproj
Normal 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>
|
||||
Reference in New Issue
Block a user