/// <summary> /// Show page with user/aurhors list. /// </summary> /// <returns>Page with list of users.</returns> public async Task <IActionResult> Index() { var authorsQuery = new GetAuthorsQuery(); var authors = await _mediator.Send(authorsQuery); var authorsVM = _mapper.Map <IEnumerable <AuthorDTO>, ICollection <AuthorViewModel> >(authors); foreach (var author in authorsVM) { author.Email = await _identityService.GetEmailByIdAsync(author.UserId); author.PostsNumber = _mediator.Send(new GetPostsByAuthorIdQuery { AuthorId = author.AuthorId }).GetAwaiter().GetResult().Count; author.CommentsNumber = _mediator.Send(new GetCommentsByAuthorIdQuery { AuthorId = author.AuthorId }).GetAwaiter().GetResult().Count; } var isAdmin = HttpContext.User.IsInRole("admin"); var model = new AuthorsViewModel { Authors = authorsVM, IsAdmin = isAdmin, }; return(View(model)); }
/// <summary> /// Get authors info. /// </summary> /// <param name="request">Info request.</param> /// <param name="cancellationToken">Cancellation token.</param> /// <returns>Number of authors DTO.</returns> public async Task <IEnumerable <AuthorDTO> > Handle(GetAuthorsQuery request, CancellationToken cancellationToken) { var entites = await _context.Authors.ToArrayAsync(cancellationToken); var authors = _mapper.Map <IEnumerable <AuthorDTO> >(entites); return(authors); }
public async Task Handler_ReturnsAuthorDTOCollection() { // Arrange var query = new GetAuthorsQuery(); // Act var handler = new GetAuthorsQuery.GetAuthorsQueryHandler(Context, Mapper); var result = await handler.Handle(query, CancellationToken.None); // Assert result.ShouldBeOfType <List <AuthorDTO> >(); result.ShouldNotBeNull(); }
public async Task <IActionResult> GetAuthors(int libraryId, string query, AuthorTypes?authorType = null, int pageNumber = 1, int pageSize = 10, CancellationToken token = default(CancellationToken)) { var authorsQuery = new GetAuthorsQuery(libraryId, pageNumber, pageSize) { Query = query, AuthorType = authorType }; var result = await _queryProcessor.ExecuteAsync(authorsQuery, token); var args = new PageRendererArgs <AuthorModel> { Page = result, RouteArguments = new PagedRouteArgs { PageNumber = pageNumber, PageSize = pageSize, Query = query }, }; return(new OkObjectResult(_authorRenderer.Render(args, libraryId))); }
public async Task <IActionResult> Index() { var authorsQuery = new GetAuthorsQuery(); var authors = await _mediator.Send(authorsQuery); if (authors == null) { return(RedirectToAction("Index", "Home")); } var models = _mapper.Map <IEnumerable <AuthorDTO>, IEnumerable <AuthorViewModel> >(authors); foreach (var model in models) { model.Email = await _identityService.GetEmailByIdAsync(model.UserId); } return(View(models)); }
public Task <PagableListResult <AuthorCommonResult> > GetAuthors( [FromQuery] GetAuthorsQuery query) => Mediator.Send(query);
public async Task <IEnumerable <AuthorModel> > ExecuteAsync(GetAuthorsQuery query) { var authors = await _authors.GetListAsync(); return(_mapper.Map <IEnumerable <AuthorModel> >(authors)); }