ProjectController.cs 8.12 KB
Newer Older
1
2
3
using Coscine.Action;
using Coscine.Action.EventArgs;
using Coscine.Action.Implementations.Project;
4
5
using Coscine.Api.Project.Models;
using Coscine.Api.Project.ReturnObjects;
6
7
8
using Coscine.ApiCommons;
using Coscine.ApiCommons.Exceptions;
using Coscine.ApiCommons.Factories;
9
using Coscine.Database.Model;
10
11
using Microsoft.AspNetCore.Mvc;
using System;
12
using System.Collections.Generic;
13
using System.Linq;
14
using Coscine.Configuration;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
15
16
17
18
19
20

namespace Coscine.Api.Project.Controllers
{
    public class ProjectController : Controller
    {
        private readonly Authenticator _authenticator;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
21
        private readonly ProjectModel _projectModel;
22
23
        private readonly IConfiguration _configuration;
        private readonly Emitter _emitter; 
24

Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
25
26
        public ProjectController()
        {
27
            _authenticator = new Authenticator(this, Program.Configuration);
28
            _configuration = Program.Configuration;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
29
            _projectModel = new ProjectModel();
30
            _emitter = new Emitter(this._configuration);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
31
32
33
34
35
        }

        [Route("[controller]")]
        public IActionResult Index()
        {
36
37
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
38
                return _projectModel.GetAllWhere((project) =>                
39
                    (from projectRole in project.ProjectRolesProjectIdIds
40
41
                            where projectRole.User == user
                            && projectRole.Role.DisplayName == "Owner"
42
                            select projectRole).Any()
43
                ).Select((project) => _projectModel.CreateReturnObjectFromDatabaseObject(project));
44
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
45
46
47
48
49
        }

        [HttpGet("[controller]/{id}")]
        public IActionResult Get(string id)
        {
50
51
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
52
53
54
                var project = _projectModel.GetById(Guid.Parse(id));
                if (_projectModel.CanSeeProject(user, project))
                {
55
                    return _projectModel.CreateReturnObjectFromDatabaseObject(project);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
56
57
58
59
60
                }
                else
                {
                    throw new UnauthorizedAccessException("User is not allowed to see given project Id!");
                }
61
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
62
63
        }

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
        [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) =>
                            {
80
                                return resourceModel.CreateReturnObjectFromDatabaseObject(resource);
81
82
83
84
85
86
87
88
89
                            });
                }
                else
                {
                    throw new UnauthorizedAccessException("User cannot see resources of given project!");
                }
            }));
        }

Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
90
91
92
        [HttpPost("[controller]/{id}")]
        public IActionResult Update(string id)
        {
93
94
95
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ProjectObject projectObject = ObjectFactory<ProjectObject>.DeserializeFromStream(Request.Body);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
96
97
                var project = _projectModel.GetById(Guid.Parse(id));
                if(_projectModel.OwnsProject(user, project))
98
                {
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
99
                    return _projectModel.UpdateByObject(project, projectObject);
100
101
102
                }
                else
                {
103
                    throw new NotAuthorizedException("The user is not authorized to perform an update on the selected project!");
104
105
                }
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
106
107
        }

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
        [HttpDelete("[controller]/{id}")]
        public IActionResult Delete(string id)
        {
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                var project = _projectModel.GetById(Guid.Parse(id));
                if (_projectModel.OwnsProject(user, project))
                {
                    DeleteProject(project);
                    return _projectModel.CreateReturnObjectFromDatabaseObject(project);
                }
                else
                {
                    throw new NotAuthorizedException("The user is not authorized to perform an update on the selected project!");
                }
            }));
        }

        public void DeleteProject(Coscine.Database.Model.Project project)
        {
            SubProjectModel subProjectModel = new SubProjectModel();
            foreach(var subProject in subProjectModel.GetAllWhere((subProject) => subProject.ProjectId == project.Id))
            {
                subProjectModel.Delete(subProject);
                DeleteProject(_projectModel.GetById(subProject.SubProjectId));
            }

            foreach (var subProject in subProjectModel.GetAllWhere((subProject) => subProject.SubProjectId == project.Id))
            {
                subProjectModel.Delete(subProject);
            }

            ProjectResourceModel projectResourceModel = new ProjectResourceModel();
            ResourceModel resourceModel = new ResourceModel();
            foreach (var projectResource in projectResourceModel.GetAllWhere((projectResource) => projectResource.ProjectId == project.Id))
            {
                projectResourceModel.Delete(projectResource);
                resourceModel.Delete(resourceModel.GetById(projectResource.ResourceId));
            }

            ProjectRoleModel projectRoleModel = new ProjectRoleModel();
            foreach (var projectRole in projectRoleModel.GetAllWhere((projectRole) => projectRole.ProjectId == project.Id))
            {
                projectRoleModel.Delete(projectRole);
            }

            ProjectDisciplineModel projectDisciplineModel = new ProjectDisciplineModel();
            foreach (var projectDiscipline in projectDisciplineModel.GetAllWhere((projectDiscipline) => projectDiscipline.ProjectId == project.Id))
            {
                projectDisciplineModel.Delete(projectDiscipline);
            }

            ProjectInstituteModel projectInstituteModel = new ProjectInstituteModel();
            foreach (var projectInstitute in projectInstituteModel.GetAllWhere((projectInstitute) => projectInstitute.ProjectId == project.Id))
            {
                projectInstituteModel.Delete(projectInstitute);
            }

166
            _emitter.EmitProjectDelete(new ProjectEventArgs(_configuration)
167
            {
168
                Project = project
169
170
171
172
173
            });

            _projectModel.Delete(project);
        }

174
        [HttpPost("[controller]")]
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
175
176
        public IActionResult Store()
        {
177
            return base.Ok(_authenticator.ValidateAndExecute((user) =>
178
179
            {
                ProjectObject projectObject = ObjectFactory<ProjectObject>.DeserializeFromStream(Request.Body);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
180
                var project = _projectModel.StoreFromObject(projectObject, user);
181

182
183
                if (projectObject.ParentId != null 
                    && projectObject.ParentId != new Guid()
184
                    && _projectModel.IsMemberOrHigher(user, _projectModel.GetById(projectObject.ParentId))) // for now, only an owner can add subprojects to projects
185
186
187
188
189
                {
                    SubProjectModel subProjectModel = new SubProjectModel();
                    subProjectModel.LinkSubProject(projectObject.ParentId, project.Id);
                }

190
191
192
193
194
                _emitter.EmitProjectCreate(new ProjectEventArgs(_configuration)
                {
                    Project = project,
                    ProjectOwner = user
                });
195

196
                return _projectModel.CreateReturnObjectFromDatabaseObject(project);
197
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
198
199
200
        }
    }
}