From dfefd0452dee2d9bc5fcd501d4c05499f9889e47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Thu, 26 Jan 2023 21:00:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84DDD=E9=A2=86=E5=9F=9F?= =?UTF-8?q?=E9=A9=B1=E5=8A=A8=E8=AE=BE=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../YiFrameworkCoreAutofacModule.cs | 27 +++++++++ .../ApplicationContractsSwaggerDoc.xml | 10 ++++ .../ApplicationSwaggerDoc.xml | 26 ++++++++- .../src/project/BBS/Yi.BBS.Web/Program.cs | 5 +- .../project/BBS/Yi.BBS.Web/YiBBSWebModule.cs | 2 + .../project/BBS/Yi.BBS.Web/yi-sqlsugar-dev.db | Bin 53248 -> 61440 bytes .../Dtos/Comment/CommentCreateInputVo.cs | 3 - .../Dtos/Comment/CommentGetListInputVo.cs | 5 -- .../Dtos/Discuss/DiscussCreateInputVo.cs | 2 + .../Forum/CommentService.cs | 54 ++++++++++++++++++ .../Forum/DiscussService.cs | 29 +++++----- .../Forum/ConstClasses/DiscussConst.cs | 1 + .../Forum/Entities/CommentEntity.cs | 10 ++++ .../bbs/Yi.BBS.Domain/Forum/ForumManager.cs | 22 ++++--- .../src/project/bbs/Yi.BBS.Web/TestAutofac.cs | 8 +++ .../project/bbs/Yi.BBS.Web/TestAutofac2.cs | 6 ++ 16 files changed, 175 insertions(+), 35 deletions(-) create mode 100644 Yi.Framework.Net6/src/framework/Yi.Framework.Core.Autofac/YiFrameworkCoreAutofacModule.cs create mode 100644 Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/TestAutofac.cs create mode 100644 Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/TestAutofac2.cs diff --git a/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Autofac/YiFrameworkCoreAutofacModule.cs b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Autofac/YiFrameworkCoreAutofacModule.cs new file mode 100644 index 00000000..19467e7b --- /dev/null +++ b/Yi.Framework.Net6/src/framework/Yi.Framework.Core.Autofac/YiFrameworkCoreAutofacModule.cs @@ -0,0 +1,27 @@ +using Autofac.Core; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Mvc.Controllers; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using StartupModules; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.Core.Autofac +{ + public class YiFrameworkCoreAutofacModule : IStartupModule + { + public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context) + { + } + + public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context) + { + //将控制器交由autofac注册 + services.Replace(ServiceDescriptor.Transient()); + } + } +} diff --git a/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Application.Contracts/ApplicationContractsSwaggerDoc.xml b/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Application.Contracts/ApplicationContractsSwaggerDoc.xml index 51871bf0..ca95e020 100644 --- a/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Application.Contracts/ApplicationContractsSwaggerDoc.xml +++ b/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Application.Contracts/ApplicationContractsSwaggerDoc.xml @@ -14,6 +14,11 @@ Banner抽象 + + + Comment输入创建对象 + + Discuss输入创建对象 @@ -24,6 +29,11 @@ Plate输入创建对象 + + + Comment服务抽象 + + Discuss服务抽象 diff --git a/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Application/ApplicationSwaggerDoc.xml b/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Application/ApplicationSwaggerDoc.xml index 5529edee..98986fbc 100644 --- a/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Application/ApplicationSwaggerDoc.xml +++ b/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Application/ApplicationSwaggerDoc.xml @@ -9,9 +9,30 @@ Banner服务实现 + + + Comment服务实现 + + + + + 获取改主题下的评论 + + + + + + + + 发表评论 + + + + + - Discuss服务实现 + Discuss应用服务实现,用于参数效验、领域服务业务组合、日志记录、事务处理、账户信息 @@ -22,11 +43,10 @@ - + 创建主题 - diff --git a/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/Program.cs b/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/Program.cs index 4f4eab28..5697f10f 100644 --- a/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/Program.cs +++ b/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/Program.cs @@ -3,6 +3,8 @@ using Yi.Framework.Core.Autofac.Extensions; using Yi.Framework.Core.Autofac.Modules; using Yi.Framework.Core.Extensions; using Yi.BBS.Web; +using Yi.BBS.Application; + var builder = WebApplication.CreateBuilder(args); builder.WebHost.UseStartUrlsServer(builder.Configuration); @@ -11,9 +13,10 @@ builder.UseYiModules(typeof(YiBBSWebModule)); builder.Host.ConfigureAutoFacContainer(container => { - container.RegisterYiModule(AutoFacModuleEnum.PropertiesAutowiredModule, typeof(YiBBSWebModule).Assembly); + container.RegisterYiModule(AutoFacModuleEnum.PropertiesAutowiredModule,typeof(YiBBSWebModule).Assembly , typeof(YiBBSApplicationModule).Assembly); }); + var app = builder.Build(); app.UseErrorHandlingServer(); diff --git a/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/YiBBSWebModule.cs b/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/YiBBSWebModule.cs index b9bf3ce0..84c7f995 100644 --- a/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/YiBBSWebModule.cs +++ b/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/YiBBSWebModule.cs @@ -6,10 +6,12 @@ using Yi.Framework.Core.Attributes; using Yi.BBS.Application; using Yi.BBS.Sqlsugar; using Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection; +using Yi.Framework.Core.Autofac; namespace Yi.BBS.Web { [DependsOn( + typeof(YiFrameworkCoreAutofacModule), typeof(YiBBSSqlsugarModule), typeof(YiBBSApplicationModule) )] diff --git a/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/yi-sqlsugar-dev.db b/Yi.Framework.Net6/src/project/BBS/Yi.BBS.Web/yi-sqlsugar-dev.db index a29b96376f03944c9eaff1dd38794dfd01781186..3f90022a93c8f2a4f7e20ca09c173af0013a7ce3 100644 GIT binary patch delta 442 zcmZozz})bFd4jZ{1Oo#DKM=!!@I)PBE(r!b_Z7TcoeX>vGo@L$CUJLd78KB8-MoqQ z28;Dh22OEFNygU3QeJj(X=%o0$CAXPoK)xh+}zZ>5-7#z9OUX4;;Inh=;Y(7pad12 z+`z6mS&UtFaygs))-oWGmj1L#OA{(47db~X-YZ8i?Eve-w=hXfdm z42+C*4GeXS%oL0atPCx!42|_nEG!I6O^gc5Q;Q1AOA5;?QWA?(62(fn-t21d~YN3VbAgs$1Z(#qIe&%(^u nz{0}9(8$=(1g3M7!hL>4sGFI2nAsSC9st|U#Ldjc2{R1 public class CommentCreateInputVo { - public long Id { get; set; } - public DateTime? CreateTime { get; set; } public string Content { get; set; } public long DiscussId { get; set; } - public long UserId { get; set; } } } diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentGetListInputVo.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentGetListInputVo.cs index 59e9a9d5..7543cb16 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentGetListInputVo.cs +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentGetListInputVo.cs @@ -9,10 +9,5 @@ namespace Yi.BBS.Application.Contracts.Forum.Dtos { public class CommentGetListInputVo : PagedAndSortedResultRequestDto { - public long Id { get; set; } - public DateTime? CreateTime { get; set; } - public string Content { get; set; } - public long DiscussId { get; set; } - public long UserId { get; set; } } } diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Discuss/DiscussCreateInputVo.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Discuss/DiscussCreateInputVo.cs index 0f3bbcc7..a8f4aedf 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Discuss/DiscussCreateInputVo.cs +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Discuss/DiscussCreateInputVo.cs @@ -17,5 +17,7 @@ namespace Yi.BBS.Application.Contracts.Forum.Dtos.Discuss public DateTime? CreateTime { get; set; } public string Content { get; set; } public string? Color { get; set; } + + public long plateId { get; set; } } } diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application/Forum/CommentService.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application/Forum/CommentService.cs index 15600324..9f12340c 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application/Forum/CommentService.cs +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application/Forum/CommentService.cs @@ -3,6 +3,13 @@ using NET.AutoWebApi.Setting; using Yi.BBS.Application.Contracts.Forum.Dtos; using Yi.BBS.Domain.Forum.Entities; using Yi.Framework.Ddd.Services; +using Microsoft.AspNetCore.Mvc; +using Yi.BBS.Domain.Forum; +using Yi.Framework.Core.CurrentUsers; +using Yi.Framework.Ddd.Repositories; +using Yi.BBS.Domain.Shared.Forum.ConstClasses; +using Yi.BBS.Application.Contracts.Forum.Dtos.Discuss; +using Yi.Framework.Ddd.Dtos; namespace Yi.BBS.Application.Forum { @@ -13,5 +20,52 @@ namespace Yi.BBS.Application.Forum public class CommentService : CrudAppService, ICommentService, IAutoApiService { + [Autowired] + private ForumManager _forumManager { get; set; } + + [Autowired] + private ICurrentUser _currentUser { get; set; } + + [Autowired] + private IRepository _discussRepository { get; set; } + + + /// + /// 获取改主题下的评论 + /// + /// + /// + /// + + public async Task> GetDiscussIdAsync([FromRoute] long discussId, [FromQuery] CommentGetListInputVo input) + { + var entities = await _repository.GetPageListAsync(x => x.DiscussId == discussId, input); + var items = await MapToGetListOutputDtosAsync(entities); + var total = await _repository.CountAsync(x => x.IsDeleted == false); + return new PagedResultDto(total, items); + } + + + /// + /// 发表评论 + /// + /// + /// + /// + public override async Task CreateAsync(CommentCreateInputVo input) + { + //if (_currentUser.Id == default(long)) + //{ + // throw new UserFriendlyException("用户不存在"); + //} + if (!await _discussRepository.IsAnyAsync(x => x.Id == input.DiscussId)) + { + throw new UserFriendlyException(DiscussConst.主题不存在); + + } + var entity = await _forumManager.CreateCommentAsync(input.DiscussId, _currentUser.Id, input.Content); + return await MapToGetOutputDtoAsync(entity); + } + } } diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application/Forum/DiscussService.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application/Forum/DiscussService.cs index 32984772..e7c2b809 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application/Forum/DiscussService.cs +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application/Forum/DiscussService.cs @@ -10,21 +10,23 @@ using Yi.BBS.Domain.Forum; using AutoMapper; using SqlSugar; using Microsoft.AspNetCore.Routing; +using Yi.BBS.Domain.Shared.Forum.ConstClasses; +using Yi.Framework.Ddd.Repositories; namespace Yi.BBS.Application.Forum { /// - /// Discuss服务实现 + /// Discuss应用服务实现,用于参数效验、领域服务业务组合、日志记录、事务处理、账户信息 /// [AppService] public class DiscussService : CrudAppService, IDiscussService, IAutoApiService { - private readonly ForumManager _forumManager; - public DiscussService(ForumManager forumManager) - { - _forumManager = forumManager; - } + [Autowired] + private ForumManager _forumManager { get; set; } + + [Autowired] + private IRepository _plateEntityRepository { get; set; } /// /// 获取改板块下的主题 @@ -41,21 +43,18 @@ namespace Yi.BBS.Application.Forum return new PagedResultDto(total, items); } - [AutoApi(false)] - public override Task CreateAsync(DiscussCreateInputVo input) - { - return base.CreateAsync(input); - } - /// /// 创建主题 /// - /// /// /// - public async Task CreatePlateIdAsync([FromRoute] long plateId, DiscussCreateInputVo input) + public override async Task CreateAsync(DiscussCreateInputVo input) { - var entity = await _forumManager.CreateDiscussAsync(plateId, input.Title, input.Types, input.Content, input.Introduction); + if (!await _plateEntityRepository.IsAnyAsync(x => x.Id == input.plateId)) + { + throw new UserFriendlyException(PlateConst.板块不存在); + } + var entity = await _forumManager.CreateDiscussAsync(input.plateId, input.Title, input.Types, input.Content, input.Introduction); return await MapToGetOutputDtoAsync(entity); } } diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain.Shared/Forum/ConstClasses/DiscussConst.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain.Shared/Forum/ConstClasses/DiscussConst.cs index 7f8531a2..ca1b491b 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain.Shared/Forum/ConstClasses/DiscussConst.cs +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain.Shared/Forum/ConstClasses/DiscussConst.cs @@ -12,5 +12,6 @@ namespace Yi.BBS.Domain.Shared.Forum.ConstClasses public class DiscussConst { + public const string 主题不存在 = "传入的主题id不存在"; } } diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain/Forum/Entities/CommentEntity.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain/Forum/Entities/CommentEntity.cs index 0f84f69d..6fbba1b0 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain/Forum/Entities/CommentEntity.cs +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain/Forum/Entities/CommentEntity.cs @@ -12,6 +12,16 @@ namespace Yi.BBS.Domain.Forum.Entities [SugarTable("Comment")] public class CommentEntity : IEntity, ISoftDelete { + public CommentEntity() + { + } + + internal CommentEntity(long discussId, long userId) + { + DiscussId= discussId; + UserId = userId; + } + [SugarColumn(IsPrimaryKey = true)] public long Id { get; set; } public bool IsDeleted { get; set; } diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain/Forum/ForumManager.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain/Forum/ForumManager.cs index 6148f6ca..107903a4 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain/Forum/ForumManager.cs +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain/Forum/ForumManager.cs @@ -17,20 +17,17 @@ namespace Yi.BBS.Domain.Forum { private readonly IRepository _discussRepository; private readonly IRepository _plateEntityRepository; - private readonly IRepository commentRepository; - public ForumManager(IRepository discussRepository, IRepository plateEntityRepository,IRepository commentRepository) + private readonly IRepository _commentRepository; + public ForumManager(IRepository discussRepository, IRepository plateEntityRepository, IRepository commentRepository) { _discussRepository = discussRepository; _plateEntityRepository = plateEntityRepository; + _commentRepository = commentRepository; } //主题是不能直接创建的,需要由领域服务统一创建 public async Task CreateDiscussAsync(long plateId, string title, string types, string content, string? introduction = null) { - if (!await _plateEntityRepository.IsAnyAsync(x => x.Id == plateId)) - { - throw new UserFriendlyException(PlateConst.板块不存在); - } var entity = new DiscussEntity(plateId); entity.Id = SnowflakeHelper.NextId; entity.Title = title; @@ -38,9 +35,18 @@ namespace Yi.BBS.Domain.Forum entity.Introduction = introduction; entity.Content = content; entity.CreateTime = DateTime.Now; - entity.AgreeNum= 0; - entity.SeeNum= 0; + entity.AgreeNum = 0; + entity.SeeNum = 0; return await _discussRepository.InsertReturnEntityAsync(entity); } + + public async Task CreateCommentAsync(long discussId, long userId, string content) + { + var entity = new CommentEntity(discussId, userId); + entity.Id = SnowflakeHelper.NextId; + entity.Content = content; + entity.CreateTime = DateTime.Now; + return await _commentRepository.InsertReturnEntityAsync(entity); + } } } diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/TestAutofac.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/TestAutofac.cs new file mode 100644 index 00000000..aa35c91b --- /dev/null +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/TestAutofac.cs @@ -0,0 +1,8 @@ +namespace Yi.BBS.Web +{ + public class TestAutofac + { + [Autowired] + public TestAutofac2 _testAutofac2 { get; set; } + } +} diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/TestAutofac2.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/TestAutofac2.cs new file mode 100644 index 00000000..3f431298 --- /dev/null +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Web/TestAutofac2.cs @@ -0,0 +1,6 @@ +namespace Yi.BBS.Web +{ + public class TestAutofac2 + { + } +}