namespace Netina.Repository.Handlers.Addresses;

public class DeleteAddressCommandHandler(IRepositoryWrapper repositoryWrapper)
    : IRequestHandler<DeleteAddressCommand, bool>
{
    public async Task<bool> Handle(DeleteAddressCommand request, CancellationToken cancellationToken)
    {
        var ent = await repositoryWrapper.SetRepository<UserAddress>()
            .TableNoTracking
            .FirstOrDefaultAsync(u => u.Id == request.Id, cancellationToken);
        if (ent == null)
            throw new AppException("Address not found", ApiResultStatusCode.NotFound);
        repositoryWrapper.SetRepository<UserAddress>()
            .Delete(ent);
        await repositoryWrapper.SaveChangesAsync(cancellationToken);
        return true;
    }
}