From f8275974b148370178424584965bb97742fecd9f Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Wed, 19 Jun 2019 12:31:28 +0200 Subject: [PATCH] WIP --- collections/EntityMapper.cs | 4 +- collections/JSONCollectionResource.cs | 49 +++++++++++++++++++++++- doc/JSONCollection.txt | 7 ++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/collections/EntityMapper.cs b/collections/EntityMapper.cs index 4b02d86..c52d3cb 100644 --- a/collections/EntityMapper.cs +++ b/collections/EntityMapper.cs @@ -9,8 +9,8 @@ namespace ln.http.resources.collections { public JSONConvert JSONConvert { get; set; } - Dictionary publicFields = new Dictionary(); - Dictionary publicProperties = new Dictionary(); + public Dictionary publicFields = new Dictionary(); + public Dictionary publicProperties = new Dictionary(); public EntityMapper() { diff --git a/collections/JSONCollectionResource.cs b/collections/JSONCollectionResource.cs index fa6456d..cfa5891 100644 --- a/collections/JSONCollectionResource.cs +++ b/collections/JSONCollectionResource.cs @@ -19,6 +19,7 @@ using System.ComponentModel; using ln.types.odb.mapped; using ln.types.threads; using ln.types.json; +using System.Reflection; namespace ln.http.resources.collections { @@ -61,7 +62,11 @@ namespace ln.http.resources.collections object documentID = collection.Mapper.GetDocumentID(ni); httpResponse.SetHeader("Location", String.Format("/{0}/{1}", String.Join("/", this.Path), documentID.ToString())); httpResponse.StatusCode = 201; - responseValue = ni;*/ + responseValue = ni; +*/ + break; + case "DESC": + collectionResult = Describe(httpRequest); break; default: throw new NotSupportedException(); @@ -90,6 +95,40 @@ namespace ln.http.resources.collections return httpResponse; } + private CollectionResult Describe(HttpRequest request) + { + CollectionResult collectionResult = new CollectionResult(); + collectionResult.Descriptor = new JObject(); + + JArray jProperties = new JArray(); + + foreach (FieldInfo fieldInfo in EntityMapper.publicFields.Values) + { + String skyType = JSONConvert.GetSkyType(fieldInfo.FieldType); + if (skyType != null) + { + JObject field = new JObject(); + field.Add("name", JToken.FromObject(fieldInfo.Name)); + field.Add("type", JToken.FromObject(skyType)); + jProperties.Add(field); + } + } + foreach (PropertyInfo propertyInfo in EntityMapper.publicProperties.Values) + { + String skyType = JSONConvert.GetSkyType(propertyInfo.PropertyType); + if (skyType != null) + { + JObject field = new JObject(); + field.Add("name", JToken.FromObject(propertyInfo.Name)); + field.Add("type", JToken.FromObject(skyType)); + jProperties.Add(field); + } + } + + collectionResult.Descriptor.Add("properties", jProperties); + return collectionResult; + } + private CollectionResult Query(HttpRequest request) { CollectionResult collectionResult = new CollectionResult(); @@ -120,13 +159,17 @@ namespace ln.http.resources.collections surround.Add("values", jvalues); JObject jfailed = new JObject(); - foreach (TIDENT id in collectionResult.Failed.Keys) { jfailed.Add(id.ToString(), collectionResult.Failed[id]); } surround.Add("failed", jfailed); + if (collectionResult.Descriptor != null) + { + surround.Add("desc", collectionResult.Descriptor); + } + response.SetHeader("content-type", "application/json"); response.ContentWriter.Write(surround.ToString()); @@ -137,6 +180,8 @@ namespace ln.http.resources.collections { public List Values { get; } = new List(); public Dictionary Failed { get; } = new Dictionary(); + + public JObject Descriptor { get; set; } } } diff --git a/doc/JSONCollection.txt b/doc/JSONCollection.txt index d920e01..77654db 100644 --- a/doc/JSONCollection.txt +++ b/doc/JSONCollection.txt @@ -1,5 +1,6 @@ # Unterstützte Methoden: +DESC /collection Abruf der Objektbeschreibung für diese Collection GET /collection Abruf von Elementen aus der Kollektion, optional mit Filter GET /collection/ Abruf eines definierten Element der Kollektion POST /collection Hinzufügen neuer Elemente zur Kollektion @@ -18,6 +19,12 @@ DELETE /collection/ Entfernen eines Elements der Kollekt : , // Element wurde erfolgreich hinzugefügt / geändert : null, // Element wurde erfolgreich gelöscht }, + desc: { + properties: [ + { name: , type: , readonly: true/false, }, + ... + ], + }, } # Anfrageschemata