Browse Source

refactor blogging module application services

pull/5750/head
Yunus Emre Kalkan 6 years ago
parent
commit
00e8b2c16a
  1. 4
      modules/blogging/src/Volo.Blogging.Admin.Application.Contracts/Volo/Blogging/Admin/Blogs/CreateBlogDto.cs
  2. 6
      modules/blogging/src/Volo.Blogging.Admin.Application.Contracts/Volo/Blogging/Admin/Blogs/UpdateBlogDto.cs
  3. 6
      modules/blogging/src/Volo.Blogging.Admin.Application/Volo/Blogging/Admin/Blogs/BlogManagementAppService.cs
  4. 4
      modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Comments/Dtos/CreateCommentDto.cs
  5. 7
      modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Comments/Dtos/UpdateCommentDto.cs
  6. 1
      modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/CreatePostDto.cs
  7. 6
      modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/GetPostInput.cs
  8. 5
      modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/UpdatePostDto.cs
  9. 3
      modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Blogs/BlogAppService.cs
  10. 4
      modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Files/FileAppService.cs
  11. 4
      modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs
  12. 4
      modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/IPostRepository.cs
  13. 12
      modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Posts/EfCorePostRepository.cs
  14. 17
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Posts/MongoPostRepository.cs

4
modules/blogging/src/Volo.Blogging.Admin.Application.Contracts/Volo/Blogging/Admin/Blogs/CreateBlogDto.cs

@ -1,9 +1,13 @@
using System.ComponentModel.DataAnnotations;
namespace Volo.Blogging.Admin.Blogs
{
public class CreateBlogDto
{
[Required]
public string Name { get; set; }
[Required]
public string ShortName { get; set; }
public string Description { get; set; }

6
modules/blogging/src/Volo.Blogging.Admin.Application.Contracts/Volo/Blogging/Admin/Blogs/UpdateBlogDto.cs

@ -1,9 +1,13 @@
namespace Volo.Blogging.Admin.Blogs
using System.ComponentModel.DataAnnotations;
namespace Volo.Blogging.Admin.Blogs
{
public class UpdateBlogDto
{
[Required]
public string Name { get; set; }
[Required]
public string ShortName { get; set; }
public string Description { get; set; }

6
modules/blogging/src/Volo.Blogging.Admin.Application/Volo/Blogging/Admin/Blogs/BlogManagementAppService.cs

@ -36,10 +36,12 @@ namespace Volo.Blogging.Admin.Blogs
[Authorize(BloggingPermissions.Blogs.Create)]
public async Task<BlogDto> CreateAsync(CreateBlogDto input)
{
var newBlog = await _blogRepository.InsertAsync(new Blog(GuidGenerator.Create(), input.Name, input.ShortName)
var newBlog = new Blog(GuidGenerator.Create(), input.Name, input.ShortName)
{
Description = input.Description
});
};
newBlog = await _blogRepository.InsertAsync(newBlog);
return ObjectMapper.Map<Blog, BlogDto>(newBlog);
}

4
modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Comments/Dtos/CreateCommentDto.cs

@ -1,4 +1,5 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace Volo.Blogging.Comments.Dtos
{
@ -8,6 +9,7 @@ namespace Volo.Blogging.Comments.Dtos
public Guid PostId { get; set; }
[Required]
public string Text { get; set; }
}
}
}

7
modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Comments/Dtos/UpdateCommentDto.cs

@ -1,7 +1,10 @@
namespace Volo.Blogging.Comments.Dtos
using System.ComponentModel.DataAnnotations;
namespace Volo.Blogging.Comments.Dtos
{
public class UpdateCommentDto
{
[Required]
public string Text { get; set; }
}
}
}

1
modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/CreatePostDto.cs

@ -19,6 +19,7 @@ namespace Volo.Blogging.Posts
[DynamicStringLength(typeof(PostConsts), nameof(PostConsts.MaxUrlLength))]
public string Url { get; set; }
[Required]
[DynamicStringLength(typeof(PostConsts), nameof(PostConsts.MaxContentLength))]
public string Content { get; set; }

6
modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/GetPostInput.cs

@ -1,11 +1,13 @@
 using System;
using System.ComponentModel.DataAnnotations;
namespace Volo.Blogging.Posts
namespace Volo.Blogging.Posts
{
public class GetPostInput
{
[Required]
public string Url { get; set; }
public Guid BlogId { get; set; }
}
}
}

5
modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/UpdatePostDto.cs

