【新增】WebApi文章Controller新增【获取最新文章】【获取文章栏目】两个接口

This commit is contained in:
大灰灰
2022-11-29 01:19:55 +08:00
parent 30f1082e06
commit 9e5232762a
7 changed files with 157 additions and 17 deletions

View File

@@ -10,6 +10,7 @@
using System; using System;
using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using CoreCms.Net.Model.Entities; using CoreCms.Net.Model.Entities;
@@ -42,5 +43,18 @@ namespace CoreCms.Net.IRepository
Task<IPageList<CoreCmsArticle>> QueryPageAsync(Expression<Func<CoreCmsArticle, bool>> predicate, Task<IPageList<CoreCmsArticle>> QueryPageAsync(Expression<Func<CoreCmsArticle, bool>> predicate,
Expression<Func<CoreCmsArticle, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1, Expression<Func<CoreCmsArticle, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
int pageSize = 20); int pageSize = 20);
/// <summary>
/// 重写根据条件查询分页数据
/// </summary>
/// <param name="predicate">判断集合</param>
/// <param name="orderByType">排序方式</param>
/// <param name="orderByExpression"></param>
/// <param name="take"></param>
/// <returns></returns>
Task<List<CoreCmsArticle>> QueryListAsync(Expression<Func<CoreCmsArticle, bool>> predicate,
Expression<Func<CoreCmsArticle, object>> orderByExpression, OrderByType orderByType, int take);
} }
} }

View File

@@ -9,6 +9,7 @@
***********************************************************************/ ***********************************************************************/
using System; using System;
using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using CoreCms.Net.Model.Entities; using CoreCms.Net.Model.Entities;
@@ -41,5 +42,18 @@ namespace CoreCms.Net.IServices
Task<IPageList<CoreCmsArticle>> QueryPageAsync(Expression<Func<CoreCmsArticle, bool>> predicate, Task<IPageList<CoreCmsArticle>> QueryPageAsync(Expression<Func<CoreCmsArticle, bool>> predicate,
Expression<Func<CoreCmsArticle, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1, Expression<Func<CoreCmsArticle, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
int pageSize = 20); int pageSize = 20);
/// <summary>
/// 重写根据条件查询分页数据
/// </summary>
/// <param name="predicate">判断集合</param>
/// <param name="orderByType">排序方式</param>
/// <param name="orderByExpression"></param>
/// <param name="take"></param>
/// <returns></returns>
Task<List<CoreCmsArticle>> QueryListAsync(Expression<Func<CoreCmsArticle, bool>> predicate,
Expression<Func<CoreCmsArticle, object>> orderByExpression, OrderByType orderByType, int take);
} }
} }

View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CoreCms.Net.Model.FromBody
{
/// <summary>
/// 获取新文章
/// </summary>
public class FMGetNewArticle
{
public int num { set; get; } = 5;
public int typeId { set; get; } = 0;
}
}

View File

@@ -10,6 +10,7 @@
using System; using System;
using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using CoreCms.Net.IRepository; using CoreCms.Net.IRepository;
@@ -128,5 +129,41 @@ namespace CoreCms.Net.Repository
#endregion #endregion
#region
/// <summary>
/// 重写根据条件查询分页数据
/// </summary>
/// <param name="predicate">判断集合</param>
/// <param name="orderByType">排序方式</param>
/// <param name="orderByExpression"></param>
/// <param name="take"></param>
/// <returns></returns>
public async Task<List<CoreCmsArticle>> QueryListAsync(Expression<Func<CoreCmsArticle, bool>> predicate,
Expression<Func<CoreCmsArticle, object>> orderByExpression, OrderByType orderByType,int take)
{
var list = await DbClient.Queryable<CoreCmsArticle>()
.OrderByIF(orderByExpression != null, orderByExpression, orderByType)
.WhereIF(predicate != null, predicate).Select(p => new CoreCmsArticle
{
id = p.id,
title = p.title,
brief = p.brief,
coverImage = p.coverImage,
typeId = p.typeId,
sort = p.sort,
isPub = p.isPub,
isDel = p.isDel,
pv = p.pv,
createTime = p.createTime,
updateTime = p.updateTime
}).Take(take).ToListAsync();
return list;
}
#endregion
} }
} }

View File

@@ -9,6 +9,7 @@
***********************************************************************/ ***********************************************************************/
using System; using System;
using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using CoreCms.Net.IRepository; using CoreCms.Net.IRepository;
@@ -61,5 +62,19 @@ namespace CoreCms.Net.Services
{ {
return await _dal.QueryPageAsync(predicate, orderByExpression, orderByType, pageIndex, pageSize); return await _dal.QueryPageAsync(predicate, orderByExpression, orderByType, pageIndex, pageSize);
} }
/// <summary>
/// 重写根据条件查询分页数据
/// </summary>
/// <param name="predicate">判断集合</param>
/// <param name="orderByType">排序方式</param>
/// <param name="orderByExpression"></param>
/// <param name="take"></param>
/// <returns></returns>
public async Task<List<CoreCmsArticle>> QueryListAsync(Expression<Func<CoreCmsArticle, bool>> predicate, Expression<Func<CoreCmsArticle, object>> orderByExpression, OrderByType orderByType, int take)
{
return await _dal.QueryListAsync(predicate, orderByExpression, orderByType, take);
}
} }
} }

View File

@@ -11,13 +11,11 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using CoreCms.Net.Auth.HttpContextUser;
using CoreCms.Net.IServices; using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities; using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression; using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.FromBody; using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.UI; using CoreCms.Net.Model.ViewModels.UI;
using Essensoft.Paylink.Alipay.Domain;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using SqlSugar; using SqlSugar;
@@ -30,26 +28,20 @@ namespace CoreCms.Net.Web.WebApi.Controllers
[ApiController] [ApiController]
public class ArticleController : ControllerBase public class ArticleController : ControllerBase
{ {
private IHttpContextUser _user;
private readonly ICoreCmsArticleServices _articleServices; private readonly ICoreCmsArticleServices _articleServices;
private readonly ICoreCmsArticleTypeServices _articleTypeServices; private readonly ICoreCmsArticleTypeServices _articleTypeServices;
/// <summary> /// <summary>
/// 构造函数 /// 构造函数
/// </summary> /// </summary>
/// <param name="user"></param>
/// <param name="articleServices"></param> /// <param name="articleServices"></param>
/// <param name="articleTypeServices"></param> /// <param name="articleTypeServices"></param>
public ArticleController(IHttpContextUser user, ICoreCmsArticleServices articleServices, ICoreCmsArticleTypeServices articleTypeServices) public ArticleController(ICoreCmsArticleServices articleServices, ICoreCmsArticleTypeServices articleTypeServices)
{ {
_user = user;
_articleServices = articleServices; _articleServices = articleServices;
_articleTypeServices = articleTypeServices; _articleTypeServices = articleTypeServices;
} }
#region #region
/// <summary> /// <summary>
/// 获取通知列表 /// 获取通知列表
@@ -70,6 +62,41 @@ namespace CoreCms.Net.Web.WebApi.Controllers
#endregion #endregion
#region
/// <summary>
/// 获取文章列表
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<WebApiCallBack> GetArticleClassify()
{
var jm = new WebApiCallBack
{
status = true,
data = await _articleTypeServices.QueryListByClauseAsync(p => p.id > 0, p => p.sort, OrderByType.Desc, true, true)
};
return jm;
}
#endregion
#region
/// <summary>
/// 获取最新文章
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<WebApiCallBack> GetNewArticle([FromBody] FMGetNewArticle entity)
{
var jm = new WebApiCallBack
{
status = true,
data = await _articleServices.QueryListAsync(p => p.isPub == true, p => p.createTime, OrderByType.Desc, entity.num)
};
return jm;
}
#endregion
#region #region
/// <summary> /// <summary>
@@ -81,10 +108,10 @@ namespace CoreCms.Net.Web.WebApi.Controllers
{ {
var jm = new WebApiCallBack(); var jm = new WebApiCallBack();
var articleType = await _articleTypeServices.QueryAsync(); var articleType = await _articleTypeServices.QueryAsync(true, true);
if (articleType.Any()) if (articleType.Any())
{ {
var where = PredicateBuilder.True<Model.Entities.CoreCmsArticle>(); var where = PredicateBuilder.True<CoreCmsArticle>();
if (entity.id > 0) if (entity.id > 0)
{ {
where = where.And(p => p.isDel == false && p.typeId == entity.id); where = where.And(p => p.isDel == false && p.typeId == entity.id);
@@ -99,7 +126,10 @@ namespace CoreCms.Net.Web.WebApi.Controllers
{ {
list, list,
articleType, articleType,
count = list.TotalCount count = list.TotalCount,
list.HasNextPage,
list.HasPreviousPage,
list.TotalPages
}; };
} }
else else
@@ -117,7 +147,7 @@ namespace CoreCms.Net.Web.WebApi.Controllers
#endregion #endregion
#region
/// <summary> /// <summary>
/// 获取单个文章内容 /// 获取单个文章内容
/// </summary> /// </summary>
@@ -139,5 +169,6 @@ namespace CoreCms.Net.Web.WebApi.Controllers
return jm; return jm;
} }
#endregion
} }
} }

View File

@@ -210,11 +210,23 @@ namespace CoreCms.Net.Web.WebApi.Controllers
} }
if (!string.IsNullOrWhiteSpace(obj.brandId)) if (!string.IsNullOrWhiteSpace(obj.brandId))
{ {
var brandId = obj.brandId.ObjectToInt(0); if (obj.brandId.Contains(","))
if (brandId > 0)
{ {
where = where.And(p => p.brandId == brandId); var brandIdsIntArray = CommonHelper.StringToIntArray(obj.brandId);
if (brandIdsIntArray.Any())
{
where = where.And(p => brandIdsIntArray.Contains(p.brandId));
}
} }
else
{
var brandId = obj.brandId.ObjectToInt(0);
if (brandId > 0)
{
where = where.And(p => p.brandId == brandId);
}
}
} }
if (!string.IsNullOrWhiteSpace(obj.labelId)) if (!string.IsNullOrWhiteSpace(obj.labelId))
{ {