Commit e402e2c8 authored by Benedikt Heinrichs's avatar Benedikt Heinrichs

New: Implement ResourceController

parent e21dbddb
......@@ -63,7 +63,7 @@ namespace Coscine.Api.Project.Controllers
}
else
{
throw new NotAuthorizedException("The user is not authorized to perform an update on the selected project");
throw new NotAuthorizedException("The user is not authorized to perform an update on the selected project!");
}
}));
}
......
using Microsoft.AspNetCore.Mvc;
using Coscine.Api.Project.Exceptions;
using Coscine.Api.Project.Factories;
using Coscine.Api.Project.Models;
using Coscine.Api.Project.ReturnObjects;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
......@@ -19,7 +23,22 @@ namespace Coscine.Api.Project.Controllers
[Route("[controller]")]
public IActionResult Index()
{
return Ok("Hello World from a controller");
return Ok(_authenticator.ValidateAndExecute((user) =>
{
ResourceModel resourceModel = new ResourceModel(); ;
return resourceModel.GetAllWhere((resource) =>
{
return (from projectResource in resource.ProjectResourceResourceIdIds
where (from projectRole in projectResource.Project.ProjectRolesProjectIdIds
where projectRole.User == user
&& projectRole.Role.DisplayName == "Owner"
select projectRole).Any()
select projectResource).Any();
}).Select((resource) =>
{
return new ResourceObject(resource.Id, resource.ExternalId, resource.Url, new ResourceTypeObject(resource.Type.Id, resource.Type.DisplayName));
});
}));
}
......@@ -27,20 +46,64 @@ namespace Coscine.Api.Project.Controllers
[HttpGet("[controller]/{id}")]
public IActionResult Get(string id)
{
return Ok(id);
return Ok(_authenticator.ValidateAndExecute((user) =>
{
ResourceModel resourceModel = new ResourceModel();
var resource = resourceModel.GetById(Guid.Parse(id));
if (resourceModel.OwnsResource(user, resource))
{
return new ResourceObject(resource.Id, resource.ExternalId, resource.Url, new ResourceTypeObject(resource.Type.Id, resource.Type.DisplayName));
}
else
{
throw new NotAuthorizedException("User does not own resource!");
}
}));
}
//[Route("[controller]/greet/{username}")] would also work, but would take all commands
[HttpPost("[controller]/{id}")]
public IActionResult Update(string id)
{
return Ok(id);
return Ok(_authenticator.ValidateAndExecute((user) =>
{
ResourceObject resourceObject = ObjectFactory<ResourceObject>.DeserializeFromStream(Request.Body);
ResourceModel resourceModel = new ResourceModel();
var resource = resourceModel.GetById(Guid.Parse(id));
if (resourceModel.OwnsResource(user, resource))
{
return resourceModel.Update(resource);
}
else
{
throw new NotAuthorizedException("The user is not authorized to perform an update on the selected resource!");
}
}));
}
[HttpPost("[controller]/project/{project_id}")]
public IActionResult StoreToProject(string project_id)
{
return Ok(_authenticator.ValidateAndExecute((user) =>
{
ResourceObject resourceObject = ObjectFactory<ResourceObject>.DeserializeFromStream(Request.Body);
[HttpPut("[controller]")]
public IActionResult Store()
ProjectModel projectModel = new ProjectModel();
var project = projectModel.GetById(Guid.Parse(project_id));
if (projectModel.OwnsProject(user, project))
{
return Ok();
ResourceModel resourceModel = new ResourceModel();
var resource = resourceModel.StoreFromObject(resourceObject);
projectModel.AddResource(project, resource);
return new ResourceObject(resource.Id, resource.ExternalId, resource.Url, new ResourceTypeObject(resource.Type.Id, resource.Type.DisplayName));
}
else
{
throw new NotAuthorizedException("The user is not authorized to add a new resource to the selected project!");
}
}));
}
}
......
using Coscine.Database.Model;
using Coscine.Api.Project.ReturnObjects;
using Coscine.Database.Model;
using LinqToDB;
using System;
using System.Collections.Generic;
......@@ -15,6 +16,18 @@ namespace Coscine.Api.Project.Models
}
public Resource StoreFromObject(ResourceObject resourceObject)
{
Resource resource = new Resource()
{
ExternalId = resourceObject.ExternalId,
Url = resourceObject.Url
};
resource.Type = new ResourceTypeModel().GetById(resourceObject.Type.Id);
Insert(resource);
return resource;
}
public bool OwnsResource(User user, Resource resource)
{
return DatabaseConnection.ConnectToDatabase((db) =>
......
using Coscine.Database.Model;
using LinqToDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.Api.Project.Models
{
public class ResourceTypeModel : DatabaseModel<ResourceType>
{
public ResourceTypeModel() : base(Program.Configurator.Configuration)
{
}
public override Guid GetIdFromObject(ResourceType databaseObject)
{
return databaseObject.Id;
}
public override ITable<ResourceType> GetITableFromDatabase(CoscineDB db)
{
return db.ResourceTypes;
}
}
}
......@@ -554,11 +554,14 @@
<Compile Include="Models\DatabaseModel.cs" />
<Compile Include="Models\ProjectModel.cs" />
<Compile Include="Models\ResourceModel.cs" />
<Compile Include="Models\ResourceTypeModel.cs" />
<Compile Include="Models\RoleModel.cs" />
<Compile Include="Models\UserModel.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReturnObjects\ProjectObject.cs" />
<Compile Include="ReturnObjects\ResourceObject.cs" />
<Compile Include="ReturnObjects\ResourceTypeObject.cs" />
<Compile Include="Startup.cs" />
</ItemGroup>
<ItemGroup>
......
......@@ -13,7 +13,7 @@ namespace Coscine.Api.Project.ReturnObjects
public Guid Id { get; set; }
public string Description { get; set; }
public string DisplayName {get;set;}
public string DisplayName { get; set; }
public string Organization { get; set; }
public ProjectObject(Guid id, string description, string displayName, string organisation)
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.Api.Project.ReturnObjects
{
[Serializable]
public class ResourceObject
{
public Guid Id { get; set; }
public string ExternalId { get; set; }
public string Url { get; set; }
public ResourceTypeObject Type { get; set; }
public ResourceObject(Guid id, string externalId, string url, ResourceTypeObject type)
{
Id = id;
ExternalId = externalId;
Url = url;
Type = type;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Coscine.Api.Project.ReturnObjects
{
[Serializable]
public class ResourceTypeObject
{
public Guid Id { get; set; }
public string DisplayName { get; set; }
public ResourceTypeObject(Guid id, string displayName)
{
Id = id;
DisplayName = displayName;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment