138 lines
4.7 KiB
C#
138 lines
4.7 KiB
C#
// /**
|
|
// * 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.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 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 = 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);
|
|
|
|
responseValue = instance;
|
|
|
|
break;
|
|
default:
|
|
throw new NotSupportedException();
|
|
}
|
|
}
|
|
|
|
httpResponse.ContentWriter.Write(
|
|
JSONMapper.DefaultMapper.ToJson(responseValue).ToString()
|
|
);
|
|
return httpResponse;
|
|
}
|
|
|
|
public override HttpResponse GetResponse(HttpRequest httpRequest)
|
|
{
|
|
return GetResponse(httpRequest, new Queue<string>());
|
|
}
|
|
|
|
private T[] Query(HttpRequest httpRequest)
|
|
{
|
|
return collection.ToArray();
|
|
|
|
//if (httpRequest.Query.Count == 0)
|
|
//{
|
|
|
|
//}
|
|
//else
|
|
//{
|
|
// List<ln.types.odb.Query> queries = new List<Query>();
|
|
|
|
// 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");
|
|
|
|
return ni;
|
|
}
|
|
|
|
}
|
|
}
|