Ejemplo n.º 1
0
        /// <summary>
        /// Method to add news to multiple tags
        /// </summary>
        /// <param name="newsId">news id</param>
        /// <param name="tags">list of tag names</param>
        /// <returns></returns>
        public async Task AddToTagsAsync(Guid newsId, IEnumerable <string> tags)
        {
            if (tags == null)
            {
                throw new ArgumentNullException(nameof(tags));
            }
            var news = await newsRepository.GetAsync(newsId);

            if (news == null)
            {
                throw new InvalidOperationException($"UserId not found. No user with this id found {newsId}.");
            }

            var newsTags = await newsRepository.GetTagsAsync(news);

            foreach (var tag in tags)
            {
                if (newsTags.Contains(tag))
                {
                    throw new ArgumentException("News already has tag. Error when a news is already has a tag");
                }
                await newsRepository.AddToTagAsync(news, tag);
            }
            await newsRepository.EditAsync(news);
        }