From f62ed01ac24433dcdc0faf02400fc871caa65dbe Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Wed, 27 Jun 2018 10:41:46 +0300 Subject: [PATCH] Post Edit & create --- .../Volo/Blogging/Blogs/IBlogAppService.cs | 2 + .../Volo/Blogging/Posts/CreatePostDto.cs | 16 ++++ .../Blogging/Posts/GetPostForEditOutput.cs | 16 ++++ .../Volo/Blogging/Posts/IPostAppService.cs | 10 ++- .../Volo/Blogging/Posts/PostDto.cs | 4 +- .../Volo/Blogging/Posts/PostWithDetailsDto.cs | 16 ++++ .../Volo/Blogging/Posts/UpdatePostDto.cs | 16 ++++ .../BloggingApplicationAutoMapperProfile.cs | 2 + .../Blogging/BloggingApplicationModule.cs | 5 ++ .../Volo/Blogging/Blogs/BlogAppService.cs | 11 ++- .../Volo/Blogging/Posts/PostAppService.cs | 44 ++++++++-- .../Volo.Blogging.Web/BloggingWebModule.cs | 2 + .../Pages/Blog/Posts/Detail.cshtml | 6 +- .../Pages/Blog/Posts/Detail.cshtml.cs | 4 +- .../Pages/Blog/Posts/Edit.cshtml | 31 +++++++ .../Pages/Blog/Posts/Edit.cshtml.cs | 49 +++++++++++ .../Pages/Blog/Posts/Index.cshtml | 4 +- .../Pages/Blog/Posts/Index.cshtml.cs | 4 +- .../Pages/Blog/Posts/New.cshtml | 30 +++++++ .../Pages/Blog/Posts/New.cshtml.cs | 52 ++++++++++++ .../Volo.Blogging.Web.csproj | 10 +++ .../Volo/Blogging/PostAppService_Tests.cs | 83 +++++++++++++++++++ 22 files changed, 399 insertions(+), 18 deletions(-) create mode 100644 modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/CreatePostDto.cs create mode 100644 modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/GetPostForEditOutput.cs create mode 100644 modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/PostWithDetailsDto.cs create mode 100644 modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/UpdatePostDto.cs create mode 100644 modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Edit.cshtml create mode 100644 modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Edit.cshtml.cs create mode 100644 modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/New.cshtml create mode 100644 modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/New.cshtml.cs create mode 100644 modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/PostAppService_Tests.cs diff --git a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Blogs/IBlogAppService.cs b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Blogs/IBlogAppService.cs index 0dc5d0c922..e9e375a5c6 100644 --- a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Blogs/IBlogAppService.cs +++ b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Blogs/IBlogAppService.cs @@ -12,5 +12,7 @@ namespace Volo.Blogging.Blogs Task> GetListAsync(); Task GetByShortNameAsync(string shortName); + + Task GetAsync(Guid id); } } diff --git a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/CreatePostDto.cs b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/CreatePostDto.cs new file mode 100644 index 0000000000..e6716b6dd0 --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/CreatePostDto.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Volo.Abp.Application.Dtos; + +namespace Volo.Blogging.Posts +{ + public class CreatePostDto + { + public Guid BlogId { get; set; } + + public string Title { get; set; } + + public string Content { get; set; } + } +} diff --git a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/GetPostForEditOutput.cs b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/GetPostForEditOutput.cs new file mode 100644 index 0000000000..79a8cf025e --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/GetPostForEditOutput.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Volo.Abp.Application.Dtos; + +namespace Volo.Blogging.Posts +{ + public class GetPostForEditOutput : FullAuditedEntityDto + { + public Guid BlogId { get; set; } + + public string Title { get; set; } + + public string Content { get; set; } + } +} diff --git a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/IPostAppService.cs b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/IPostAppService.cs index fd040c2b8a..3bf60b40b2 100644 --- a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/IPostAppService.cs +++ b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/IPostAppService.cs @@ -9,8 +9,14 @@ namespace Volo.Blogging.Posts { public interface IPostAppService : IApplicationService { - ListResultDto GetPostsByBlogId(Guid id); + ListResultDto GetListByBlogIdAsync(Guid id); - Task GetPost(GetPostInput input); + Task GetByTitleAsync(GetPostInput input); + + Task GetAsync(Guid id); + + Task CreateAsync(CreatePostDto input); + + Task UpdateAsync(Guid id, UpdatePostDto input); } } diff --git a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/PostDto.cs b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/PostDto.cs index a8d5e2c126..f8ff4feae8 100644 --- a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/PostDto.cs +++ b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/PostDto.cs @@ -3,14 +3,12 @@ using Volo.Abp.Application.Dtos; namespace Volo.Blogging.Posts { - public class PostDto : EntityDto + public class PostDto : FullAuditedEntityDto { public Guid BlogId { get; protected set; } public string Title { get; protected set; } public string Content { get; set; } - - public DateTime CreationTime { get; set; } } } diff --git a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/PostWithDetailsDto.cs b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/PostWithDetailsDto.cs new file mode 100644 index 0000000000..c9b4a3788c --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/PostWithDetailsDto.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Volo.Abp.Application.Dtos; + +namespace Volo.Blogging.Posts +{ + public class PostWithDetailsDto : FullAuditedEntityDto + { + public Guid BlogId { get; set; } + + public string Title { get; set; } + + public string Content { get; set; } + } +} diff --git a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/UpdatePostDto.cs b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/UpdatePostDto.cs new file mode 100644 index 0000000000..329d18df8f --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/UpdatePostDto.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Volo.Abp.Application.Dtos; + +namespace Volo.Blogging.Posts +{ + public class UpdatePostDto + { + public Guid BlogId { get; set; } + + public string Title { get; set; } + + public string Content { get; set; } + } +} diff --git a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/BloggingApplicationAutoMapperProfile.cs b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/BloggingApplicationAutoMapperProfile.cs index 6333980ff5..f866f84f15 100644 --- a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/BloggingApplicationAutoMapperProfile.cs +++ b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/BloggingApplicationAutoMapperProfile.cs @@ -1,4 +1,5 @@ using AutoMapper; +using Volo.Abp.AutoMapper; using Volo.Blogging.Blogs; using Volo.Blogging.Posts; @@ -10,6 +11,7 @@ namespace Volo.Blogging { CreateMap(); CreateMap(); + CreateMap(); } } } diff --git a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/BloggingApplicationModule.cs b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/BloggingApplicationModule.cs index 022ebb3a44..a033eac84b 100644 --- a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/BloggingApplicationModule.cs +++ b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/BloggingApplicationModule.cs @@ -14,6 +14,11 @@ namespace Volo.Blogging { public override void ConfigureServices(IServiceCollection services) { + services.Configure(options => + { + options.AddProfile(validate: true); + }); + services.AddAssemblyOf(); } } diff --git a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Blogs/BlogAppService.cs b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Blogs/BlogAppService.cs index d6383f4a47..8f90bb5e7a 100644 --- a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Blogs/BlogAppService.cs +++ b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Blogs/BlogAppService.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; @@ -34,6 +35,12 @@ namespace Volo.Blogging.Blogs return ObjectMapper.Map(blog); } - + + public async Task GetAsync(Guid id) + { + var blog = await _blogRepository.GetAsync(id); + + return ObjectMapper.Map(blog); + } } } diff --git a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs index 03845aa8db..7c02d417e7 100644 --- a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs +++ b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; using System.Collections.Generic; +using Volo.Abp.Users; namespace Volo.Blogging.Posts { @@ -16,19 +17,52 @@ namespace Volo.Blogging.Posts _postRepository = postRepository; } - public ListResultDto GetPostsByBlogId(Guid id) + public ListResultDto GetListByBlogIdAsync(Guid id) { var posts = _postRepository.GetPostsByBlogId(id); - return new ListResultDto( - ObjectMapper.Map, List>(posts)); + return new ListResultDto( + ObjectMapper.Map, List>(posts)); } - public async Task GetPost(GetPostInput input) + public async Task GetByTitleAsync(GetPostInput input) { var post = await _postRepository.GetPost(input.BlogId, input.Title); - return ObjectMapper.Map(post); + return ObjectMapper.Map(post); + } + + public async Task GetAsync(Guid id) + { + var post = await _postRepository.GetAsync(id); + + return ObjectMapper.Map(post); + } + + public async Task UpdateAsync(Guid id, UpdatePostDto input) + { + var post = await _postRepository.GetAsync(id); + + post.SetTitle(input.Title); + post.Content = input.Content; + + post = await _postRepository.UpdateAsync(post); + + return ObjectMapper.Map(post); + } + + public async Task CreateAsync(CreatePostDto input) + { + var post = new Post( + GuidGenerator.Create(), + blogId: input.BlogId, + creatorId: CurrentUser.GetId(), + title: input.Title + ) {Content = input.Content}; + + await _postRepository.InsertAsync(post); + + return ObjectMapper.Map(post); } } } diff --git a/modules/blogging/src/Volo.Blogging.Web/BloggingWebModule.cs b/modules/blogging/src/Volo.Blogging.Web/BloggingWebModule.cs index b105c2a532..368d038a7a 100644 --- a/modules/blogging/src/Volo.Blogging.Web/BloggingWebModule.cs +++ b/modules/blogging/src/Volo.Blogging.Web/BloggingWebModule.cs @@ -35,6 +35,8 @@ namespace Volo.Blogging //TODO: Make configurable! options.Conventions.AddPageRoute("/Blog/Posts/Index", "blog/{blogShortName}"); options.Conventions.AddPageRoute("/Blog/Posts/Detail", "blog/{blogShortName}/{postTitle}"); + options.Conventions.AddPageRoute("/Blog/Posts/Edit", "blog/{blogShortName}/manage/{postId}"); + options.Conventions.AddPageRoute("/Blog/Posts/New", "blog/{blogShortName}/manage/new"); }); services.AddAssemblyOf(); diff --git a/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Detail.cshtml b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Detail.cshtml index ab76fff180..f838c1ed12 100644 --- a/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Detail.cshtml +++ b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Detail.cshtml @@ -8,4 +8,8 @@

