namespace Brizco.Repository.MartenHandlers.NewsFeeds;

public class CreateNewsFeedCommandHandler (IMartenRepositoryWrapper martenRepositoryWrapper , ICurrentUserService currentUserService) 
    : IRequestHandler<CreateNewsFeedCommand,Guid>
{
    public async Task<Guid> Handle(CreateNewsFeedCommand 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 newsFeed = new NewsFeed
        {
            ComplexId = complexId,
            Content = request.Content,
            Title = request.Title,
            IsPin = request.IsPin
        };
        await martenRepositoryWrapper.SetRepository<NewsFeed>().AddOrUpdateEntityAsync(newsFeed, cancellationToken);
        return newsFeed.Id;
    }
}