using YD_Event.Application.Blog.Dtos;
namespace YD_Event.Application.Blog;
///
/// 文章管理
///
public class ArticleService : BaseService
{
private readonly ISqlSugarRepository _repository;
private readonly IIdGenerator _idGenerator;
public ArticleService(ISqlSugarRepository repository,
IIdGenerator idGenerator) : base(repository)
{
_repository = repository;
_idGenerator = idGenerator;
}
///
/// 文章列表分页查询
///
///
///
[DisplayName("【后端】文章列表分页查询")]
[HttpGet]
public async Task> Page([FromQuery] ArticlePageQueryInput dto)
{
List categoryList = new();
if (dto.CategoryId.HasValue)
{
var list = await _repository.AsSugarClient().Queryable()
.Where(x => x.Status == AvailabilityStatus.Enable)
.ToChildListAsync(x => x.ParentId, dto.CategoryId);
categoryList = list.Select(x => x.Id).ToList();
categoryList.Add(dto.CategoryId.Value);
}
return await _repository.AsQueryable().LeftJoin((article, ac) => article.Id == ac.ArticleId)
.InnerJoin((article, ac, c) => ac.CategoryId == c.Id && c.Status == AvailabilityStatus.Enable)
.WhereIF(!string.IsNullOrWhiteSpace(dto.Title), article => article.Title.Contains(dto.Title) || article.Summary.Contains(dto.Title) || article.Content.Contains(dto.Title))
.WhereIF(categoryList.Any(), (article, ac) => categoryList.Contains(ac.CategoryId))
.OrderByDescending(article => article.IsTop)
.OrderBy(article => article.Sort)
.OrderByDescending(article => article.PublishTime)
.Select((article, ac, c) => new ArticlePageOutput
{
Id = article.Id,
Title = article.Title,
Status = article.Status,
Sort = article.Sort,
Cover = article.Cover,
IsTop = article.IsTop,
CreatedTime = article.CreatedTime,
CreationType = article.CreationType,
PublishTime = article.PublishTime,
Views = article.Views,
CategoryName = c.Name
}).ToPagedListAsync(dto);
}
///
/// 添加文章
///
///
///
[DisplayName("添加文章")]
[HttpPost("add")]
[UnitOfWork]
public async Task Add(AddArticleInput dto)
{
var article = dto.Adapt();
article.Id = _idGenerator.NewLong();
var tags = dto.Tags.Select(x => new ArticleTag()
{
ArticleId = article.Id,
TagId = x
}).ToList();
await _repository.InsertAsync(article);
await _repository.AsSugarClient().Insertable(tags).ExecuteCommandAsync();
var articleCategory = new ArticleCategory()
{
ArticleId = article.Id,
CategoryId = dto.CategoryId
};
await _repository.AsSugarClient().Insertable(articleCategory).ExecuteCommandAsync();
}
///
/// 更新文章
///
///
[DisplayName("更新文章")]
[HttpPut("edit")]
[UnitOfWork]
public async Task Update(UpdateArticleInput dto)
{
var article = await _repository.GetByIdAsync(dto.Id);
if (article == null) throw Oops.Oh("无效参数");
dto.Adapt(article);
await _repository.UpdateAsync(article);
await _repository.AsSugarClient().Deleteable()
.Where(x => x.ArticleId == dto.Id)
.ExecuteCommandHasChangeAsync();
var tags = dto.Tags.Select(x => new ArticleTag()
{
ArticleId = article.Id,
TagId = x
}).ToList();
await _repository.AsSugarClient()
.Insertable(tags)
.ExecuteCommandAsync();
await _repository.AsSugarClient()
.Updateable()
.SetColumns(x => x.CategoryId == dto.CategoryId)
.Where(x => x.ArticleId == dto.Id)
.ExecuteCommandHasChangeAsync();
}
///
/// 文章详情
///
///
///
[DisplayName("文章详情")]
[HttpGet]
public async Task Detail([FromQuery] long id)
{
return await _repository.AsQueryable().LeftJoin((article, ac) => article.Id == ac.ArticleId)
.InnerJoin((article, ac, c) => ac.CategoryId == c.Id && c.Status == AvailabilityStatus.Enable)
.Where(article => article.Id == id)
.Select((article, ac, c) => new ArticleDetailOutput
{
Id = article.Id,
Title = article.Title,
Summary = article.Summary,
Cover = article.Cover,
Status = article.Status,
Link = article.Link,
IsTop = article.IsTop,
Sort = article.Sort,
Author = article.Author,
Content = article.Content,
IsAllowComments = article.IsAllowComments,
IsHtml = article.IsHtml,
CreationType = article.CreationType,
CategoryId = c.Id,
ExpiredTime = article.ExpiredTime,
PublishTime = article.PublishTime,
Tags = SqlFunc.Subqueryable().InnerJoin((tags, at) => tags.Id == at.TagId)
.Where((tags, at) => at.ArticleId == article.Id && tags.Status == AvailabilityStatus.Enable)
.ToList(tags => tags.Id)
}).FirstAsync();
}
}