Ejemplo n.º 1
0
        public async Task <IEnumerable <BlogDTO> > GetAllBlogsByUserId(string id)
        {
            var userEntity = await _userManager.FindByIdAsync(id);

            if (userEntity == null)
            {
                throw new ArgumentNullException(nameof(userEntity), "Couldn't find user with this id");
            }
            var blogs = _unitOfWork.BlogRepository.Get(b => b.OwnerId == id);

            return(BlogMapper.Map(blogs));
        }
Ejemplo n.º 2
0
        public BlogDTO GetBlogById(int id)
        {
            var blog = _unitOfWork.BlogRepository.Get((b => b.Id == id), includeProperties: "Articles,Owner").FirstOrDefault();

            if (blog == null)
            {
                throw new ArgumentNullException(nameof(blog), "Couldn't find blog by id");
            }
            var dto = BlogMapper.Map(blog);

            if (blog.Articles.Count > 0)
            {
                dto.Articles = ArticleMapper.Map(blog.Articles);
            }
            if (blog.Owner != null)
            {
                dto.OwnerUsername = blog.Owner.UserName;
            }
            return(dto);
        }
Ejemplo n.º 3
0
        public async Task <BlogDTO> CreateBlog(BlogDTO blog, string token)
        {
            if (blog == null)
            {
                throw new ArgumentNullException(nameof(blog));
            }
            string claimsId   = _jwtFactory.GetUserIdClaim(token);
            var    blogEntity = BlogMapper.Map(blog);

            blogEntity.OwnerId = claimsId;

            _unitOfWork.BlogRepository.Insert(blogEntity);
            await _unitOfWork.SaveAsync();

            blogEntity = _unitOfWork.BlogRepository.Get(b => b.Name == blog.Name, includeProperties: "Owner").FirstOrDefault();
            if (blogEntity == null)
            {
                throw new ArgumentNullException(nameof(blogEntity));
            }
            var result = BlogMapper.Map(blogEntity);

            result.OwnerUsername = blogEntity.Owner.UserName;
            return(BlogMapper.Map(blogEntity));
        }
Ejemplo n.º 4
0
 public IEnumerable <BlogDTO> GetAllBlogs()
 {
     return(BlogMapper.Map(_unitOfWork.BlogRepository.Get()));
 }