ResourceController.cs 5.24 KB
Newer Older
1
using Coscine.Api.Project.Models;
2
using Coscine.Api.Project.ReturnObjects;
3
4
5
using Coscine.ApiCommons;
using Coscine.ApiCommons.Exceptions;
using Coscine.ApiCommons.Factories;
6
using Microsoft.AspNetCore.Mvc;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
7
8
using System;
using System.Linq;
9
using Newtonsoft.Json.Linq;
10
11
12
using Coscine.Action;
using Coscine.Configuration;
using Coscine.Action.EventArgs;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
13
14
15
16
17
18

namespace Coscine.Api.Project.Controllers
{
    public class ResourceController : Controller
    {
        private readonly Authenticator _authenticator;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
19
        private readonly ResourceModel _resourceModel;
20
21
        private readonly IConfiguration _configuration;
        private readonly Emitter _emitter;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
22
23
24

        public ResourceController()
        {
25
            _authenticator = new Authenticator(this, Program.Configuration);
26
            _configuration = Program.Configuration;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
27
            _resourceModel = new ResourceModel();
28
            _emitter = new Emitter(this._configuration);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
29
        }
30
        
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
31
32
33
        [Route("[controller]")]
        public IActionResult Index()
        {
34
35
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
36
                return _resourceModel.GetAllWhere((resource) =>                
37
                    (from projectResource in resource.ProjectResourceResourceIdIds
38
39
40
41
                            where (from projectRole in projectResource.Project.ProjectRolesProjectIdIds
                                   where projectRole.User == user
                                   && projectRole.Role.DisplayName == "Owner"
                                   select projectRole).Any()
42
                            select projectResource).Any()
43
                ).Select((resource) => _resourceModel.CreateReturnObjectFromDatabaseObject(resource));
44
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
45
46
47
48
49
50
        }


        [HttpGet("[controller]/{id}")]
        public IActionResult Get(string id)
        {
51
52
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
53
54
                var resource = _resourceModel.GetById(Guid.Parse(id));
                if (_resourceModel.OwnsResource(user, resource))
55
                {
56
57
                    _resourceModel.SetType(resource);
                    return _resourceModel.CreateReturnObjectFromDatabaseObject(resource);
58
59
60
61
62
63
                }
                else
                {
                    throw new NotAuthorizedException("User does not own resource!");
                }
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
64
65
66
67
68
        }

        [HttpPost("[controller]/{id}")]
        public IActionResult Update(string id)
        {
69
70
71
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ResourceObject resourceObject = ObjectFactory<ResourceObject>.DeserializeFromStream(Request.Body);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
72
73
                var resource = _resourceModel.GetById(Guid.Parse(id));
                if (_resourceModel.OwnsResource(user, resource))
74
                {
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
75
                    return _resourceModel.UpdateByObject(resource, resourceObject);
76
77
78
79
80
81
                }
                else
                {
                    throw new NotAuthorizedException("The user is not authorized to perform an update on the selected resource!");
                }
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
82
83
        }

84
85
86
87
88
89
90
91
92
        [HttpDelete("[controller]/{id}")]
        public IActionResult Delete(string id)
        {
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                var resource = _resourceModel.GetById(Guid.Parse(id));
                if (_resourceModel.OwnsResource(user, resource))
                {
                    var returnObject = _resourceModel.CreateReturnObjectFromDatabaseObject(resource);
93
94
95
96
                    _emitter.EmitResourceDelete(new ResourceEventArgs(_configuration)
                    {
                        Resource = resource
                    });
97
98
99
100
101
102
103
104
105
106
                    _resourceModel.DeleteResource(resource);
                    return returnObject;
                }
                else
                {
                    throw new NotAuthorizedException("The user is not authorized to perform an update on the selected resource!");
                }
            }));
        }

L. Ellenbeck's avatar
L. Ellenbeck committed
107
108
        [HttpPost("[controller]/project/{projectId}")]
        public IActionResult StoreToProject(string projectId)
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
109
        {
110
111
112
113
114
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ResourceObject resourceObject = ObjectFactory<ResourceObject>.DeserializeFromStream(Request.Body);

                ProjectModel projectModel = new ProjectModel();
L. Ellenbeck's avatar
L. Ellenbeck committed
115
                var project = projectModel.GetById(Guid.Parse(projectId));
116
117
                if (projectModel.OwnsProject(user, project))
                {
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
118
                    var resource = _resourceModel.StoreFromObject(resourceObject);
119
120
121

                    projectModel.AddResource(project, resource);

122
123
124
125
126
                    _emitter.EmitResourceCreate(new ResourceEventArgs(_configuration)
                    {
                        Resource = resource
                    });

127
                    return _resourceModel.CreateReturnObjectFromDatabaseObject(resource);
128
129
130
131
132
133
                }
                else
                {
                    throw new NotAuthorizedException("The user is not authorized to add a new resource to the selected project!");
                }                
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
134
135
136
        }
    }
}