160 lines
5.0 KiB
C#
Raw Normal View History

2022-05-13 21:46:27 +08:00
using Infrastructure;
using Infrastructure.Attribute;
using Infrastructure.Enums;
using Infrastructure.Model;
using Mapster;
2021-08-23 16:57:25 +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)
{
//开始拼装查询条件
var predicate = Expressionable.Create<Article>();
//搜索条件
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.Title), m => m.Title.Contains(parm.Title));
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.Status), m => m.Status == parm.Status);
2021-11-27 09:43:04 +08:00
var response = _ArticleService.GetPages(predicate.ToExpression(), parm, f => f.Cid, OrderByType.Desc);
2021-08-23 16:57:25 +08:00
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");
var response = _ArticleService.Queryable()
.Where(predicate.ToExpression())
.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}")]
public IActionResult Get(int id)
{
var response = _ArticleService.GetId(id);
return SUCCESS(response);
}
/// <summary>
/// 添加文章
/// </summary>
/// <returns></returns>
[HttpPost("add")]
[ActionPermissionFilter(Permission = "system:article:add")]
[Log(Title = "文章添加", BusinessType = BusinessType.INSERT)]
public IActionResult Create([FromBody] Article parm)
{
if (parm == null)
{
throw new CustomException("请求参数错误");
}
//从 Dto 映射到 实体
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
return SUCCESS(_ArticleService.Add(addModel));
}
/// <summary>
/// 更新文章
/// </summary>
/// <returns></returns>
[HttpPut("edit")]
[ActionPermissionFilter(Permission = "system:article:update")]
[Log(Title = "文章修改", BusinessType = BusinessType.UPDATE)]
public IActionResult Update([FromBody] Article parm)
{
2022-05-16 18:09:06 +08:00
if (parm == null)
{
throw new CustomException("请求参数错误");
}
parm.AuthorName = HttpContext.GetName();
2021-08-23 16:57:25 +08:00
2022-05-16 18:09:06 +08:00
var response = _ArticleService.Update(it => it.Cid == parm.Cid,
2021-08-23 16:57:25 +08:00
f => new Article
{
2022-05-16 18:09:06 +08:00
Title = parm.Title,
Content = parm.Content,
Tags = parm.Tags,
Category_Id = parm.Category_Id,
UpdateTime = parm.UpdateTime,
Status = parm.Status,
CoverUrl = parm.CoverUrl
});
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)
{
if (id <= 0)
{
2021-09-27 16:07:55 +08:00
return ToResponse(ApiResult.Error($"删除失败Id 不能为空"));
2021-08-23 16:57:25 +08:00
}
// 删除文章
var response = _ArticleService.Delete(id);
return SUCCESS(response);
}
}
}