From 2c5ef22ed866982ea5b6baaa758f81e3e501d5bc Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Fri, 30 Aug 2019 09:03:51 +0200 Subject: [PATCH] WIP --- CollectionResource.cs | 198 +++++++++++------------ collections/JSONCollectionResource.cs | 8 - 2 files changed, 98 insertions(+), 108 deletions(-) diff --git a/CollectionResource.cs b/CollectionResource.cs index 5483b14..cd9b072 100644 --- a/CollectionResource.cs +++ b/CollectionResource.cs @@ -10,128 +10,126 @@ using System; using System.Collections.Generic; using System.Linq; -using ln.types.odb; using ln.http.exceptions; using System.ComponentModel; -using ln.types.odb.mapped; using ln.json.mapping; using ln.json; namespace ln.http.resources { - public class CollectionResource : Resource where T:class - { - ODBCollection collection; + //public class CollectionResource : Resource where T:class + //{ + // ODBCollection collection; - public CollectionResource(Resource container, ODBCollection collection) - : this(container, typeof(T).Name,collection) - {} + // public CollectionResource(Resource container, ODBCollection collection) + // : this(container, typeof(T).Name,collection) + // {} - public CollectionResource(Resource container, String name,ODBCollection collection) - : base(container, name) - { - this.collection = collection; - } + // public CollectionResource(Resource container, String name,ODBCollection 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 GetResources() => throw new NotImplementedException(); - public override bool Contains(string name) => throw new NotImplementedException(); + // 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 GetResources() => throw new NotImplementedException(); + // public override bool Contains(string name) => throw new NotImplementedException(); - public override HttpResponse GetResponse(HttpRequest httpRequest,Queue pathStack) - { - object responseValue = null; - HttpResponse httpResponse = new HttpResponse(httpRequest); - httpResponse.SetHeader("content-type", "application/json"); + // public override HttpResponse GetResponse(HttpRequest httpRequest,Queue 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 = Query(httpRequest); - 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)]; + // if (pathStack.Count == 0) + // { + // switch (httpRequest.Method) + // { + // case "GET": + // responseValue = Query(httpRequest); + // 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 "GET": - responseValue = instance; - break; - case "PUT": - JSONMapper.DefaultMapper.Apply((JSONObject)JSONParser.Parse(httpRequest.ContentReader.ReadToEnd()), instance); - bool updated = collection.Update(instance); + // switch (httpRequest.Method) + // { + // case "GET": + // responseValue = instance; + // break; + // case "PUT": + // JSONMapper.DefaultMapper.Apply((JSONObject)JSONParser.Parse(httpRequest.ContentReader.ReadToEnd()), instance); + // bool updated = collection.Update(instance); - responseValue = instance; + // responseValue = instance; - break; - default: - throw new NotSupportedException(); - } - } + // break; + // default: + // throw new NotSupportedException(); + // } + // } - httpResponse.ContentWriter.Write( - JSONMapper.DefaultMapper.ToJson(responseValue).ToString() - ); - return httpResponse; - } + // httpResponse.ContentWriter.Write( + // JSONMapper.DefaultMapper.ToJson(responseValue).ToString() + // ); + // return httpResponse; + // } - public override HttpResponse GetResponse(HttpRequest httpRequest) - { - return GetResponse(httpRequest, new Queue()); - } + // public override HttpResponse GetResponse(HttpRequest httpRequest) + // { + // return GetResponse(httpRequest, new Queue()); + // } - private T[] Query(HttpRequest httpRequest) - { - return collection.ToArray(); + // private T[] Query(HttpRequest httpRequest) + // { + // return collection.ToArray(); - //if (httpRequest.Query.Count == 0) - //{ + // //if (httpRequest.Query.Count == 0) + // //{ - //} - //else - //{ - // List queries = new List(); + // //} + // //else + // //{ + // // List queries = new List(); - // foreach (string qKey in httpRequest.Query.Keys) - // { - // if (collection.HasProperty(qKey)) - // { - // } - // } - //} - } + // // foreach (string qKey in httpRequest.Query.Keys) + // // { + // // if (collection.HasProperty(qKey)) + // // { + // // } + // // } + // //} + // } - private T PostItem(HttpRequest httpRequest) - { - T ni = JSONMapper.DefaultMapper.FromJson(httpRequest.ContentReader.ReadToEnd()); - if (!collection.Insert(ni)) - throw new HttpException(500, "Object not created, may already exist"); + // private T PostItem(HttpRequest httpRequest) + // { + // T ni = JSONMapper.DefaultMapper.FromJson(httpRequest.ContentReader.ReadToEnd()); + // if (!collection.Insert(ni)) + // throw new HttpException(500, "Object not created, may already exist"); - return ni; - } + // return ni; + // } - } + //} } diff --git a/collections/JSONCollectionResource.cs b/collections/JSONCollectionResource.cs index f8aae5e..1412e2b 100644 --- a/collections/JSONCollectionResource.cs +++ b/collections/JSONCollectionResource.cs @@ -9,14 +9,6 @@ // **/ using System; using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; -using ln.types.odb; -using ln.http.exceptions; -using System.ComponentModel; -using ln.types.odb.mapped; -using ln.types.threads; -using System.Reflection; using ln.json.mapping; using ln.json;