【新增】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.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreCms.Net.Model.Entities;
@@ -42,5 +43,18 @@ namespace CoreCms.Net.IRepository
Task<IPageList<CoreCmsArticle>> QueryPageAsync(Expression<Func<CoreCmsArticle, bool>> predicate,
Expression<Func<CoreCmsArticle, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
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.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreCms.Net.Model.Entities;
@@ -41,5 +42,18 @@ namespace CoreCms.Net.IServices
Task<IPageList<CoreCmsArticle>> QueryPageAsync(Expression<Func<CoreCmsArticle, bool>> predicate,
Expression<Func<CoreCmsArticle, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
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.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreCms.Net.IRepository;
@@ -128,5 +129,41 @@ namespace CoreCms.Net.Repository
#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.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreCms.Net.IRepository;
@@ -61,5 +62,19 @@ namespace CoreCms.Net.Services
{
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.Linq;
using System.Threading.Tasks;
using CoreCms.Net.Auth.HttpContextUser;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.UI;
using Essensoft.Paylink.Alipay.Domain;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
@@ -30,26 +28,20 @@ namespace CoreCms.Net.Web.WebApi.Controllers
[ApiController]
public class ArticleController : ControllerBase
{
private IHttpContextUser _user;
private readonly ICoreCmsArticleServices _articleServices;
private readonly ICoreCmsArticleTypeServices _articleTypeServices;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="user"></param>
/// <param name="articleServices"></param>
/// <param name="articleTypeServices"></param>
public ArticleController(IHttpContextUser user, ICoreCmsArticleServices articleServices, ICoreCmsArticleTypeServices articleTypeServices)
public ArticleController(ICoreCmsArticleServices articleServices, ICoreCmsArticleTypeServices articleTypeServices)
{
_user = user;
_articleServices = articleServices;
_articleTypeServices = articleTypeServices;
}
#region
/// <summary>
/// 获取通知列表
@@ -70,6 +62,41 @@ namespace CoreCms.Net.Web.WebApi.Controllers
#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
/// <summary>
@@ -81,10 +108,10 @@ namespace CoreCms.Net.Web.WebApi.Controllers
{
var jm = new WebApiCallBack();
var articleType = await _articleTypeServices.QueryAsync();
var articleType = await _articleTypeServices.QueryAsync(true, true);
if (articleType.Any())
{
var where = PredicateBuilder.True<Model.Entities.CoreCmsArticle>();
var where = PredicateBuilder.True<CoreCmsArticle>();
if (entity.id > 0)
{
where = where.And(p => p.isDel == false && p.typeId == entity.id);
@@ -99,7 +126,10 @@ namespace CoreCms.Net.Web.WebApi.Controllers
{
list,
articleType,
count = list.TotalCount
count = list.TotalCount,
list.HasNextPage,
list.HasPreviousPage,
list.TotalPages
};
}
else
@@ -117,7 +147,7 @@ namespace CoreCms.Net.Web.WebApi.Controllers
#endregion
#region
/// <summary>
/// 获取单个文章内容
/// </summary>
@@ -139,5 +169,6 @@ namespace CoreCms.Net.Web.WebApi.Controllers
return jm;
}
#endregion
}
}

View File

@@ -209,6 +209,16 @@ namespace CoreCms.Net.Web.WebApi.Controllers
}
}
if (!string.IsNullOrWhiteSpace(obj.brandId))
{
if (obj.brandId.Contains(","))
{
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)
@@ -216,6 +226,8 @@ namespace CoreCms.Net.Web.WebApi.Controllers
where = where.And(p => p.brandId == brandId);
}
}
}
if (!string.IsNullOrWhiteSpace(obj.labelId))
{
where = where.And(p => (',' + p.labelIds.Trim(',') + ',').Contains(',' + obj.labelId.Trim(',') + ','));