使用.net core ABP和Angular模板构建博客管理系统(实现自己的业务逻辑)
来源:互联网 发布:cst仿真软件价格 编辑:程序博客网 时间:2024/06/05 02:21
之前写到使用.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; } }}
创建映射
创建NoteMapProfile.cs文件,并添加相关映射
关于ABP框架映射的更多内容请参考我这篇文章:http://www.jianshu.com/p/6ef125e873e9
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); } }
阅读全文
0 0
- 使用.net core ABP和Angular模板构建博客管理系统(实现自己的业务逻辑)
- 使用.net core ABP和Angular模板构建博客管理系统(实现博客列表页面)
- 使用.net core ABP和Angular模板构建博客管理系统(实现编辑页面功能)
- 使用.net core ABP和Angular模板构建博客管理系统(创建后端服务)
- 使用.net core ABP和Angular模板构建博客管理系统(完善前台服务)
- 开始使用ABP.CORE模板 (ASP.NET Core with Angular)
- ABP+AdminLTE+Bootstrap Table权限管理系统第一节--使用ASP.NET Boilerplate模板创建解决方案
- Abp项目模板使用Oracle数据库包括系统权限管理
- NET使用ABP框架搭建博客项目(一)
- ASP.NET MVC+EF框架+EasyUI实现权限管理系列(4)-业务逻辑层的封装
- 搭建你的Spring.Net+Nhibernate+Asp.Net Mvc 框架 (三)实现数据库接口层和业务逻辑层
- 自己写“俄罗斯方块”(一).实现基本业务逻辑和绘图
- 构建自己的博客系统的技术
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
- 业务逻辑层JDBC模板代码(使用Manager代理类,具有控制事务和管理connection功能)
- 使用Angular 4、Bootstrap 4、TypeScript和ASP.NET Core开发的Apworks框架案例应用
- Asp.net+Vue2构建简单记账WebApp之二(使用ABP迅速搭建.Net后台)
- C#酒店管理系统(内部)业务逻辑
- Octave教程(一)
- 56G 线缆在数据中心的应用
- Octave教程(二)
- Octave教程(三)
- Octave教程(四)
- 使用.net core ABP和Angular模板构建博客管理系统(实现自己的业务逻辑)
- Octave教程(五)
- Octave教程(六)
- 编程作业(一)
- 逻辑回归(一)
- 【中国大学MOOC-陈越、何钦铭-数据结构-2017秋】最大子列和问题
- 逻辑回归(二)
- 逻辑回归(三)
- 正则化