using Domain.Entities;
using Features.Breweries.Dtos;
using Features.Breweries.Repository;
using MediatR;
namespace Features.Breweries.Commands.CreateBrewery;
///
/// Handles by persisting a new brewery post and its location.
///
/// Repository used to persist the new brewery post.
public class CreateBreweryHandler(IBreweryRepository repository)
: IRequestHandler
{
///
/// Creates a new brewery post, generating new identifiers for the post and its location.
///
/// The details of the brewery post to create.
/// A token to observe for cancellation requests.
/// The newly created brewery post.
public async Task Handle(
CreateBreweryCommand request,
CancellationToken cancellationToken
)
{
BreweryPost entity = new()
{
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 entity.ToDto();
}
}