master
Harald Wolff 2019-08-30 09:03:51 +02:00
parent 2ce2809530
commit 2c5ef22ed8
2 changed files with 98 additions and 108 deletions

View File

@ -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<T> : Resource where T:class
{
ODBCollection<T> collection;
//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, 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 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 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");
// 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 = 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<string>());
}
// public override HttpResponse GetResponse(HttpRequest httpRequest)
// {
// return GetResponse(httpRequest, new Queue<string>());
// }
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<ln.types.odb.Query> queries = new List<Query>();
// //}
// //else
// //{
// // List<ln.types.odb.Query> queries = new List<Query>();
// 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<T>(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<T>(httpRequest.ContentReader.ReadToEnd());
// if (!collection.Insert(ni))
// throw new HttpException(500, "Object not created, may already exist");
return ni;
}
// return ni;
// }
}
//}
}

View File

@ -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;