Refactor new post page

pull/318/head
Halil ibrahim Kalkan 7 years ago
parent 0bb97c6a7f
commit e258bcaef9

@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Application.Dtos;
using System.ComponentModel.DataAnnotations;
namespace Volo.Blogging.Posts
{
@ -9,8 +7,11 @@ namespace Volo.Blogging.Posts
{
public Guid BlogId { get; set; }
[Required]
[StringLength(PostConsts.MaxTitleLength)]
public string Title { get; set; }
[StringLength(PostConsts.MaxContentLength)]
public string Content { get; set; }
}
}

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
@ -18,7 +17,7 @@ namespace Volo.Blogging.Pages.Blog
_blogAppService = blogAppService;
}
public async Task<IActionResult> OnGet()
public async Task<IActionResult> OnGetAsync()
{
var result = await _blogAppService.GetListAsync();
@ -29,6 +28,7 @@ namespace Volo.Blogging.Pages.Blog
}
Blogs = result.Items;
return Page();
}
}

@ -7,11 +7,11 @@
Posts
</h2>
<a asp-page="./New" asp-route-blogShortName="@Model.BlogShortName">Create New Post</a>
<ul>
@foreach (var post in Model.Posts)
{
<li><a asp-page="./Detail" asp-route-postTitle="@post.Title" asp-route-blogShortName="@Model.BlogShortName">@post.Title</a></li>
}
</ul>
<a asp-page="./New" asp-route-blogShortName="@Model.BlogShortName">Create New Post</a>

@ -1,10 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Volo.Abp.Application.Dtos;
using Volo.Blogging.Blogs;
using Volo.Blogging.Posts;
@ -27,10 +24,9 @@ namespace Volo.Blogging.Pages.Blog.Posts
_blogAppService = blogAppService;
}
public async Task OnGet()
public async Task OnGetAsync()
{
var blog = await _blogAppService.GetByShortNameAsync(BlogShortName);
Posts = _postAppService.GetListByBlogIdAsync(blog.Id).Items;
}
}

@ -1,23 +1,14 @@
@page
@using Volo.Blogging.Pages.Blog.Posts
@model NewModel
@{
}
<form method="post" id="new-post-form">
<abp-input asp-for="@Model.Post.Title" autofocus />
<form method="post">
<abp-input asp-for="Post.Title" auto-focus="true" />
<div class="form-group">
<label>Content</label>
<textarea rows="4" class="form-control" name="Post.Content">@Model.Post.Content</textarea>
</div>
<abp-input asp-for="@Model.Post.BlogId" hidden="" />
<abp-button type="submit" form="new-post-form">
<span>Submit</span>
</abp-button>
<abp-input asp-for="Post.BlogId" label=" " /> @* TODO: Remove label when the issue is fixed *@
<abp-button type="submit" text="Submit" />
</form>

@ -1,3 +1,5 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
@ -14,7 +16,8 @@ namespace Volo.Blogging.Pages.Blog.Posts
[BindProperty(SupportsGet = true)]
public string BlogShortName { get; set; }
public CreatePostDto Post { get; set; }
[BindProperty]
public CreatePostViewModel Post { get; set; }
public BlogDto Blog { get; set; }
@ -24,24 +27,42 @@ namespace Volo.Blogging.Pages.Blog.Posts
_blogAppService = blogAppService;
}
public async void OnGet()
public async void OnGetAsync()
{
var blog = await _blogAppService.GetByShortNameAsync(BlogShortName);
Post = new CreatePostDto()
Blog = await _blogAppService.GetByShortNameAsync(BlogShortName);
Post = new CreatePostViewModel
{
BlogId = blog.Id
BlogId = Blog.Id
};
}
Blog = blog;
public async Task<ActionResult> OnPost()
{
var blog = await _blogAppService.GetAsync(Post.BlogId);
var postWithDetailsDto = await _postAppService.CreateAsync(
new CreatePostDto //TODO: Use automapper
{
BlogId = Post.BlogId,
Title = Post.Title,
Content = Post.Content
}
);
//TODO: Try Url.Page(...)
return Redirect(Url.Content($"~/blog/{blog.ShortName}/{postWithDetailsDto.Title}"));
}
public async Task<ActionResult> OnPost(CreatePostDto post)
public class CreatePostViewModel
{
var insertedPost = await _postAppService.CreateAsync(post);
var blog = await _blogAppService.GetAsync(insertedPost.BlogId);
[HiddenInput]
public Guid BlogId { get; set; }
[Required]
[StringLength(PostConsts.MaxTitleLength)]
public string Title { get; set; }
return Redirect(Url.Content($"~/blog/{blog.ShortName}/{insertedPost.Title}"));
[StringLength(PostConsts.MaxContentLength)]
public string Content { get; set; }
}
}
}

@ -0,0 +1,3 @@
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI
@addTagHelper *, Volo.Abp.AspNetCore.Mvc.UI.Bootstrap

@ -26,6 +26,7 @@
<ItemGroup>
<Content Remove="Pages\Blog\Posts\Detail.cshtml" />
<Content Remove="Pages\Blog\Posts\Index.cshtml" />
<Content Remove="Pages\Blog\_ViewImports.cshtml" />
</ItemGroup>
<ItemGroup>

Loading…
Cancel
Save