使用.net core ABP和Angular模板構建部落格管理系統(實現自己的業務邏輯)
阿新 • • 發佈:2019-01-04
之前寫到使用.net core ABP 和Angular模板構建專案,建立後端服務。文章地址:http://www.jianshu.com/p/fde1ea20331f
建立完成後的api基本是不能用的,現在根據我們自己的業務邏輯來實現後端服務。
部分業務邏輯流程圖
建立Dto並新增資料校驗
關於ABP的資料校驗可以參考我這篇文章:http://www.jianshu.com/p/144f5cdd3ac8
ICustomValidate 介面用於自定義資料驗證,IShouldNormalize介面用於資料標準化
這裡就直接貼程式碼了
namespace MZC.Blog.Notes
{
/// <summary>
/// 建立的時候不需要太多資訊,內容更新主要依靠update
/// 在使用者點選建立的時候資料庫便建立資料,在使用者編輯過程中自動更新儲存資料。
/// </summary>
public class CreateNoteDto : IShouldNormalize
{
/// <summary>
/// 建立時間
/// </summary>
public DateTime? CreationTime { get; set; }
/// <summary>
/// 建立人
/// </summary>
public long CreatorUserId { get; set; }
/// <summary>
/// 內容的資料型別 markdown內容,html內容,或者其他
/// </summary>
public int TextType { get; set; }
public void Normalize()
{
if (!CreationTime.HasValue) CreationTime = DateTime.Now;
}
}
/// <summary>
/// 自動更新所傳的資料
/// </summary>
public class UpdateNoteDto : EntityDto<int>, IShouldNormalize
{
/// <summary>
/// 標題
/// </summary>
public string Title { get; set; }
/// <summary>
/// 內容
/// </summary>
public string Content { get; set; }
/// <summary>
/// 上次修改時間
/// </summary>
public DateTime? LastModificationTime { get; set; }
public virtual void Normalize()
{
if (!LastModificationTime.HasValue)
{
LastModificationTime = DateTime.Now;
}
}
}
/// <summary>
/// 釋出更新時所用
/// </summary>
public class PublicNoteDto : UpdateNoteDto, ICustomValidate, IShouldNormalize
{
/// <summary>
/// 簡單描述,用於微信推送時的描述或者其他
/// </summary>
public string Des { get; set; }
/// <summary>
/// 封面圖片,可用於微信推送時或者其他
/// </summary>
[Required]
public string Img { get; set; }
/// <summary>
/// 關鍵字,可用於搜尋,分類等
/// </summary>
public string Tags { get; set; }
/// <summary>
/// 是否釋出
/// </summary>
public bool? IsPublic { get; set; }
public override void Normalize()
{
base.Normalize();
IsPublic = true;
}
public void AddValidationErrors(CustomValidationContext context)
{
if (string.IsNullOrEmpty(Des))
{
string error = "描述不能為空!";
context.Results.Add(new ValidationResult(error));
}
if (Des.Length < 10)
{
string error = "描述不能少於10個字!";
context.Results.Add(new ValidationResult(error));
}
if (Des.Length > 200)
{
string error = "描述不能大於200個字!";
context.Results.Add(new ValidationResult(error));
}
}
}
/// <summary>
/// 用於列表展示
/// </summary>
public class NoteDto : EntityDto<int>
{
/// <summary>
/// 標題
/// </summary>
public string Title { get; set; }
/// <summary>
/// 建立時間
/// </summary>
public string CreationTime { get; set; }
/// <summary>
/// 點贊次數
/// </summary>
public long Like { get; set; }
/// <summary>
/// 收藏次數
/// </summary>
public long Collect { get; set; }
/// <summary>
/// 瀏覽次數
/// </summary>
public long Scan { get; set; }
/// <summary>
/// 是否釋出
/// </summary>
public string IsPublic { get; set; }
}
public class GetNoteListDto: PagedResultRequestDto
{
/// <summary>
/// 用於搜尋的關鍵字
/// </summary>
public string key { get; set; }
}
}
建立對映
namespace MZC.Blog.Notes
{
public class NoteMapProfile : Profile
{
public NoteMapProfile()
{
CreateMap<CreateNoteDto, Note>();
CreateMap<UpdateNoteDto, Note>();
CreateMap<PublicNoteDto, Note>();
//使用自定義解析
CreateMap<Note, NoteDto>().ForMember(x=>x.IsPublic,opt=> {
opt.ResolveUsing<NoteToNoteDtoResolver>();
});
CreateMap<Note, PublicNoteDto>();
}
}
/// <summary>
/// 自定義解析
/// </summary>
public class NoteToNoteDtoResolver : IValueResolver<Note, NoteDto, string>
{
public string Resolve(Note source, NoteDto destination, string destMember, ResolutionContext context)
{
return source.IsPublic ? "已釋出" : "未釋出";
}
}
}
使用授權
關於ABP授權詳細的介紹和使用請看我的另一篇文章:http://www.jianshu.com/p/6e224f4f9705
在core專案Authorization資料夾下有模板提供的授權模組。
在PermissionNames 中定義許可權,在AuthorizationProvider中新增定義的許可權,然後再專案中就可以通過AbpAuthorize特性或者PermissionChecker類來驗證
public static class PermissionNames
{
public const string Pages_Tenants = "Pages.Tenants";
public const string Pages_Users = "Pages.Users";
public const string Pages_Roles = "Pages.Roles";
/// <summary>
/// 部落格管理頁面許可權
/// </summary>
public const string Pages_Blogs = "Pages.Blogs";
public const string Pages_Blogs_Notes = "Pages.Blogs.Notes";
public const string Blogs_Notes_Edit = "Pages.Blogs.Notes.Edit";
public const string Blogs_Notes_Delete = "Pages.Blogs.Notes.Delete";
}
public class MZCAuthorizationProvider : AuthorizationProvider
{
public override void SetPermissions(IPermissionDefinitionContext context)
{
context.CreatePermission(PermissionNames.Pages_Users, L("Users"));
context.CreatePermission(PermissionNames.Pages_Roles, L("Roles"));
context.CreatePermission(PermissionNames.Pages_Tenants, L("Tenants"), multiTenancySides: MultiTenancySides.Host);
var BlogPermission = context.CreatePermission(PermissionNames.Pages_Blogs, L("Blogs"));
var NotePermission = BlogPermission.CreateChildPermission(PermissionNames.Pages_Blogs_Notes,L("Notes"));
NotePermission.CreateChildPermission(PermissionNames.Blogs_Notes_Edit, L("EditNotes"));
NotePermission.CreateChildPermission(PermissionNames.Blogs_Notes_Delete, L("DeleteNotes"));
}
private static ILocalizableString L(string name)
{
return new LocalizableString(name, MZCConsts.LocalizationSourceName);
}
}
完善我們的服務和介面
因為是自己的部落格系統,沒必要那麼麻煩就只使用了入口許可權定義在類的上面。
public interface INoteAppServer: IAsyncCrudAppService<NoteDto,int, GetNoteListDto, CreateNoteDto,UpdateNoteDto>
{
Task PublicNote(PublicNoteDto input);
Task<PublicNoteDto> GetNote(EntityDto<int> input);
}
[AbpAuthorize(PermissionNames.Pages_Blogs_Notes)]
public class NoteAppServer : AsyncCrudAppService<Note, NoteDto, int, GetNoteListDto, CreateNoteDto, UpdateNoteDto>, INoteAppServer
{
public NoteAppServer(IRepository<Note> repository)
: base(repository)
{
}
public override async Task<NoteDto> Create(CreateNoteDto input)
{
var note = ObjectMapper.Map<Note>(input);
var result = await Repository.InsertAsync(note);
return ObjectMapper.Map<NoteDto>(result);
}
public async Task PublicNote(PublicNoteDto input)
{
var note = Repository.Get(input.Id);
ObjectMapper.Map(input,note);
var result = await Repository.UpdateAsync(note);
}
public override async Task<NoteDto> Update(UpdateNoteDto input)
{
var note = Repository.Get(input.Id);
ObjectMapper.Map(input,note);
var result = await Repository.UpdateAsync(note);
return ObjectMapper.Map<NoteDto>(result);
}
public override async Task<PagedResultDto<NoteDto>> GetAll(GetNoteListDto input)
{
var data = Repository.GetAll().Where(m => !m.IsDeleted);
data = data.WhereIf(!string.IsNullOrEmpty(input.key), m => m.Title.Contains(input.key) || m.Tags.Contains(input.key));
int count = await data.CountAsync();
var notes = await data.OrderByDescending(q => q.CreationTime)
.PageBy(input)
.ToListAsync();
return new PagedResultDto<NoteDto>()
{
TotalCount = count,
Items = ObjectMapper.Map<List<NoteDto>>(notes)
};
}
public async Task<PublicNoteDto> GetNote(EntityDto<int> input)
{
var note = await Repository.GetAsync(input.Id);
return ObjectMapper.Map<PublicNoteDto>(note);
}
}