Browse Source

blogging Module Refactoring dto's, viewmodels, mappers

pull/318/head
Yunus Emre Kalkan 8 years ago
parent
commit
dcf87da74b
  1. 2
      modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/GetPostForEditOutput.cs
  2. 2
      modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/IPostAppService.cs
  3. 1
      modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/BloggingApplicationAutoMapperProfile.cs
  4. 15
      modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs
  5. 19
      modules/blogging/src/Volo.Blogging.Web/AbpBloggingWebAutoMapperProfile.cs
  6. 7
      modules/blogging/src/Volo.Blogging.Web/BloggingWebModule.cs
  7. 2
      modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Edit.cshtml
  8. 40
      modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Edit.cshtml.cs
  9. 13
      modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/New.cshtml.cs
  10. 1
      modules/blogging/src/Volo.Blogging.Web/Volo.Blogging.Web.csproj

2
modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/GetPostForEditOutput.cs

@ -5,7 +5,7 @@ using Volo.Abp.Application.Dtos;
namespace Volo.Blogging.Posts
{
public class GetPostForEditOutput : FullAuditedEntityDto<Guid>
public class GetPostForEditOutput : EntityDto<Guid>
{
public Guid BlogId { get; set; }

2
modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/IPostAppService.cs

@ -15,6 +15,8 @@ namespace Volo.Blogging.Posts
Task<PostWithDetailsDto> GetAsync(Guid id);
Task<GetPostForEditOutput> GetForEditAsync(Guid id);
Task<PostWithDetailsDto> CreateAsync(CreatePostDto input);
Task<PostWithDetailsDto> UpdateAsync(Guid id, UpdatePostDto input);

1
modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/BloggingApplicationAutoMapperProfile.cs

@ -12,6 +12,7 @@ namespace Volo.Blogging
CreateMap<Blog, BlogDto>();
CreateMap<Post, PostDto>();
CreateMap<Post, PostWithDetailsDto>();
CreateMap<Post, GetPostForEditOutput>();
}
}
}

15
modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs

@ -41,6 +41,21 @@ namespace Volo.Blogging.Posts
return ObjectMapper.Map<Post, PostWithDetailsDto>(post);
}
public async Task<GetPostForEditOutput> GetForEditAsync(Guid id)
{
var post = await _postRepository.GetAsync(id);
var dto = new GetPostForEditOutput
{
Id = post.Id,
BlogId = post.BlogId,
Content = post.Content,
Title = post.Title
};
return dto;
}
[Authorize(BloggingPermissions.Posts.Update)]
public async Task<PostWithDetailsDto> UpdateAsync(Guid id, UpdatePostDto input)
{

19
modules/blogging/src/Volo.Blogging.Web/AbpBloggingWebAutoMapperProfile.cs

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Volo.Blogging.Pages.Blog.Posts;
using Volo.Blogging.Posts;
namespace Volo.Blogging
{
public class AbpBloggingWebAutoMapperProfile : Profile
{
public AbpBloggingWebAutoMapperProfile()
{
CreateMap<GetPostForEditOutput, EditPostViewModel>();
CreateMap<NewModel.CreatePostViewModel, CreatePostDto>();
}
}
}

7
modules/blogging/src/Volo.Blogging.Web/BloggingWebModule.cs

@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Volo.Abp.AspNetCore.Mvc.Localization;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap;
using Volo.Abp.AutoMapper;
using Volo.Abp.Localization;
using Volo.Abp.Localization.Resources.AbpValidation;
using Volo.Abp.Modularity;
@ -14,6 +15,7 @@ namespace Volo.Blogging
{
[DependsOn(
typeof(BloggingHttpApiModule),
typeof(AbpAutoMapperModule),
typeof(AbpAspNetCoreMvcUiBootstrapModule)
)]
public class BloggingWebModule : AbpModule
@ -42,6 +44,11 @@ namespace Volo.Blogging
.AddVirtualJson("/Localization/Resources/Blogging/Web");
});
services.Configure<AbpAutoMapperOptions>(options =>
{
options.AddProfile<AbpBloggingWebAutoMapperProfile>(validate: true);
});
services.Configure<RazorPagesOptions>(options =>
{
//TODO: Make configurable!

2
modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Edit.cshtml

@ -17,7 +17,7 @@
</div>
<abp-input asp-for="@Model.Post.BlogId" hidden="" />
<input name="Id" value="@Model.Post.Id" hidden="">
<abp-input asp-for="@Model.Post.Id" hidden="" />
<abp-button type="submit" form="edit-post-form">
<span>Submit</span>

40
modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Edit.cshtml.cs

@ -1,13 +1,14 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Volo.Abp.AspNetCore.Mvc.UI.RazorPages;
using Volo.Blogging.Blogs;
using Volo.Blogging.Posts;
namespace Volo.Blogging.Pages.Blog.Posts
{
public class EditModel : PageModel
public class EditModel : AbpPageModel
{
private readonly IPostAppService _postAppService;
private readonly IBlogAppService _blogAppService;
@ -15,7 +16,8 @@ namespace Volo.Blogging.Pages.Blog.Posts
[BindProperty(SupportsGet = true)]
public string PostId { get; set; }
public PostWithDetailsDto Post { get; set; }
[BindProperty]
public EditPostViewModel Post { get; set; }
public EditModel(IPostAppService postAppService, IBlogAppService blogAppService)
{
@ -25,15 +27,41 @@ namespace Volo.Blogging.Pages.Blog.Posts
public async void OnGet()
{
Post = await _postAppService.GetAsync(new Guid(PostId));
var postDto = await _postAppService.GetForEditAsync(new Guid(PostId));
Post = ObjectMapper.Map<GetPostForEditOutput, EditPostViewModel>(postDto);
}
public async Task<ActionResult> OnPost(Guid id, UpdatePostDto post)
public async Task<ActionResult> OnPost()
{
var editedPost = await _postAppService.UpdateAsync(id, post);
var post = new UpdatePostDto
{
BlogId = Post.BlogId,
Title = Post.Title,
Content = Post.Content
};
var editedPost = await _postAppService.UpdateAsync(Post.Id, post);
var blog = await _blogAppService.GetAsync(editedPost.BlogId);
return Redirect(Url.Content($"~/blog/{blog.ShortName}/{editedPost.Title}"));
}
}
public class EditPostViewModel
{
[HiddenInput]
public Guid Id { get; set; }
[HiddenInput]
public Guid BlogId { get; set; }
[Required]
[StringLength(PostConsts.MaxTitleLength)]
[Display(Name = "Title")]
public string Title { get; set; }
[StringLength(PostConsts.MaxContentLength)]
[Display(Name = "Content")]
public string Content { get; set; }
}
}

13
modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/New.cshtml.cs

@ -2,13 +2,13 @@ using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Volo.Abp.AspNetCore.Mvc.UI.RazorPages;
using Volo.Blogging.Blogs;
using Volo.Blogging.Posts;
namespace Volo.Blogging.Pages.Blog.Posts
{
public class NewModel : PageModel
public class NewModel : AbpPageModel
{
private readonly IPostAppService _postAppService;
private readonly IBlogAppService _blogAppService;
@ -39,14 +39,7 @@ namespace Volo.Blogging.Pages.Blog.Posts
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
}
);
var postWithDetailsDto = await _postAppService.CreateAsync(ObjectMapper.Map<CreatePostViewModel,CreatePostDto>(Post));
//TODO: Try Url.Page(...)
return Redirect(Url.Content($"~/blog/{blog.ShortName}/{postWithDetailsDto.Title}"));

1
modules/blogging/src/Volo.Blogging.Web/Volo.Blogging.Web.csproj

@ -14,6 +14,7 @@
<ItemGroup>
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.AspNetCore.Mvc.UI.Bootstrap\Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.AutoMapper\Volo.Abp.AutoMapper.csproj" />
<ProjectReference Include="..\Volo.Blogging.HttpApi\Volo.Blogging.HttpApi.csproj" />
<PackageReference Include="CommonMark.NET" Version="0.15.1" />
</ItemGroup>

Loading…
Cancel
Save