Commit d7317f08 authored by Benedikt Heinrichs's avatar Benedikt Heinrichs
Browse files

Merge branch 'Topic/1115-docuProjectApi' into 'Product/932-docuProjectApi'

Update: Document the Project Api

See merge request coscine/api/project!113
parents 94de2aff 779e1477
......@@ -86,8 +86,8 @@
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.SqlServer.dll</HintPath>
</Reference>
<Reference Include="HtmlAgilityPack, Version=1.8.2.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<HintPath>..\packages\HtmlAgilityPack.1.8.2\lib\Net45\HtmlAgilityPack.dll</HintPath>
<Reference Include="HtmlAgilityPack, Version=1.11.17.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<HintPath>..\packages\HtmlAgilityPack.1.11.17\lib\Net45\HtmlAgilityPack.dll</HintPath>
</Reference>
<Reference Include="linq2db, Version=3.1.1.0, Culture=neutral, PublicKeyToken=e41013125f9e410a, processorArchitecture=MSIL">
<HintPath>..\packages\linq2db.3.1.1\lib\net46\linq2db.dll</HintPath>
......
......@@ -21,19 +21,22 @@ namespace Coscine.Api.Project.Tests
{
private static ILogger<ProjectController> projectLogger = null;
private static ILogger<ProjectController> ProjectLogger { get
private static ILogger<ProjectController> ProjectLogger
{
if (projectLogger == null)
get
{
CoscineLoggerConfiguration.SetConfig();
projectLogger = new NullLogger<ProjectController>();
if (projectLogger == null)
{
CoscineLoggerConfiguration.SetConfig();
projectLogger = new NullLogger<ProjectController>();
}
return projectLogger;
}
return projectLogger;
} }
}
public ProjectControllerTests() : base(new ProjectController(ProjectLogger))
{
}
[Test]
......@@ -55,14 +58,14 @@ namespace Coscine.Api.Project.Tests
[Test]
public void TestControllerIndex()
{
var actionResult = Controller.Index();
var actionResult = Controller.Index().Result;
Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
}
[Test]
public void TestControllerGet()
{
var actionResult = Controller.Get(Projects[0].Id.ToString());
var actionResult = Controller.Get(Projects[0].Id.ToString()).Result;
Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
OkObjectResult okObjectResult = (OkObjectResult)actionResult;
......@@ -83,17 +86,17 @@ namespace Coscine.Api.Project.Tests
[Test]
public void TestControllerUpdate()
{
var actionResult = Controller.Get(Projects[0].Id.ToString());
var actionResult = Controller.Get(Projects[0].Id.ToString()).Result;
ProjectObject projectObject = (ProjectObject)((OkObjectResult)actionResult).Value;
projectObject.Description = "ChangedDescription";
Stream stream = ObjectFactory<ProjectObject>.SerializeToStream(projectObject);
FakeControllerContext(Users[0], stream);
actionResult = Controller.Update(Projects[0].Id.ToString());
Assert.IsTrue(actionResult.GetType() == typeof(OkObjectResult));
var updateResult = Controller.Update(Projects[0].Id.ToString());
Assert.IsTrue(updateResult.GetType() == typeof(OkObjectResult));
// Cleanup
stream.Close();
......@@ -102,8 +105,8 @@ namespace Coscine.Api.Project.Tests
FakeControllerContext(Users[0], stream);
actionResult = Controller.Update(Projects[1].Id.ToString());
Assert.IsTrue(actionResult.GetType() == typeof(UnauthorizedObjectResult));
updateResult = Controller.Update(Projects[1].Id.ToString());
Assert.IsTrue(updateResult.GetType() == typeof(UnauthorizedObjectResult));
// Cleanup
stream.Close();
......@@ -112,7 +115,7 @@ namespace Coscine.Api.Project.Tests
[Test]
public void TestControllerStore()
{
ProjectObject newProjectObject = new ProjectObject(Guid.NewGuid(), "NewProject", "NewDisplayName", DateTime.Now, DateTime.Now.AddYears(1), "test2;test3", "abc", "investigator", "grandId",
ProjectObject newProjectObject = new ProjectObject(Guid.NewGuid(), "NewProject", "NewDisplayName", DateTime.Now, DateTime.Now.AddYears(1), "test2;test3", "abc", "investigator", "grandId",
new List<DisciplineObject>() { new DisciplineObject(Discipline.Id, Discipline.Url, Discipline.DisplayNameDe, Discipline.DisplayNameEn) },
new List<OrganizationObject>() { new OrganizationObject(Organization, Organization) },
new VisibilityObject(Visibility.Id, Visibility.DisplayName), Guid.NewGuid().ToString());
......@@ -181,7 +184,7 @@ namespace Coscine.Api.Project.Tests
stream.Close();
Controller.DeleteProject(project, true);
}
[Test]
public void TestControllerDeleteWithSubProjects()
{
......@@ -247,7 +250,7 @@ namespace Coscine.Api.Project.Tests
ProjectObject newSubProjectObject = new ProjectObject(Guid.NewGuid(), "NewSubProject", "NewDisplayNameSub", DateTime.Now, DateTime.Now.AddYears(1), "test2;test3", "abc", "investigator", "grandId",
new List<DisciplineObject>() { new DisciplineObject(Discipline.Id, Discipline.Url, Discipline.DisplayNameDe, Discipline.DisplayNameEn) },
new List<OrganizationObject>() { new OrganizationObject(Organization, Organization) },
new VisibilityObject(Visibility.Id, Visibility.DisplayName), Guid.NewGuid().ToString(),
new VisibilityObject(Visibility.Id, Visibility.DisplayName), Guid.NewGuid().ToString(),
createdProjectObject.Id);
var subStream = ObjectFactory<ProjectObject>.SerializeToStream(newSubProjectObject);
......@@ -259,7 +262,7 @@ namespace Coscine.Api.Project.Tests
SubProjectModel subProjectModel = new SubProjectModel();
var subProjects = subProjectModel.GetAllWhere((x) => x.ProjectId == createdProjectObject.Id);
foreach(var subProject in subProjects)
foreach (var subProject in subProjects)
{
Assert.IsTrue(subProject.SubProjectId == createdSubProjectObject.Id);
Assert.IsTrue(subProject.ProjectId == createdProjectObject.Id);
......@@ -281,4 +284,4 @@ namespace Coscine.Api.Project.Tests
Assert.IsTrue(Previous == projectModel.GetAll().ToArray().Length - Projects.Count);
}
}
}
\ No newline at end of file
}
......@@ -11,7 +11,7 @@
<package id="Coscine.Logging" version="1.2.0" targetFramework="net472" />
<package id="Coscine.ProxyApi" version="1.3.0" targetFramework="net472" />
<package id="EntityFramework" version="6.4.4" targetFramework="net472" />
<package id="HtmlAgilityPack" version="1.8.2" targetFramework="net472" />
<package id="HtmlAgilityPack" version="1.11.17" targetFramework="net472" />
<package id="linq2db" version="3.1.1" targetFramework="net472" />
<package id="linq2db.SqlServer" version="2.6.4" targetFramework="net472" />
<package id="linq2db.t4models" version="2.6.4" targetFramework="net472" />
......
......@@ -2,22 +2,34 @@
using Coscine.Database.ReturnObjects;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
namespace Coscine.Api.Project.Controllers
{
/// <summary>
/// This controller represents the actions which can be taken with a discipline object.
/// </summary>
[Authorize]
public class DisciplineController : Controller
{
private readonly DisciplineModel _disciplineModel;
/// <summary>
/// DisciplineController constructor specifying a DisciplineModel
/// </summary>
public DisciplineController()
{
_disciplineModel = new DisciplineModel();
}
/// <summary>
/// Returns all available disciplines
/// </summary>
/// <returns>All Disciplines</returns>
[Route("[controller]")]
public IActionResult Index()
public ActionResult<IEnumerable<DisciplineObject>> Index()
{
return Json(_disciplineModel.GetAll()
.OrderBy(discipline => discipline.DisplayNameDe.Substring(discipline.DisplayNameDe.Length - 3))
......
......@@ -2,22 +2,34 @@
using Coscine.Database.ReturnObjects;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
namespace Coscine.Api.Project.Controllers
{
/// <summary>
/// This controller represents the actions which can be taken with a license object.
/// </summary>
[Authorize]
public class LicenseController : Controller
{
private readonly LicenseModel _licenseModel;
/// <summary>
/// LicenseController constructor specifying a LicenseModel
/// </summary>
public LicenseController()
{
_licenseModel = new LicenseModel();
}
/// <summary>
/// Returns all available licenses
/// </summary>
/// <returns>All Licenses</returns>
[Route("[controller]")]
public IActionResult Index()
public ActionResult<IEnumerable<LicenseObject>> Index()
{
return Json(_licenseModel.GetAll()
.Select((license) => new LicenseObject(license.Id, license.DisplayName)));
......
......@@ -20,6 +20,10 @@ using Coscine.Metadata;
namespace Coscine.Api.Project.Controllers
{
/// <summary>
/// /// This controller represents the actions which can be taken with a project object.
/// </summary>
[Authorize]
public class ProjectController : Controller
{
......@@ -32,6 +36,10 @@ namespace Coscine.Api.Project.Controllers
private readonly CoscineLogger _coscineLogger;
private readonly AnalyticsLogObject _analyticsLogObject;
/// <summary>
/// ProjectController constructor
/// </summary>
/// <param name="logger">Logger</param>
public ProjectController(ILogger<ProjectController> logger)
{
_authenticator = new Authenticator(this, Program.Configuration);
......@@ -43,9 +51,13 @@ namespace Coscine.Api.Project.Controllers
_coscineLogger = new CoscineLogger(logger);
_analyticsLogObject = new AnalyticsLogObject();
}
/// <summary>
/// Returns all available projects (including sub projects)
/// </summary>
/// <returns>Ok</returns>
[Route("[controller]")]
public IActionResult Index()
public ActionResult<IEnumerable<ProjectObject>> Index()
{
var user = _authenticator.GetUser();
var result = _projectModel.GetWithAccess(user, UserRoles.Member, UserRoles.Owner).ToList()
......@@ -60,8 +72,12 @@ namespace Coscine.Api.Project.Controllers
return Ok(result);
}
/// <summary>
/// Retrieves all top level projects
/// </summary>
/// <returns>Ok</returns>
[Route("[controller]/-/topLevel")]
public IActionResult GetTopLevelProjects()
public ActionResult<IEnumerable<ProjectObject>> GetTopLevelProjects()
{
var user = _authenticator.GetUser();
var result = _projectModel.GetTopLevelWithAccess(user, UserRoles.Member, UserRoles.Owner).ToList()
......@@ -76,8 +92,13 @@ namespace Coscine.Api.Project.Controllers
return Ok(result);
}
/// <summary>
/// This returns the the project if the user has access to it
/// </summary>
/// <param name="id">Id of the resource</param>
/// <returns>Ok or Statuscode 401</returns>
[HttpGet("[controller]/{id}")]
public IActionResult Get(string id)
public ActionResult<ProjectObject> Get(string id)
{
var user = _authenticator.GetUser();
var project = _projectModel.GetById(Guid.Parse(id));
......@@ -99,8 +120,13 @@ namespace Coscine.Api.Project.Controllers
}
}
/// <summary>
/// Gets the resources
/// </summary>
/// <param name="id">Id of the resource</param>
/// <returns>Json object or Statuscode 401</returns>
[HttpGet("[controller]/{id}/resources")]
public IActionResult GetResources(string id)
public ActionResult<IEnumerable<ResourceObject>> GetResources(string id)
{
var project = _projectModel.GetById(Guid.Parse(id));
var user = _authenticator.GetUser();
......@@ -129,8 +155,13 @@ namespace Coscine.Api.Project.Controllers
}
}
/// <summary>
/// Retrieves the quota for the selected project
/// </summary>
/// <param name="id">Id of the resource</param>
/// <returns>Json object or Statuscode 401</returns>
[HttpGet("[controller]/{id}/quotas")]
public IActionResult Quotas(string id)
public ActionResult<IEnumerable<ProjectQuota>> Quotas(string id)
{
var user = _authenticator.GetUser();
var projectObject = ObjectFactory<ProjectObject>.DeserializeFromStream(Request.Body);
......@@ -177,6 +208,11 @@ namespace Coscine.Api.Project.Controllers
}
}
/// <summary>
/// Updates the selected project
/// </summary>
/// <param name="id">Id of the resource</param>
/// <returns>Ok or Statuscode 401</returns>
[HttpPost("[controller]/{id}")]
public IActionResult Update(string id)
{
......@@ -194,6 +230,11 @@ namespace Coscine.Api.Project.Controllers
}
}
/// <summary>
/// Deletes the selected project
/// </summary>
/// <param name="id">Id of the resource</param>
/// <returns>Json object or Statuscode 401</returns>
[HttpDelete("[controller]/{id}")]
public IActionResult Delete(string id)
{
......@@ -211,6 +252,12 @@ namespace Coscine.Api.Project.Controllers
}
}
/// <summary>
/// Deletes the project
/// </summary>
/// <param name="project">Project</param>
/// <param name="isHard">isHard</param>
/// <param name="propegateAction">propegate Action</param>
public void DeleteProject(Database.DataModel.Project project, bool isHard = false, bool propegateAction = true)
{
var subProjectModel = new SubProjectModel();
......@@ -292,6 +339,10 @@ namespace Coscine.Api.Project.Controllers
}
}
/// <summary>
/// Creates a project
/// </summary>
/// <returns>Json object or Statuscode 401</returns>
[HttpPost("[controller]")]
public IActionResult Store()
{
......@@ -299,8 +350,7 @@ namespace Coscine.Api.Project.Controllers
var isRWTHMember = IsRWTHMember(user);
var projectObject = ObjectFactory<ProjectObject>.DeserializeFromStream(Request.Body);
if (projectObject.ParentId != null
&& projectObject.ParentId != new Guid()
if (projectObject?.ParentId != new Guid()
&& !_projectModel.HasAccess(user, _projectModel.GetById(projectObject.ParentId), UserRoles.Owner))
{
return Unauthorized("User is not allowed to create SubProjects.");
......@@ -328,6 +378,11 @@ namespace Coscine.Api.Project.Controllers
return Json(_projectModel.CreateReturnObjectFromDatabaseObject(project));
}
/// <summary>
/// Checks if the given user is a member of the RWTH
/// </summary>
/// <param name="user">User object</param>
/// <returns>True, if member of RWTH or false, if not a member of RWTH</returns>
private bool IsRWTHMember(User user)
{
var externalIds = new ExternalIdModel().GetAllWhere((externalId) => externalId.UserId == user.Id);
......@@ -344,6 +399,14 @@ namespace Coscine.Api.Project.Controllers
return new RdfStoreConnector(Program.Configuration.GetStringAndWait("coscine/local/virtuoso/additional/url")).GetTriples(new Uri("https://ror.org/04xfq0f34"), null, null, 1, externalIdList).Count() != 0;
}
/// <summary>
/// LogAnalytics
/// </summary>
/// <param name="operation">Operation</param>
/// <param name="projects">Projects</param>
/// <param name="resources">Resources</param>
/// <param name="projectId">Id of the project</param>
/// <param name="user">User object</param>
private void LogAnalytics(string operation,
IEnumerable<ProjectObject> projects = null,
IEnumerable<ResourceObject> resources = null,
......
......@@ -18,6 +18,10 @@ using Coscine.Database.DataModel;
namespace Coscine.Api.Project.Controllers
{
/// <summary>
/// This represents the actions which can be taken with a ProjectRole Object
/// </summary>
[Authorize]
public class ProjectRoleController : Controller
{
......@@ -28,6 +32,10 @@ namespace Coscine.Api.Project.Controllers
private readonly CoscineLogger _coscineLogger;
private readonly AnalyticsLogObject _analyticsLogObject;
/// <summary>
/// ProjectRoleController specifying an Emmiter, an Authentication,a ProjectRoleModel and a AnalyticsLogObject
/// </summary>
/// <param name="logger">Logger</param>
public ProjectRoleController(ILogger<ProjectRoleController> logger)
{
_emitter = new Emitter(Program.Configuration);
......@@ -39,8 +47,13 @@ namespace Coscine.Api.Project.Controllers
_analyticsLogObject = new AnalyticsLogObject();
}
/// <summary>
/// Lists all users to the given project
/// </summary>
/// <param name="projectId">Id of the project</param>
/// <returns>ProjectRoleObject or Json Object or Statuscode 401</returns>
[Route("[controller]/{projectId}")]
public IActionResult Index(string projectId)
public ActionResult<IEnumerable<ProjectRoleObject>> Index(string projectId)
{
var userModel = new UserModel();
var roleModel = new RoleModel();
......@@ -79,9 +92,13 @@ namespace Coscine.Api.Project.Controllers
}
}
//Get all roles for current user and given project
/// <summary>
/// Gets all roles for current user and given object
/// </summary>
/// <param name="projectId">Id of the project</param>
/// <returns>Json Object or ProjectRoleObject</returns>
[HttpGet("[controller]/project/{projectId}")]
public IActionResult Get(string projectId)
public ActionResult<IEnumerable<ProjectRoleObject>> Get(string projectId)
{
var roleModel = new RoleModel();
Guid.TryParse(projectId, out Guid projectIdGuid);
......@@ -101,8 +118,12 @@ namespace Coscine.Api.Project.Controllers
}));
}
/// <summary>
/// sets a project role for the given project
/// </summary>
/// <returns>Json Object or Statuscode 401</returns>
[HttpPost("[controller]")]
public IActionResult Set()
public ActionResult<ProjectRoleObject> Set()
{
var projectRoleObject = ObjectFactory<ProjectRoleObject>.DeserializeFromStream(Request.Body);
var projectModel = new ProjectModel();
......@@ -133,6 +154,13 @@ namespace Coscine.Api.Project.Controllers
}
}
/// <summary>
/// deletes project role for the given project
/// </summary>
/// <param name="projectId">Id of the project</param>
/// <param name="userId">Id of the user</param>
/// <param name="roleId">Id of the role</param>
/// <returns>Json Object or Statuscode 401</returns>
[HttpDelete("[controller]/project/{projectId}/user/{userId}/role/{roleId}")]
public IActionResult Delete(Guid projectId, Guid userId, Guid roleId)
{
......@@ -166,6 +194,11 @@ namespace Coscine.Api.Project.Controllers
}
}
/// <summary>
/// Deletes user from a project
/// </summary>
/// <param name="projectId">Id of the project</param>
/// <returns>Json Object or Statuscode 401</returns>
[HttpDelete("[controller]/project/{projectId}/user")]
public IActionResult Delete(Guid projectId)
{
......@@ -195,6 +228,13 @@ namespace Coscine.Api.Project.Controllers
}
}
/// <summary>
/// Log Analytics
/// </summary>
/// <param name="operation">Operation</param>
/// <param name="user">User object</param>
/// <param name="users">Users</param>
/// <param name="projectId">Id of the project</param>
private void LogAnalytics(string operation,
User user,
IEnumerable<ProjectRoleObject> users = null,
......
......@@ -2,22 +2,34 @@
using Coscine.Database.ReturnObjects;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
namespace Coscine.Api.Project.Controllers
{
/// <summary>
/// This controller represents the actions which can be taken with a role object
/// </summary>
[Authorize]
public class RoleController : Controller
{
private readonly RoleModel _roleModel;
/// <summary>
/// RoleController specifying a RoleModel
/// </summary>
public RoleController()
{
_roleModel = new RoleModel();
}
/// <summary>
/// Returns all available roles
/// </summary>
/// <returns>All Roles </returns>
[Route("[controller]")]
public IActionResult Index()
public ActionResult<IEnumerable<RoleObject>> Index()
{
return Json(_roleModel.GetAll()
.Select((role) => new RoleObject(role.Id, role.DisplayName)));
......
......@@ -15,6 +15,10 @@ using Coscine.Logging;
namespace Coscine.Api.Project.Controllers
{
/// <summary>
/// This controller represents the actions which can be taken with a search object
/// </summary>
[Authorize]
public class SearchController : Controller
{
......@@ -23,6 +27,10 @@ namespace Coscine.Api.Project.Controllers
private readonly CoscineLogger _coscineLogger;
private readonly AnalyticsLogObject _analyticsLogObject;
/// <summary>
/// SearchController specifying a ProjectRoleModel and an AnalyticsObject
/// </summary>
/// <param name="logger">Logger</param>
public SearchController(ILogger<SearchController> logger)
{
_authenticator = new Authenticator(this, Program.Configuration);
......@@ -32,6 +40,10 @@ namespace Coscine.Api.Project.Controllers
_analyticsLogObject = new AnalyticsLogObject();
}
/// <summary>
/// Searchs no filter
/// </summary>
/// <returns>Ok</returns>
[HttpGet("[controller]/allNoFilter/")]
public IActionResult SearchNoFilter()
{
......@@ -39,6 +51,11 @@ namespace Coscine.Api.Project.Controllers
return Ok(GetSearchResults(user.Id, "", ""));
}
/// <summary>
/// Returns results for the search word
/// </summary>
/// <param name="encodedSearchWord">Encoded search word</param>
/// <returns>Ok</returns>
[HttpGet("[controller]/all/{encodedSearchWord}")]
public IActionResult Search(string encodedSearchWord)
{
......@@ -46,6 +63,11 @@ namespace Coscine.Api.Project.Controllers
return Ok(GetSearchResults(user.Id, encodedSearchWord, ""));
}
/// <summary>
/// Returns searched projects with no filter
/// </summary>
/// <param name="projectId">Id of the project</param>
/// <returns>Ok</returns>
[HttpGet("[controller]/projectNoFilter/{projectId}")]
public IActionResult SearchProjectNoFilter(string projectId)
{
......@@ -53,6 +75,12 @@ namespace Coscine.Api.Project.Controllers
return Ok(GetSearchResults(user.Id, "", projectId));
}
/// <summary>
/// Returns searched projects
/// </summary>
/// <param name="projectId">Id of the project</param>
/// <param name="encodedSearchWord">Encoded search word</param>
/// <returns>Ok</returns>
[HttpGet("[controller]/project/{projectId}/{encodedSearchWord}")]
public IActionResult SearchProject(string projectId, string encodedSearchWord)
{
......@@ -60,6 +88,13 @@ namespace Coscine.Api.Project.Controllers
return Ok(GetSearchResults(user.Id, encodedSearchWord, projectId));
}
/// <summary>
/// This method gets the search results
/// </summary>
/// <param name="userId">Id of the user</param>
/// <param name="encodedSearchWord">Encoded search word</param>
/// <param name="projectId">Id of the project</param>
/// <returns>Search results</returns>