public async Task HandlesReturnListSuccess() { var repo = new Mock <IRepository>(); var logger = new Mock <ILogger>(); var mapper = new Mock <IMapper>(); var handler = new GetBlogsHandler(repo.Object, logger.Object, mapper.Object); var request = new GetBlogsQuery(); var cancellationToken = new System.Threading.CancellationToken(); var expected = new List <BlogDto>() { new BlogDto { Id = 1, Name = "My Test Blog" } }; repo.Setup(x => x.List <Blog>()).Returns(new List <Blog>() { new Blog { Id = 1, Name = "My Test Blog" } }); mapper.Setup(x => x.Map <IEnumerable <BlogDto> >(expected)).Returns(expected); var actual = await handler.Handle(request, cancellationToken); Assert.Equal(expected, actual); }
public async Task <List <BlogListViewModel> > Handle(GetBlogsQuery request, CancellationToken cancellationToken) { var blogs = await _myBlogDbContext.Blogs.Where(x => !x.IsDraft).OrderByDescending(x => x.PostedDate).Include(x => x.Category).Select(x => new BlogListViewModel { Id = x.Id, Title = x.Title, CategoryName = x.Category.Name, PostedDate = Convert.ToDateTime(x.PostedDate), ShortDescription = x.ShortDescription, Tags = x.Tags, AuthorName = x.Author.Name.FullName }).ToListAsync(); return(blogs); }
public async Task <List <BlogDto> > Handle(GetBlogsQuery request, CancellationToken cancellationToken) { return(await _context.Blogs .Select(c => new BlogDto { Id = c.Id, Author = c.Author, Post = c.Post, CreatedBy = c.CreatedBy, CreatedDate = c.CreatedDate, Comments = c.Comments.Select(comm => new CommentDto { Id = comm.Id, BlogId = comm.BlogId, CommentText = comm.CommentText, CreatedBy = comm.CreatedBy, CreatedDate = comm.CreatedDate }) }) .OrderBy(t => t.Author) .ToListAsync(cancellationToken)); }
public async Task <IEnumerable <BlogDto> > Handle(GetBlogsQuery request, CancellationToken cancellationToken) { return(await _context.Blog.ProjectTo <BlogDto>(_mapper.ConfigurationProvider) .OrderBy(t => t.Title) .ToListAsync(cancellationToken)); }
public Task <IEnumerable <BlogDto> > Handle(GetBlogsQuery request, CancellationToken cancellationToken) { return(Task.FromResult(_mapper.Map <IEnumerable <BlogDto> >(_repository.List <Blog>()))); }