使用.net core ABP和Angular模板構(gòu)建博客管理系統(tǒng)(實現(xiàn)自己的業(yè)務(wù)邏輯)

返回目錄

之前寫到使用.net core ABP 和Angular模板構(gòu)建項目,創(chuàng)建后端服務(wù)。文章地址:http://www.reibang.com/p/fde1ea20331f
創(chuàng)建完成后的api基本是不能用的,現(xiàn)在根據(jù)我們自己的業(yè)務(wù)邏輯來實現(xiàn)后端服務(wù)橙依。

部分業(yè)務(wù)邏輯流程圖

其他功能流程省略

創(chuàng)建Dto并添加數(shù)據(jù)校驗

關(guān)于ABP的數(shù)據(jù)校驗可以參考我這篇文章:http://www.reibang.com/p/144f5cdd3ac8
ICustomValidate 接口用于自定義數(shù)據(jù)驗證,IShouldNormalize接口用于數(shù)據(jù)標(biāo)準化
這里就直接貼代碼了

namespace MZC.Blog.Notes
{
    /// <summary>
    /// 創(chuàng)建的時候不需要太多信息,內(nèi)容更新主要依靠update
    /// 在用戶點擊創(chuàng)建的時候數(shù)據(jù)庫便創(chuàng)建數(shù)據(jù)椒功,在用戶編輯過程中自動更新保存數(shù)據(jù)。
    /// </summary>
    public class CreateNoteDto : IShouldNormalize
    {
        /// <summary>
        /// 創(chuàng)建時間
        /// </summary>
        public DateTime? CreationTime { get; set; }
        /// <summary>
        /// 創(chuàng)建人
        /// </summary>
        public long CreatorUserId { get; set; }
        /// <summary>
        /// 內(nèi)容的數(shù)據(jù)類型 markdown內(nèi)容智什,html內(nèi)容动漾,或者其他
        /// </summary>
        public int TextType { get; set; }

        public void Normalize()
        {
            if (!CreationTime.HasValue) CreationTime = DateTime.Now;
        }
    }
    /// <summary>
    /// 自動更新所傳的數(shù)據(jù)
    /// </summary>
    public class UpdateNoteDto : EntityDto<int>, IShouldNormalize
    {
        /// <summary>
        /// 標(biāo)題
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 內(nèi)容
        /// </summary>
        public string Content { get; set; }
        /// <summary>
        /// 上次修改時間
        /// </summary>
        public DateTime? LastModificationTime { get; set; }

        public virtual void Normalize()
        {
            if (!LastModificationTime.HasValue)
            {
                LastModificationTime = DateTime.Now;
            }
        }
    }
    /// <summary>
    /// 發(fā)布更新時所用
    /// </summary>
    public class PublicNoteDto : UpdateNoteDto, ICustomValidate, IShouldNormalize
    {
        /// <summary>
        /// 簡單描述,用于微信推送時的描述或者其他
        /// </summary>
        public string Des { get; set; }
        /// <summary>
        /// 封面圖片荠锭,可用于微信推送時或者其他
        /// </summary>
        [Required]
        public string Img { get; set; }
        /// <summary>
        /// 關(guān)鍵字旱眯,可用于搜索,分類等
        /// </summary>
        public string Tags { get; set; }
        /// <summary>
        /// 是否發(fā)布
        /// </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>
        /// 標(biāo)題
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 創(chuàng)建時間
        /// </summary>
        public string CreationTime { get; set; }
        /// <summary>
        /// 點贊次數(shù)
        /// </summary>
        public long Like { get; set; }
        /// <summary>
        /// 收藏次數(shù)
        /// </summary>
        public long Collect { get; set; }
        /// <summary>
        /// 瀏覽次數(shù)
        /// </summary>
        public long Scan { get; set; }
        /// <summary>
        /// 是否發(fā)布
        /// </summary>
        public string IsPublic { get; set; }
    }

    public class GetNoteListDto: PagedResultRequestDto
    {
        /// <summary>
        /// 用于搜索的關(guān)鍵字
        /// </summary>
        public string key { get; set; }
    }
}

