forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopicDetailController.cs
More file actions
165 lines (144 loc) · 5.5 KB
/
TopicDetailController.cs
File metadata and controls
165 lines (144 loc) · 5.5 KB
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
using System;
using System.Linq;
using System.Threading.Tasks;
using Blog.Core.Common.Helper;
using Blog.Core.IServices;
using Blog.Core.Model;
using Blog.Core.Model.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Blog.Core.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize("Permission")]
public class TopicDetailController : ControllerBase
{
readonly ITopicServices _topicServices;
readonly ITopicDetailServices _topicDetailServices;
/// <summary>
/// TopicDetailController
/// </summary>
/// <param name="topicServices"></param>
/// <param name="topicDetailServices"></param>
public TopicDetailController(ITopicServices topicServices, ITopicDetailServices topicDetailServices)
{
_topicServices = topicServices;
_topicDetailServices = topicDetailServices;
}
/// <summary>
/// 获取Bug数据列表(带分页)
/// </summary>
/// <param name="page">页数</param>
/// <param name="tname">专题类型</param>
/// <returns></returns>
[HttpGet]
[AllowAnonymous]
public async Task<MessageModel<PageModel<TopicDetail>>> Get(int page = 1, string tname = "", string key = "")
{
var data = new MessageModel<PageModel<TopicDetail>>();
int intTotalCount = 6;
int totalCount = 0;
int pageCount = 1;
//总数据,使用AOP切面缓存
//topicDetails = await _topicDetailServices.GetTopicDetails();
var topicDetails = await _topicDetailServices.Query(a => !a.tdIsDelete && a.tdSectendDetail == "tbug");
if (!string.IsNullOrEmpty(key))
{
topicDetails = topicDetails.Where(t => (t.tdName != null && t.tdName.Contains(key)) || (t.tdDetail != null && t.tdDetail.Contains(key))).ToList();
}
tname = UnicodeHelper.UnicodeToString(tname);
if (!string.IsNullOrEmpty(tname))
{
var tid = (await _topicServices.Query(ts => ts.tName == tname)).FirstOrDefault()?.Id.ObjToInt();
topicDetails = topicDetails.Where(t => t.TopicId == tid).ToList();
}
//筛选后的数据总数
totalCount = topicDetails.Count;
//筛选后的总页数
pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intTotalCount.ObjToDecimal())).ObjToInt();
topicDetails = topicDetails.OrderByDescending(d => d.Id).Skip((page - 1) * intTotalCount).Take(intTotalCount).ToList();
return new MessageModel<PageModel<TopicDetail>>()
{
msg = "获取成功",
success = totalCount >= 0,
response = new PageModel<TopicDetail>()
{
page = page,
pageCount = pageCount,
dataCount = totalCount,
data = topicDetails,
}
};
}
// GET: api/TopicDetail/5
[HttpGet("{id}")]
[AllowAnonymous]
public async Task<MessageModel<TopicDetail>> Get(int id)
{
var data = new MessageModel<TopicDetail>();
var response = await _topicDetailServices.QueryById(id);
data.response = response.tdIsDelete ? null : response;
if (data.response != null)
{
data.success = true;
data.msg = "";
}
return data;
}
// POST: api/TopicDetail
[HttpPost]
[AllowAnonymous]
public async Task<MessageModel<string>> Post([FromBody] TopicDetail topicDetail)
{
var data = new MessageModel<string>();
topicDetail.tdCreatetime = DateTime.Now;
topicDetail.tdRead = 0;
topicDetail.tdCommend = 0;
topicDetail.tdGood = 0;
topicDetail.tdTop = 0;
var id = (await _topicDetailServices.Add(topicDetail));
data.success = id > 0;
if (data.success)
{
data.response = id.ObjToString();
data.msg = "添加成功";
}
return data;
}
// PUT: api/TopicDetail/5
[HttpPut]
public async Task<MessageModel<string>> Update([FromBody] TopicDetail topicDetail)
{
var data = new MessageModel<string>();
if (topicDetail != null && topicDetail.Id > 0)
{
data.success = await _topicDetailServices.Update(topicDetail);
if (data.success)
{
data.msg = "更新成功";
data.response = topicDetail?.Id.ObjToString();
}
}
return data;
}
// DELETE: api/ApiWithActions/5
[HttpDelete]
public async Task<MessageModel<string>> Delete(int id)
{
var data = new MessageModel<string>();
if (id > 0)
{
var topicDetail = await _topicDetailServices.QueryById(id);
topicDetail.tdIsDelete = true;
data.success = await _topicDetailServices.Update(topicDetail);
if (data.success)
{
data.msg = "删除成功";
data.response = topicDetail?.Id.ObjToString();
}
}
return data;
}
}
}