Skip to content

Commit 1db131c

Browse files
authored
Merge pull request #16 from Research-Institute/v0.1.4
V0.1.4-beta
2 parents 5f94cfa + c60efb4 commit 1db131c

File tree

13 files changed

+181
-157
lines changed

13 files changed

+181
-157
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
using System;
2-
using AutoMapper;
32
using JsonApiDotNetCore.Configuration;
43
using JsonApiDotNetCore.Controllers;
54
using JsonApiDotNetCore.Routing;
65
using Microsoft.Extensions.DependencyInjection;
76

87
namespace JsonApiDotNetCore.Extensions
98
{
10-
public static class IServiceCollectionExtensions
11-
{
12-
public static void AddJsonApi(this IServiceCollection services, Action<IJsonApiModelConfiguration> configurationAction)
9+
public static class IServiceCollectionExtensions
1310
{
14-
var configBuilder = new JsonApiConfigurationBuilder(configurationAction);
15-
var config = configBuilder.Build();
16-
IRouter router = new Router(config, new RouteBuilder(config), new ControllerBuilder());
17-
services.AddSingleton(_ => router);
11+
public static void AddJsonApi(this IServiceCollection services, Action<IJsonApiModelConfiguration> configurationAction)
12+
{
13+
var configBuilder = new JsonApiConfigurationBuilder(configurationAction);
14+
var config = configBuilder.Build();
15+
services.AddTransient(_ =>
16+
{
17+
return (IRouter)new Router(config, new RouteBuilder(config), new ControllerBuilder());
18+
});
19+
}
1820
}
19-
}
2021
}

JsonApiDotNetCore/Extensions/StringExtensions.cs

+13-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,19 @@ public static string ToProperCase(this string str)
2121
var chars = str.ToCharArray();
2222
if (chars.Length > 0)
2323
{
24-
chars[0] = new string(chars[0], 1).ToUpper().ToCharArray()[0];
25-
return new String(chars);
24+
chars[0] = char.ToUpper(chars[0]);
25+
var builder = new StringBuilder();
26+
for(var i = 0; i < chars.Length; i++)
27+
{
28+
if((chars[i]) == '-') {
29+
i = i + 1;
30+
builder.Append(char.ToUpper(chars[i]));
31+
}
32+
else {
33+
builder.Append(chars[i]);
34+
}
35+
}
36+
return builder.ToString();
2637
}
2738
return str;
2839
}

JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public async Task Invoke(HttpContext context)
3030
_logger.LogInformation("Passing request to JsonApiService: " + context.Request.Path);
3131

3232
if(IsJsonApiRequest(context)) {
33-
var routeWasHandled = _router.HandleJsonApiRoute(context, _serviceProvider);
33+
var routeWasHandled = await _router.HandleJsonApiRouteAsync(context, _serviceProvider);
3434
if(!routeWasHandled)
3535
RespondNotFound(context);
3636
}

JsonApiDotNetCore/Routing/IRouter.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System;
2+
using System.Threading.Tasks;
23
using Microsoft.AspNetCore.Http;
34

45
namespace JsonApiDotNetCore.Routing
56
{
67
public interface IRouter
78
{
8-
bool HandleJsonApiRoute(HttpContext context, IServiceProvider serviceProvider);
9+
Task<bool> HandleJsonApiRouteAsync(HttpContext context, IServiceProvider serviceProvider);
910
}
1011
}

JsonApiDotNetCore/Routing/Router.cs

+62-64
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,88 @@
11
using System;
22
using System.Text;
3+
using System.Threading.Tasks;
34
using JsonApiDotNetCore.Abstractions;
45
using JsonApiDotNetCore.Configuration;
56
using JsonApiDotNetCore.Controllers;
67
using JsonApiDotNetCore.Services;
78
using Microsoft.AspNetCore.Http;
89
using Microsoft.AspNetCore.Mvc;
9-
using Newtonsoft.Json;
1010

1111
namespace JsonApiDotNetCore.Routing
1212
{
13-
public class Router : IRouter
14-
{
15-
private readonly JsonApiModelConfiguration _jsonApiModelConfiguration;
16-
private IServiceProvider _serviceProvider;
17-
private JsonApiContext _jsonApiContext;
18-
private IRouteBuilder _routeBuilder;
19-
private IControllerBuilder _controllerBuilder;
20-
21-
public Router(JsonApiModelConfiguration configuration, IRouteBuilder routeBuilder, IControllerBuilder controllerBuilder)
13+
public class Router : IRouter
2214
{
23-
_jsonApiModelConfiguration = configuration;
24-
_routeBuilder = routeBuilder;
25-
_controllerBuilder = controllerBuilder;
26-
}
15+
private readonly JsonApiModelConfiguration _jsonApiModelConfiguration;
16+
private IServiceProvider _serviceProvider;
17+
private IRouteBuilder _routeBuilder;
18+
private IControllerBuilder _controllerBuilder;
2719

28-
public bool HandleJsonApiRoute(HttpContext context, IServiceProvider serviceProvider)
29-
{
30-
_serviceProvider = serviceProvider;
20+
public Router(JsonApiModelConfiguration configuration, IRouteBuilder routeBuilder, IControllerBuilder controllerBuilder)
21+
{
22+
_jsonApiModelConfiguration = configuration;
23+
_routeBuilder = routeBuilder;
24+
_controllerBuilder = controllerBuilder;
25+
}
3126

32-
var route = _routeBuilder.BuildFromRequest(context.Request);
33-
if (route == null) return false;
27+
public async Task<bool> HandleJsonApiRouteAsync(HttpContext context, IServiceProvider serviceProvider)
28+
{
29+
_serviceProvider = serviceProvider;
3430

35-
InitializeContext(context, route);
36-
CallController();
31+
var route = _routeBuilder.BuildFromRequest(context.Request);
32+
if (route == null) return false;
3733

38-
return true;
39-
}
34+
var jsonApiContext = InitializeContext(context, route);
35+
await CallController(jsonApiContext);
4036

41-
private void InitializeContext(HttpContext context, Route route)
42-
{
43-
var dbContext = _serviceProvider.GetService(_jsonApiModelConfiguration.ContextType);
44-
_jsonApiContext = new JsonApiContext(context, route, dbContext, _jsonApiModelConfiguration);
45-
}
37+
return true;
38+
}
4639

47-
private void CallController()
48-
{
49-
var controller = _controllerBuilder.BuildController(_jsonApiContext);
40+
private JsonApiContext InitializeContext(HttpContext context, Route route)
41+
{
42+
var dbContext = _serviceProvider.GetService(_jsonApiModelConfiguration.ContextType);
43+
Console.WriteLine("InitializingContext");
44+
return new JsonApiContext(context, route, dbContext, _jsonApiModelConfiguration);
45+
}
5046

51-
var result = ActivateControllerMethod(controller);
47+
private async Task CallController(JsonApiContext jsonApiContext)
48+
{
49+
var controller = _controllerBuilder.BuildController(jsonApiContext);
5250

53-
result.Value = SerializeResult(result.Value);
51+
var result = ActivateControllerMethod(controller, jsonApiContext);
5452

55-
SendResponse(result);
56-
}
53+
result.Value = SerializeResult(result.Value, jsonApiContext);
5754

58-
private ObjectResult ActivateControllerMethod(IJsonApiController controller)
59-
{
60-
var route = _jsonApiContext.Route;
61-
switch (route.RequestMethod)
62-
{
63-
case "GET":
64-
return string.IsNullOrEmpty(route.ResourceId) ? controller.Get() : controller.Get(route.ResourceId);
65-
case "POST":
66-
return controller.Post(new JsonApiDeserializer(_jsonApiContext).GetEntityFromRequest());
67-
case "PATCH":
68-
return controller.Patch(route.ResourceId, new JsonApiDeserializer(_jsonApiContext).GetEntityPatch());
69-
case "DELETE":
70-
return controller.Delete(route.ResourceId);
71-
default:
72-
throw new ArgumentException("Request method not supported", nameof(route));
73-
}
74-
}
55+
await SendResponse(jsonApiContext.HttpContext, result);
56+
}
7557

76-
private object SerializeResult(object result)
77-
{
78-
return result == null ? null : new JsonApiSerializer(_jsonApiContext).ToJsonApiDocument(result);
79-
}
58+
private ObjectResult ActivateControllerMethod(IJsonApiController controller, JsonApiContext jsonApiContext)
59+
{
60+
var route = jsonApiContext.Route;
61+
switch (route.RequestMethod)
62+
{
63+
case "GET":
64+
return string.IsNullOrEmpty(route.ResourceId) ? controller.Get() : controller.Get(route.ResourceId);
65+
case "POST":
66+
return controller.Post(new JsonApiDeserializer(jsonApiContext).GetEntityFromRequest());
67+
case "PATCH":
68+
return controller.Patch(route.ResourceId, new JsonApiDeserializer(jsonApiContext).GetEntityPatch());
69+
case "DELETE":
70+
return controller.Delete(route.ResourceId);
71+
default:
72+
throw new ArgumentException("Request method not supported", nameof(route));
73+
}
74+
}
8075

81-
private void SendResponse(ObjectResult result)
82-
{
83-
var context = _jsonApiContext.HttpContext;
84-
context.Response.StatusCode = result.StatusCode ?? 500;
85-
context.Response.ContentType = "application/vnd.api+json";
86-
context.Response.WriteAsync(result.Value == null ? "" : result.Value.ToString(), Encoding.UTF8);
87-
context.Response.Body.Flush();
76+
private object SerializeResult(object result, JsonApiContext jsonApiContext)
77+
{
78+
return result == null ? null : new JsonApiSerializer(jsonApiContext).ToJsonApiDocument(result);
79+
}
80+
81+
private async Task SendResponse(HttpContext context, ObjectResult result)
82+
{
83+
context.Response.StatusCode = result.StatusCode ?? 500;
84+
context.Response.ContentType = "application/vnd.api+json";
85+
await context.Response.WriteAsync(result.Value == null ? "" : result.Value.ToString(), Encoding.UTF8);
86+
}
8887
}
89-
}
9088
}

JsonApiDotNetCore/project.json

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
{
2-
"version": "0.1.3",
2+
"version": "0.1.4-beta-*",
33
"packOptions": {
4+
"summary": "Complete JSONAPI Middleware, Routing, and Controller package for .Net Core",
5+
"tags": [
6+
"dotnetcore",
7+
"jsonapi",
8+
"emberjs"
9+
],
10+
"owners": [
11+
"Jared Nance",
12+
"Children's Research Institue"
13+
],
414
"licenseUrl": "https://github.com/Research-Institute/json-api-dotnet-core/tree/master/JsonApiDotNetCore/LICENSE",
5-
"projectUrl": "https://github.com/Research-Institute/json-api-dotnet-core/"
15+
"projectUrl": "https://github.com/Research-Institute/json-api-dotnet-core",
16+
"repository": {
17+
"url": "https://github.com/Research-Institute/json-api-dotnet-core"
18+
}
619
},
720
"dependencies": {
821
"NETStandard.Library": "1.6.0",

JsonApiDotNetCoreExample/Startup.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public Startup(IHostingEnvironment env)
3232
public void ConfigureServices(IServiceCollection services)
3333
{
3434
// Add framework services.
35-
services.AddMvc();
35+
services.AddCors();
36+
3637
services.AddDbContext<ApplicationDbContext>(options =>
3738
options.UseNpgsql(Configuration["Data:ConnectionString"]),
3839
ServiceLifetime.Transient);
@@ -54,6 +55,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
5455
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
5556
loggerFactory.AddDebug();
5657

58+
app.UseCors(builder =>
59+
builder.WithOrigins("http://localhost:4200").AllowAnyHeader().AllowAnyMethod().AllowCredentials());
60+
5761
app.UseJsonApi();
5862
}
5963
}

JsonApiDotNetCoreExample/project.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"Microsoft.Extensions.Logging.Console": "1.0.0",
1212
"Microsoft.Extensions.Logging.Debug": "1.0.0",
1313
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
14-
"JsonApiDotNetCore": "0.1.2",
14+
"JsonApiDotNetCore": "0.1.4-beta-*",
1515
"Npgsql.EntityFrameworkCore.PostgreSQL": "1.0.0",
1616
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
1717
},

JsonApiDotNetCoreTests/Extensions/UnitTests/StringExtensionsTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public void ToCamelCase_ConvertsString_ToCamelCase(string input, string expected
1919

2020
[Theory]
2121
[InlineData("todoItem", "TodoItem")]
22+
[InlineData("todo-items", "TodoItems")]
2223
public void ToProperCase_ConvertsString_ToProperCase(string input, string expectedOutput)
2324
{
2425
// arrange
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
using System;
2+
using System.Threading.Tasks;
23
using JsonApiDotNetCore.Routing;
34
using Microsoft.AspNetCore.Http;
45

56
namespace JsonApiDotNetCoreTests.Helpers
67
{
7-
public class TestRouter : IRouter
8-
{
9-
public bool DidHandleRoute { get; set; }
10-
public bool HandleJsonApiRoute(HttpContext context, IServiceProvider serviceProvider)
8+
public class TestRouter : IRouter
119
{
12-
DidHandleRoute = true;
13-
return true;
10+
public bool DidHandleRoute { get; set; }
11+
12+
Task<bool> IRouter.HandleJsonApiRouteAsync(HttpContext context, IServiceProvider serviceProvider)
13+
{
14+
DidHandleRoute = true;
15+
return Task.Run(() => true);
16+
}
1417
}
15-
}
1618
}

0 commit comments

Comments
 (0)