优化文章管理
This commit is contained in:
parent
a18d6cb959
commit
8195c342a8
@ -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<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);
|
||||
|
||||
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
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("{id}")]
|
||||
[AllowAnonymous]
|
||||
public IActionResult Get(int id)
|
||||
{
|
||||
var response = _ArticleService.GetId(id);
|
||||
|
||||
return SUCCESS(response);
|
||||
var model = response.Adapt<ArticleDto>();
|
||||
if (model != null)
|
||||
{
|
||||
model.ArticleCategoryNav = _ArticleCategoryService.GetById(model.CategoryId);
|
||||
model.TagList = model.Tags.Split(',', StringSplitOptions.RemoveEmptyEntries);
|
||||
}
|
||||
return SUCCESS(model);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -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<Article>().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);
|
||||
}
|
||||
|
||||
@ -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; }
|
||||
/// <summary>
|
||||
/// 文章内容
|
||||
/// </summary>
|
||||
@ -38,16 +38,21 @@ namespace ZR.Model.System
|
||||
/// <summary>
|
||||
/// 编辑器类型 markdown,html
|
||||
/// </summary>
|
||||
public string Fmt_type { get; set; }
|
||||
[SugarColumn(ColumnName = "fmt_type")]
|
||||
public string FmtType { get; set; }
|
||||
/// <summary>
|
||||
/// 文章标签eg:Net5,java
|
||||
/// </summary>
|
||||
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; }
|
||||
/// <summary>
|
||||
/// 封面地址
|
||||
/// </summary>
|
||||
public string CoverUrl { get; set; }
|
||||
|
||||
[Navigate(NavigateType.OneToOne, nameof(CategoryId), nameof(ArticleCategory.CategoryId))] //自定义关系映射
|
||||
public ArticleCategory ArticleCategoryNav { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@ -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; }
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 输入输出对象
|
||||
/// </summary>
|
||||
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; }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<Article>, IArticleService
|
||||
{
|
||||
/// <summary>
|
||||
/// 查询文章管理列表
|
||||
/// </summary>
|
||||
/// <param name="parm"></param>
|
||||
/// <returns></returns>
|
||||
public PagedInfo<ArticleDto> GetList(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);
|
||||
|
||||
//搜索条件查询语法参考Sqlsugar
|
||||
var response = Queryable()
|
||||
.Includes(x => x.ArticleCategoryNav) //填充子对象
|
||||
.Where(predicate.ToExpression())
|
||||
.OrderBy(x => x.CreateTime, OrderByType.Desc)
|
||||
.ToPage<Article, ArticleDto>(parm);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改文章管理
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<Article>
|
||||
{
|
||||
|
||||
PagedInfo<ArticleDto> GetList(ArticleQueryDto parm);
|
||||
/// <summary>
|
||||
/// 修改文章管理
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public int UpdateArticle(Article model);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user