Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Coscine
backend
apis
Project
Commits
e402e2c8
Commit
e402e2c8
authored
Jul 04, 2019
by
Benedikt Heinrichs
Browse files
New: Implement ResourceController
parent
e21dbddb
Changes
8
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/Project/Controllers/ProjectController.cs
View file @
e402e2c8
...
...
@@ -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
!
"
);
}
}));
}
...
...
src/Project/Controllers/ResourceController.cs
View file @
e402e2c8
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!"
);
}
}));
}
[
HttpP
u
t
(
"[controller]"
)]
public
IActionResult
Store
(
)
[
HttpP
os
t
(
"[controller]
/project/{project_id}
"
)]
public
IActionResult
Store
ToProject
(
string
project_id
)
{
return
Ok
();
return
Ok
(
_authenticator
.
ValidateAndExecute
((
user
)
=>
{
ResourceObject
resourceObject
=
ObjectFactory
<
ResourceObject
>.
DeserializeFromStream
(
Request
.
Body
);
ProjectModel
projectModel
=
new
ProjectModel
();
var
project
=
projectModel
.
GetById
(
Guid
.
Parse
(
project_id
));
if
(
projectModel
.
OwnsProject
(
user
,
project
))
{
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!"
);
}
}));
}
}
...
...
src/Project/Models/ResourceModel.cs
View file @
e402e2c8
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
)
=>
...
...
src/Project/Models/ResourceTypeModel.cs
0 → 100644
View file @
e402e2c8
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
;
}
}
}
src/Project/Project.csproj
View file @
e402e2c8
...
...
@@ -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>
...
...
src/Project/ReturnObjects/ProjectObject.cs
View file @
e402e2c8
...
...
@@ -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
)
...
...
src/Project/ReturnObjects/ResourceObject.cs
0 → 100644
View file @
e402e2c8
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
;
}
}
}
src/Project/ReturnObjects/ResourceTypeObject.cs
0 → 100644
View file @
e402e2c8
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
;
}
}
}
Ghost User
@ghost
mentioned in commit
bd1ed418
·
Jul 22, 2019
mentioned in commit
bd1ed418
mentioned in commit bd1ed418d4e4bc2448f76df133601d6cbf31b6c7
Toggle commit list
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment