master
Harald Wolff 2019-06-06 11:56:59 +02:00
parent a9c5d37b43
commit a4b2b1d9c3
1 changed files with 46 additions and 3 deletions

View File

@ -18,17 +18,22 @@ using Newtonsoft.Json.Linq;
using System.ComponentModel;
using ln.types.odb.mapped;
using ln.types.threads;
using ln.types.json;
namespace ln.http.resources.collections
{
public class JSONCollectionResource<TENTITY, TIDENT> : Resource where TENTITY : class
{
public JSONConvert JSONConvert { get; set; }
IEntityCollectionInterface<TENTITY, TIDENT> entityCollectionInterface;
public JSONCollectionResource(Resource container, IEntityCollectionInterface<TENTITY,TIDENT> entityCollectionInterface)
: base(container, typeof(TENTITY).Name)
{
this.entityCollectionInterface = entityCollectionInterface;
JSONConvert = JSONConvert.DefaultInstance;
}
public override bool HandlesDispatching => true;
@ -98,22 +103,60 @@ namespace ln.http.resources.collections
return httpResponse;
}
private string ConstructEntityURL(HttpRequest request,TIDENT identity)
{
JToken jId = null;
if (!JSONConvert.Value2JSON(identity, ref jId))
throw new HttpException(500,"Identity can not be serialized");
return string.Format("/{0}/{1}", String.Join("/", Path), jId.ToObject(typeof(string)));
}
private HttpResponse ConstructResponse(HttpRequest request,IEnumerable<TIDENT> values,IEnumerable<TIDENT> failed)
{
HttpResponse response = new HttpResponse(request);
JObject surround = new JObject();
JArray jvalues = new JArray();
JObject jvalues = new JObject();
if (values != null)
{
foreach (TIDENT id in values)
{
JToken jId = null;
if (this.JSONConvert.Value2JSON(id, ref jId))
{
jvalues.Add(jId.ToObject(typeof(string)).ToString(), ConstructEntityURL(request, id));
}
}
}
surround.Add("values", jvalues);
if (failed!= null)
{
JObject jfailed = new JObject();
foreach (TIDENT id in failed)
{
JToken jId = null;
if (this.JSONConvert.Value2JSON(id, ref jId))
{
jfailed.Add(jId.ToObject(typeof(string)).ToString(), null);
}
}
}
surround.Add("failed", jvalues);
response.SetHeader("content-type", "application/json");
response.ContentWriter.Write(surround.ToString());
return response;
}
class CollectionResult
{
}
}
}