using Domain.Entities;
using Infrastructure.Repository.Breweries;
namespace Service.Breweries;
///
/// Handles retrieval, creation, update, and deletion of brewery posts.
///
/// Repository used to persist and query brewery post data.
public class BreweryService(IBreweryRepository repository) : IBreweryService
{
///
/// Retrieves a brewery post by its unique identifier.
///
/// The unique identifier of the brewery post.
/// The matching , or null if no such post exists.
public Task GetByIdAsync(Guid id) =>
repository.GetByIdAsync(id);
///
/// Retrieves all brewery posts, optionally paginated.
///
/// The maximum number of results to return, or null for no limit.
/// The number of results to skip, or null to start from the beginning.
/// A collection of entities.
public Task> GetAllAsync(int? limit = null, int? offset = null) =>
repository.GetAllAsync(limit, offset);
///
/// Creates a new brewery post, generating new identifiers for the post and its location.
///
/// The details of the brewery post to create.
/// A successful wrapping the newly created brewery post.
public async Task CreateAsync(BreweryCreateRequest request)
{
var entity = new BreweryPost
{
BreweryPostId = Guid.NewGuid(),
PostedById = request.PostedById,
BreweryName = request.BreweryName,
Description = request.Description,
CreatedAt = DateTime.UtcNow,
Location = new BreweryPostLocation
{
BreweryPostLocationId = Guid.NewGuid(),
CityId = request.Location.CityId,
AddressLine1 = request.Location.AddressLine1,
AddressLine2 = request.Location.AddressLine2,
PostalCode = request.Location.PostalCode,
Coordinates = request.Location.Coordinates,
},
};
await repository.CreateAsync(entity);
return new BreweryServiceReturn(entity);
}
///
/// Updates an existing brewery post. If has no Location,
/// the brewery's location is cleared.
///
/// The updated details of the brewery post.
/// A successful wrapping the updated brewery post.
public async Task UpdateAsync(BreweryUpdateRequest request)
{
var entity = new BreweryPost
{
BreweryPostId = request.BreweryPostId,
PostedById = request.PostedById,
BreweryName = request.BreweryName,
Description = request.Description,
UpdatedAt = DateTime.UtcNow,
Location = request.Location is null ? null : new BreweryPostLocation
{
BreweryPostLocationId = request.Location.BreweryPostLocationId,
BreweryPostId = request.BreweryPostId,
CityId = request.Location.CityId,
AddressLine1 = request.Location.AddressLine1,
AddressLine2 = request.Location.AddressLine2,
PostalCode = request.Location.PostalCode,
Coordinates = request.Location.Coordinates,
},
};
await repository.UpdateAsync(entity);
return new BreweryServiceReturn(entity);
}
///
/// Deletes a brewery post by its unique identifier.
///
/// The unique identifier of the brewery post to delete.
/// A task that completes once the deletion has finished.
public Task DeleteAsync(Guid id) =>
repository.DeleteAsync(id);
}