@Model.Post.Content

-Posted to @Model.Blog.Name On @Model.Post.CreationTime \ No newline at end of file +

+ Posted to @Model.Blog.Name On @Model.Post.CreationTime +

+ +edit \ No newline at end of file diff --git a/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Detail.cshtml.cs b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Detail.cshtml.cs index bfc49ccb31..b11b745e64 100644 --- a/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Detail.cshtml.cs +++ b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Detail.cshtml.cs @@ -20,7 +20,7 @@ namespace Volo.Blogging.Pages.Blog.Posts [BindProperty(SupportsGet = true)] public string PostTitle { get; set; } - public PostDto Post { get; set; } + public PostWithDetailsDto Post { get; set; } public BlogDto Blog { get; set; } @@ -34,7 +34,7 @@ namespace Volo.Blogging.Pages.Blog.Posts { var blog = await _blogAppService.GetByShortNameAsync(BlogShortName); - Post = await _postAppService.GetPost(new GetPostInput {BlogId = blog.Id , Title = PostTitle}); + Post = await _postAppService.GetByTitleAsync(new GetPostInput {BlogId = blog.Id , Title = PostTitle}); Blog = blog; } } diff --git a/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Edit.cshtml b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Edit.cshtml new file mode 100644 index 0000000000..601af19e39 --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Edit.cshtml @@ -0,0 +1,31 @@ +@page +@using Volo.Blogging.Pages.Blog.Posts +@model EditModel +@{ + +} +@using (Html.BeginForm(FormMethod.Post)) +{ +
+ +
+ +
+
+ +
+ +
+ +
+
+ + + + +
+
+ +
+
+} \ No newline at end of file diff --git a/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Edit.cshtml.cs b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Edit.cshtml.cs new file mode 100644 index 0000000000..575d9cb539 --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Edit.cshtml.cs @@ -0,0 +1,49 @@ +using System; +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; + +namespace Volo.Blogging.Pages.Blog.Posts +{ + public class EditModel : PageModel + { + private readonly IPostAppService _postAppService; + private readonly IBlogAppService _blogAppService; + + [BindProperty(SupportsGet = true)] + public string BlogShortName { get; set; } + + [BindProperty(SupportsGet = true)] + public string PostId { get; set; } + + public PostWithDetailsDto Post { get; set; } + + public BlogDto Blog { get; set; } + + public EditModel(IPostAppService postAppService, IBlogAppService blogAppService) + { + _postAppService = postAppService; + _blogAppService = blogAppService; + } + + public async void OnGet() + { + var blog = await _blogAppService.GetByShortNameAsync(BlogShortName); + + Post = await _postAppService.GetAsync(new Guid(PostId)); + + Blog = blog; + } + + public async Task OnPost(Guid id, UpdatePostDto post) + { + var editedPost = await _postAppService.UpdateAsync(id, post); + var blog = await _blogAppService.GetAsync(editedPost.BlogId); + + return Redirect(Url.Content($"~/blog/{blog.ShortName}/{editedPost.Title}")); + } + } +} \ No newline at end of file diff --git a/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Index.cshtml b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Index.cshtml index 0ab68f2858..1f22986485 100644 --- a/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Index.cshtml +++ b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Index.cshtml @@ -10,6 +10,8 @@ + +Create New Post diff --git a/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Index.cshtml.cs b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Index.cshtml.cs index 18c82322ac..e9af2b77cc 100644 --- a/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Index.cshtml.cs +++ b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Index.cshtml.cs @@ -19,7 +19,7 @@ namespace Volo.Blogging.Pages.Blog.Posts [BindProperty(SupportsGet = true)] public string BlogShortName { get; set; } - public IReadOnlyList Posts { get; set; } + public IReadOnlyList Posts { get; set; } public IndexModel(IPostAppService postAppService, IBlogAppService blogAppService) { @@ -31,7 +31,7 @@ namespace Volo.Blogging.Pages.Blog.Posts { var blog = await _blogAppService.GetByShortNameAsync(BlogShortName); - Posts = _postAppService.GetPostsByBlogId(blog.Id).Items; + Posts = _postAppService.GetListByBlogIdAsync(blog.Id).Items; } } } \ No newline at end of file diff --git a/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/New.cshtml b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/New.cshtml new file mode 100644 index 0000000000..7c991668f4 --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/New.cshtml @@ -0,0 +1,30 @@ +@page +@using Volo.Blogging.Pages.Blog.Posts +@model NewModel +@{ + +} +@using (Html.BeginForm(FormMethod.Post)) +{ +
+ +
+ +
+
+ +
+ +
+ +
+
+ + + +
+
+ +
+
+} \ No newline at end of file diff --git a/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/New.cshtml.cs b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/New.cshtml.cs new file mode 100644 index 0000000000..48ed76d049 --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/New.cshtml.cs @@ -0,0 +1,52 @@ +using System; +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; + +namespace Volo.Blogging.Pages.Blog.Posts +{ + public class NewModel : PageModel + { + private readonly IPostAppService _postAppService; + private readonly IBlogAppService _blogAppService; + + [BindProperty(SupportsGet = true)] + public string BlogShortName { get; set; } + + [BindProperty(SupportsGet = true)] + public string PostId { get; set; } + + public PostWithDetailsDto Post { get; set; } + + public BlogDto Blog { get; set; } + + public NewModel(IPostAppService postAppService, IBlogAppService blogAppService) + { + _postAppService = postAppService; + _blogAppService = blogAppService; + } + + public async void OnGet() + { + var blog = await _blogAppService.GetByShortNameAsync(BlogShortName); + + Post = new PostWithDetailsDto() + { + BlogId = blog.Id + }; + + Blog = blog; + } + + public async Task OnPost(CreatePostDto post) + { + var insertedPost = await _postAppService.CreateAsync(post); + var blog = await _blogAppService.GetAsync(insertedPost.BlogId); + + return Redirect(Url.Content($"~/blog/{blog.ShortName}/{insertedPost.Title}")); + } + } +} \ No newline at end of file diff --git a/modules/blogging/src/Volo.Blogging.Web/Volo.Blogging.Web.csproj b/modules/blogging/src/Volo.Blogging.Web/Volo.Blogging.Web.csproj index 629c022e8e..6bea7aecf6 100644 --- a/modules/blogging/src/Volo.Blogging.Web/Volo.Blogging.Web.csproj +++ b/modules/blogging/src/Volo.Blogging.Web/Volo.Blogging.Web.csproj @@ -24,4 +24,14 @@ + + + + + + + $(IncludeRazorContentInPack) + + + diff --git a/modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/PostAppService_Tests.cs b/modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/PostAppService_Tests.cs new file mode 100644 index 0000000000..2e81e45b2a --- /dev/null +++ b/modules/blogging/test/Volo.Blogging.Application.Tests/Volo/Blogging/PostAppService_Tests.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Shouldly; +using Volo.Blogging.Blogs; +using Volo.Blogging.Posts; +using Xunit; + +namespace Volo.Blogging +{ + public class PostAppService_Tests : BloggingApplicationTestBase + { + private readonly IPostAppService _postAppService; + private readonly IBlogRepository _blogRepository; + private readonly BloggingTestData _testData; + + public PostAppService_Tests() + { + _testData = GetRequiredService(); + _postAppService = GetRequiredService(); + _blogRepository = GetRequiredService(); + } + + [Fact] + public async Task Should_Create_A_Post() + { + var blogId = (await _blogRepository.GetListAsync()).First().Id; + var title = "title"; + var content = "content"; + + var newPost = await _postAppService.CreateAsync(new CreatePostDto() + { + BlogId = blogId, + Title = title, + Content = content + }); + + UsingDbContext(context => + { + var post = context.Posts.FirstOrDefault(q => q.Title == title); + post.ShouldNotBeNull(); + post.Title.ShouldBe(title); + post.Content.ShouldBe(content); + post.BlogId.ShouldBe(blogId); + }); + } + + [Fact] + public async Task Should_Create_And_Update_A_Post() + { + var blogId = (await _blogRepository.GetListAsync()).First().Id; + var title = "title"; + var newTitle = "newtitle"; + var content = "content"; + + var newPost = await _postAppService.CreateAsync(new CreatePostDto() + { + BlogId = blogId, + Title = title, + Content = content + }); + + await _postAppService.UpdateAsync(newPost.Id, new UpdatePostDto() + { + BlogId = blogId, + Title = newTitle, + Content = content + }); + + + UsingDbContext(context => + { + var post = context.Posts.FirstOrDefault(q => q.Id == newPost.Id); + post.ShouldNotBeNull(); + post.Title.ShouldBe(newTitle); + post.Content.ShouldBe(content); + post.BlogId.ShouldBe(blogId); + }); + } + } +}