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.
public class UpdateBreweryHandler(IBreweryRepository repository)
: IRequestHandler
{
///
/// Updates an existing brewery post. If has no Location,
/// the brewery's location is cleared.
///
///
/// Thrown when no brewery exists with 's BreweryPostId, or its
/// location's CityId does not exist.
///
///
/// Thrown when the brewery was modified by another request since .
/// RowVersion was read.
///
public async Task Handle(
UpdateBreweryCommand request,
CancellationToken cancellationToken
)
{
BreweryPost entity = new()
{
BreweryPostId = request.BreweryPostId,
PostedById = request.PostedById,
BreweryName = request.BreweryName,
Description = request.Description,
RowVersion = request.RowVersion,
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,
},
};
BreweryPost updated = await repository.UpdateAsync(entity);
return updated.ToDto();
}
}