Skip to content

增加“ApiJson.Common”项目,主要的解析和执行移动到此项目 #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
添加简化的API
  • Loading branch information
zhuzhiqing committed Jan 18, 2019
commit 3ad195dfa34cf62d9541560f1c58b8dfb953f663
19 changes: 17 additions & 2 deletions APIJSON.NET/APIJSON.NET/APIJSON.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Remove="FuncList.cs" />
<Compile Remove="Infrastructure\StringExtensions.cs" />
<Compile Remove="Models\DbOptions.cs" />
<Compile Remove="Models\RoleItem.cs" />
<Compile Remove="SelectTable.cs" />
<Compile Remove="Services\IIdentityService.cs" />
<Compile Remove="Services\ITableMapper.cs" />
<Compile Remove="Services\TableMapper.cs" />
</ItemGroup>

<ItemGroup>
<None Remove="Dockerfile" />
</ItemGroup>
Expand All @@ -16,7 +27,7 @@

<ItemGroup>
<PackageReference Include="AspectCore.Extensions.Reflection" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.App"/>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Rewrite" Version="2.1.1" />
Expand All @@ -26,12 +37,16 @@
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="2.18.3" />
<PackageReference Include="SqlKata" Version="1.1.3" />
<PackageReference Include="SqlKata.Execution" Version="1.1.3" />
<PackageReference Include="sqlSugarCore" Version="4.9.3" />
<PackageReference Include="sqlSugarCore" Version="4.9.7.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="3.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="3.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\APIJSONCommon\ApiJson.Common.csproj" />
</ItemGroup>

<ProjectExtensions><VisualStudio><UserProperties appsettings_1json__JSONSchema="http://json.schemastore.org/config" /></VisualStudio></ProjectExtensions>

</Project>
177 changes: 23 additions & 154 deletions APIJSON.NET/APIJSON.NET/Controllers/JsonController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace APIJSON.NET.Controllers
{
using APIJSON.NET.Services;
using ApiJson.Common;
using ApiJson.Common.Services;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
Expand All @@ -20,15 +21,16 @@
public class JsonController : ControllerBase
{

private SelectTable selectTable;
private DbContext db;
private SelectTable _selectTable;
private DbContext _db;
private readonly IIdentityService _identitySvc;
public JsonController(SelectTable _selectTable, DbContext _db, IIdentityService identityService)
private ITableMapper _tableMapper;
public JsonController(ITableMapper tableMapper, DbContext db, IIdentityService identityService)
{

selectTable = _selectTable;
db = _db;
_db = db;
_identitySvc = identityService;
_tableMapper = tableMapper;
_selectTable = new SelectTable(_identitySvc, _tableMapper, _db.Db);
}

/// <summary>
Expand All @@ -39,7 +41,7 @@ public ActionResult Test()
{
string str = "{\"page\":1,\"count\":3,\"query\":2,\"Org\":{\"@column\":\"Id,Name\"}}";
var content = new StringContent(str);

HttpClient hc = new HttpClient();
var response = hc.PostAsync("http://localhost:89/api/json/org", content).Result;
string result = (response.Content.ReadAsStringAsync().Result);//result就是返回的结果。
Expand All @@ -50,11 +52,10 @@ public ActionResult Test()

public async Task<ActionResult> Query1([FromRoute]string table)
{

string json = string.Empty;
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
{
json= await reader.ReadToEndAsync();
json = await reader.ReadToEndAsync();
}

json = HttpUtility.UrlDecode(json);
Expand All @@ -75,11 +76,13 @@ public async Task<ActionResult> Query1([FromRoute]string table)
}
if (!hasTableKey)
{
jobject.Add(table,new JObject());
jobject.Add(table, new JObject());
}
var newJson = Newtonsoft.Json.JsonConvert.SerializeObject(ht);
return Query(newJson);

JObject resultJobj = new SelectTable(_identitySvc, _tableMapper, _db.Db).Query(ht);
return Ok(resultJobj);
}

/// <summary>
/// 查询
/// </summary>
Expand All @@ -90,142 +93,8 @@ public async Task<ActionResult> Query1([FromRoute]string table)
public ActionResult Query([FromBody]string json)
{
json = HttpUtility.UrlDecode(json);
JObject ht = new JObject();
ht.Add("code", "200");
ht.Add("msg", "success");
try
{
JObject jobject = JObject.Parse(json);
int page = 0, count = 0, query = 0, total = 0;
foreach (var item in jobject)
{
string key = item.Key.Trim();
JObject jb;
if (key.Equals("[]"))
{
jb = JObject.Parse(item.Value.ToString());
page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
count = jb["count"] == null ? 0 : int.Parse(jb["count"].ToString());
query = jb["query"] == null ? 0 : int.Parse(jb["query"].ToString());
jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
var htt = new JArray();
List<string> tables = new List<string>(), where = new List<string>();
foreach (var t in jb)
{
tables.Add(t.Key); where.Add(t.Value.ToString());
}
if (tables.Count > 0)
{
string table = tables[0];
var temp = selectTable.GetTableData(table, page, count, where[0], null);
if (query > 0)
{
total = temp.Item2;
}

foreach (var dd in temp.Item1)
{
var zht = new JObject();
zht.Add(table, JToken.FromObject(dd));
for (int i = 1; i < tables.Count; i++)
{
string subtable = tables[i];
if (subtable.EndsWith("[]"))
{
subtable = subtable.TrimEnd("[]".ToCharArray());
var jbb = JObject.Parse(where[i]);
page = jbb["page"] == null ? 0 : int.Parse(jbb["page"].ToString());
count = jbb["count"] == null ? 0 : int.Parse(jbb["count"].ToString());

var lt = new JArray();
foreach (var d in selectTable.GetTableData(subtable, page, count, jbb[subtable].ToString(), zht).Item1)
{
lt.Add(JToken.FromObject(d));
}
zht.Add(tables[i], lt);
}
else
{
var ddf = selectTable.GetFirstData(subtable, where[i].ToString(), zht);
if (ddf != null)
{
zht.Add(subtable, JToken.FromObject(ddf));

}
}
}
htt.Add(zht);
}

}
if (query != 1)
{
ht.Add("[]", htt);
}
}
else if (key.EndsWith("[]"))
{
jb = JObject.Parse(item.Value.ToString());
page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
count = jb["count"] == null ? 0 : int.Parse(jb["count"].ToString());
query = jb["query"] == null ? 0 : int.Parse(jb["query"].ToString());
jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
var htt = new JArray();
foreach (var t in jb)
{
var temp = selectTable.GetTableData(t.Key, page, count, t.Value.ToString(), null);
if (query > 0)
{
total = temp.Item2;
}
foreach (var d in temp.Item1)
{
htt.Add(JToken.FromObject(d));
}
}
ht.Add(key, htt);
}
else if (key.Equals("func"))
{
jb = JObject.Parse(item.Value.ToString());
Type type = typeof(FuncList);
Object obj = Activator.CreateInstance(type);
var bb = new JObject();
foreach (var f in jb)
{
var types = new List<Type>();
var param = new List<object>();
foreach (var va in JArray.Parse(f.Value.ToString()))
{
types.Add(typeof(object));
param.Add(va);
}
bb.Add(f.Key, JToken.FromObject(selectTable.ExecFunc(f.Key, param.ToArray(), types.ToArray())));
}
ht.Add("func", bb);
}
else if (key.Equals("total@"))
{
ht.Add("total", total);
}
else
{
var template = selectTable.GetFirstData(key, item.Value.ToString(), ht);
if (template != null)
{
ht.Add(key, JToken.FromObject(template));
}
}

}
}
catch (Exception ex)
{
ht["code"] = "500";
ht["msg"] = ex.Message;

}
return Ok(ht);
JObject resultJobj = new SelectTable(_identitySvc, _tableMapper, _db.Db).Query(json);
return Ok(resultJobj);
}
/// <summary>
/// 新增
Expand Down Expand Up @@ -257,10 +126,10 @@ public ActionResult Add([FromBody]string json)
var dt = new Dictionary<string, object>();
foreach (var f in JObject.Parse(item.Value.ToString()))
{
if (f.Key.ToLower() != "id" && selectTable.IsCol(key, f.Key) && (role.Insert.Column.Contains("*") || role.Insert.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
if (f.Key.ToLower() != "id" && _selectTable.IsCol(key, f.Key) && (role.Insert.Column.Contains("*") || role.Insert.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
dt.Add(f.Key, f.Value);
}
int id = db.Db.Insertable(dt).AS(key).ExecuteReturnIdentity();
int id = _db.Db.Insertable(dt).AS(key).ExecuteReturnIdentity();
ht.Add(key, JToken.FromObject(new { code = 200, msg = "success", id }));
}
}
Expand Down Expand Up @@ -309,12 +178,12 @@ public ActionResult Edit([FromBody]string json)
dt.Add("id", value["id"].ToString());
foreach (var f in value)
{
if (f.Key.ToLower() != "id" && selectTable.IsCol(key, f.Key) && (role.Update.Column.Contains("*") || role.Update.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
if (f.Key.ToLower() != "id" && _selectTable.IsCol(key, f.Key) && (role.Update.Column.Contains("*") || role.Update.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
{
dt.Add(f.Key, f.Value);
}
}
db.Db.Updateable(dt).AS(key).ExecuteCommand();
_db.Db.Updateable(dt).AS(key).ExecuteCommand();
ht.Add(key, JToken.FromObject(new { code = 200, msg = "success", id = value["id"].ToString() }));
}
}
Expand Down Expand Up @@ -373,7 +242,7 @@ public ActionResult Remove([FromBody]string json)
p.Add(new SugarParameter($"@{f.Key}", f.Value.ToString()));
}
string sql = sb.ToString().TrimEnd(',');
db.Db.Ado.ExecuteCommand(sql, p);
_db.Db.Ado.ExecuteCommand(sql, p);
ht.Add(key, JToken.FromObject(new { code = 200, msg = "success", id = value["id"].ToString() }));

}
Expand Down
4 changes: 3 additions & 1 deletion APIJSON.NET/APIJSON.NET/Services/IdentityService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using APIJSON.NET.Models;
using ApiJson.Common.Models;
using ApiJson.Common.Services;
using APIJSON.NET.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using System;
Expand Down
34 changes: 16 additions & 18 deletions APIJSON.NET/APIJSON.NET/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace APIJSON.NET
{
using System;
using System.Collections.Generic;
using System.Text;
using APIJSON.NET.Models;
using ApiJson.Common;
using ApiJson.Common.Models;
using ApiJson.Common.Services;
using APIJSON.NET.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -12,10 +11,10 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using SqlKata.Execution;
using Swashbuckle.AspNetCore.Swagger;
using MySql.Data.MySqlClient;
using SqlKata.Compilers;
using System;
using System.Collections.Generic;
using System.Text;

public class Startup
{
Expand All @@ -30,9 +29,9 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{

services.Configure<List<Role>>(Configuration.GetSection("RoleList"));
services.Configure<Dictionary<string,string>>(Configuration.GetSection("tablempper"));
services.Configure<Dictionary<string, string>>(Configuration.GetSection("tablempper"));
services.Configure<TokenAuthConfiguration>(tokenAuthConfig =>
{
tokenAuthConfig.SecurityKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration["Authentication:JwtBearer:SecurityKey"]));
Expand All @@ -43,19 +42,18 @@ public void ConfigureServices(IServiceCollection services)
});
AuthConfigurer.Configure(services, Configuration);

services.AddCors( options => options.AddPolicy( _defaultCorsPolicyName,
builder =>
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod().AllowCredentials()
));
services.AddCors(options => options.AddPolicy(_defaultCorsPolicyName,
builder =>
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod().AllowCredentials()
));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "APIJSON.NET", Version = "v1" });
});
services.AddSingleton<DbContext>();
services.AddSingleton<SelectTable>();
services.AddSingleton<TokenAuthConfiguration>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddTransient<IIdentityService, IdentityService>();
Expand All @@ -81,9 +79,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");

});

app.UseJwtTokenMiddleware();
}
}
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy