master
Harald Wolff 2019-04-08 08:47:09 +02:00
parent e94811994f
commit b86655a1f1
2 changed files with 131 additions and 0 deletions

View File

@ -0,0 +1,126 @@
// /**
// * 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.Collections.ObjectModel;
using System.Linq;
using ln.types.odb;
using Newtonsoft.Json;
using ln.http.exceptions;
using Newtonsoft.Json.Linq;
using System.ComponentModel;
using ln.types.odb.mapped;
using ln.types.threads;
namespace ln.http.resources
{
public class CollectionResource<T> : Resource where T:class
{
ODBCollection<T> collection;
public CollectionResource(Resource container, ODBCollection<T> collection)
: this(container, typeof(T).Name,collection)
{}
public CollectionResource(Resource container, String name,ODBCollection<T> collection)
: base(container, name)
{
this.collection = collection;
}
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":
responseValue = Timing.Meassure("collection.ToArray()",() => collection.ToArray());
break;
case "POST":
T ni = PostItem(httpRequest);
object documentID = collection.Mapper.GetDocumentID(ni);
httpResponse.SetHeader("Location", String.Format("/{0}/{1}", String.Join("/", this.Path),documentID.ToString()));
httpResponse.StatusCode = 201;
responseValue = ni;
break;
default:
throw new NotSupportedException();
}
}
else
{
String documentID = pathStack.Dequeue();
Type idType = collection.IDType;
object docID = documentID;
if (!typeof(string).Equals(idType))
{
TypeConverter converter = TypeDescriptor.GetConverter(idType);
docID = converter.ConvertFromString(documentID);
}
T instance = collection[ODBMapper.Default.MapValue(docID)];
switch (httpRequest.Method)
{
case "PUT":
JObject jInstance = JObject.FromObject(instance);
JObject jUpdate = JObject.Parse(httpRequest.ContentReader.ReadToEnd());
foreach (JProperty jProperty in jUpdate.Properties())
{
if (jInstance.ContainsKey(jProperty.Name))
jInstance.Remove(jProperty.Name);
jInstance.Add(jProperty.Name, jProperty.Value);
}
instance = (T)jInstance.ToObject(instance.GetType());
bool updated = collection.Update(instance);
responseValue = instance;
break;
default:
throw new NotSupportedException();
}
}
httpResponse.ContentWriter.Write(
JsonConvert.SerializeObject(responseValue)
);
return httpResponse;
}
public override HttpResponse GetResponse(HttpRequest httpRequest)
{
return GetResponse(httpRequest, new Queue<string>());
}
private T PostItem(HttpRequest httpRequest)
{
T ni = JsonConvert.DeserializeObject<T>(httpRequest.ContentReader.ReadToEnd());
if (!collection.Insert(ni))
throw new HttpException(500, "Object not created, may already exist");
return ni;
}
}
}

View File

@ -45,6 +45,7 @@
<Compile Include="JsonCallResource.cs" />
<Compile Include="ReflectiveResource.cs" />
<Compile Include="reflection\Reflector.cs" />
<Compile Include="CollectionResource&lt;T&gt;.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ln.http\ln.http.csproj">
@ -59,6 +60,10 @@
<Project>{D471A566-9FB6-41B2-A777-3C32874ECD0E}</Project>
<Name>ln.logging</Name>
</ProjectReference>
<ProjectReference Include="..\ln.types\ln.types.csproj">
<Project>{8D9AB9A5-E513-4BA7-A450-534F6456BF28}</Project>
<Name>ln.types</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />