WIP
parent
d45758bbf3
commit
a9c5d37b43
|
@ -41,6 +41,23 @@ namespace ln.http.resources
|
|||
: base(container, name)
|
||||
{
|
||||
}
|
||||
public ObjectContainerResource(Resource container, String name,
|
||||
EnumerationDelegate<T> Enumeration,
|
||||
LookupDelegate<T> Lookup,
|
||||
GetResourceNameDelegate<T> GetResourceName,
|
||||
CreateDelegate<T> Create,
|
||||
DeleteDelegate<T> Delete,
|
||||
StoreDelegate<T> Store
|
||||
)
|
||||
: base(container, name)
|
||||
{
|
||||
this.Enumeration = Enumeration;
|
||||
this.Lookup = Lookup;
|
||||
this.GetResourceName = GetResourceName;
|
||||
this.Create = Create;
|
||||
this.Delete = Delete;
|
||||
this.Store = Store;
|
||||
}
|
||||
|
||||
public override bool HandlesDispatching => true;
|
||||
public override void AddResource(Resource resource) => throw new NotSupportedException();
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections;
|
||||
namespace ln.http.resources.collections
|
||||
{
|
||||
public interface IEntityCollectionInterface<TVALUE,TIDENT>
|
||||
{
|
||||
IEnumerable<TVALUE> List();
|
||||
// ToDo: IEnumerable<TVALUE> Query(/* Filter expressions ?! */);
|
||||
|
||||
object GetIdentity(TIDENT entity);
|
||||
TVALUE CreateEntity();
|
||||
|
||||
bool StoreEntity(TVALUE entity);
|
||||
bool GetEntity(TIDENT identity);
|
||||
bool RemoveEntity(TIDENT identity);
|
||||
|
||||
string GetLastErrorMessage();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,119 @@
|
|||
// /**
|
||||
// * 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.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using ln.types.odb;
|
||||
using Newtonsoft.Json;
|
||||
using ln.http.exceptions;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.ComponentModel;
|
||||
using ln.types.odb.mapped;
|
||||
using ln.types.threads;
|
||||
|
||||
namespace ln.http.resources.collections
|
||||
{
|
||||
public class JSONCollectionResource<TENTITY, TIDENT> : Resource where TENTITY : class
|
||||
{
|
||||
IEntityCollectionInterface<TENTITY, TIDENT> entityCollectionInterface;
|
||||
|
||||
public JSONCollectionResource(Resource container, IEntityCollectionInterface<TENTITY,TIDENT> entityCollectionInterface)
|
||||
: base(container, typeof(TENTITY).Name)
|
||||
{
|
||||
this.entityCollectionInterface = entityCollectionInterface;
|
||||
}
|
||||
|
||||
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) => GetResponse(httpRequest, new Queue<string>());
|
||||
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":
|
||||
JsonConvert.PopulateObject(httpRequest.ContentReader.ReadToEnd(), instance);
|
||||
bool updated = collection.Update(instance);
|
||||
|
||||
responseValue = instance;
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
httpResponse.ContentWriter.Write(
|
||||
JsonConvert.SerializeObject(responseValue)
|
||||
);
|
||||
return httpResponse;
|
||||
}
|
||||
|
||||
private HttpResponse ConstructResponse(HttpRequest request,IEnumerable<TIDENT> values,IEnumerable<TIDENT> failed)
|
||||
{
|
||||
HttpResponse response = new HttpResponse(request);
|
||||
|
||||
JObject surround = new JObject();
|
||||
JArray jvalues = new JArray();
|
||||
if (values != null)
|
||||
{
|
||||
foreach (TIDENT id in values)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
# Unterstützte Methoden:
|
||||
|
||||
GET /collection Abruf von Elementen aus der Kollektion, optional mit Filter
|
||||
GET /collection/<element identifier> Abruf eines definierten Element der Kollektion
|
||||
POST /collection Hinzufügen neuer Elemente zur Kollektion
|
||||
PUT /collection/<element identifier> Hinzufügen / Ändern eines Elements der Kollektion
|
||||
DELETE /collection/<element identifier> Entfernen eines Elements der Kollektion
|
||||
|
||||
# Response Schema der Kollektion
|
||||
|
||||
{
|
||||
collection: <URL> // Die offizielle URL der Kollektion (z.B.: http://myapi.test/mycollection )
|
||||
failed: {
|
||||
<IDENTIFIER99>: <MESSAGE> or null, // Element <IDENTIFIER99> konnte nicht bearbeitet werden, optionale Nachricht an Anwender in <MESSAGE>
|
||||
...
|
||||
},
|
||||
values: {
|
||||
<IDENTIFIER0>: <ELEMENTURL0>, // Element <IDENTIFIER0> wurde erfolgreich hinzugefügt / geändert
|
||||
<IDENTIFIER1>: null, // Element <IDENTIFIER1> wurde erfolgreich gelöscht
|
||||
},
|
||||
}
|
||||
|
||||
# Anfrageschemata
|
||||
|
||||
## GET /collection
|
||||
|
||||
"" oder
|
||||
|
||||
{
|
||||
filter: [ // Liste alternierender Filter
|
||||
{
|
||||
<KEY0>: <FILTEREXPR>, // Filtert die Ergebnisliste durch prüfen der Eigenschaft <KEY> mit <FILTEREXPR>
|
||||
<KEY1>: <FILTEREXPR1>,
|
||||
...
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
## GET /collection/<element identifier>
|
||||
|
||||
"" oder
|
||||
{}
|
||||
|
||||
## POST /collection
|
||||
|
||||
{
|
||||
values: [ // Liste hinzuzufügender Elemente
|
||||
{ // Hinzuzufügendes Element
|
||||
<KEY0>: <VALUE0>,
|
||||
<KEY1>: <VALUE1>,
|
||||
...
|
||||
},
|
||||
{
|
||||
...
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
## DELETE /collection/<element identifier>
|
||||
|
||||
"" oder
|
||||
{}
|
||||
|
||||
## PUT /collection/<element identifier>
|
||||
|
||||
{
|
||||
clear: {
|
||||
<KEY0>: null, // Feld welches gelöscht werden soll
|
||||
...
|
||||
},
|
||||
set: {
|
||||
<KEY1>: <VALUE1>, // Feld welches auf einen definierten Wert gesetzt werden soll
|
||||
...
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -47,6 +47,8 @@
|
|||
<Compile Include="reflection\Reflector.cs" />
|
||||
<Compile Include="CollectionResource<T>.cs" />
|
||||
<Compile Include="ObjectContainerResource.cs" />
|
||||
<Compile Include="collections\JSONCollectionResource<T>.cs" />
|
||||
<Compile Include="collections\IEntityCollectionInterface.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ln.http\ln.http.csproj">
|
||||
|
@ -68,9 +70,12 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="reflection\" />
|
||||
<Folder Include="doc\" />
|
||||
<Folder Include="collections\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
<None Include="doc\JSONCollection.txt" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
Loading…
Reference in New Issue