From 8195c342a8da5c868a5c7d629ec36e06557fb78f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Tue, 14 Mar 2023 12:21:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E7=AB=A0=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/System/ArticleController.cs | 41 ++++++++-------- ZR.Model/System/Article.cs | 11 +++-- ZR.Model/System/Dto/ArticleDto.cs | 40 +++++++++++++++- ZR.Service/System/ArticleService.cs | 47 +++++++++++++++++++ ZR.Service/System/IService/IArticleService.cs | 16 ++++--- 5 files changed, 122 insertions(+), 33 deletions(-) diff --git a/ZR.Admin.WebApi/Controllers/System/ArticleController.cs b/ZR.Admin.WebApi/Controllers/System/ArticleController.cs index cb52d64..66f75f3 100644 --- a/ZR.Admin.WebApi/Controllers/System/ArticleController.cs +++ b/ZR.Admin.WebApi/Controllers/System/ArticleController.cs @@ -1,12 +1,16 @@ -using Infrastructure; +using Aliyun.OSS; +using Infrastructure; using Infrastructure.Attribute; using Infrastructure.Enums; using Infrastructure.Model; using Mapster; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using SqlSugar; +using System; using ZR.Admin.WebApi.Extensions; using ZR.Admin.WebApi.Filters; +using ZR.Common; using ZR.Model.System; using ZR.Model.System.Dto; using ZR.Service.System.IService; @@ -40,12 +44,7 @@ namespace ZR.Admin.WebApi.Controllers [ActionPermissionFilter(Permission = "system:article:list")] public IActionResult Query([FromQuery] ArticleQueryDto parm) { - var predicate = Expressionable.Create
(); - - 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); - - var response = _ArticleService.GetPages(predicate.ToExpression(), parm, f => f.Cid, OrderByType.Desc); + var response = _ArticleService.GetList(parm); return SUCCESS(response); } @@ -62,6 +61,7 @@ namespace ZR.Admin.WebApi.Controllers var response = _ArticleService.Queryable() .Where(predicate.ToExpression()) + .Includes(x => x.ArticleCategoryNav) //填充子对象 .Take(10) .OrderBy(f => f.UpdateTime, OrderByType.Desc).ToList(); @@ -74,11 +74,17 @@ namespace ZR.Admin.WebApi.Controllers /// /// [HttpGet("{id}")] + [AllowAnonymous] public IActionResult Get(int id) { var response = _ArticleService.GetId(id); - - return SUCCESS(response); + var model = response.Adapt(); + if (model != null) + { + model.ArticleCategoryNav = _ArticleCategoryService.GetById(model.CategoryId); + model.TagList = model.Tags.Split(',', StringSplitOptions.RemoveEmptyEntries); + } + return SUCCESS(model); } /// @@ -88,7 +94,7 @@ namespace ZR.Admin.WebApi.Controllers [HttpPost("add")] [ActionPermissionFilter(Permission = "system:article:add")] [Log(Title = "文章添加", BusinessType = BusinessType.INSERT)] - public IActionResult Create([FromBody] Article parm) + public IActionResult Create([FromBody] ArticleDto parm) { if (parm == null) { @@ -107,25 +113,16 @@ namespace ZR.Admin.WebApi.Controllers [HttpPut("edit")] [ActionPermissionFilter(Permission = "system:article:update")] [Log(Title = "文章修改", BusinessType = BusinessType.UPDATE)] - public IActionResult Update([FromBody] Article parm) + public IActionResult Update([FromBody] ArticleDto parm) { if (parm == null) { throw new CustomException("请求参数错误"); } parm.AuthorName = HttpContext.GetName(); + var modal = parm.Adapt
().ToUpdate(HttpContext); - var response = _ArticleService.Update(it => it.Cid == parm.Cid, - f => new Article - { - Title = parm.Title, - Content = parm.Content, - Tags = parm.Tags, - Category_Id = parm.Category_Id, - UpdateTime = parm.UpdateTime, - Status = parm.Status, - CoverUrl = parm.CoverUrl - }); + var response = _ArticleService.UpdateArticle(modal); return SUCCESS(response); } diff --git a/ZR.Model/System/Article.cs b/ZR.Model/System/Article.cs index 9efa057..0e2346e 100644 --- a/ZR.Model/System/Article.cs +++ b/ZR.Model/System/Article.cs @@ -17,7 +17,7 @@ namespace ZR.Model.System public string Title { get; set; } public DateTime? CreateTime { get; set; } [SugarColumn(IsOnlyIgnoreInsert = true)] - public DateTime UpdateTime { get; set; } + public DateTime? UpdateTime { get; set; } /// /// 文章内容 /// @@ -38,16 +38,21 @@ namespace ZR.Model.System /// /// 编辑器类型 markdown,html /// - public string Fmt_type { get; set; } + [SugarColumn(ColumnName = "fmt_type")] + public string FmtType { get; set; } /// /// 文章标签eg:Net5,java /// public string Tags { get; set; } public int Hits { get; set; } - public int Category_Id { get; set; } + [SugarColumn(ColumnName = "category_Id")] + public int CategoryId { get; set; } /// /// 封面地址 /// public string CoverUrl { get; set; } + + [Navigate(NavigateType.OneToOne, nameof(CategoryId), nameof(ArticleCategory.CategoryId))] //自定义关系映射 + public ArticleCategory ArticleCategoryNav { get; set; } } } diff --git a/ZR.Model/System/Dto/ArticleDto.cs b/ZR.Model/System/Dto/ArticleDto.cs index 368b1ea..28fd031 100644 --- a/ZR.Model/System/Dto/ArticleDto.cs +++ b/ZR.Model/System/Dto/ArticleDto.cs @@ -1,10 +1,11 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Text; namespace ZR.Model.System.Dto { - public class ArticleQueryDto: PagerInfo + public class ArticleQueryDto : PagerInfo { public string Status { get; set; } public string Title { get; set; } @@ -12,4 +13,41 @@ namespace ZR.Model.System.Dto public DateTime EndTime { get; set; } } + + /// + /// 输入输出对象 + /// + public class ArticleDto + { + [Required(ErrorMessage = "Cid不能为空")] + public int Cid { get; set; } + + public string Title { get; set; } + + public string Content { get; set; } + + public long? UserId { get; set; } + + public string Status { get; set; } + + public string FmtType { get; set; } + + public string Tags { get; set; } + + public int? Hits { get; set; } + + public int? CategoryId { get; set; } + + public DateTime? CreateTime { get; set; } + + public DateTime? UpdateTime { get; set; } + + public string AuthorName { get; set; } + + public string CoverUrl { get; set; } + + public ArticleCategory ArticleCategoryNav { get; set; } + public string[] TagList { get; set; } + + } } diff --git a/ZR.Service/System/ArticleService.cs b/ZR.Service/System/ArticleService.cs index 7948e3b..480e688 100644 --- a/ZR.Service/System/ArticleService.cs +++ b/ZR.Service/System/ArticleService.cs @@ -1,5 +1,10 @@ using Infrastructure.Attribute; +using SqlSugar; +using System; +using ZR.Model; using ZR.Model.System; +using ZR.Model.System.Dto; +using ZR.Repository; using ZR.Service.System.IService; namespace ZR.Service.System @@ -10,5 +15,47 @@ namespace ZR.Service.System [AppService(ServiceType = typeof(IArticleService), ServiceLifetime = LifeTime.Transient)] public class ArticleService : BaseService
, IArticleService { + /// + /// 查询文章管理列表 + /// + /// + /// + public PagedInfo GetList(ArticleQueryDto parm) + { + var predicate = Expressionable.Create
(); + + 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); + + //搜索条件查询语法参考Sqlsugar + var response = Queryable() + .Includes(x => x.ArticleCategoryNav) //填充子对象 + .Where(predicate.ToExpression()) + .OrderBy(x => x.CreateTime, OrderByType.Desc) + .ToPage(parm); + + return response; + } + + /// + /// 修改文章管理 + /// + /// + /// + public int UpdateArticle(Article model) + { + var response = Update(w => w.Cid == model.Cid, it => new Article() + { + Title = model.Title, + Content = model.Content, + Status = model.Status, + Tags = model.Tags, + UpdateTime = DateTime.Now, + CoverUrl = model.CoverUrl, + CategoryId = model.CategoryId, + FmtType = model.FmtType, + }); + return response; + } } } diff --git a/ZR.Service/System/IService/IArticleService.cs b/ZR.Service/System/IService/IArticleService.cs index 0f4e7de..dc7a229 100644 --- a/ZR.Service/System/IService/IArticleService.cs +++ b/ZR.Service/System/IService/IArticleService.cs @@ -1,15 +1,17 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using ZR.Model.System.Dto; +using ZR.Model; using ZR.Model.System; +using ZR.Model.System.Dto; namespace ZR.Service.System.IService { public interface IArticleService : IBaseService
{ - + PagedInfo GetList(ArticleQueryDto parm); + /// + /// 修改文章管理 + /// + /// + /// + public int UpdateArticle(Article model); } }