Skip to content

Instantly share code, notes, and snippets.

@thangchung
Created October 16, 2017 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thangchung/a0185b50302905ff6391a000a3511fa5 to your computer and use it in GitHub Desktop.
Save thangchung/a0185b50302905ff6391a000a3511fa5 to your computer and use it in GitHub Desktop.
Applying clean architecture on web application with modular patterns
using BlogCore.AccessControlContext.Core.Domain;
using BlogCore.Core;
using BlogCore.PostContext.UseCases.ListOutPostByBlog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
namespace BlogCore.Api.Features.Posts.ListOutPostByBlog
{
public class ListOutPostByBlogPresenter
{
private readonly IUserRepository _userRepository;
public ListOutPostByBlogPresenter(IUserRepository userRepository)
{
_userRepository = userRepository;
}
public async Task<PaginatedItem<ListOutPostByBlogResponse>> Transform(IObservable<PaginatedItem<ListOutPostByBlogResponse>> stream)
{
var result = await stream.Select(x => x);
var authors = result.Items
.Select(x => x.Author.Id)
.Distinct()
.Select(y => _userRepository.GetByIdAsync(y).Result)
.ToList();
var items = result.Items.Select(x =>
{
var author = authors.FirstOrDefault(au => au.Id == x.Author.Id.ToString());
return x.SetAuthor(author?.Id, author?.FamilyName, author?.GivenName);
});
return new PaginatedItem<ListOutPostByBlogResponse>(
result.TotalItems,
(int)result.TotalPages,
items.ToList());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment