2023-12-11 09:55:12 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
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;
|
|
|
|
|
|
using Yi.Framework.Bbs.Domain.Entities;
|
2023-12-19 12:40:49 +08:00
|
|
|
|
using Yi.Framework.Bbs.Domain.Extensions;
|
2023-12-11 09:55:12 +08:00
|
|
|
|
using Yi.Framework.Bbs.Domain.Repositories;
|
|
|
|
|
|
using Yi.Framework.Bbs.Domain.Shared.Consts;
|
|
|
|
|
|
using Yi.Framework.Core.Extensions;
|
|
|
|
|
|
using Yi.Framework.Ddd.Application;
|
2023-12-24 11:45:43 +08:00
|
|
|
|
using Yi.Framework.Rbac.Domain.Authorization;
|
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
|
|
|
|
|
2023-12-11 09:55:12 +08:00
|
|
|
|
namespace Yi.Framework.Bbs.Application.Services
|
2023-04-15 22:44:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Article服务实现
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
2023-12-11 09:55:12 +08:00
|
|
|
|
public class ArticleService : YiCrudAppService<ArticleEntity, ArticleGetOutputDto, ArticleGetListOutputDto, Guid, ArticleGetListInputVo, ArticleCreateInputVo, ArticleUpdateInputVo>,
|
|
|
|
|
|
IArticleService
|
2023-04-15 22:44:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
public ArticleService(IArticleRepository articleRepository,
|
2023-12-11 09:55:12 +08:00
|
|
|
|
ISqlSugarRepository<DiscussEntity> discussRepository,
|
|
|
|
|
|
IDiscussService discussService) : base(articleRepository)
|
2023-04-15 22:44:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
_articleRepository = articleRepository;
|
|
|
|
|
|
_discussRepository = discussRepository;
|
|
|
|
|
|
_discussService = discussService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
private IArticleRepository _articleRepository { get; set; }
|
2023-12-11 09:55:12 +08:00
|
|
|
|
private ISqlSugarRepository<DiscussEntity> _discussRepository { get; set; }
|
2023-04-15 22:44:33 +08:00
|
|
|
|
private IDiscussService _discussService { get; set; }
|
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);
|
|
|
|
|
|
//var result = entities.Tile();
|
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-04-15 22:44:33 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-12-19 13:00:14 +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
|
|
|
|
|
2023-12-24 11:45:43 +08:00
|
|
|
|
if (this.CurrentUser.GetPermissions().Contains(UserConst.AdminPermissionCode))
|
|
|
|
|
|
{
|
|
|
|
|
|
//如果是超管,直接跳过
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|