liaoxujun@qq.com
2024-04-01 68bcb554b91c0da1b7765142cc95ba0b5ade1a5a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/***********************************************************************
 *            Project: baifenBinfa
 *        ProjectName: 百分兵法管理系统                               
 *                Web: http://chuanyin.com                     
 *             Author:                                        
 *              Email:                               
 *         CreateTime: 202403/02   
 *        Description: 暂无
 ***********************************************************************/
 
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
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 Microsoft.AspNetCore.Mvc;
using SqlSugar;
 
namespace CoreCms.Net.Web.WebApi.Controllers
{
    /// <summary>
    /// 文章api控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ArticleController : ControllerBase
    {
        private readonly ICoreCmsArticleServices _articleServices;
        private readonly ICoreCmsArticleTypeServices _articleTypeServices;
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="articleServices"></param>
        /// <param name="articleTypeServices"></param>
        public ArticleController(ICoreCmsArticleServices articleServices, ICoreCmsArticleTypeServices articleTypeServices)
        {
            _articleServices = articleServices;
            _articleTypeServices = articleTypeServices;
        }
 
        #region 获取通知列表
        /// <summary>
        /// 获取通知列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> NoticeList([FromBody] FMPageByIntId entity)
        {
            var jm = new WebApiCallBack();
 
            var list = await _articleServices.QueryPageAsync(p => p.isDel == false, p => p.createTime, OrderByType.Desc,
                entity.page, entity.limit);
            jm.status = true;
            jm.data = list;
 
            return jm;
        }
 
        #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>
        /// 获取文章列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> GetArticleList([FromBody] FMPageByIntId entity)
        {
            var jm = new WebApiCallBack();
 
            var articleType = await _articleTypeServices.QueryListByClauseAsync(p => p.id > 0, p => p.sort, OrderByType.Asc, true, true);
            if (articleType.Any())
            {
                var where = PredicateBuilder.True<CoreCmsArticle>();
                if (entity.id > 0)
                {
                    where = where.And(p => p.isDel == false && p.typeId == entity.id);
                }
                else
                {
                    var typeId = articleType.FirstOrDefault()!.id;
                    where = where.And(p => p.isDel == false && p.typeId == typeId);
                }
                var list = await _articleServices.QueryPageAsync(where, p => p.createTime, OrderByType.Desc, entity.page, entity.limit);
                jm.data = new
                {
                    list,
                    articleType,
                    count = list.TotalCount,
                    list.HasNextPage,
                    list.HasPreviousPage,
                    list.TotalPages
                };
            }
            else
            {
                jm.data = new
                {
                    list = new List<CoreCmsArticle>(),
                    articleType,
                    count = 0
                };
            }
            jm.status = true;
            return jm;
        }
 
        #endregion
 
        #region 获取单个文章内容
        /// <summary>
        /// 获取单个文章内容
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> GetArticleDetail([FromBody] FMIntId entity)
        {
            var jm = new WebApiCallBack();
 
            var model = await _articleServices.ArticleDetail(entity.id);
            if (model == null)
            {
                jm.msg = "数据获取失败";
                return jm;
            }
            jm.status = true;
            jm.data = model;
            return jm;
 
        } 
        #endregion
    }
}