diff --git a/src/Metadata/Controllers/MetadataController.cs b/src/Metadata/Controllers/MetadataController.cs index 8fd2ef827fc47107783348a9fb20b65326696d64..843de98414263e7900751a2b5c1bfb766b7c3cca 100644 --- a/src/Metadata/Controllers/MetadataController.cs +++ b/src/Metadata/Controllers/MetadataController.cs @@ -84,7 +84,7 @@ namespace Coscine.Api.Metadata.Controllers var tripleStore = new TripleStore(); tripleStore.Add(graph); - var outStoreJson = VDS.RDF.Writing.StringWriter.Write(tripleStore, new JsonLdWriter()); + var outStoreJson = StringWriter.Write(tripleStore, new JsonLdWriter()); var json = JToken.Parse(outStoreJson); @@ -96,6 +96,7 @@ namespace Coscine.Api.Metadata.Controllers /// </summary> /// <throws>Exception for the code that has not been implemented</throws> [HttpGet("[controller]/vocabularies/")] + [AllowAnonymous] public IActionResult GetVocabularies() { throw new NotImplementedException(); @@ -107,94 +108,66 @@ namespace Coscine.Api.Metadata.Controllers /// <param name="path">Url of the vocabulary</param> /// <returns>JSON with the requested vocabulary</returns> [HttpGet("[controller]/vocabularies/{path}")] - public IActionResult GetVocabulary(string path) + [AllowAnonymous] + public ActionResult<BilingualLabels> GetVocabulary(string path) { var graph = _rdfStoreConnector.GetGraph(HttpUtility.UrlDecode(path)); - var de = new JArray(); - foreach (var kv in _rdfStoreConnector.GetVocabularyLabels(graph, "de")) - { - JObject obj = new JObject - { - ["value"] = kv.Key, - ["name"] = kv.Value - }; - de.Add(obj); - } + var bilingualLabels = RetrieveBilingualLabels(graph); - var en = new JArray(); - foreach (var kv in _rdfStoreConnector.GetVocabularyLabels(graph, "en")) - { - JObject obj = new JObject - { - ["value"] = kv.Key, - ["name"] = kv.Value - }; - en.Add(obj); - } - - JObject json = new JObject - { - ["de"] = de, - ["en"] = en - }; - - return Json(json); + return Ok(bilingualLabels); } /// <summary> /// This method returns instances. /// </summary> - /// <param name="projectId">Id of the project</param> /// <param name="className">class name</param> /// <returns>instances as Json, or throw an Exception if the user has not beed authorized</returns> - [HttpGet("[controller]/instances/{projectId}/{className}")] - public IActionResult GetClassInstances(Guid projectId, string className) + [HttpGet("[controller]/instances/{className}")] + [AllowAnonymous] + public ActionResult<BilingualLabels> GetClassInstances(string className) { - var user = _authenticator.GetUser(); - ProjectModel projectModel = new ProjectModel(); - if (projectModel.HasAccess(user, projectModel.GetById(projectId), UserRoles.Owner, UserRoles.Member)) + if (!Uri.TryCreate(HttpUtility.UrlDecode(className), UriKind.Absolute, out Uri uri)) { - if (!Uri.TryCreate(HttpUtility.UrlDecode(className), UriKind.Absolute, out Uri uri)) - { - throw new ArgumentException("ClassName is not a valid Uri."); - } - var graph = _rdfStoreConnector.GetClassGraph(uri); + return BadRequest("ClassName is not a valid Uri."); + } + var graph = _rdfStoreConnector.GetClassGraph(uri); - var de = new JArray(); - foreach (var kv in _rdfStoreConnector.GetVocabularyLabels(graph, "de")) - { - JObject obj = new JObject - { - ["value"] = kv.Key, - ["name"] = kv.Value - }; - de.Add(obj); - } - - var en = new JArray(); - foreach (var kv in _rdfStoreConnector.GetVocabularyLabels(graph, "en")) - { - JObject obj = new JObject - { - ["value"] = kv.Key, - ["name"] = kv.Value - }; - en.Add(obj); - } - - JObject json = new JObject + var bilingualLabels = RetrieveBilingualLabels(graph); + + return Ok(bilingualLabels); + } + + /// <summary> + /// Helper method which converts a graph to the described labels + /// </summary> + /// <param name="graph">RDF graph with labels</param> + /// <returns>BilingualLabels</returns> + private BilingualLabels RetrieveBilingualLabels(IGraph graph) + { + var bilingualLabels = new BilingualLabels(); + + foreach (var kv in _rdfStoreConnector.GetVocabularyLabels(graph, "de")) + { + var label = new Label { - ["de"] = de, - ["en"] = en + Value = kv.Key, + Name = kv.Value }; - - return Json(json); + bilingualLabels.De.Add(label); } - else + + foreach (var kv in _rdfStoreConnector.GetVocabularyLabels(graph, "en")) { - throw new NotAuthorizedException("User is no project member!"); + var label = new Label + { + Value = kv.Key, + Name = kv.Value + }; + bilingualLabels.En.Add(label); } + + return bilingualLabels; } /// <summary> diff --git a/src/Metadata/ParameterObjects/BilingualLabels.cs b/src/Metadata/ParameterObjects/BilingualLabels.cs new file mode 100644 index 0000000000000000000000000000000000000000..ac6d0147f07d3dca3b2ce9cde13df3466ce64f23 --- /dev/null +++ b/src/Metadata/ParameterObjects/BilingualLabels.cs @@ -0,0 +1,22 @@ +using Newtonsoft.Json; +using System.Collections.Generic; + +namespace Coscine.Api.Metadata.ParameterObjects +{ + /// <summary> + /// Bilingual labels + /// </summary> + public class BilingualLabels + { + /// <summary> + /// English labels + /// </summary> + [JsonProperty("en")] + public ICollection<Label> En { get; set; } = new List<Label>(); + /// <summary> + /// German labels + /// </summary> + [JsonProperty("de")] + public ICollection<Label> De { get; set; } = new List<Label>(); + } +} diff --git a/src/Metadata/ParameterObjects/Label.cs b/src/Metadata/ParameterObjects/Label.cs new file mode 100644 index 0000000000000000000000000000000000000000..556e80c73685bdb6da043606832b96126876f7fc --- /dev/null +++ b/src/Metadata/ParameterObjects/Label.cs @@ -0,0 +1,21 @@ +using Newtonsoft.Json; + +namespace Coscine.Api.Metadata.ParameterObjects +{ + /// <summary> + /// Label of a vocabulary entry + /// </summary> + public class Label + { + /// <summary> + /// Name of the application profile + /// </summary> + [JsonProperty("name")] + public string Name { get; set; } + /// <summary> + /// Name of the application profile + /// </summary> + [JsonProperty("value")] + public string Value { get; set; } + } +}