149 lines
4.8 KiB
C#
149 lines
4.8 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using SqlSugar;
|
|
using ZR.Admin.WebApi.Extensions;
|
|
using ZR.Admin.WebApi.Filters;
|
|
using ZR.Model;
|
|
using ZR.Model.System;
|
|
using ZR.Service.System.IService;
|
|
using ZR.ServiceCore.Filters;
|
|
using ZR.ServiceCore.Model;
|
|
using ZR.ServiceCore.Model.Dto;
|
|
|
|
namespace ZR.Admin.WebApi.Controllers
|
|
{
|
|
/// <summary>
|
|
/// 文章管理
|
|
/// </summary>
|
|
[Verify]
|
|
[Route("article")]
|
|
[ApiExplorerSettings(GroupName = "article")]
|
|
public class ArticleController : BaseController
|
|
{
|
|
/// <summary>
|
|
/// 文章接口
|
|
/// </summary>
|
|
private readonly IArticleService _ArticleService;
|
|
private readonly IArticleCategoryService _ArticleCategoryService;
|
|
|
|
public ArticleController(IArticleService ArticleService, IArticleCategoryService articleCategoryService)
|
|
{
|
|
_ArticleService = ArticleService;
|
|
_ArticleCategoryService = articleCategoryService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询文章列表
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("list")]
|
|
// [ActionPermissionFilter(RolePermi = "admin")]
|
|
[DataFieldFilter(typeof(PagedInfo<ArticleDto>))]
|
|
public IActionResult Query([FromQuery] ArticleQueryDto parm)
|
|
{
|
|
var response = _ArticleService.GetList(parm);
|
|
|
|
return SUCCESS(response);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询我的文章列表
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("mylist")]
|
|
public IActionResult QueryMyList([FromQuery] ArticleQueryDto parm)
|
|
{
|
|
parm.UserId = HttpContext.GetUId();
|
|
var response = _ArticleService.GetMyList(parm);
|
|
|
|
return SUCCESS(response);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询最新文章列表
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet("newList")]
|
|
public IActionResult QueryNew()
|
|
{
|
|
var predicate = Expressionable.Create<Article>();
|
|
predicate = predicate.And(m => m.Status == "1");
|
|
predicate = predicate.And(m => m.IsPublic == 1);
|
|
|
|
var response = _ArticleService.Queryable()
|
|
.Where(predicate.ToExpression())
|
|
.Includes(x => x.ArticleCategoryNav) //填充子对象
|
|
.Take(10)
|
|
.OrderBy(f => f.UpdateTime, OrderByType.Desc).ToList();
|
|
|
|
return SUCCESS(response);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询文章详情
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("{id}")]
|
|
[AllowAnonymous]
|
|
public IActionResult Get(int id)
|
|
{
|
|
long userId = HttpContext.GetUId();
|
|
var response = _ArticleService.GetId(id);
|
|
var model = response.Adapt<ArticleDto>();
|
|
if (model.IsPublic == 0 && userId != model.UserId)
|
|
{
|
|
return ToResponse(ResultCode.CUSTOM_ERROR, "访问失败");
|
|
}
|
|
if (model != null)
|
|
{
|
|
model.ArticleCategoryNav = _ArticleCategoryService.GetById(model.CategoryId);
|
|
model.TagList = model.Tags?.Split(',', StringSplitOptions.RemoveEmptyEntries);
|
|
}
|
|
return SUCCESS(model);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加文章
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost("add")]
|
|
[ActionPermissionFilter(Permission = "system:article:add")]
|
|
[Log(Title = "发布文章", BusinessType = BusinessType.INSERT)]
|
|
public IActionResult Create([FromBody] ArticleDto parm)
|
|
{
|
|
var addModel = parm.Adapt<Article>().ToCreate(context: HttpContext);
|
|
addModel.AuthorName = HttpContext.GetName();
|
|
|
|
return SUCCESS(_ArticleService.InsertReturnIdentity(addModel));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新文章
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPut("edit")]
|
|
[ActionPermissionFilter(Permission = "system:article:update")]
|
|
[Log(Title = "文章修改", BusinessType = BusinessType.UPDATE)]
|
|
public IActionResult Update([FromBody] ArticleDto parm)
|
|
{
|
|
parm.AuthorName = HttpContext.GetName();
|
|
var modal = parm.Adapt<Article>().ToUpdate(HttpContext);
|
|
var response = _ArticleService.UpdateArticle(modal);
|
|
|
|
return SUCCESS(response);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除文章
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpDelete("{id}")]
|
|
[ActionPermissionFilter(Permission = "system:article:delete")]
|
|
[Log(Title = "文章删除", BusinessType = BusinessType.DELETE)]
|
|
public IActionResult Delete(int id = 0)
|
|
{
|
|
var response = _ArticleService.Delete(id);
|
|
return SUCCESS(response);
|
|
}
|
|
}
|
|
} |