|
1 | 1 | using System; |
2 | 2 | using System.Text; |
| 3 | +using System.Threading.Tasks; |
3 | 4 | using JsonApiDotNetCore.Abstractions; |
4 | 5 | using JsonApiDotNetCore.Configuration; |
5 | 6 | using JsonApiDotNetCore.Controllers; |
6 | 7 | using JsonApiDotNetCore.Services; |
7 | 8 | using Microsoft.AspNetCore.Http; |
8 | 9 | using Microsoft.AspNetCore.Mvc; |
9 | | -using Newtonsoft.Json; |
10 | 10 |
|
11 | 11 | namespace JsonApiDotNetCore.Routing |
12 | 12 | { |
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 |
22 | 14 | { |
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; |
27 | 19 |
|
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 | + } |
31 | 26 |
|
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; |
34 | 30 |
|
35 | | - InitializeContext(context, route); |
36 | | - CallController(); |
| 31 | + var route = _routeBuilder.BuildFromRequest(context.Request); |
| 32 | + if (route == null) return false; |
37 | 33 |
|
38 | | - return true; |
39 | | - } |
| 34 | + var jsonApiContext = InitializeContext(context, route); |
| 35 | + await CallController(jsonApiContext); |
40 | 36 |
|
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 | + } |
46 | 39 |
|
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 | + } |
50 | 46 |
|
51 | | - var result = ActivateControllerMethod(controller); |
| 47 | + private async Task CallController(JsonApiContext jsonApiContext) |
| 48 | + { |
| 49 | + var controller = _controllerBuilder.BuildController(jsonApiContext); |
52 | 50 |
|
53 | | - result.Value = SerializeResult(result.Value); |
| 51 | + var result = ActivateControllerMethod(controller, jsonApiContext); |
54 | 52 |
|
55 | | - SendResponse(result); |
56 | | - } |
| 53 | + result.Value = SerializeResult(result.Value, jsonApiContext); |
57 | 54 |
|
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 | + } |
75 | 57 |
|
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 | + } |
80 | 75 |
|
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 | + } |
88 | 87 | } |
89 | | - } |
90 | 88 | } |
0 commit comments