ChangelogService.cs
7.3 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NCC.Common.Core.Manager;
using NCC.Common.Filter;
using NCC.DynamicApiController;
using NCC.Extend.Entitys.Dto.Changelog;
using NCC.Extend.Entitys.lq_changelog;
using NCC.Dependency;
using SqlSugar;
using Yitter.IdGenerator;
namespace NCC.Extend
{
/// <summary>
/// 系统更新日志服务
/// </summary>
[ApiDescriptionSettings(Tag = "Common", Name = "Changelog", Order = 170)]
[Route("api/Extend/[controller]")]
public class ChangelogService : IDynamicApiController, ITransient
{
private readonly ISqlSugarClient _db;
private readonly IUserManager _userManager;
public ChangelogService(ISqlSugarClient db, IUserManager userManager)
{
_db = db;
_userManager = userManager;
_db.CodeFirst.InitTables(typeof(LqChangelogEntity));
}
/// <summary>
/// 获取更新日志列表(分页)
/// </summary>
[HttpGet("GetList")]
public async Task<dynamic> GetList([FromQuery] ChangelogListQueryInput input)
{
var data = await _db.Queryable<LqChangelogEntity>()
.WhereIF(input.isPublished.HasValue, x => x.IsPublished == input.isPublished.Value)
.WhereIF(input.type.HasValue, x => x.Type == input.type.Value)
.WhereIF(!string.IsNullOrWhiteSpace(input.keyword),
x => x.Title.Contains(input.keyword) || x.Version.Contains(input.keyword))
.OrderBy(x => x.PublishedTime, OrderByType.Desc)
.OrderBy(x => x.CreateTime, OrderByType.Desc)
.Select(x => new ChangelogListOutput
{
id = x.Id,
version = x.Version,
title = x.Title,
content = x.Content,
type = x.Type,
isPublished = x.IsPublished,
publishedTime = x.PublishedTime,
createTime = x.CreateTime,
modifyTime = x.ModifyTime
})
.ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult<ChangelogListOutput>.SqlSugarPageResult(data);
}
/// <summary>
/// 获取已发布的更新日志列表(前端用)
/// </summary>
[HttpGet("GetPublishedList")]
public async Task<dynamic> GetPublishedList([FromQuery] int currentPage = 1, int pageSize = 20)
{
var data = await _db.Queryable<LqChangelogEntity>()
.Where(x => x.IsPublished)
.OrderBy(x => x.PublishedTime, OrderByType.Desc)
.Select(x => new ChangelogListOutput
{
id = x.Id,
version = x.Version,
title = x.Title,
content = x.Content,
type = x.Type,
isPublished = x.IsPublished,
publishedTime = x.PublishedTime,
createTime = x.CreateTime,
modifyTime = x.ModifyTime
})
.ToPagedListAsync(currentPage, pageSize);
return PageResult<ChangelogListOutput>.SqlSugarPageResult(data);
}
/// <summary>
/// 获取最新一条已发布更新日志(右上角红点提示用)
/// </summary>
[HttpGet("GetLatest")]
public async Task<dynamic> GetLatest()
{
var latest = await _db.Queryable<LqChangelogEntity>()
.Where(x => x.IsPublished)
.OrderBy(x => x.PublishedTime, OrderByType.Desc)
.FirstAsync();
return latest;
}
/// <summary>
/// 获取更新日志详情
/// </summary>
[HttpGet("GetDetail/{id}")]
public async Task<dynamic> GetDetail(string id)
{
var entity = await _db.Queryable<LqChangelogEntity>()
.Where(x => x.Id == id)
.FirstAsync();
if (entity == null)
throw new Exception("更新日志不存在");
return entity;
}
/// <summary>
/// 添加更新日志
/// </summary>
[HttpPost("Add")]
public async Task<dynamic> Add([FromBody] ChangelogAddInput input)
{
var entity = new LqChangelogEntity
{
Id = YitIdHelper.NextId().ToString(),
Version = input.Version,
Title = input.Title,
Content = input.Content,
Type = input.Type,
IsPublished = input.IsPublished,
PublishedTime = input.IsPublished ? DateTime.Now : null,
CreateUserId = _userManager?.UserId,
CreateTime = DateTime.Now
};
await _db.Insertable(entity).ExecuteCommandAsync();
return new { success = true, id = entity.Id };
}
/// <summary>
/// 修改更新日志
/// </summary>
[HttpPost("Update")]
public async Task<dynamic> Update([FromBody] ChangelogUpdateInput input)
{
var entity = await _db.Queryable<LqChangelogEntity>()
.Where(x => x.Id == input.Id)
.FirstAsync();
if (entity == null)
throw new Exception("更新日志不存在");
entity.Version = input.Version ?? entity.Version;
entity.Title = input.Title ?? entity.Title;
entity.Content = input.Content ?? entity.Content;
entity.Type = input.Type;
entity.ModifyTime = DateTime.Now;
if (input.IsPublished && !entity.IsPublished)
{
entity.IsPublished = true;
entity.PublishedTime = DateTime.Now;
}
else if (!input.IsPublished)
{
entity.IsPublished = false;
entity.PublishedTime = null;
}
await _db.Updateable(entity).ExecuteCommandAsync();
return new { success = true };
}
/// <summary>
/// 删除更新日志
/// </summary>
[HttpPost("Delete/{id}")]
public async Task<dynamic> Delete(string id)
{
await _db.Deleteable<LqChangelogEntity>(id).ExecuteCommandAsync();
return new { success = true };
}
/// <summary>
/// 切换发布状态
/// </summary>
[HttpPost("TogglePublish/{id}")]
public async Task<dynamic> TogglePublish(string id)
{
var entity = await _db.Queryable<LqChangelogEntity>()
.Where(x => x.Id == id)
.FirstAsync();
if (entity == null)
throw new Exception("更新日志不存在");
entity.IsPublished = !entity.IsPublished;
entity.PublishedTime = entity.IsPublished ? DateTime.Now : null;
entity.ModifyTime = DateTime.Now;
await _db.Updateable(entity)
.UpdateColumns(x => new { x.IsPublished, x.PublishedTime, x.ModifyTime })
.ExecuteCommandAsync();
return new { success = true, isPublished = entity.IsPublished };
}
}
}