namespace Brizco.Repository.MartenHandlers.NewsFeeds;

public class DeleteNewsFeedCommandHandler(IMartenRepositoryWrapper martenRepositoryWrapper, ICurrentUserService currentUserService)
    : IRequestHandler<DeleteNewsFeedCommand, bool>
{
    public async Task<bool> Handle(DeleteNewsFeedCommand request, CancellationToken cancellationToken)
    {
        if (currentUserService.ComplexId == null)
            throw new BaseApiException(ApiResultStatusCode.BadRequest, "Complex id is null");
        if (!Guid.TryParse(currentUserService.ComplexId, out Guid complexId))
            throw new BaseApiException(ApiResultStatusCode.BadRequest, "Complex id is wrong");

        var ent = await martenRepositoryWrapper.SetRepository<NewsFeed>().GetEntityAsync(request.Id, cancellationToken);
        if (ent.ComplexId != complexId)
            throw new BaseApiException(ApiResultStatusCode.BadRequest, "Complex id is wrong");
        await martenRepositoryWrapper.SetRepository<NewsFeed>().RemoveEntityAsync(ent, cancellationToken);
        return true;
    }
}