From a4570b7b2f9ca4ad40c1b0bead32129d7b9e9d8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Sat, 30 Dec 2023 02:42:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=AE=8C=E6=88=90=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/ArticleService.cs | 6 +- .../ArticleImport/AbstractArticleImport.cs | 24 ++++++++ .../ArticleImport/DefaultArticleImport.cs | 18 ++++++ .../ArticleImport/VuePressArticleImport.cs | 60 +++++++++++++++++++ .../Managers/ForumManager.cs | 23 ++++++- 5 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/AbstractArticleImport.cs create mode 100644 Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/DefaultArticleImport.cs create mode 100644 Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/VuePressArticleImport.cs diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application/Services/ArticleService.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application/Services/ArticleService.cs index 5c9c245b..5cbb0787 100644 --- a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application/Services/ArticleService.cs +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application/Services/ArticleService.cs @@ -161,12 +161,16 @@ namespace Yi.Framework.Bbs.Application.Services // 将字节转换成字符串 var content = Encoding.UTF8.GetString(bytes); - fileObjs.Add(new FileObject() { FileName=item.FileName,Content=content}); + fileObjs.Add(new FileObject() { FileName = item.FileName, Content = content }); } } } } } + else + { + throw new UserFriendlyException("未选择文件"); + } //使用简单工厂根据传入的类型进行判断 await _forumManager.PostImportAsync(input.DiscussId, fileObjs, input.ImportType); } diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/AbstractArticleImport.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/AbstractArticleImport.cs new file mode 100644 index 00000000..3a240256 --- /dev/null +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/AbstractArticleImport.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Bbs.Domain.Entities; +using Yi.Framework.Bbs.Domain.Shared.Model; + +namespace Yi.Framework.Bbs.Domain.Managers.ArticleImport +{ + public abstract class AbstractArticleImport + { + public virtual List Import(Guid discussId, List fileObjs) + { + var articles = Convert(fileObjs); + articles.ForEach(article => + { + article.DiscussId = discussId; + }); + return articles; + } + public abstract List Convert(List fileObjs); + } +} diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/DefaultArticleImport.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/DefaultArticleImport.cs new file mode 100644 index 00000000..8ca58332 --- /dev/null +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/DefaultArticleImport.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Bbs.Domain.Entities; +using Yi.Framework.Bbs.Domain.Shared.Model; + +namespace Yi.Framework.Bbs.Domain.Managers.ArticleImport +{ + internal class DefaultArticleImport : AbstractArticleImport + { + public override List Convert(List fileObjs) + { + return fileObjs.OrderBy(x => x.FileName).Select(x => new ArticleEntity { Name = x.FileName, Content = x.Content }).ToList(); + } + } +} diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/VuePressArticleImport.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/VuePressArticleImport.cs new file mode 100644 index 00000000..c0f8867d --- /dev/null +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ArticleImport/VuePressArticleImport.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Bbs.Domain.Entities; +using Yi.Framework.Bbs.Domain.Shared.Model; + +namespace Yi.Framework.Bbs.Domain.Managers.ArticleImport +{ + internal class VuePressArticleImport : AbstractArticleImport + { + public override List Convert(List fileObjs) + { + //排序及处理目录名称 + var fileNameHandler = fileObjs.OrderBy(x => x.FileName).Select(x => + { + var f = new FileObject { Content = x.Content }; + + //除去数字 + f.FileName = x.FileName.Split('.')[1]; + return f; + }); + + + //处理内容 + var fileContentHandler= fileNameHandler.Select(x => + { + var f = new FileObject { FileName = x.FileName }; + var lines = x.Content.SplitToLines(); + + var num = 0; + var startIndex = 0; + for (int i = 0; i < lines.Length; i++) + { + if (lines[i] == "---") + { + num++; + if (num == 2) + { + startIndex = i; + + break; + } + + } + + } + lines.ToList().RemoveRange(0, num); + var result = string.Join(Environment.NewLine, lines); + f.Content = result; + return f; + }); + + var output = fileContentHandler.Select(x => new ArticleEntity() { Content = x.Content, Name = x.FileName }).ToList(); + + return output; + } + } +} diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ForumManager.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ForumManager.cs index 43e82011..5e822fea 100644 --- a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ForumManager.cs +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/ForumManager.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc; using Volo.Abp.Domain.Services; using Yi.Framework.Bbs.Domain.Entities; +using Yi.Framework.Bbs.Domain.Managers.ArticleImport; using Yi.Framework.Bbs.Domain.Shared.Enums; using Yi.Framework.Bbs.Domain.Shared.Model; using Yi.Framework.SqlSugarCore.Abstractions; @@ -16,11 +17,13 @@ namespace Yi.Framework.Bbs.Domain.Managers public readonly ISqlSugarRepository _discussRepository; public readonly ISqlSugarRepository _plateEntityRepository; public readonly ISqlSugarRepository _commentRepository; - public ForumManager(ISqlSugarRepository discussRepository, ISqlSugarRepository plateEntityRepository, ISqlSugarRepository commentRepository) + public readonly ISqlSugarRepository _articleRepository; + public ForumManager(ISqlSugarRepository discussRepository, ISqlSugarRepository plateEntityRepository, ISqlSugarRepository commentRepository, ISqlSugarRepository articleRepository) { _discussRepository = discussRepository; _plateEntityRepository = plateEntityRepository; _commentRepository = commentRepository; + _articleRepository = articleRepository; } //主题是不能直接创建的,需要由领域服务统一创建 @@ -50,6 +53,24 @@ namespace Yi.Framework.Bbs.Domain.Managers /// public async Task PostImportAsync(Guid discussId, List fileObjs, ArticleImportTypeEnum importType) { + AbstractArticleImport abstractArticleImport = default; + switch (importType) + { + case ArticleImportTypeEnum.Defalut: + abstractArticleImport = new DefaultArticleImport(); + + break; + case ArticleImportTypeEnum.VuePress: + abstractArticleImport = new VuePressArticleImport(); + break; + + default: abstractArticleImport = new DefaultArticleImport(); break; + } + + var articleHandled = abstractArticleImport.Import(discussId, fileObjs); + + await _articleRepository.InsertManyAsync(articleHandled); + } } }