Skip to content

Commit

Permalink
refactor code (cont.)
Browse files Browse the repository at this point in the history
  • Loading branch information
thangchungnt committed Oct 10, 2017
1 parent cef1787 commit 5806db2
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using BlogCore.Core;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reactive.Linq;
Expand Down Expand Up @@ -96,16 +95,5 @@ private static async Task<PaginatedItem<TResponse>> GetDataAsync<TDbContext, TEn

return new PaginatedItem<TResponse>(totalRecord, totalPages, results);
}

public static IEnumerable<TResponse> GetPagingDataWithConditionStream<TEntity, TResponse>(
this IReadOnlyList<TEntity> source,
Criterion criterion,
Func<TEntity, TResponse> selector)
where TEntity : EntityBase
{
return source.Skip(criterion.CurrentPage * criterion.PageSize)
.Take(criterion.PageSize)
.Select(selector);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using BlogCore.AccessControlContext.Domain;
using BlogCore.Core;
using BlogCore.PostContext.UseCases.ListOutPostByBlog;
using System.Collections.Generic;
using System.Linq;

namespace BlogCore.Api.Posts.ListOutPostByBlog
{
public class ListOutPostByBlogPresenter
{
private readonly IUserRepository _userRepository;

public ListOutPostByBlogPresenter(IUserRepository userRepository)
{
_userRepository = userRepository;
}

public PaginatedItem<ListOutPostByBlogResponse> Transform(PaginatedItem<ListOutPostByBlogResponse> input)
{
var authors = input.Items
.Select(x => x.Author.Id)
.Distinct()
.Select(x => _userRepository.GetByIdAsync(x).Result)
.ToList();

var posts = input.Items
.Select(x =>
{
var author = authors.FirstOrDefault(y => y.Id == x.Author.Id.ToString());
return x.SetAuthor(new ListOutPostByBlogUserResponse(author.Id, author.FamilyName, author.GivenName));
})
.ToList();

return new PaginatedItem<ListOutPostByBlogResponse>(
input.TotalItems,
(int)input.TotalPages,
posts);
}
}
}
43 changes: 43 additions & 0 deletions src/Hosts/BlogCore.Api/Posts/PostPublicApiController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using BlogCore.Api.Posts.ListOutPostByBlog;
using BlogCore.Core;
using BlogCore.Core.Helpers;
using BlogCore.PostContext.Domain;
using BlogCore.PostContext.UseCases.ListOutPostByBlog;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace BlogCore.Api.Posts
{
[Route("api/public/blogs")]
public class PostPublicApiController : Controller
{
private readonly IMediator _eventAggregator;
private readonly ListOutPostByBlogPresenter _listOutPostByBlogPresenter;

public PostPublicApiController(IMediator eventAggregator, ListOutPostByBlogPresenter listOutPostByBlogPresenter)
{
_eventAggregator = eventAggregator;
_listOutPostByBlogPresenter = listOutPostByBlogPresenter;
}

[HttpGet("{blogId:guid}/posts")]
public async Task<PaginatedItem<ListOutPostByBlogResponse>> GetForBlog(Guid blogId, [FromQuery] int page)
{
var postResult = await _eventAggregator.Send(new ListOutPostByBlogRequest(blogId, page <= 0 ? 1 : page));
return _listOutPostByBlogPresenter.Transform(postResult);
}

[HttpGet("{blogId:guid}/posts/{postId:guid}")]
public Post Get(Guid blogId, Guid postId)
{
return Post.CreateInstance(
new BlogId(IdHelper.GenerateId("34C96712-2CDF-4E79-9E2F-768CB68DD552")),
"sample title",
"sample excerpt",
"sample body",
new AuthorId(IdHelper.GenerateId("4B5F26CE-DF97-494C-B747-121D215847D8")));
}
}
}
5 changes: 5 additions & 0 deletions src/Hosts/BlogCore.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#region libs

using BlogCore.AccessControl;
using BlogCore.Api.Posts;
using BlogCore.Api.Posts.ListOutPostByBlog;
using BlogCore.BlogContext;
using BlogCore.BlogContext.Infrastructure;
using BlogCore.Core;
Expand Down Expand Up @@ -60,6 +62,9 @@ public IServiceProvider ConfigureServices(IServiceCollection services)

services.AddIdentityServerForBlog(OnTokenValidated);

// register presenters
services.AddScoped<ListOutPostByBlogPresenter>();

return services.InitServices(RegisteredAssemblies(), Configuration);
}

