using Lada.Framework.Data.Domains.Cms;
using Lada.Framework.Data.Repositories;
using Lada.Framework.DTO;

namespace Ladaer.BackEnd.Infrastructures.Services.Cms
{
    public interface IPostCategoryService
    {
        Task<BaseResult> RemovePostCategoryByPostIdAsync(int postId);
    }
    public class PostCategoryService: IPostCategoryService
    {
        private readonly IRepository<PostCategory> _postCategoryRepository;

        public PostCategoryService(IRepository<PostCategory> postCategoryRepository)
        {
            _postCategoryRepository = postCategoryRepository;
        }

        public async Task<BaseResult> RemovePostCategoryByPostIdAsync(int postId)
        {
            var result = new BaseResult();
            var postCategories = _postCategoryRepository.Query().Where(pt => pt.PostId == postId).ToList();
            try
            {
                if (postCategories != null && postCategories.Any())
                {
                    foreach (var postCategory in postCategories)
                    {
                        await _postCategoryRepository.RemoveAsync(postCategory);
                    }
                }
            }
            catch (Exception ex)
            {
                result.Result = Result.Failed;
                result.Message = ex.Message;
            }

            return result;
        }
    }
}
