WIP
parent
2ce2809530
commit
2c5ef22ed8
|
@ -10,128 +10,126 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using ln.types.odb;
|
|
||||||
using ln.http.exceptions;
|
using ln.http.exceptions;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
using ln.types.odb.mapped;
|
|
||||||
using ln.json.mapping;
|
using ln.json.mapping;
|
||||||
using ln.json;
|
using ln.json;
|
||||||
namespace ln.http.resources
|
namespace ln.http.resources
|
||||||
{
|
{
|
||||||
public class CollectionResource<T> : Resource where T:class
|
//public class CollectionResource<T> : Resource where T:class
|
||||||
{
|
//{
|
||||||
ODBCollection<T> collection;
|
// ODBCollection<T> collection;
|
||||||
|
|
||||||
public CollectionResource(Resource container, ODBCollection<T> collection)
|
// public CollectionResource(Resource container, ODBCollection<T> collection)
|
||||||
: this(container, typeof(T).Name,collection)
|
// : this(container, typeof(T).Name,collection)
|
||||||
{}
|
// {}
|
||||||
|
|
||||||
public CollectionResource(Resource container, String name,ODBCollection<T> collection)
|
// public CollectionResource(Resource container, String name,ODBCollection<T> collection)
|
||||||
: base(container, name)
|
// : base(container, name)
|
||||||
{
|
// {
|
||||||
this.collection = collection;
|
// this.collection = collection;
|
||||||
}
|
// }
|
||||||
|
|
||||||
public override bool HandlesDispatching => true;
|
// public override bool HandlesDispatching => true;
|
||||||
public override void AddResource(Resource resource) => throw new NotSupportedException();
|
// public override void AddResource(Resource resource) => throw new NotSupportedException();
|
||||||
public override void RemoveResource(Resource resource) => throw new NotSupportedException();
|
// public override void RemoveResource(Resource resource) => throw new NotSupportedException();
|
||||||
public override IEnumerable<Resource> GetResources() => throw new NotImplementedException();
|
// public override IEnumerable<Resource> GetResources() => throw new NotImplementedException();
|
||||||
public override bool Contains(string name) => throw new NotImplementedException();
|
// public override bool Contains(string name) => throw new NotImplementedException();
|
||||||
|
|
||||||
public override HttpResponse GetResponse(HttpRequest httpRequest,Queue<string> pathStack)
|
// public override HttpResponse GetResponse(HttpRequest httpRequest,Queue<string> pathStack)
|
||||||
{
|
// {
|
||||||
object responseValue = null;
|
// object responseValue = null;
|
||||||
HttpResponse httpResponse = new HttpResponse(httpRequest);
|
// HttpResponse httpResponse = new HttpResponse(httpRequest);
|
||||||
httpResponse.SetHeader("content-type", "application/json");
|
// httpResponse.SetHeader("content-type", "application/json");
|
||||||
|
|
||||||
|
|
||||||
if (pathStack.Count == 0)
|
// if (pathStack.Count == 0)
|
||||||
{
|
// {
|
||||||
switch (httpRequest.Method)
|
// switch (httpRequest.Method)
|
||||||
{
|
// {
|
||||||
case "GET":
|
// case "GET":
|
||||||
responseValue = Query(httpRequest);
|
// responseValue = Query(httpRequest);
|
||||||
break;
|
// break;
|
||||||
case "POST":
|
// case "POST":
|
||||||
T ni = PostItem(httpRequest);
|
// T ni = PostItem(httpRequest);
|
||||||
object documentID = collection.Mapper.GetDocumentID(ni);
|
// object documentID = collection.Mapper.GetDocumentID(ni);
|
||||||
httpResponse.SetHeader("Location", String.Format("/{0}/{1}", String.Join("/", this.Path),documentID.ToString()));
|
// httpResponse.SetHeader("Location", String.Format("/{0}/{1}", String.Join("/", this.Path),documentID.ToString()));
|
||||||
httpResponse.StatusCode = 201;
|
// httpResponse.StatusCode = 201;
|
||||||
responseValue = ni;
|
// responseValue = ni;
|
||||||
break;
|
// break;
|
||||||
default:
|
// default:
|
||||||
throw new NotSupportedException();
|
// throw new NotSupportedException();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
String documentID = pathStack.Dequeue();
|
// String documentID = pathStack.Dequeue();
|
||||||
Type idType = collection.IDType;
|
// Type idType = collection.IDType;
|
||||||
object docID = documentID;
|
// object docID = documentID;
|
||||||
if (!typeof(string).Equals(idType))
|
// if (!typeof(string).Equals(idType))
|
||||||
{
|
// {
|
||||||
TypeConverter converter = TypeDescriptor.GetConverter(idType);
|
// TypeConverter converter = TypeDescriptor.GetConverter(idType);
|
||||||
docID = converter.ConvertFromString(documentID);
|
// docID = converter.ConvertFromString(documentID);
|
||||||
}
|
// }
|
||||||
T instance = collection[ODBMapper.Default.MapValue(docID)];
|
// T instance = collection[ODBMapper.Default.MapValue(docID)];
|
||||||
|
|
||||||
switch (httpRequest.Method)
|
// switch (httpRequest.Method)
|
||||||
{
|
// {
|
||||||
case "GET":
|
// case "GET":
|
||||||
responseValue = instance;
|
// responseValue = instance;
|
||||||
break;
|
// break;
|
||||||
case "PUT":
|
// case "PUT":
|
||||||
JSONMapper.DefaultMapper.Apply((JSONObject)JSONParser.Parse(httpRequest.ContentReader.ReadToEnd()), instance);
|
// JSONMapper.DefaultMapper.Apply((JSONObject)JSONParser.Parse(httpRequest.ContentReader.ReadToEnd()), instance);
|
||||||
bool updated = collection.Update(instance);
|
// bool updated = collection.Update(instance);
|
||||||
|
|
||||||
responseValue = instance;
|
// responseValue = instance;
|
||||||
|
|
||||||
break;
|
// break;
|
||||||
default:
|
// default:
|
||||||
throw new NotSupportedException();
|
// throw new NotSupportedException();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
httpResponse.ContentWriter.Write(
|
// httpResponse.ContentWriter.Write(
|
||||||
JSONMapper.DefaultMapper.ToJson(responseValue).ToString()
|
// JSONMapper.DefaultMapper.ToJson(responseValue).ToString()
|
||||||
);
|
// );
|
||||||
return httpResponse;
|
// return httpResponse;
|
||||||
}
|
// }
|
||||||
|
|
||||||
public override HttpResponse GetResponse(HttpRequest httpRequest)
|
// public override HttpResponse GetResponse(HttpRequest httpRequest)
|
||||||
{
|
// {
|
||||||
return GetResponse(httpRequest, new Queue<string>());
|
// return GetResponse(httpRequest, new Queue<string>());
|
||||||
}
|
// }
|
||||||
|
|
||||||
private T[] Query(HttpRequest httpRequest)
|
// private T[] Query(HttpRequest httpRequest)
|
||||||
{
|
// {
|
||||||
return collection.ToArray();
|
// return collection.ToArray();
|
||||||
|
|
||||||
//if (httpRequest.Query.Count == 0)
|
// //if (httpRequest.Query.Count == 0)
|
||||||
//{
|
// //{
|
||||||
|
|
||||||
//}
|
// //}
|
||||||
//else
|
// //else
|
||||||
//{
|
// //{
|
||||||
// List<ln.types.odb.Query> queries = new List<Query>();
|
// // List<ln.types.odb.Query> queries = new List<Query>();
|
||||||
|
|
||||||
// foreach (string qKey in httpRequest.Query.Keys)
|
// // foreach (string qKey in httpRequest.Query.Keys)
|
||||||
// {
|
// // {
|
||||||
// if (collection.HasProperty(qKey))
|
// // if (collection.HasProperty(qKey))
|
||||||
// {
|
// // {
|
||||||
// }
|
// // }
|
||||||
// }
|
// // }
|
||||||
//}
|
// //}
|
||||||
}
|
// }
|
||||||
|
|
||||||
private T PostItem(HttpRequest httpRequest)
|
// private T PostItem(HttpRequest httpRequest)
|
||||||
{
|
// {
|
||||||
T ni = JSONMapper.DefaultMapper.FromJson<T>(httpRequest.ContentReader.ReadToEnd());
|
// T ni = JSONMapper.DefaultMapper.FromJson<T>(httpRequest.ContentReader.ReadToEnd());
|
||||||
if (!collection.Insert(ni))
|
// if (!collection.Insert(ni))
|
||||||
throw new HttpException(500, "Object not created, may already exist");
|
// throw new HttpException(500, "Object not created, may already exist");
|
||||||
|
|
||||||
return ni;
|
// return ni;
|
||||||
}
|
// }
|
||||||
|
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,14 +9,6 @@
|
||||||
// **/
|
// **/
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
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.mapping;
|
||||||
using ln.json;
|
using ln.json;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue