98 lines
3.9 KiB
C#
98 lines
3.9 KiB
C#
using System;
|
|
using ln.http.router;
|
|
using ln.http;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using ln.types;
|
|
using ln.json.mapping;
|
|
using ln.http.exceptions;
|
|
using System.Linq;
|
|
using ln.logging;
|
|
using ln.types.reflection;
|
|
using ln.json.reflection;
|
|
namespace ln.json.http
|
|
{
|
|
public class ObjectPoolContainerRestAPI : SimpleRouter
|
|
{
|
|
public ObjectPoolContainer ObjectPoolContainer { get; }
|
|
public ObjectPoolContainerSerializer ObjectPoolContainerSerializer { get; }
|
|
|
|
public ObjectPoolContainerRestAPI():this(new ObjectPoolContainer()) { }
|
|
public ObjectPoolContainerRestAPI(ObjectPoolContainer objectPoolContainer)
|
|
{
|
|
AddSimpleRoute("/:type", RequestType);
|
|
AddSimpleRoute("/:type/:identity", RequestInstance);
|
|
AddSimpleRoute("/:type/:identity/*", RequestInstancePath);
|
|
|
|
ObjectPoolContainer = objectPoolContainer;
|
|
ObjectPoolContainerSerializer = new ObjectPoolContainerSerializer(objectPoolContainer);
|
|
}
|
|
|
|
HttpResponse RequestType(HttpRequest httpRequest)
|
|
{
|
|
ObjectPool objectPool = ObjectPoolContainer[httpRequest.GetParameter("type")];
|
|
|
|
switch (httpRequest.Method)
|
|
{
|
|
case "GET":
|
|
JSONArray instanceList = new JSONArray();
|
|
|
|
foreach (object instance in objectPool.Instances)
|
|
{
|
|
instanceList.Add(ObjectPoolContainerSerializer.SerializeObject(objectPool, instance));
|
|
}
|
|
|
|
return httpRequest.SendJSON(instanceList);
|
|
case "POST":
|
|
object o = null;
|
|
try
|
|
{
|
|
o = ObjectPoolContainerSerializer.UnserializeObject(objectPool, httpRequest.GetJSON() as JSONObject);
|
|
objectPool.Add(o);
|
|
|
|
return httpRequest.Redirect(objectPool.GetIdentity(o).ToString());
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Logging.Log(e);
|
|
(o as IDisposable)?.Dispose();
|
|
throw new HttpException(500, String.Format("Internal Error {0}", e.ToString()));
|
|
}
|
|
}
|
|
throw new NotImplementedException();
|
|
}
|
|
HttpResponse RequestInstance(HttpRoutingContext routingContext, HttpRequest httpRequest)
|
|
{
|
|
ObjectPool objectPool = ObjectPoolContainer[httpRequest.GetParameter("type")];
|
|
object o = objectPool[Cast.To(httpRequest.GetParameter("identity"), objectPool.IdentityType)];
|
|
|
|
switch (httpRequest.Method)
|
|
{
|
|
case "DELETE":
|
|
objectPool.Remove(objectPool.GetIdentity(o));
|
|
HttpResponse httpResponse = new HttpResponse(httpRequest);
|
|
httpResponse.StatusCode = 204;
|
|
return httpResponse;
|
|
case "PUT":
|
|
object identity = objectPool.GetIdentity(o);
|
|
ObjectPoolContainerSerializer.UpdateObject(objectPool, o, httpRequest.GetJSON() as JSONObject);
|
|
if (!objectPool.GetIdentity(o).Equals(identity))
|
|
{
|
|
objectPool.Remove(identity);
|
|
objectPool.Add(o);
|
|
}
|
|
return httpRequest.Redirect(objectPool.GetIdentity(o).ToString());
|
|
case "GET":
|
|
return httpRequest.SendJSON(ObjectPoolContainerSerializer.SerializeObject(objectPool, o));
|
|
}
|
|
throw new BadRequestException();
|
|
}
|
|
HttpResponse RequestInstancePath(HttpRoutingContext routingContext, HttpRequest httpRequest)
|
|
{
|
|
// ToDo: Implement Sub-Instance Level access...
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
}
|
|
}
|