Skip to content

Commit 3ad195d

Browse files
author
zhuzhiqing
committed
添加简化的API
1 parent 527b941 commit 3ad195d

File tree

5 files changed

+82
-175
lines changed

5 files changed

+82
-175
lines changed

APIJSON.NET/APIJSON.NET/APIJSON.NET.csproj

+17-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
<TargetFramework>netcoreapp2.1</TargetFramework>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<Compile Remove="FuncList.cs" />
9+
<Compile Remove="Infrastructure\StringExtensions.cs" />
10+
<Compile Remove="Models\DbOptions.cs" />
11+
<Compile Remove="Models\RoleItem.cs" />
12+
<Compile Remove="SelectTable.cs" />
13+
<Compile Remove="Services\IIdentityService.cs" />
14+
<Compile Remove="Services\ITableMapper.cs" />
15+
<Compile Remove="Services\TableMapper.cs" />
16+
</ItemGroup>
17+
718
<ItemGroup>
819
<None Remove="Dockerfile" />
920
</ItemGroup>
@@ -16,7 +27,7 @@
1627

1728
<ItemGroup>
1829
<PackageReference Include="AspectCore.Extensions.Reflection" Version="1.1.0" />
19-
<PackageReference Include="Microsoft.AspNetCore.App"/>
30+
<PackageReference Include="Microsoft.AspNetCore.App" />
2031
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.1.2" />
2132
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.1.1" />
2233
<PackageReference Include="Microsoft.AspNetCore.Rewrite" Version="2.1.1" />
@@ -26,12 +37,16 @@
2637
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="2.18.3" />
2738
<PackageReference Include="SqlKata" Version="1.1.3" />
2839
<PackageReference Include="SqlKata.Execution" Version="1.1.3" />
29-
<PackageReference Include="sqlSugarCore" Version="4.9.3" />
40+
<PackageReference Include="sqlSugarCore" Version="4.9.7.2" />
3041
<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0" />
3142
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="3.0.0" />
3243
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="3.0.0" />
3344
</ItemGroup>
3445

46+
<ItemGroup>
47+
<ProjectReference Include="..\APIJSONCommon\ApiJson.Common.csproj" />
48+
</ItemGroup>
49+
3550
<ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JSONSchema="http://json.schemastore.org/config" /></VisualStudio></ProjectExtensions>
3651

3752
</Project>

APIJSON.NET/APIJSON.NET/Controllers/JsonController.cs

+23-154
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace APIJSON.NET.Controllers
22
{
3-
using APIJSON.NET.Services;
3+
using ApiJson.Common;
4+
using ApiJson.Common.Services;
45
using Microsoft.AspNetCore.Cors;
56
using Microsoft.AspNetCore.Mvc;
67
using Newtonsoft.Json.Linq;
@@ -20,15 +21,16 @@
2021
public class JsonController : ControllerBase
2122
{
2223

23-
private SelectTable selectTable;
24-
private DbContext db;
24+
private SelectTable _selectTable;
25+
private DbContext _db;
2526
private readonly IIdentityService _identitySvc;
26-
public JsonController(SelectTable _selectTable, DbContext _db, IIdentityService identityService)
27+
private ITableMapper _tableMapper;
28+
public JsonController(ITableMapper tableMapper, DbContext db, IIdentityService identityService)
2729
{
28-
29-
selectTable = _selectTable;
30-
db = _db;
30+
_db = db;
3131
_identitySvc = identityService;
32+
_tableMapper = tableMapper;
33+
_selectTable = new SelectTable(_identitySvc, _tableMapper, _db.Db);
3234
}
3335

3436
/// <summary>
@@ -39,7 +41,7 @@ public ActionResult Test()
3941
{
4042
string str = "{\"page\":1,\"count\":3,\"query\":2,\"Org\":{\"@column\":\"Id,Name\"}}";
4143
var content = new StringContent(str);
42-
44+
4345
HttpClient hc = new HttpClient();
4446
var response = hc.PostAsync("http://localhost:89/api/json/org", content).Result;
4547
string result = (response.Content.ReadAsStringAsync().Result);//result就是返回的结果。
@@ -50,11 +52,10 @@ public ActionResult Test()
5052

5153
public async Task<ActionResult> Query1([FromRoute]string table)
5254
{
53-
5455
string json = string.Empty;
5556
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
5657
{
57-
json= await reader.ReadToEndAsync();
58+
json = await reader.ReadToEndAsync();
5859
}
5960

6061
json = HttpUtility.UrlDecode(json);
@@ -75,11 +76,13 @@ public async Task<ActionResult> Query1([FromRoute]string table)
7576
}
7677
if (!hasTableKey)
7778
{
78-
jobject.Add(table,new JObject());
79+
jobject.Add(table, new JObject());
7980
}
80-
var newJson = Newtonsoft.Json.JsonConvert.SerializeObject(ht);
81-
return Query(newJson);
81+
82+
JObject resultJobj = new SelectTable(_identitySvc, _tableMapper, _db.Db).Query(ht);
83+
return Ok(resultJobj);
8284
}
85+
8386
/// <summary>
8487
/// 查询
8588
/// </summary>
@@ -90,142 +93,8 @@ public async Task<ActionResult> Query1([FromRoute]string table)
9093
public ActionResult Query([FromBody]string json)
9194
{
9295
json = HttpUtility.UrlDecode(json);
93-
JObject ht = new JObject();
94-
ht.Add("code", "200");
95-
ht.Add("msg", "success");
96-
try
97-
{
98-
JObject jobject = JObject.Parse(json);
99-
int page = 0, count = 0, query = 0, total = 0;
100-
foreach (var item in jobject)
101-
{
102-
string key = item.Key.Trim();
103-
JObject jb;
104-
if (key.Equals("[]"))
105-
{
106-
jb = JObject.Parse(item.Value.ToString());
107-
page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
108-
count = jb["count"] == null ? 0 : int.Parse(jb["count"].ToString());
109-
query = jb["query"] == null ? 0 : int.Parse(jb["query"].ToString());
110-
jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
111-
var htt = new JArray();
112-
List<string> tables = new List<string>(), where = new List<string>();
113-
foreach (var t in jb)
114-
{
115-
tables.Add(t.Key); where.Add(t.Value.ToString());
116-
}
117-
if (tables.Count > 0)
118-
{
119-
string table = tables[0];
120-
var temp = selectTable.GetTableData(table, page, count, where[0], null);
121-
if (query > 0)
122-
{
123-
total = temp.Item2;
124-
}
125-
126-
foreach (var dd in temp.Item1)
127-
{
128-
var zht = new JObject();
129-
zht.Add(table, JToken.FromObject(dd));
130-
for (int i = 1; i < tables.Count; i++)
131-
{
132-
string subtable = tables[i];
133-
if (subtable.EndsWith("[]"))
134-
{
135-
subtable = subtable.TrimEnd("[]".ToCharArray());
136-
var jbb = JObject.Parse(where[i]);
137-
page = jbb["page"] == null ? 0 : int.Parse(jbb["page"].ToString());
138-
count = jbb["count"] == null ? 0 : int.Parse(jbb["count"].ToString());
139-
140-
var lt = new JArray();
141-
foreach (var d in selectTable.GetTableData(subtable, page, count, jbb[subtable].ToString(), zht).Item1)
142-
{
143-
lt.Add(JToken.FromObject(d));
144-
}
145-
zht.Add(tables[i], lt);
146-
}
147-
else
148-
{
149-
var ddf = selectTable.GetFirstData(subtable, where[i].ToString(), zht);
150-
if (ddf != null)
151-
{
152-
zht.Add(subtable, JToken.FromObject(ddf));
153-
154-
}
155-
}
156-
}
157-
htt.Add(zht);
158-
}
159-
160-
}
161-
if (query != 1)
162-
{
163-
ht.Add("[]", htt);
164-
}
165-
}
166-
else if (key.EndsWith("[]"))
167-
{
168-
jb = JObject.Parse(item.Value.ToString());
169-
page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
170-
count = jb["count"] == null ? 0 : int.Parse(jb["count"].ToString());
171-
query = jb["query"] == null ? 0 : int.Parse(jb["query"].ToString());
172-
jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
173-
var htt = new JArray();
174-
foreach (var t in jb)
175-
{
176-
var temp = selectTable.GetTableData(t.Key, page, count, t.Value.ToString(), null);
177-
if (query > 0)
178-
{
179-
total = temp.Item2;
180-
}
181-
foreach (var d in temp.Item1)
182-
{
183-
htt.Add(JToken.FromObject(d));
184-
}
185-
}
186-
ht.Add(key, htt);
187-
}
188-
else if (key.Equals("func"))
189-
{
190-
jb = JObject.Parse(item.Value.ToString());
191-
Type type = typeof(FuncList);
192-
Object obj = Activator.CreateInstance(type);
193-
var bb = new JObject();
194-
foreach (var f in jb)
195-
{
196-
var types = new List<Type>();
197-
var param = new List<object>();
198-
foreach (var va in JArray.Parse(f.Value.ToString()))
199-
{
200-
types.Add(typeof(object));
201-
param.Add(va);
202-
}
203-
bb.Add(f.Key, JToken.FromObject(selectTable.ExecFunc(f.Key, param.ToArray(), types.ToArray())));
204-
}
205-
ht.Add("func", bb);
206-
}
207-
else if (key.Equals("total@"))
208-
{
209-
ht.Add("total", total);
210-
}
211-
else
212-
{
213-
var template = selectTable.GetFirstData(key, item.Value.ToString(), ht);
214-
if (template != null)
215-
{
216-
ht.Add(key, JToken.FromObject(template));
217-
}
218-
}
219-
220-
}
221-
}
222-
catch (Exception ex)
223-
{
224-
ht["code"] = "500";
225-
ht["msg"] = ex.Message;
226-
227-
}
228-
return Ok(ht);
96+
JObject resultJobj = new SelectTable(_identitySvc, _tableMapper, _db.Db).Query(json);
97+
return Ok(resultJobj);
22998
}
23099
/// <summary>
231100
/// 新增
@@ -257,10 +126,10 @@ public ActionResult Add([FromBody]string json)
257126
var dt = new Dictionary<string, object>();
258127
foreach (var f in JObject.Parse(item.Value.ToString()))
259128
{
260-
if (f.Key.ToLower() != "id" && selectTable.IsCol(key, f.Key) && (role.Insert.Column.Contains("*") || role.Insert.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
129+
if (f.Key.ToLower() != "id" && _selectTable.IsCol(key, f.Key) && (role.Insert.Column.Contains("*") || role.Insert.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
261130
dt.Add(f.Key, f.Value);
262131
}
263-
int id = db.Db.Insertable(dt).AS(key).ExecuteReturnIdentity();
132+
int id = _db.Db.Insertable(dt).AS(key).ExecuteReturnIdentity();
264133
ht.Add(key, JToken.FromObject(new { code = 200, msg = "success", id }));
265134
}
266135
}
@@ -309,12 +178,12 @@ public ActionResult Edit([FromBody]string json)
309178
dt.Add("id", value["id"].ToString());
310179
foreach (var f in value)
311180
{
312-
if (f.Key.ToLower() != "id" && selectTable.IsCol(key, f.Key) && (role.Update.Column.Contains("*") || role.Update.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
181+
if (f.Key.ToLower() != "id" && _selectTable.IsCol(key, f.Key) && (role.Update.Column.Contains("*") || role.Update.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
313182
{
314183
dt.Add(f.Key, f.Value);
315184
}
316185
}
317-
db.Db.Updateable(dt).AS(key).ExecuteCommand();
186+
_db.Db.Updateable(dt).AS(key).ExecuteCommand();
318187
ht.Add(key, JToken.FromObject(new { code = 200, msg = "success", id = value["id"].ToString() }));
319188
}
320189
}
@@ -373,7 +242,7 @@ public ActionResult Remove([FromBody]string json)
373242
p.Add(new SugarParameter($"@{f.Key}", f.Value.ToString()));
374243
}
375244
string sql = sb.ToString().TrimEnd(',');
376-
db.Db.Ado.ExecuteCommand(sql, p);
245+
_db.Db.Ado.ExecuteCommand(sql, p);
377246
ht.Add(key, JToken.FromObject(new { code = 200, msg = "success", id = value["id"].ToString() }));
378247

379248
}

APIJSON.NET/APIJSON.NET/Services/IdentityService.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using APIJSON.NET.Models;
1+
using ApiJson.Common.Models;
2+
using ApiJson.Common.Services;
3+
using APIJSON.NET.Models;
24
using Microsoft.AspNetCore.Http;
35
using Microsoft.Extensions.Options;
46
using System;

APIJSON.NET/APIJSON.NET/Startup.cs

+16-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
namespace APIJSON.NET
22
{
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Text;
6-
using APIJSON.NET.Models;
3+
using ApiJson.Common;
4+
using ApiJson.Common.Models;
5+
using ApiJson.Common.Services;
76
using APIJSON.NET.Services;
87
using Microsoft.AspNetCore.Builder;
98
using Microsoft.AspNetCore.Hosting;
@@ -12,10 +11,10 @@
1211
using Microsoft.Extensions.Configuration;
1312
using Microsoft.Extensions.DependencyInjection;
1413
using Microsoft.IdentityModel.Tokens;
15-
using SqlKata.Execution;
1614
using Swashbuckle.AspNetCore.Swagger;
17-
using MySql.Data.MySqlClient;
18-
using SqlKata.Compilers;
15+
using System;
16+
using System.Collections.Generic;
17+
using System.Text;
1918

2019
public class Startup
2120
{
@@ -30,9 +29,9 @@ public Startup(IConfiguration configuration)
3029
// This method gets called by the runtime. Use this method to add services to the container.
3130
public void ConfigureServices(IServiceCollection services)
3231
{
33-
32+
3433
services.Configure<List<Role>>(Configuration.GetSection("RoleList"));
35-
services.Configure<Dictionary<string,string>>(Configuration.GetSection("tablempper"));
34+
services.Configure<Dictionary<string, string>>(Configuration.GetSection("tablempper"));
3635
services.Configure<TokenAuthConfiguration>(tokenAuthConfig =>
3736
{
3837
tokenAuthConfig.SecurityKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration["Authentication:JwtBearer:SecurityKey"]));
@@ -43,19 +42,18 @@ public void ConfigureServices(IServiceCollection services)
4342
});
4443
AuthConfigurer.Configure(services, Configuration);
4544

46-
services.AddCors( options => options.AddPolicy( _defaultCorsPolicyName,
47-
builder =>
48-
builder.AllowAnyOrigin()
49-
.AllowAnyHeader()
50-
.AllowAnyMethod().AllowCredentials()
51-
));
45+
services.AddCors(options => options.AddPolicy(_defaultCorsPolicyName,
46+
builder =>
47+
builder.AllowAnyOrigin()
48+
.AllowAnyHeader()
49+
.AllowAnyMethod().AllowCredentials()
50+
));
5251
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
5352
services.AddSwaggerGen(c =>
5453
{
5554
c.SwaggerDoc("v1", new Info { Title = "APIJSON.NET", Version = "v1" });
5655
});
5756
services.AddSingleton<DbContext>();
58-
services.AddSingleton<SelectTable>();
5957
services.AddSingleton<TokenAuthConfiguration>();
6058
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
6159
services.AddTransient<IIdentityService, IdentityService>();
@@ -81,9 +79,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
8179
app.UseSwaggerUI(c =>
8280
{
8381
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
84-
82+
8583
});
86-
84+
8785
app.UseJwtTokenMiddleware();
8886
}
8987
}

0 commit comments

Comments
 (0)