創(chuàng)建映射

創(chuàng)建NoteMapProfile.cs文件,并添加相關(guān)映射
關(guān)于ABP框架映射的更多內(nèi)容請參考我這篇文章:http://www.reibang.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 ? "已發(fā)布" : "未發(fā)布";
        }
    }
}

使用授權(quán)

關(guān)于ABP授權(quán)詳細的介紹和使用請看我的另一篇文章:http://www.reibang.com/p/6e224f4f9705
在core項目Authorization文件夾下有模板提供的授權(quán)模塊呀页。
在PermissionNames 中定義權(quán)限妈拌,在AuthorizationProvider中添加定義的權(quán)限,然后再項目中就可以通過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>
        /// 博客管理頁面權(quán)限
        /// </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);
        }
    }

完善我們的服務(wù)和接口

因為是自己的博客系統(tǒng)蓬蝶,沒必要那么麻煩就只使用了入口權(quán)限定義在類的上面尘分。

    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);
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市丸氛,隨后出現(xiàn)的幾起案子培愁,更是在濱河造成了極大的恐慌,老刑警劉巖缓窜,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件定续,死亡現(xiàn)場離奇詭異,居然都是意外死亡禾锤,警方通過查閱死者的電腦和手機香罐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來时肿,“玉大人庇茫,你說我怎么就攤上這事◇Τ桑” “怎么了旦签?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長寸宏。 經(jīng)常有香客問我宁炫,道長,這世上最難降的妖魔是什么氮凝? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任羔巢,我火速辦了婚禮,結(jié)果婚禮上罩阵,老公的妹妹穿的比我還像新娘竿秆。我一直安慰自己,他們只是感情好稿壁,可當(dāng)我...
    茶點故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布幽钢。 她就那樣靜靜地躺著,像睡著了一般傅是。 火紅的嫁衣襯著肌膚如雪匪燕。 梳的紋絲不亂的頭發(fā)上蕾羊,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天,我揣著相機與錄音帽驯,去河邊找鬼龟再。 笑死,一個胖子當(dāng)著我的面吹牛尼变,可吹牛的內(nèi)容都是我干的利凑。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼享甸,長吁一口氣:“原來是場噩夢啊……” “哼截碴!你這毒婦竟也來了梳侨?” 一聲冷哼從身側(cè)響起蛉威,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎走哺,沒想到半個月后蚯嫌,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡丙躏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年择示,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片晒旅。...
    茶點故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡栅盲,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出废恋,到底是詐尸還是另有隱情谈秫,我是刑警寧澤,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布鱼鼓,位于F島的核電站拟烫,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏迄本。R本人自食惡果不足惜硕淑,卻給世界環(huán)境...
    茶點故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望嘉赎。 院中可真熱鬧置媳,春花似錦、人聲如沸公条。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽赃份。三九已至寂拆,卻和暖如春奢米,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背纠永。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工鬓长, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人尝江。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓涉波,卻偏偏與公主長得像,于是被迫代替她去往敵國和親炭序。 傳聞我的和親對象是個殘疾皇子啤覆,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,305評論 25 707
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)惭聂,斷路器窗声,智...
    卡卡羅2017閱讀 134,711評論 18 139
  • # Python 資源大全中文版 我想很多程序員應(yīng)該記得 GitHub 上有一個 Awesome - XXX 系列...
    aimaile閱讀 26,503評論 6 427
  • 聽了一年的《南山南》,每次點開播放辜纲,依然會被這個滄桑低沉的聲音迷住笨觅,百聽不厭。 直到現(xiàn)在我也不知道這個嗓音迷人的男...
    八寶一飯閱讀 635評論 4 2
  • 早起上班坐地鐵耕腾,每次都能在車廂里看見靠著座位打瞌睡的人见剩。這時候就想,年輕真是好啊扫俺,這種地方都可以小憩一會兒苍苞,以后老...
    獨木Atree閱讀 167評論 0 0