feat:框架基础设施搭建
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
using Yi.BBS.Application.Contracts.Forum;
|
||||
using Cike.AutoWebApi.Setting;
|
||||
using Yi.BBS.Application.Contracts.Forum.Dtos;
|
||||
using Yi.BBS.Domain.Forum.Entities;
|
||||
using Yi.Framework.Ddd.Services;
|
||||
using Yi.Framework.Ddd.Dtos;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Yi.BBS.Domain.Forum.Repositories;
|
||||
using Yi.Framework.Ddd.Repositories;
|
||||
using Yi.BBS.Domain.Shared.Forum.ConstClasses;
|
||||
using Yi.BBS.Domain.Shared.Forum.EnumClasses;
|
||||
using Yi.Framework.Core.CurrentUsers;
|
||||
using Yi.RBAC.Domain.Shared.Identity.ConstClasses;
|
||||
|
||||
namespace Yi.BBS.Application.Forum
|
||||
{
|
||||
/// <summary>
|
||||
/// Article服务实现
|
||||
/// </summary>
|
||||
[AppService]
|
||||
|
||||
public class ArticleService : CrudAppService<ArticleEntity, ArticleGetOutputDto, ArticleGetListOutputDto, long, ArticleGetListInputVo, ArticleCreateInputVo, ArticleUpdateInputVo>,
|
||||
IArticleService, IAutoApiService
|
||||
{
|
||||
[Autowired]
|
||||
private IArticleRepository _articleRepository { get; set; }
|
||||
|
||||
|
||||
[Autowired]
|
||||
private IRepository<DiscussEntity> _discussRepository { get; set; }
|
||||
|
||||
[Autowired]
|
||||
private ICurrentUser _currentUser { get; set; }
|
||||
|
||||
[Autowired]
|
||||
private IDiscussService _discussService { get; set; }
|
||||
/// <summary>
|
||||
/// 获取文章全部平铺信息
|
||||
/// </summary>
|
||||
/// <param name="discussId"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="UserFriendlyException"></exception>
|
||||
[Route("/api/article/all/discuss-id/{discussId}")]
|
||||
public async Task<List<ArticleAllOutputDto>> GetAllAsync([FromRoute] long discussId)
|
||||
{
|
||||
await _discussService.VerifyDiscussPermissionAsync(discussId);
|
||||
|
||||
|
||||
var entities = await _articleRepository.GetTreeAsync(x => x.DiscussId == discussId);
|
||||
//var result = entities.Tile();
|
||||
var items = _mapper.Map<List<ArticleAllOutputDto>>(entities);
|
||||
return items;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询文章
|
||||
/// </summary>
|
||||
/// <param name="discussId"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="UserFriendlyException"></exception>
|
||||
public async Task<List<ArticleGetListOutputDto>> GetDiscussIdAsync([FromRoute] long discussId)
|
||||
{
|
||||
if (!await _discussRepository.IsAnyAsync(x => x.Id == discussId))
|
||||
{
|
||||
throw new UserFriendlyException(DiscussConst.主题不存在);
|
||||
}
|
||||
|
||||
var entities = await _articleRepository.GetTreeAsync(x => x.DiscussId == discussId);
|
||||
var items = await MapToGetListOutputDtosAsync(entities);
|
||||
return items;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发表文章
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="UserFriendlyException"></exception>
|
||||
public async override Task<ArticleGetOutputDto> CreateAsync(ArticleCreateInputVo input)
|
||||
{
|
||||
var discuss = await _discussRepository.GetFirstAsync(x => x.Id == input.DiscussId);
|
||||
if (discuss is null)
|
||||
{
|
||||
throw new UserFriendlyException(DiscussConst.主题不存在);
|
||||
}
|
||||
if (input.ParentId != 0 && !await _repository.IsAnyAsync(x => x.Id == input.ParentId))
|
||||
{
|
||||
throw new UserFriendlyException(ArticleConst.文章不存在);
|
||||
}
|
||||
await VerifyDiscussCreateIdAsync(discuss.CreatorId);
|
||||
return await base.CreateAsync(input);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 效验创建权限
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task VerifyDiscussCreateIdAsync(long? userId)
|
||||
{
|
||||
//只有文章是特殊的,不能在其他主题下创建
|
||||
//主题的创建者不是当前用户,同时,没有权限或者超级管理
|
||||
|
||||
//false & true & false ,三个条件任意满意一个,即可成功使用||,最后取反,一个都不满足
|
||||
//
|
||||
if (userId != _currentUser.Id && !UserConst.Admin.Equals( _currentUser.UserName)&& !_currentUser.Permission.Contains("bbs:discuss:add"))
|
||||
{
|
||||
throw new UserFriendlyException("无权限在其他用户主题中创建子文章");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
using Yi.BBS.Application.Contracts.Forum;
|
||||
using Cike.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;
|
||||
using SqlSugar;
|
||||
using System.Security.AccessControl;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Yi.BBS.Application.Forum
|
||||
{
|
||||
/// <summary>
|
||||
/// 评论
|
||||
/// </summary>
|
||||
[AppService]
|
||||
public class CommentService : CrudAppService<CommentEntity, CommentGetOutputDto, CommentGetListOutputDto, long, CommentGetListInputVo, CommentCreateInputVo, CommentUpdateInputVo>,
|
||||
ICommentService, IAutoApiService
|
||||
{
|
||||
[Autowired]
|
||||
private ForumManager _forumManager { get; set; }
|
||||
|
||||
[Autowired]
|
||||
private ICurrentUser _currentUser { get; set; }
|
||||
|
||||
[Autowired]
|
||||
private IRepository<DiscussEntity> _discussRepository { get; set; }
|
||||
|
||||
[Autowired]
|
||||
private IDiscussService _discussService { get; set; }
|
||||
/// <summary>
|
||||
/// 获取改主题下的评论,结构为二维列表,该查询无分页
|
||||
/// </summary>
|
||||
/// <param name="discussId"></param>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<PagedResultDto<CommentGetListOutputDto>> GetDiscussIdAsync([FromRoute] long discussId, [FromQuery] CommentGetListInputVo input)
|
||||
{
|
||||
await _discussService.VerifyDiscussPermissionAsync(discussId);
|
||||
|
||||
var entities = await _DbQueryable.WhereIF(!string.IsNullOrEmpty(input.Content), x => x.Content.Contains(input.Content))
|
||||
.Where(x => x.DiscussId == discussId)
|
||||
.Includes(x=>x.CreateUser)
|
||||
.ToListAsync();
|
||||
|
||||
//结果初始值,第一层等于全部根节点
|
||||
var outPut = entities.Where(x => x.ParentId == 0).OrderByDescending(x=>x.CreationTime).ToList();
|
||||
|
||||
//将全部数据进行hash
|
||||
var dic = entities.ToDictionary(x => x.Id);
|
||||
|
||||
|
||||
foreach (var comment in entities)
|
||||
{
|
||||
//不是根节点,需要赋值 被评论者用户信息等
|
||||
if (comment.ParentId != 0)
|
||||
{
|
||||
var parentComment = dic[comment.ParentId];
|
||||
comment.CommentedUser = parentComment.CreateUser;
|
||||
}
|
||||
|
||||
//root或者parent id,根节点都是等于0的
|
||||
var id = comment.RootId;
|
||||
if (id is not 0)
|
||||
{
|
||||
dic[id].Children.Add(comment);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//子类需要排序
|
||||
outPut.ForEach(x =>
|
||||
{
|
||||
x.Children = x.Children.OrderByDescending(x => x.CreationTime).ToList();
|
||||
|
||||
});
|
||||
|
||||
//获取全量主题评论, 先获取顶级的,将其他子组合到顶级下,形成一个二维,先转成dto
|
||||
List<CommentGetListOutputDto>? items = await MapToGetListOutputDtosAsync(outPut);
|
||||
return new PagedResultDto<CommentGetListOutputDto>(entities.Count(), items);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 发表评论
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="UserFriendlyException"></exception>
|
||||
public override async Task<CommentGetOutputDto> CreateAsync(CommentCreateInputVo input)
|
||||
{
|
||||
if (!await _discussRepository.IsAnyAsync(x => x.Id == input.DiscussId))
|
||||
{
|
||||
throw new UserFriendlyException(DiscussConst.主题不存在);
|
||||
}
|
||||
var entity = await _forumManager.CreateCommentAsync(input.DiscussId, input.ParentId,input.RootId, input.Content);
|
||||
return await MapToGetOutputDtoAsync(entity);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
using Yi.BBS.Application.Contracts.Forum;
|
||||
using Cike.AutoWebApi.Setting;
|
||||
using Yi.BBS.Application.Contracts.Forum.Dtos;
|
||||
using Yi.BBS.Domain.Forum.Entities;
|
||||
using Yi.Framework.Ddd.Services;
|
||||
using Yi.BBS.Application.Contracts.Forum.Dtos.Discuss;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Yi.Framework.Ddd.Dtos;
|
||||
using Yi.BBS.Domain.Forum;
|
||||
using MapsterMapper;
|
||||
using SqlSugar;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Yi.BBS.Domain.Shared.Forum.ConstClasses;
|
||||
using Yi.Framework.Ddd.Repositories;
|
||||
using Yi.RBAC.Domain.Identity.Entities;
|
||||
using Yi.RBAC.Application.Contracts.Identity.Dtos;
|
||||
using Cike.EventBus.DistributedEvent;
|
||||
using Yi.BBS.Domain.Shared.Forum.Etos;
|
||||
using Yi.BBS.Domain.Shared.Forum.EnumClasses;
|
||||
using Yi.Framework.Core.CurrentUsers;
|
||||
using Yi.BBS.Domain.Exhibition.Entities;
|
||||
|
||||
namespace Yi.BBS.Application.Forum
|
||||
{
|
||||
/// <summary>
|
||||
/// Discuss应用服务实现,用于参数效验、领域服务业务组合、日志记录、事务处理、账户信息
|
||||
/// </summary>
|
||||
[AppService]
|
||||
public class DiscussService : CrudAppService<DiscussEntity, DiscussGetOutputDto, DiscussGetListOutputDto, long, DiscussGetListInputVo, DiscussCreateInputVo, DiscussUpdateInputVo>,
|
||||
IDiscussService, IAutoApiService
|
||||
{
|
||||
public DiscussService(ICurrentUser currentUser)
|
||||
{
|
||||
_currentUser = currentUser;
|
||||
}
|
||||
[Autowired]
|
||||
private ForumManager _forumManager { get; set; }
|
||||
|
||||
[Autowired]
|
||||
private IRepository<PlateEntity> _plateEntityRepository { get; set; }
|
||||
|
||||
[Autowired]
|
||||
private IDistributedEventBus _distributedEventBus { get; set; }
|
||||
|
||||
//[Autowired]
|
||||
private ICurrentUser _currentUser { get; set; }
|
||||
/// <summary>
|
||||
/// 单查
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public async override Task<DiscussGetOutputDto> GetAsync(long id)
|
||||
{
|
||||
|
||||
//查询主题发布 浏览主题 事件,浏览数+1
|
||||
var item = await _DbQueryable.LeftJoin<UserEntity>((discuss, user) => discuss.CreatorId == user.Id)
|
||||
.Select((discuss, user) => new DiscussGetOutputDto
|
||||
{
|
||||
User = new UserGetListOutputDto() { UserName = user.UserName, Nick = user.Nick, Icon = user.Icon }
|
||||
}, true).SingleAsync(discuss => discuss.Id == id);
|
||||
|
||||
await VerifyDiscussPermissionAsync(item.Id);
|
||||
|
||||
if (item is not null)
|
||||
{
|
||||
_distributedEventBus.PublishAsync(new SeeDiscussEventArgs { DiscussId = item.Id, OldSeeNum = item.SeeNum });
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 查询
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
public override async Task<PagedResultDto<DiscussGetListOutputDto>> GetListAsync([FromQuery] DiscussGetListInputVo input)
|
||||
{
|
||||
//需要关联创建者用户
|
||||
RefAsync<int> total = 0;
|
||||
var items = await _DbQueryable
|
||||
.WhereIF(!string.IsNullOrEmpty(input.Title), x => x.Title.Contains(input.Title))
|
||||
.WhereIF(input.PlateId is not null, x => x.PlateId == input.PlateId)
|
||||
.Where(x => x.IsTop == input.IsTop)
|
||||
|
||||
.LeftJoin<UserEntity>((discuss, user) => discuss.CreatorId == user.Id)
|
||||
.OrderByIF(input.Type == QueryDiscussTypeEnum.New, discuss => discuss.CreationTime, OrderByType.Desc)
|
||||
.OrderByIF(input.Type == QueryDiscussTypeEnum.Host, discuss => discuss.SeeNum, OrderByType.Desc)
|
||||
.OrderByIF(input.Type == QueryDiscussTypeEnum.Suggest, discuss => discuss.AgreeNum, OrderByType.Desc)
|
||||
.Select((discuss, user) => new DiscussGetListOutputDto
|
||||
{
|
||||
Id=discuss.Id,
|
||||
IsAgree = SqlFunc.Subqueryable<AgreeEntity>().Where(x => x.CreatorId == _currentUser.Id && x.DiscussId == discuss.Id).Any(),
|
||||
|
||||
User = new UserGetListOutputDto() { Id=user.Id, UserName = user.UserName, Nick = user.Nick, Icon = user.Icon }
|
||||
|
||||
}, true)
|
||||
.ToPageListAsync(input.PageNum, input.PageSize, total);
|
||||
|
||||
//查询完主题之后,要过滤一下私有的主题信息
|
||||
items.ApplyPermissionTypeFilter(_currentUser.Id);
|
||||
return new PagedResultDto<DiscussGetListOutputDto>(total, items);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建主题
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public override async Task<DiscussGetOutputDto> CreateAsync(DiscussCreateInputVo input)
|
||||
{
|
||||
if (!await _plateEntityRepository.IsAnyAsync(x => x.Id == input.PlateId))
|
||||
{
|
||||
throw new UserFriendlyException(PlateConst.板块不存在);
|
||||
}
|
||||
var entity = await _forumManager.CreateDiscussAsync(await MapToEntityAsync(input));
|
||||
return await MapToGetOutputDtoAsync(entity);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 效验主题查询权限
|
||||
/// </summary>
|
||||
/// <param name="discussId"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="UserFriendlyException"></exception>
|
||||
public async Task VerifyDiscussPermissionAsync(long discussId)
|
||||
{
|
||||
var discuss = await _repository.GetFirstAsync(x => x.Id == discussId);
|
||||
if (discuss is null)
|
||||
{
|
||||
throw new UserFriendlyException(DiscussConst.主题不存在);
|
||||
}
|
||||
if (discuss.PermissionType == DiscussPermissionTypeEnum.Oneself)
|
||||
{
|
||||
if (discuss.CreatorId != _currentUser.Id)
|
||||
{
|
||||
throw new UserFriendlyException(DiscussConst.私密);
|
||||
}
|
||||
}
|
||||
if (discuss.PermissionType == DiscussPermissionTypeEnum.User)
|
||||
{
|
||||
if (discuss.CreatorId != _currentUser.Id && !discuss.PermissionUserIds.Contains(_currentUser.Id))
|
||||
{
|
||||
throw new UserFriendlyException(DiscussConst.私密);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using Yi.BBS.Application.Contracts.Forum;
|
||||
using Cike.AutoWebApi.Setting;
|
||||
using Yi.BBS.Application.Contracts.Forum.Dtos;
|
||||
using Yi.BBS.Domain.Forum.Entities;
|
||||
using Yi.Framework.Ddd.Services;
|
||||
using Yi.Framework.Core.CurrentUsers;
|
||||
using SqlSugar;
|
||||
using Yi.Framework.Ddd.Dtos;
|
||||
using Yi.Framework.Data.Filters;
|
||||
|
||||
namespace Yi.BBS.Application.Forum
|
||||
{
|
||||
/// <summary>
|
||||
/// Label服务实现
|
||||
/// </summary>
|
||||
[AppService]
|
||||
public class MyTypeService : CrudAppService<MyTypeEntity, MyTypeOutputDto, MyTypeGetListOutputDto, long, MyTypeGetListInputVo, MyTypeCreateInputVo, MyTypeUpdateInputVo>,
|
||||
ILabelService, IAutoApiService
|
||||
{
|
||||
[Autowired]
|
||||
private ICurrentUser _currentUser { get; set; }
|
||||
|
||||
[Autowired]
|
||||
private IDataFilter _dataFilter { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前用户的主题类型
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public Task<PagedResultDto<MyTypeGetListOutputDto>> GetListCurrentAsync(MyTypeGetListInputVo input)
|
||||
{
|
||||
|
||||
_dataFilter.AddFilter<MyTypeEntity>(x => x.UserId == _currentUser.Id);
|
||||
return base.GetListAsync(input);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public override async Task<MyTypeOutputDto> CreateAsync(MyTypeCreateInputVo input)
|
||||
{
|
||||
var entity = await MapToEntityAsync(input);
|
||||
entity.Id = SnowflakeHelper.NextId;
|
||||
entity.UserId = _currentUser.Id;
|
||||
entity.IsDeleted = false;
|
||||
var outputEntity = await _repository.InsertReturnEntityAsync(entity);
|
||||
return await MapToGetOutputDtoAsync(outputEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using Yi.BBS.Application.Contracts.Forum;
|
||||
using Cike.AutoWebApi.Setting;
|
||||
using Yi.BBS.Application.Contracts.Forum.Dtos;
|
||||
using Yi.BBS.Domain.Forum.Entities;
|
||||
using Yi.Framework.Ddd.Services;
|
||||
using Yi.BBS.Application.Contracts.Forum.Dtos.Plate;
|
||||
|
||||
namespace Yi.BBS.Application.Forum
|
||||
{
|
||||
/// <summary>
|
||||
/// Plate服务实现
|
||||
/// </summary>
|
||||
[AppService]
|
||||
public class PlateService : CrudAppService<PlateEntity, PlateGetOutputDto, PlateGetListOutputDto, long, PlateGetListInputVo, PlateCreateInputVo, PlateUpdateInputVo>,
|
||||
IPlateService, IAutoApiService
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user