ArticleController.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using Infrastructure;
  2. using Infrastructure.Attribute;
  3. using Infrastructure.Enums;
  4. using Infrastructure.Model;
  5. using Mapster;
  6. using Microsoft.AspNetCore.Mvc;
  7. using SqlSugar;
  8. using ZR.Admin.WebApi.Extensions;
  9. using ZR.Admin.WebApi.Filters;
  10. using ZR.Model.System;
  11. using ZR.Model.System.Dto;
  12. using ZR.Service.System.IService;
  13. namespace ZR.Admin.WebApi.Controllers
  14. {
  15. /// <summary>
  16. /// 文章管理
  17. /// </summary>
  18. [Verify]
  19. [Route("article")]
  20. public class ArticleController : BaseController
  21. {
  22. /// <summary>
  23. /// 文章接口
  24. /// </summary>
  25. private readonly IArticleService _ArticleService;
  26. private readonly IArticleCategoryService _ArticleCategoryService;
  27. public ArticleController(IArticleService ArticleService, IArticleCategoryService articleCategoryService)
  28. {
  29. _ArticleService = ArticleService;
  30. _ArticleCategoryService = articleCategoryService;
  31. }
  32. /// <summary>
  33. /// 查询文章列表
  34. /// </summary>
  35. /// <returns></returns>
  36. [HttpGet("list")]
  37. [ActionPermissionFilter(Permission = "system:article:list")]
  38. public IActionResult Query([FromQuery] ArticleQueryDto parm)
  39. {
  40. var predicate = Expressionable.Create<Article>();
  41. predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.Title), m => m.Title.Contains(parm.Title));
  42. predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.Status), m => m.Status == parm.Status);
  43. var response = _ArticleService.GetPages(predicate.ToExpression(), parm, f => f.Cid, OrderByType.Desc);
  44. return SUCCESS(response);
  45. }
  46. /// <summary>
  47. /// 查询最新文章列表
  48. /// </summary>
  49. /// <returns></returns>
  50. [HttpGet("newList")]
  51. public IActionResult QueryNew()
  52. {
  53. var predicate = Expressionable.Create<Article>();
  54. predicate = predicate.And(m => m.Status == "1");
  55. var response = _ArticleService.Queryable()
  56. .Where(predicate.ToExpression())
  57. .Take(10)
  58. .OrderBy(f => f.UpdateTime, OrderByType.Desc).ToList();
  59. return SUCCESS(response);
  60. }
  61. /// <summary>
  62. /// 查询文章详情
  63. /// </summary>
  64. /// <param name="id"></param>
  65. /// <returns></returns>
  66. [HttpGet("{id}")]
  67. public IActionResult Get(int id)
  68. {
  69. var response = _ArticleService.GetId(id);
  70. return SUCCESS(response);
  71. }
  72. /// <summary>
  73. /// 添加文章
  74. /// </summary>
  75. /// <returns></returns>
  76. [HttpPost("add")]
  77. [ActionPermissionFilter(Permission = "system:article:add")]
  78. [Log(Title = "文章添加", BusinessType = BusinessType.INSERT)]
  79. public IActionResult Create([FromBody] Article parm)
  80. {
  81. if (parm == null)
  82. {
  83. throw new CustomException("请求参数错误");
  84. }
  85. var addModel = parm.Adapt<Article>().ToCreate(context: HttpContext);
  86. addModel.AuthorName = HttpContext.GetName();
  87. return SUCCESS(_ArticleService.Add(addModel));
  88. }
  89. /// <summary>
  90. /// 更新文章
  91. /// </summary>
  92. /// <returns></returns>
  93. [HttpPut("edit")]
  94. [ActionPermissionFilter(Permission = "system:article:update")]
  95. [Log(Title = "文章修改", BusinessType = BusinessType.UPDATE)]
  96. public IActionResult Update([FromBody] Article parm)
  97. {
  98. if (parm == null)
  99. {
  100. throw new CustomException("请求参数错误");
  101. }
  102. parm.AuthorName = HttpContext.GetName();
  103. var response = _ArticleService.Update(it => it.Cid == parm.Cid,
  104. f => new Article
  105. {
  106. Title = parm.Title,
  107. Content = parm.Content,
  108. Tags = parm.Tags,
  109. Category_Id = parm.Category_Id,
  110. UpdateTime = parm.UpdateTime,
  111. Status = parm.Status,
  112. CoverUrl = parm.CoverUrl
  113. });
  114. return SUCCESS(response);
  115. }
  116. /// <summary>
  117. /// 删除文章
  118. /// </summary>
  119. /// <returns></returns>
  120. [HttpDelete("{id}")]
  121. [ActionPermissionFilter(Permission = "system:article:delete")]
  122. [Log(Title = "文章删除", BusinessType = BusinessType.DELETE)]
  123. public IActionResult Delete(int id = 0)
  124. {
  125. if (id <= 0)
  126. {
  127. return ToResponse(ApiResult.Error($"删除失败Id 不能为空"));
  128. }
  129. // 删除文章
  130. var response = _ArticleService.Delete(id);
  131. return SUCCESS(response);
  132. }
  133. }
  134. }