Пример #1
0
        public async Task <IEnumerable <TagEntity> > AddTags(Guid postId, IEnumerable <string> tags)
        {
            foreach (var tag in tags)
            {
                var newTag = new Tag
                {
                    Title     = tag.ToUpper(),
                    CreatedAt = DateTimeOffset.Now
                };

                var tagEntity = await AddIfNotExists(newTag, tag);

                if (tagEntity != null)
                {
                    await _postTagService.AddPostTag(postId, tagEntity.Id);
                }
                else
                {
                    var tagId = _tagRepository.Where(t => t.Title == tag).FirstOrDefault().Id;
                    await _postTagService.AddPostTag(postId, tagId);
                }
            }

            return(_postTagService.GetTagsForPost(postId));
        }
Пример #2
0
        public PostEntity GetPostAsync(Guid id)
        {
            if (id == Guid.Empty)
            {
                throw new ArgumentNullException("Post Id is null.");
            }

            var post = _postRepository.Where(p => p.Id == id).FirstOrDefault();

            if (post == null)
            {
                throw new NullReferenceException("Post not found.");
            }

            var postEntity = _mapper.Map <PostEntity>(post);

            postEntity.Date   = post.CreatedAt;
            postEntity.Author = _mapper.Map <UserEntity>(_userRepository
                                                         .Where(u => u.Id == post.UserId).FirstOrDefault());
            postEntity.Tags = _postTagService.GetTagsForPost(id).Select(t => $"#{t.Title}") ?? null;

            return(postEntity);
        }