master
Harald Wolff 2019-04-16 18:23:01 +02:00
parent cead9562d4
commit a782d1c4be
2 changed files with 135 additions and 0 deletions

View File

@ -0,0 +1,134 @@
// /**
// * File: CollectionResource<T>.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using ln.http.exceptions;
using Newtonsoft.Json.Linq;
namespace ln.http.resources
{
public delegate IEnumerable<T> EnumerationDelegate<T>();
public delegate T LookupDelegate<T>(string ID);
public delegate String GetResourceNameDelegate<T>(T o);
public delegate T CreateDelegate<T>();
public delegate bool DeleteDelegate<T>(T o);
public delegate bool StoreDelegate<T>(T o);
public class ObjectContainerResource<T> : Resource where T:class
{
public EnumerationDelegate<T> Enumeration { get; set; }
public LookupDelegate<T> Lookup { get; set; }
public GetResourceNameDelegate<T> GetResourceName { get; set; }
public CreateDelegate<T> Create { get; set; }
public DeleteDelegate<T> Delete { get; set; }
public StoreDelegate<T> Store { get; set; }
public ObjectContainerResource(Resource container)
: this(container, typeof(T).Name)
{}
public ObjectContainerResource(Resource container, String name)
: base(container, name)
{
}
public override bool HandlesDispatching => true;
public override void AddResource(Resource resource) => throw new NotSupportedException();
public override void RemoveResource(Resource resource) => throw new NotSupportedException();
public override IEnumerable<Resource> GetResources() => throw new NotImplementedException();
public override bool Contains(string name) => throw new NotImplementedException();
public override HttpResponse GetResponse(HttpRequest httpRequest,Queue<string> pathStack)
{
object responseValue = null;
HttpResponse httpResponse = new HttpResponse(httpRequest);
httpResponse.SetHeader("content-type", "application/json");
if (pathStack.Count == 0)
{
switch (httpRequest.Method)
{
case "GET":
T[] resources = Query(httpRequest);
JObject result = new JObject();
foreach (T resource in resources)
result.Add(String.Format("{0}/{1}",String.Join("/",Path),GetResourceName(resource)), JObject.FromObject(resource));
responseValue = result;
break;
case "POST":
T ni = PostItem(httpRequest);
string resourceName = GetResourceName(ni);
httpResponse.SetHeader("Location", String.Format("/{0}/{1}", String.Join("/", this.Path),resourceName.ToString()));
httpResponse.StatusCode = 201;
responseValue = ni;
break;
default:
throw new NotSupportedException();
}
}
else
{
String resourceName = String.Join("/", pathStack);
T resource = Lookup(resourceName);
switch (httpRequest.Method)
{
case "PUT":
JsonConvert.PopulateObject(httpRequest.ContentReader.ReadToEnd(), resource);
bool updated = Store(resource);
if (updated)
{
responseValue = resource;
httpResponse.StatusCode = 200;
} else
{
httpResponse.StatusCode = 500;
}
break;
default:
throw new NotSupportedException();
}
}
httpResponse.ContentWriter.Write(
responseValue is JValue ? (responseValue as JValue).ToString() : JsonConvert.SerializeObject(responseValue)
);
return httpResponse;
}
public override HttpResponse GetResponse(HttpRequest httpRequest)
{
return GetResponse(httpRequest, new Queue<string>());
}
private T[] Query(HttpRequest httpRequest)
{
return Enumeration().ToArray();
}
private T PostItem(HttpRequest httpRequest)
{
T newResource = Create();
JsonConvert.PopulateObject(httpRequest.ContentReader.ReadToEnd(), newResource);
if (!Store(newResource))
throw new HttpException(500, "Object not created, may already exist");
return newResource;
}
}
}

View File

@ -46,6 +46,7 @@
<Compile Include="ReflectiveResource.cs" />
<Compile Include="reflection\Reflector.cs" />
<Compile Include="CollectionResource&lt;T&gt;.cs" />
<Compile Include="ObjectContainerResource.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ln.http\ln.http.csproj">