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

New: ResourceTypeController and listing all resources to a project (coscine/issues#181)

parent d6a3c2ca
Branches
Tags
2 merge requests!7Product/168 basic structure,!6Topic/181 resource handling
......@@ -109,6 +109,21 @@ namespace Coscine.Api.Project.Tests
resourceModel.Insert(resource2);
projectModel.AddResource(project2, resource2);
Resources.Add(resource2);
project = new Coscine.Database.Model.Project()
{
Description = "Description",
Organization = "Organization",
DisplayName = "TestProject",
StartDate = DateTime.Now,
EndDate = DateTime.Now.AddYears(1),
Keywords = "Test1;Test2"
};
Guid.TryParse("3d058224-8bbc-45f1-8ad1-042b65d0e83b", out Guid newId);
project.Id = newId;
projectModel.Insert(project);
projectRole = projectModel.SetOwner(project, user);
}
......
......@@ -6,6 +6,7 @@ using Coscine.Api.Project.ReturnObjects;
using Coscine.ApiCommons;
using Coscine.ApiCommons.Exceptions;
using Coscine.ApiCommons.Factories;
using Coscine.Database.Model;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
......@@ -61,6 +62,37 @@ namespace Coscine.Api.Project.Controllers
}));
}
[HttpGet("[controller]/{id}/resources")]
public IActionResult GetResources(string id)
{
return Ok(_authenticator.ValidateAndExecute((user) =>
{
var project = _projectModel.GetById(Guid.Parse(id));
ResourceModel resourceModel = new ResourceModel();
ResourceTypeModel resourceTypeModel = new ResourceTypeModel();
if (_projectModel.CanSeeProject(user, project))
{
return resourceModel.GetAllWhere((resource) =>
(from projectResource in resource.ProjectResourceResourceIdIds
where projectResource.ProjectId == project.Id
select projectResource).Any())
.Select((resource) =>
{
ResourceType resourceType = resource.Type;
if (resourceType == null)
{
resourceType = resourceTypeModel.GetById(resource.TypeId);
}
return new ResourceObject(resource.Id, resource.ExternalId, resource.Url, new ResourceTypeObject(resourceType.Id, resourceType.DisplayName));
});
}
else
{
throw new UnauthorizedAccessException("User cannot see resources of given project!");
}
}));
}
[HttpPost("[controller]/{id}")]
public IActionResult Update(string id)
{
......
using Coscine.Api.Project.Models;
using Coscine.Api.Project.ReturnObjects;
using Coscine.ApiCommons;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.Api.Project.Controllers
{
public class ResourceTypeController : Controller
{
private readonly Authenticator _authenticator;
private readonly ResourceTypeModel _resourceTypeModel;
public ResourceTypeController()
{
_authenticator = new Authenticator(this, Program.Configuration);
_resourceTypeModel = new ResourceTypeModel();
}
[Route("[controller]")]
public IActionResult Index()
{
return Ok(_authenticator.ValidateAndExecute((user) =>
{
return _resourceTypeModel.GetAll().Select((resourceType) => new ResourceTypeObject(resourceType.Id, resourceType.DisplayName));
}));
}
}
}
......@@ -565,6 +565,7 @@
<ItemGroup>
<Compile Include="Controllers\ProjectController.cs" />
<Compile Include="Controllers\ResourceController.cs" />
<Compile Include="Controllers\ResourceTypeController.cs" />
<Compile Include="Controllers\SubProjectController.cs" />
<Compile Include="Models\ProjectModel.cs" />
<Compile Include="Models\ProjectResourceModel.cs" />
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment