Player data initial

This commit is contained in:
2021-10-26 00:26:52 -04:00
parent ab8937f7cd
commit 7b73d23b9e
8 changed files with 80 additions and 40 deletions

View File

@@ -12,7 +12,7 @@ namespace TOOHUCardAPI.Controllers
public class MethodBasedController : ControllerBase
{
public delegate Task<object> EndpointHandler(string requestBody);
private static Dictionary<Type, MethodMap> MethodMapByType;
private static Dictionary<Type, MethodMap> MethodMapByType = new Dictionary<Type, MethodMap>();
protected Task<object> InvokeEndpointHandlerForMethod<T>(object _this, string method, string body)
{
@@ -20,7 +20,7 @@ namespace TOOHUCardAPI.Controllers
if (registeredEndpointHandlers.ContainsKey(method))
{
return ((EndpointHandler) registeredEndpointHandlers[method]
.CreateDelegate(typeof(EndpointHandler), _this)).Invoke(body);
.CreateDelegate(typeof(EndpointHandler), _this))(body);
}
throw new MissingEndpointHandlerException(method);
}
@@ -29,15 +29,14 @@ namespace TOOHUCardAPI.Controllers
{
if (!MethodMapByType.ContainsKey(typeof(T)))
{
RegisterEndpointHandlers<T>();
MethodMapByType[typeof(T)] = RegisterEndpointHandlers<T>();
}
return MethodMapByType[typeof(T)];
}
private static MethodMap RegisterEndpointHandlers<T>()
{
MethodInfo[] methods = typeof(T).GetMethods();
MethodInfo[] methods = typeof(T).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
return methods
.Aggregate(new MethodMap(), (handlers, m) =>
{
@@ -51,4 +50,11 @@ namespace TOOHUCardAPI.Controllers
});
}
}
public class MissingEndpointHandlerException : Exception
{
public MissingEndpointHandlerException(string method) : base($"Handler for [{method}] is either missing or incorrectly setup.")
{
}
}
}

View File

@@ -1,10 +1,14 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using TOOHUCardAPI.Data;
using TOOHUCardAPI.Models;
@@ -20,17 +24,22 @@ namespace TOOHUCardAPI.Controllers
* So let's use a single entry point and redirect based on that
*/
[HttpPost]
public async Task<object> EntryPoint(IMethodBasedRequest methodRequest)
public async Task<object> EntryPoint([FromBody] object bodyObj)
{
return null;
string body = JsonConvert.SerializeObject(bodyObj);
JObject request = JObject.Parse(body);
string method = request["method"].ToString();
return await InvokeEndpointHandlerForMethod<PlayerDataController>(this, method, body);
}
[EndpointHandler("get")]
private async Task<object> Test(string body)
{
PlayerDataGetRequestObject requestObject = JsonConvert.DeserializeObject<PlayerDataGetRequestObject>(body);
PlayerDataGetResponseObject response = new PlayerDataGetResponseObject(requestObject.Ids.Length);
return response;
}
}
public class MissingEndpointHandlerException : Exception
{
public MissingEndpointHandlerException(string? method) : base($"Handler for [{method}] is either missing or incorrectly setup.")
{
}
}
}