Expand Down
28 changes: 2 additions & 26 deletions src/Modules/BlogCore.PostContext/PostPublicApiController.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
using BlogCore.Core;
using BlogCore.Core.Helpers;
using BlogCore.PostContext.Domain;
using BlogCore.PostContext.UseCases.ListOutPostByBlog;
using MediatR;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace BlogCore.PostContext
{
[Route("api/public/blog")]
[Route("api/public/posts")]
public class PostPublicApiController : Controller
{
private readonly IMediator _eventAggregator;
Expand All @@ -18,23 +12,5 @@ public PostPublicApiController(IMediator eventAggregator)
{
_eventAggregator = eventAggregator;
}

[HttpGet("{blogId:guid}/posts")]
public async Task<PaginatedItem<ListOutPostByBlogResponse>> GetForBlog(Guid blogId, [FromQuery] int page)
{
if (page <= 0) page = 1;
return await _eventAggregator.Send(new ListOutPostByBlogRequest(blogId, page));
}

[HttpGet("{blogId:guid}/posts/{postId:guid}")]
public Domain.Post Get(Guid blogId, Guid postId)
{
return Domain.Post.CreateInstance(
new BlogId(IdHelper.GenerateId("34C96712-2CDF-4E79-9E2F-768CB68DD552")),
"sample title",
"sample excerpt",
"sample body",
new AuthorId(IdHelper.GenerateId("4B5F26CE-DF97-494C-B747-121D215847D8")));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@ public ListOutPostByBlogResponse(
public ListOutPostByBlogUserResponse Author { get; private set; }
public List<ListOutPostByBlogTagResponse> Tags { get; private set; }
public DateTime CreatedAt { get; private set; }

public ListOutPostByBlogResponse SetAuthor(ListOutPostByBlogUserResponse author)
{
Author = author;
return this;
}
}
}
1 change: 0 additions & 1 deletion src/Web/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

<!-- Main styles for this application -->
<link href="%PUBLIC_URL%/css/style.css" rel="stylesheet">
<link href="%PUBLIC_URL%/css/blog.css" rel="stylesheet">
</head>
<body class="app">
<div id="root"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import Header from "../../components/Header";
import Footer from "../../components/Footer";

// our containers
import Restricted from "../../auth/Restricted";
import DashboardPage from "../Dashboard/Index";
import PostManagementPage from "../Post/PostManagement";
import ProfileSettingPage from "../Setting/ProfileSetting";
import Restricted from "../Restricted";
import DashboardContainer from "../../containers/Dashboard/Index";
import PostManagementContainer from "../../containers/Post/PostManagement";
import ProfileSettingContainer from "../../containers/Setting/ProfileSetting";

export default class AdminLayout extends Component {
render() {
Expand All @@ -26,28 +26,31 @@ export default class AdminLayout extends Component {
exact
path={`${match.url}/dashboard`}
key="dashboard"
render={routeProps =>
render={routeProps => (
<Restricted {...routeProps}>
{React.createElement(DashboardPage)}
</Restricted>}
{React.createElement(DashboardContainer)}
</Restricted>
)}
/>
<Route
exact
path={`${match.url}/posts`}
key="post-management"
render={routeProps =>
render={routeProps => (
<Restricted {...routeProps}>
{React.createElement(PostManagementPage)}
</Restricted>}
{React.createElement(PostManagementContainer)}
</Restricted>
)}
/>
<Route
exact
path={`${match.url}/profile-setting`}
key="profile-setting"
render={routeProps =>
render={routeProps => (
<Restricted {...routeProps}>
{React.createElement(ProfileSettingPage)}
</Restricted>}
{React.createElement(ProfileSettingContainer)}
</Restricted>
)}
/>
</Switch>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import Footer from "../../components/Footer";
import About from "../../components/About";

// our containers
import HomePage from "../Home/Index";
import BlogPage from "../Blog/Index";
import PostDetailPage from "../Post/PostDetail";
import HomeContainer from "../../containers/Home/Index";
import BlogContainer from "../../containers/Blog/Index";
import PostDetailContainer from "../../containers/Post/PostDetail";

// style
import "./blog.css";

export default class PublicLayout extends Component {
render() {
Expand All @@ -25,19 +28,19 @@ export default class PublicLayout extends Component {
exact
path={`${match.url}`}
key="index"
component={HomePage}
component={HomeContainer}
/>
<Route
exact
path={`${match.url}blogs/:blogId`}
key="blogId"
component={BlogPage}
component={BlogContainer}
/>
<Route
exact
path={`${match.url}blogs/:blogId/posts/:postId`}
key="postDetail"
component={PostDetailPage}
component={PostDetailContainer}
/>
</Switch>
</div>
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/Web/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { ConnectedRouter } from "react-router-redux";
// import getRoutes from "./routes";
import createStore, { routerHistory } from "./redux/configureStore";
import userManager from "./utils/userManager";
import PublicLayout from "./containers/App/PublicLayout";
import AdminLayout from "./containers/App/AdminLayout";
import PublicLayout from "./components/Layout/PublicLayout";
import AdminLayout from "./components/Layout/AdminLayout";

import Login from "./containers/Login/Index";
import Callback from "./containers/Login/Callback";
Expand Down

0 comments on commit 5806db2

Please sign in to comment.