using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.UI;
using YGNT.Infrastructure.Extension;

namespace YGNT.QuestionLibrary.Core.Textbooks.Domain
{
    public class TextbookManager : ITextbookManager
    {
        private readonly IRepository<Textbook> _textBookRepository;

        public TextbookManager(IRepository<Textbook> textBookRepository)
        {
            _textBookRepository = textBookRepository;
        }

        /// <summary>
        /// 检查教材名称是否被占用
        /// </summary>
        /// <param name="name"></param>
        /// <param name="disciplineId"></param>
        /// <returns></returns>
        public async Task CheckNameAsync(string name, int disciplineId)
        {
            var isExist = await _textBookRepository.AnyAsync(x => x.DisciplineId == disciplineId && x.Name == name);
            if (isExist)
                throw new UserFriendlyException($"教材名称:{name}已被占用.");
        }
    }
}