forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathModuleController.cs
More file actions
142 lines (125 loc) · 4.03 KB
/
ModuleController.cs
File metadata and controls
142 lines (125 loc) · 4.03 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Blog.Core.Common.HttpContextUser;
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
{
/// <summary>
/// 接口管理
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class ModuleController : ControllerBase
{
readonly IModuleServices _moduleServices;
readonly IUser _user;
public ModuleController(IModuleServices moduleServices, IUser user)
{
_moduleServices = moduleServices;
_user = user;
}
/// <summary>
/// 获取全部接口api
/// </summary>
/// <param name="page"></param>
/// <param name="key"></param>
/// <returns></returns>
// GET: api/User
[HttpGet]
public async Task<MessageModel<PageModel<Module>>> Get(int page = 1, string key = "")
{
if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
{
key = "";
}
int intPageSize = 50;
Expression<Func<Module, bool>> whereExpression = a => a.IsDeleted != true && (a.Name != null && a.Name.Contains(key));
var data = await _moduleServices.QueryPage(whereExpression, page, intPageSize, " Id desc ");
return new MessageModel<PageModel<Module>>()
{
msg = "获取成功",
success = data.dataCount >= 0,
response = data
};
}
// GET: api/User/5
[HttpGet("{id}")]
public string Get(string id)
{
return "value";
}
/// <summary>
/// 添加一条接口信息
/// </summary>
/// <param name="module"></param>
/// <returns></returns>
// POST: api/User
[HttpPost]
public async Task<MessageModel<string>> Post([FromBody] Module module)
{
var data = new MessageModel<string>();
module.CreateId = _user.ID;
module.CreateBy = _user.Name;
var id = (await _moduleServices.Add(module));
data.success = id > 0;
if (data.success)
{
data.response = id.ObjToString();
data.msg = "添加成功";
}
return data;
}
/// <summary>
/// 更新接口信息
/// </summary>
/// <param name="module"></param>
/// <returns></returns>
// PUT: api/User/5
[HttpPut]
public async Task<MessageModel<string>> Put([FromBody] Module module)
{
var data = new MessageModel<string>();
if (module != null && module.Id > 0)
{
data.success = await _moduleServices.Update(module);
if (data.success)
{
data.msg = "更新成功";
data.response = module?.Id.ObjToString();
}
}
return data;
}
/// <summary>
/// 删除一条接口
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
// DELETE: api/ApiWithActions/5
[HttpDelete]
public async Task<MessageModel<string>> Delete(int id)
{
var data = new MessageModel<string>();
if (id > 0)
{
var userDetail = await _moduleServices.QueryById(id);
userDetail.IsDeleted = true;
data.success = await _moduleServices.Update(userDetail);
if (data.success)
{
data.msg = "删除成功";
data.response = userDetail?.Id.ObjToString();
}
}
return data;
}
}
}