using Domain.Entities;
using Features.Breweries.Dtos;
using Features.Breweries.Repository;
using MediatR;
namespace Features.Breweries.Commands.UpdateBrewery;
///
/// Handles by persisting changes to an existing brewery post.
///
/// Repository used to persist the updated brewery post.
public class UpdateBreweryHandler(IBreweryRepository repository)
: IRequestHandler
{
///
/// Updates an existing brewery post. If has no Location,
/// the brewery's location is cleared.
///
/// The updated details of the brewery post.
/// A token to observe for cancellation requests.
/// The updated brewery post.
public async Task Handle(UpdateBreweryCommand request, CancellationToken cancellationToken)
{
BreweryPost entity = new()
{
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 entity.ToDto();
}
}