Skip to content

Commit

Permalink
get posts on the home page
Browse files Browse the repository at this point in the history
  • Loading branch information
thangchung committed Jul 21, 2019
1 parent b524acc commit dc00334
Show file tree
Hide file tree
Showing 26 changed files with 296 additions and 146 deletions.
2 changes: 1 addition & 1 deletion src/BlogCore.ComponentsLibrary/BadminLteCard.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="card">
<div class="card card-primary card-outline">
<div class="card-header">
@CardHeader
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/BlogCore.Hosts.Tests/MessageTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using BlogCore.Shared.v1.Blog;
using BlogCore.Shared.v1.Blogs.Validators;
using BlogCore.Shared.v1.ValidationModel;
using BlogCore.Shared.v1.Validators;
using Google.Protobuf;
using System;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@page "/settings"
@page "/blog-settings"
@attribute [Authorize]

@code {
Expand Down
3 changes: 2 additions & 1 deletion src/BlogCore.Hosts.Web.Client/Pages/Blogs.razor
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
@page "/blogs"
@page "/blogs/{Page:int}"
@attribute [Authorize]
@inject BlogService BlogService
@inject IJSRuntime JS
@inject BlogService BlogService

<div class="container-fluid">
<BadminLteCard>
<CardHeader>
<NavLink href="create-blog" class="btn btn-primary btn-sm">New</NavLink>
<NavLink href="blog-settings" class="btn btn-info btn-sm">Settings</NavLink>
<button class="btn btn-danger btn-sm">Delete Selected</button>
</CardHeader>
<CardBody>
Expand Down
108 changes: 53 additions & 55 deletions src/BlogCore.Hosts.Web.Client/Pages/PublicHome.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@page "/"
@page "/{Page:int}"
@layout NoLayout
@inject PostService PostService

<div class="jumbotron jumbotron-fluid">
<div class="container">
Expand All @@ -12,62 +14,35 @@
<div class="container">
<div class="row">
<div class="col-md-12">
<!-- Post -->
<div class="post">
<div class="user-block">
<h1>
<NavLink href="/post/1">This is post 1</NavLink>
</h1>
<div>Created at 7:30 PM today</div>
@foreach (var post in Posts)
{
<!-- Post -->
<div class="post">
<div class="user-block">
<h1>
<a href="/post/@post.Id">@post.Title</a>
</h1>
<div>Created at @post.CreatedAt</div>
</div>
<!-- /.user-block -->
<p>
@post.Excerpt...
</p>
<p>
@RenderTag(post.Id)
</p>
<p>
<a href="#" class="link-black text-sm mr-2"><i class="fas fa-share mr-1"></i> Share</a>
<a href="#" class="link-black text-sm"><i class="far fa-thumbs-up mr-1"></i> Like</a>
<span class="float-right">
<NavLink href="#" class="link-black text-sm">
<i class="far fa-comments mr-1"></i> Comments (5)
</NavLink>
</span>
</p>
</div>
<!-- /.user-block -->
<p>
Lorem ipsum represents a long-held tradition for designers,
typographers and the like. Some people hate it and argue for
its demise, but others ignore the hate as they create awesome
tools to help create filler text for everyone from bacon lovers
to Charlie Sheen fans...
</p>

<p>
<a href="#" class="link-black text-sm mr-2"><i class="fas fa-share mr-1"></i> Share</a>
<a href="#" class="link-black text-sm"><i class="far fa-thumbs-up mr-1"></i> Like</a>
<span class="float-right">
<NavLink href="#" class="link-black text-sm">
<i class="far fa-comments mr-1"></i> Comments (5)
</NavLink>
</span>
</p>
</div>
<!-- /.post -->
<!-- Post -->
<div class="post clearfix">
<div class="user-block">
<h1>
<NavLink href="/post/2">This is post 2</NavLink>
</h1>
<div>Created at 3 days ago</div>
</div>
<!-- /.user-block -->
<p>
Lorem ipsum represents a long-held tradition for designers,
typographers and the like. Some people hate it and argue for
its demise, but others ignore the hate as they create awesome
tools to help create filler text for everyone from bacon lovers
to Charlie Sheen fans...
</p>

<p>
<a href="#" class="link-black text-sm mr-2"><i class="fas fa-share mr-1"></i> Share</a>
<a href="#" class="link-black text-sm"><i class="far fa-thumbs-up mr-1"></i> Like</a>
<span class="float-right">
<NavLink href="#" class="link-black text-sm">
<i class="far fa-comments mr-1"></i> Comments (5)
</NavLink>
</span>
</p>
</div>
<!-- /.post -->
<!-- /.post -->
}
</div>
</div>
</div>
Expand All @@ -83,4 +58,27 @@
</div>

@code {
[Parameter] int Page { get; set; } = 1;

List<PostDto> Posts = new List<PostDto>();
MapField<string, PostTagsDto> PostTags = new MapField<string, PostTagsDto>();

protected override async Task OnInitAsync()
{
var resultModel = await PostService.GetPostsByBlog(Guid.NewGuid(), Page);
Posts = resultModel.Data.Posts.ToList();
PostTags = resultModel.Data.TagFragment;
}

MarkupString RenderTag(string postId)
{
var result = PostTags
.Where(x => x.Key == postId)
.Select(x => x.Value)
.SelectMany(x => x.Tags)
.Select(x => x?.Name)
.Aggregate("", (r, x) => r + string.Format("<span class=\"badge badge-info\">{0}</span>", x));

return (MarkupString)result;
}
}
5 changes: 2 additions & 3 deletions src/BlogCore.Hosts.Web.Client/Pages/PublicPost.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@page "/post/{PostId:int}"
@page "/post/{PostId:guid}"
@layout NoLayout

<div class="jumbotron jumbotron-fluid">
Expand Down Expand Up @@ -93,6 +93,5 @@
</div>

@code {
[Parameter]
int PostId { get; set; } = 1;
[Parameter] Guid PostId { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System;
using System.Threading.Tasks;

namespace BlogCore.Hosts.Web.Client.Services
namespace BlogCore.Hosts.Web.Client.Services.Impl
{
public class BlogService : ServiceBase
{
Expand All @@ -16,19 +16,19 @@ public BlogService(IServiceProvider serviceProvider) : base(serviceProvider)
public async Task<ProtoResultModel<PaginatedItemResponse>> GetBlogs(int page)
{
var httpClient = await SecureHttpClientAsync();
return await httpClient.GetProtoAsync<PaginatedItemResponse>($"api/blogs?page={page}");
return await httpClient.GetProtoAsync<PaginatedItemResponse>($"api/@blogs?page={page}");
}

public async Task<ProtoResultModel<RetrieveBlogResponse>> GetBlogById(Guid blogId)
{
var httpClient = await SecureHttpClientAsync();
return await httpClient.GetProtoAsync<RetrieveBlogResponse>($"api/blogs/{blogId}");
return await httpClient.GetProtoAsync<RetrieveBlogResponse>($"api/@blogs/{blogId}");
}

public async Task<ProtoResultModel<CreateBlogResponse>> CreateBlog(CreateBlogRequest model)
{
var httpClient = await SecureHttpClientAsync();
return await httpClient.PostProtoAsync<CreateBlogResponse>($"api/blogs", model);
return await httpClient.PostProtoAsync<CreateBlogResponse>($"api/@blogs", model);
}
}
}
20 changes: 20 additions & 0 deletions src/BlogCore.Hosts.Web.Client/Services/Impl/PostService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using BlogCore.Shared;
using BlogCore.Shared.v1;
using BlogCore.Shared.v1.Post;
using System;
using System.Threading.Tasks;

namespace BlogCore.Hosts.Web.Client.Services.Impl
{
public class PostService : ServiceBase
{
public PostService(IServiceProvider serviceProvider) : base(serviceProvider)
{
}

public async Task<ProtoResultModel<GetPostsByBlogResponse>> GetPostsByBlog(Guid blogId, int page)
{
return await HttpClient.GetProtoAsync<GetPostsByBlogResponse>($"api/posts/{blogId}/posts?page={page}");
}
}
}
8 changes: 0 additions & 8 deletions src/BlogCore.Hosts.Web.Client/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@
</p>
</NavLink>
</li>
<li class="nav-item">
<NavLink class="nav-link" href="settings">
<i class="nav-icon fas fa-cogs"></i>
<p>
Settings
</p>
</NavLink>
</li>
<li class="nav-item">
<NavLink class="nav-link" href="/" Match="NavLinkMatch.All">
<i class="nav-icon fas fa-copy"></i>
Expand Down
2 changes: 2 additions & 0 deletions src/BlogCore.Hosts.Web.Client/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BlogCore.Hosts.Web.Client.Services;
using BlogCore.Hosts.Web.Client.Services.Impl;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Builder;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -16,6 +17,7 @@ public void ConfigureServices(IServiceCollection services)
// services and state
services.AddScoped<AppState>();
services.AddScoped<BlogService>();
services.AddScoped<PostService>();
}

public void Configure(IComponentsApplicationBuilder app)
Expand Down
2 changes: 2 additions & 0 deletions src/BlogCore.Hosts.Web.Client/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
@using Microsoft.AspNetCore.Components.Layouts
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.JSInterop
@using Google.Protobuf.Collections
@using BlogCore.Hosts.Web.Client.Shared
@using BlogCore.Hosts.Web.Client.Services
@using BlogCore.Hosts.Web.Client.Services.Impl
@using BlogCore.Shared
@using BlogCore.Shared.v1.Common
@using BlogCore.Shared.v1.Blog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.0.0-rc2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.0.0-rc2" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.0.0-rc2" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.6.0-preview6.19303.8" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions src/BlogCore.Hosts.Web.Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Linq;
using System.Net.Http;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;

namespace BlogCore.Hosts.Web.Server
Expand All @@ -32,6 +33,9 @@ public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = configuration;
Environment = env;

// https://github.com/dotnet/corefx/issues/9158
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}

public IConfiguration Configuration { get; }
Expand Down
17 changes: 16 additions & 1 deletion src/BlogCore.Modules.BlogContext/BlogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,22 @@ public BlogController(IServiceProvider serviceProvider)
_serviceProvider = serviceProvider;
}


[HttpGet("owner")]
public Task<ActionResult<ProtoResultModel<PaginatedItemResponse>>> GetOwner()
{
throw new NotImplementedException();
}

[HttpGet]
public async Task<ActionResult<ProtoResultModel<PaginatedItemResponse>>> RetrieveBlogs([FromQuery] int page = 1)
{
var useCase = _serviceProvider.GetService<IUseCase<RetrieveBlogsRequest, PaginatedItemResponse>>().NotNull();
var response = await useCase.ExecuteAsync(new RetrieveBlogsRequest
{
CurrentPage = page
});
return new OkObjectResult(new ProtoResultModel<PaginatedItemResponse>(response));
}

[HttpGet("{id:guid}")]
public async Task<ActionResult<RetrieveBlogResponse>> Get(Guid id)
Expand Down
26 changes: 1 addition & 25 deletions src/BlogCore.Modules.BlogContext/PublicBlogController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
using BlogCore.Shared.v1;
using BlogCore.Shared.v1.Blog;
using BlogCore.Shared.v1.Common;
using BlogCore.Shared.v1.Guard;
using BlogCore.Shared.v1.Usecase;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace BlogCore.Modules.BlogContext
{
Expand All @@ -20,22 +13,5 @@ public PublicBlogController(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

[HttpGet("owner")]
public Task<ActionResult<ProtoResultModel<PaginatedItemResponse>>> GetOwner()
{
throw new NotImplementedException();
}

[HttpGet]
public async Task<ActionResult<ProtoResultModel<PaginatedItemResponse>>> RetrieveBlogs([FromQuery] int page = 1)
{
var useCase = _serviceProvider.GetService<IUseCase<RetrieveBlogsRequest, PaginatedItemResponse>>().NotNull();
var response = await useCase.ExecuteAsync(new RetrieveBlogsRequest
{
CurrentPage = page
});
return new OkObjectResult(new ProtoResultModel<PaginatedItemResponse>(response));
}
}
}
11 changes: 11 additions & 0 deletions src/BlogCore.Modules.PostContext/PostController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.AspNetCore.Mvc;

namespace BlogCore.Modules.PostContext
{
[Route("api/@blogs")]
[ApiController]
public class PostsController : ControllerBase
{

}
}
25 changes: 0 additions & 25 deletions src/BlogCore.Modules.PostContext/PostsController.cs

This file was deleted.

Loading

0 comments on commit dc00334

Please sign in to comment.