Skip to content
Snippets Groups Projects
Commit fa35ed45 authored by Benedikt Heinrichs's avatar Benedikt Heinrichs
Browse files

Adapt vocabulary retrieval methods (coscine/issues#1804)

parent ee4a0fc9
No related branches found
No related tags found
1 merge request!44Breaking: FixedValue correction (coscine/issues#1804)
......@@ -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 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
};
var bilingualLabels = RetrieveBilingualLabels(graph);
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)
{
var user = _authenticator.GetUser();
ProjectModel projectModel = new ProjectModel();
if (projectModel.HasAccess(user, projectModel.GetById(projectId), UserRoles.Owner, UserRoles.Member))
[HttpGet("[controller]/instances/{className}")]
[AllowAnonymous]
public ActionResult<BilingualLabels> GetClassInstances(string className)
{
if (!Uri.TryCreate(HttpUtility.UrlDecode(className), UriKind.Absolute, out Uri uri))
{
throw new ArgumentException("ClassName is not a valid Uri.");
return BadRequest("ClassName is not a valid Uri.");
}
var graph = _rdfStoreConnector.GetClassGraph(uri);
var de = new JArray();
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"))
{
JObject obj = new JObject
var label = new Label
{
["value"] = kv.Key,
["name"] = kv.Value
Value = kv.Key,
Name = kv.Value
};
de.Add(obj);
bilingualLabels.De.Add(label);
}
var en = new JArray();
foreach (var kv in _rdfStoreConnector.GetVocabularyLabels(graph, "en"))
{
JObject obj = new JObject
var label = new Label
{
["value"] = kv.Key,
["name"] = kv.Value
Value = kv.Key,
Name = kv.Value
};
en.Add(obj);
bilingualLabels.En.Add(label);
}
JObject json = new JObject
{
["de"] = de,
["en"] = en
};
return Json(json);
}
else
{
throw new NotAuthorizedException("User is no project member!");
}
return bilingualLabels;
}
/// <summary>
......
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>();
}
}
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; }
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment