144 lines
4.7 KiB
C#
Raw Permalink Normal View History

2023-07-31 18:41:23 +08:00
using Microsoft.AspNetCore.Mvc;
2022-05-13 21:46:27 +08:00
using SqlSugar;
using ZR.Admin.WebApi.Extensions;
2021-08-23 16:57:25 +08:00
using ZR.Admin.WebApi.Filters;
using ZR.Model.System;
2021-09-16 19:07:49 +08:00
using ZR.Model.System.Dto;
2022-05-13 21:46:27 +08:00
using ZR.Service.System.IService;
2021-08-23 16:57:25 +08:00
namespace ZR.Admin.WebApi.Controllers
{
/// <summary>
2021-12-06 12:54:53 +08:00
/// 文章管理
2021-08-23 16:57:25 +08:00
/// </summary>
[Verify]
[Route("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(Permission = "system:article:list")]
public IActionResult Query([FromQuery] ArticleQueryDto parm)
{
2023-03-14 12:21:43 +08:00
var response = _ArticleService.GetList(parm);
2021-08-23 16:57:25 +08:00
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);
}
2022-02-28 18:37:41 +08:00
/// <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);
2022-02-28 18:37:41 +08:00
var response = _ArticleService.Queryable()
.Where(predicate.ToExpression())
2023-03-14 12:21:43 +08:00
.Includes(x => x.ArticleCategoryNav) //填充子对象
2022-02-28 18:37:41 +08:00
.Take(10)
.OrderBy(f => f.UpdateTime, OrderByType.Desc).ToList();
return SUCCESS(response);
}
2021-08-23 16:57:25 +08:00
/// <summary>
/// 查询文章详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("{id}")]
2023-03-14 12:21:43 +08:00
[AllowAnonymous]
2021-08-23 16:57:25 +08:00
public IActionResult Get(int id)
{
long userId = HttpContext.GetUId();
2021-08-23 16:57:25 +08:00
var response = _ArticleService.GetId(id);
2023-03-14 12:21:43 +08:00
var model = response.Adapt<ArticleDto>();
if (model.IsPublic == 0 && userId != model.UserId)
{
return ToResponse(Infrastructure.ResultCode.CUSTOM_ERROR, "访问失败");
}
2023-03-14 12:21:43 +08:00
if (model != null)
{
model.ArticleCategoryNav = _ArticleCategoryService.GetById(model.CategoryId);
model.TagList = model.Tags?.Split(',', StringSplitOptions.RemoveEmptyEntries);
2023-03-14 12:21:43 +08:00
}
return SUCCESS(model);
2021-08-23 16:57:25 +08:00
}
/// <summary>
/// 添加文章
/// </summary>
/// <returns></returns>
[HttpPost("add")]
[ActionPermissionFilter(Permission = "system:article:add")]
2023-05-31 07:46:59 +08:00
[Log(Title = "发布文章", BusinessType = BusinessType.INSERT)]
2023-03-14 12:21:43 +08:00
public IActionResult Create([FromBody] ArticleDto parm)
2021-08-23 16:57:25 +08:00
{
var addModel = parm.Adapt<Article>().ToCreate(context: HttpContext);
2022-05-13 21:46:27 +08:00
addModel.AuthorName = HttpContext.GetName();
2021-08-23 16:57:25 +08:00
2023-05-31 07:46:59 +08:00
return SUCCESS(_ArticleService.InsertReturnIdentity(addModel));
2021-08-23 16:57:25 +08:00
}
/// <summary>
/// 更新文章
/// </summary>
/// <returns></returns>
[HttpPut("edit")]
[ActionPermissionFilter(Permission = "system:article:update")]
[Log(Title = "文章修改", BusinessType = BusinessType.UPDATE)]
2023-03-14 12:21:43 +08:00
public IActionResult Update([FromBody] ArticleDto parm)
2021-08-23 16:57:25 +08:00
{
2022-05-16 18:09:06 +08:00
parm.AuthorName = HttpContext.GetName();
2023-03-14 12:21:43 +08:00
var modal = parm.Adapt<Article>().ToUpdate(HttpContext);
var response = _ArticleService.UpdateArticle(modal);
2021-08-23 16:57:25 +08:00
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);
}
}
}