@ -1,4 +1,5 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
namespace Volo.Blogging.Posts
@ -7,12 +8,16 @@ namespace Volo.Blogging.Posts
{
public Guid BlogId { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string CoverImage { get; set; }
[Required]
public string Url { get; set; }
[Required]
public string Content { get; set; }
public string Description { get; set; }

3
modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Blogs/BlogAppService.cs

@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Entities;
using Volo.Blogging.Blogs.Dtos;
@ -28,6 +29,8 @@ namespace Volo.Blogging.Blogs
public async Task<BlogDto> GetByShortNameAsync(string shortName)
{
Check.NotNullOrWhiteSpace(shortName, nameof(shortName));
var blog = await _blogRepository.FindByShortNameAsync(shortName);
if (blog == null)

4
modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Files/FileAppService.cs

@ -34,7 +34,7 @@ namespace Volo.Blogging.Files
{
if (input.Bytes.IsNullOrEmpty())
{
ThrowValidationException("Bytes can not be null or empty!", "Bytes");
ThrowValidationException("Bytes of file can not be null or empty!", "Bytes");
}
if (input.Bytes.Length > BloggingWebConsts.FileUploading.MaxFileSize)
@ -44,7 +44,7 @@ namespace Volo.Blogging.Files
if (!ImageFormatHelper.IsValidImage(input.Bytes, FileUploadConsts.AllowedImageUploadFormats))
{
throw new UserFriendlyException("Not a valid image format!");
throw new UserFriendlyException("Invalid image format!");
}
var uniqueFileName = GenerateUniqueFileName(Path.GetExtension(input.Name));

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

@ -187,9 +187,7 @@ namespace Volo.Blogging.Posts
private async Task<string> RenameUrlIfItAlreadyExistAsync(Guid blogId, string url, Post existingPost = null)
{
var postList = await _postRepository.GetListAsync();
if (postList.Where(p => p.Url == url).WhereIf(existingPost != null, p => existingPost.Id != p.Id).Any())
if (await _postRepository.IsPostUrlInUseAsync(blogId, url, existingPost?.Id))
{
return url + "-" + Guid.NewGuid().ToString().Substring(0, 5);
}

4
modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/IPostRepository.cs

@ -9,8 +9,10 @@ namespace Volo.Blogging.Posts
{
Task<List<Post>> GetPostsByBlogId(Guid id);
Task<bool> IsPostUrlInUseAsync(Guid blogId, string url, Guid? excludingPostId = null);
Task<Post> GetPostByUrl(Guid blogId, string url);
Task<List<Post>> GetOrderedList(Guid blogId,bool descending = false);
}
}

12
modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Posts/EfCorePostRepository.cs

@ -23,6 +23,18 @@ namespace Volo.Blogging.Posts
return await DbSet.Where(p => p.BlogId == id).OrderByDescending(p=>p.CreationTime).ToListAsync();
}
public Task<bool> IsPostUrlInUseAsync(Guid blogId, string url, Guid? excludingPostId = null)
{
var query = DbSet.Where(p => blogId == p.BlogId && p.Url == url);
if (excludingPostId != null)
{
query = query.Where(p => excludingPostId != p.Id);
}
return query.AnyAsync();
}
public async Task<Post> GetPostByUrl(Guid blogId, string url)
{
var post = await DbSet.FirstOrDefaultAsync(p => p.BlogId == blogId && p.Url == url);

17
modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Posts/MongoPostRepository.cs

@ -21,6 +21,19 @@ namespace Volo.Blogging.Posts
return await GetMongoQueryable().Where(p => p.BlogId == id).OrderByDescending(p => p.CreationTime).ToListAsync();
}
public Task<bool> IsPostUrlInUseAsync(Guid blogId, string url, Guid? excludingPostId = null)
{
var query = GetMongoQueryable().Where(p => blogId == p.BlogId && p.Url == url);
if (excludingPostId != null)
{
query = query.Where(p => excludingPostId != p.Id);
}
return query.AnyAsync();
}
public async Task<Post> GetPostByUrl(Guid blogId, string url)
{
var post = await GetMongoQueryable().FirstOrDefaultAsync(p => p.BlogId == blogId && p.Url == url);
@ -36,12 +49,12 @@ namespace Volo.Blogging.Posts
public async Task<List<Post>> GetOrderedList(Guid blogId, bool @descending = false)
{
var query = GetMongoQueryable().Where(x => x.BlogId == blogId);
if (!descending)
{
return await query.OrderBy(x => x.CreationTime).ToListAsync();
}
return await query.OrderByDescending(x => x.CreationTime).ToListAsync();
}
}

Loading…
Cancel
Save