2023-12-11 09:55:12 +08:00
|
|
|
|
using System.Collections.Generic;
|
2023-12-30 16:10:30 +08:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2023-12-29 18:19:35 +08:00
|
|
|
|
using System.Text;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Mapster;
|
2023-12-24 11:45:43 +08:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2023-12-11 22:14:13 +08:00
|
|
|
|
using SqlSugar;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Volo.Abp;
|
2023-12-11 22:14:13 +08:00
|
|
|
|
using Volo.Abp.Application.Dtos;
|
|
|
|
|
|
using Volo.Abp.Domain.Repositories;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Yi.Framework.Bbs.Application.Contracts.Dtos.Article;
|
2023-12-11 22:14:13 +08:00
|
|
|
|
using Yi.Framework.Bbs.Application.Contracts.Dtos.Plate;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Yi.Framework.Bbs.Application.Contracts.IServices;
|
2024-01-11 18:51:53 +08:00
|
|
|
|
using Yi.Framework.Bbs.Domain.Entities.Forum;
|
2023-12-29 18:19:35 +08:00
|
|
|
|
using Yi.Framework.Bbs.Domain.Managers;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Yi.Framework.Bbs.Domain.Repositories;
|
|
|
|
|
|
using Yi.Framework.Bbs.Domain.Shared.Consts;
|
2023-12-29 18:19:35 +08:00
|
|
|
|
using Yi.Framework.Bbs.Domain.Shared.Model;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Yi.Framework.Core.Extensions;
|
|
|
|
|
|
using Yi.Framework.Ddd.Application;
|
2023-12-24 11:45:43 +08:00
|
|
|
|
using Yi.Framework.Rbac.Domain.Authorization;
|
2024-01-18 16:28:09 +08:00
|
|
|
|
using Yi.Framework.Rbac.Domain.Extensions;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Consts;
|
|
|
|
|
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
2023-04-15 22:44:33 +08:00
|
|
|
|
|
2024-01-11 18:51:53 +08:00
|
|
|
|
namespace Yi.Framework.Bbs.Application.Services.Forum
|
2023-04-15 22:44:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Article服务实现
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
2024-05-22 14:35:08 +08:00
|
|
|
|
public class ArticleService : YiCrudAppService<ArticleAggregateRoot, ArticleGetOutputDto, ArticleGetListOutputDto, Guid, ArticleGetListInputVo, ArticleCreateInputVo, ArticleUpdateInputVo>,
|
2023-12-11 09:55:12 +08:00
|
|
|
|
IArticleService
|
2023-04-15 22:44:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
public ArticleService(IArticleRepository articleRepository,
|
2024-05-22 14:35:08 +08:00
|
|
|
|
ISqlSugarRepository<DiscussAggregateRoot> discussRepository,
|
2023-12-29 18:19:35 +08:00
|
|
|
|
IDiscussService discussService,
|
|
|
|
|
|
ForumManager forumManager) : base(articleRepository)
|
2023-04-15 22:44:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
_articleRepository = articleRepository;
|
|
|
|
|
|
_discussRepository = discussRepository;
|
|
|
|
|
|
_discussService = discussService;
|
2023-12-29 18:19:35 +08:00
|
|
|
|
_forumManager = forumManager;
|
2023-04-15 22:44:33 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
2023-12-29 18:19:35 +08:00
|
|
|
|
private ForumManager _forumManager;
|
|
|
|
|
|
private IArticleRepository _articleRepository;
|
2024-05-22 14:35:08 +08:00
|
|
|
|
private ISqlSugarRepository<DiscussAggregateRoot> _discussRepository;
|
2023-12-29 18:19:35 +08:00
|
|
|
|
private IDiscussService _discussService;
|
2023-12-11 22:14:13 +08:00
|
|
|
|
|
|
|
|
|
|
public override async Task<PagedResultDto<ArticleGetListOutputDto>> GetListAsync(ArticleGetListInputVo input)
|
|
|
|
|
|
{
|
|
|
|
|
|
RefAsync<int> total = 0;
|
|
|
|
|
|
|
|
|
|
|
|
var entities = await _articleRepository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.Name), x => x.Name.Contains(input.Name!))
|
2023-12-19 12:40:49 +08:00
|
|
|
|
//.WhereIF(!string.IsNullOrEmpty(input.Code), x => x.Name.Contains(input.Code!))
|
2023-12-11 22:14:13 +08:00
|
|
|
|
.WhereIF(input.StartTime is not null && input.EndTime is not null, x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime)
|
|
|
|
|
|
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
|
|
|
|
|
|
return new PagedResultDto<ArticleGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-04-15 22:44:33 +08:00
|
|
|
|
/// <summary>
|
2023-12-14 22:57:01 +08:00
|
|
|
|
/// 获取文章全部树级信息
|
2023-04-15 22:44:33 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="discussId"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="UserFriendlyException"></exception>
|
2023-12-11 09:55:12 +08:00
|
|
|
|
[Route("article/all/discuss-id/{discussId}")]
|
|
|
|
|
|
public async Task<List<ArticleAllOutputDto>> GetAllAsync([FromRoute] Guid discussId)
|
2023-04-15 22:44:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
await _discussService.VerifyDiscussPermissionAsync(discussId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var entities = await _articleRepository.GetTreeAsync(x => x.DiscussId == discussId);
|
2023-12-11 09:55:12 +08:00
|
|
|
|
var items = entities.Adapt<List<ArticleAllOutputDto>>();
|
2023-04-15 22:44:33 +08:00
|
|
|
|
return items;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 查询文章
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="discussId"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="UserFriendlyException"></exception>
|
2023-12-11 09:55:12 +08:00
|
|
|
|
public async Task<List<ArticleGetListOutputDto>> GetDiscussIdAsync([FromRoute] Guid discussId)
|
2023-04-15 22:44:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (!await _discussRepository.IsAnyAsync(x => x.Id == discussId))
|
|
|
|
|
|
{
|
2023-10-04 23:16:07 +08:00
|
|
|
|
throw new UserFriendlyException(DiscussConst.No_Exist);
|
2023-04-15 22:44:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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>
|
2023-12-24 11:45:43 +08:00
|
|
|
|
[Permission("bbs:article:add")]
|
|
|
|
|
|
[Authorize]
|
2023-04-15 22:44:33 +08:00
|
|
|
|
public async override Task<ArticleGetOutputDto> CreateAsync(ArticleCreateInputVo input)
|
|
|
|
|
|
{
|
2023-12-19 13:00:14 +08:00
|
|
|
|
await VerifyDiscussCreateIdAsync(input.DiscussId);
|
2023-04-15 22:44:33 +08:00
|
|
|
|
return await base.CreateAsync(input);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-19 13:00:14 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新文章
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public override async Task<ArticleGetOutputDto> UpdateAsync(Guid id, ArticleUpdateInputVo input)
|
|
|
|
|
|
{
|
|
|
|
|
|
var entity = await _articleRepository.GetByIdAsync(id);
|
|
|
|
|
|
await VerifyDiscussCreateIdAsync(entity.DiscussId);
|
|
|
|
|
|
return await base.UpdateAsync(id, input);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 删除文章
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public override async Task DeleteAsync(Guid id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var entity = await _articleRepository.GetByIdAsync(id);
|
|
|
|
|
|
await VerifyDiscussCreateIdAsync(entity.DiscussId);
|
|
|
|
|
|
await base.DeleteAsync(id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-12-29 18:19:35 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 导入文章
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2023-12-30 16:10:30 +08:00
|
|
|
|
public async Task PostImportAsync([FromQuery] ArticleImprotDto input, [FromForm][Required] IFormFileCollection file)
|
2023-12-29 18:19:35 +08:00
|
|
|
|
{
|
|
|
|
|
|
var fileObjs = new List<FileObject>();
|
|
|
|
|
|
if (file.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var item in file)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (item.Length > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var stream = item.OpenReadStream())
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var fileStream = new MemoryStream())
|
|
|
|
|
|
{
|
|
|
|
|
|
await item.CopyToAsync(fileStream);
|
|
|
|
|
|
var bytes = fileStream.ToArray();
|
|
|
|
|
|
|
|
|
|
|
|
// 将字节转换成字符串
|
|
|
|
|
|
var content = Encoding.UTF8.GetString(bytes);
|
2023-12-30 02:42:12 +08:00
|
|
|
|
fileObjs.Add(new FileObject() { FileName = item.FileName, Content = content });
|
2023-12-29 18:19:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-12-30 02:42:12 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("未选择文件");
|
|
|
|
|
|
}
|
2023-12-29 18:19:35 +08:00
|
|
|
|
//使用简单工厂根据传入的类型进行判断
|
2023-12-30 16:10:30 +08:00
|
|
|
|
await _forumManager.PostImportAsync(input.DiscussId, input.ArticleParentId, fileObjs, input.ImportType);
|
2023-12-29 18:19:35 +08:00
|
|
|
|
}
|
2023-12-19 13:00:14 +08:00
|
|
|
|
|
2023-04-15 22:44:33 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-03-07 11:32:49 +08:00
|
|
|
|
/// 校验创建权限,userId为主题创建者
|
2023-04-15 22:44:33 +08:00
|
|
|
|
/// </summary>
|
2023-12-19 13:00:14 +08:00
|
|
|
|
/// <param name="disucssId"></param>
|
2023-04-15 22:44:33 +08:00
|
|
|
|
/// <returns></returns>
|
2023-12-19 13:00:14 +08:00
|
|
|
|
private async Task VerifyDiscussCreateIdAsync(Guid disucssId)
|
2023-04-15 22:44:33 +08:00
|
|
|
|
{
|
2023-12-19 13:00:14 +08:00
|
|
|
|
var discuss = await _discussRepository.GetFirstAsync(x => x.Id == disucssId);
|
|
|
|
|
|
if (discuss is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException(DiscussConst.No_Exist);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-24 11:45:43 +08:00
|
|
|
|
//这块有点绕,这个版本的写法比较清晰
|
|
|
|
|
|
bool result = false;
|
2023-12-19 13:00:14 +08:00
|
|
|
|
|
2024-01-11 18:51:53 +08:00
|
|
|
|
if (CurrentUser.GetPermissions().Contains(UserConst.AdminPermissionCode))
|
2023-12-24 11:45:43 +08:00
|
|
|
|
{
|
|
|
|
|
|
//如果是超管,直接跳过
|
|
|
|
|
|
result = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
//如果不是超管,必须满足作者是自己,同时还有发布的权限
|
|
|
|
|
|
if (discuss.CreatorId == CurrentUser.Id)
|
|
|
|
|
|
{
|
|
|
|
|
|
result = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!result)
|
2023-04-15 22:44:33 +08:00
|
|
|
|
{
|
2023-12-19 13:00:14 +08:00
|
|
|
|
throw new UserFriendlyException("权限不足,请联系主题作者或管理员申请开通");
|
2023-04-15 22:44:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|