using NetinaCMS.Repository.Repositories.Base.Contracts;

namespace NetinaCMS.Repository.Handlers.Blogs;

public class CreateBlogCommandHandler : IRequestHandler<CreateBlogCommand,bool>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public CreateBlogCommandHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<bool> Handle(CreateBlogCommand request, CancellationToken cancellationToken)
    {
        var ent = Blog.Create(request.Title, request.Content, request.Tags, request.ReadingTime, request.Summery,
            request.IsSuggested, request.CategoryId);

        foreach (var file in request.Files)
        {
            ent.AddFile(file.Name, file.FileLocation, file.FileName, file.IsHeader, file.IsPrimary, file.FileType);
        }

        _repositoryWrapper.SetRepository<Blog>()
            .Add(ent);
        await _repositoryWrapper.SaveChangesAsync(cancellationToken);
        return true;
    }
}