ProjectController.cs 6.17 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;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
14
15
16
17
18
19

namespace Coscine.Api.Project.Controllers
{
    public class ProjectController : Controller
    {
        private readonly Authenticator _authenticator;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
20
21
        private readonly List<IProjectAction> _projectActions;
        private readonly ProjectModel _projectModel;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
22
23
24

        public ProjectController()
        {
25
            _authenticator = new Authenticator(this, Program.Configuration);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
26
            _projectActions = new List<IProjectAction>()
27
28
29
30
            {
                new PIDAction(),
                new SharePointSiteAction()
            };
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
31
            _projectModel = new ProjectModel();
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
32
33
34
35
36
        }

        [Route("[controller]")]
        public IActionResult Index()
        {
37
38
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
39
                return _projectModel.GetAllWhere((project) =>                
40
                    (from projectRole in project.ProjectRolesProjectIdIds
41
42
                            where projectRole.User == user
                            && projectRole.Role.DisplayName == "Owner"
43
                            select projectRole).Any()
L. Ellenbeck's avatar
L. Ellenbeck committed
44
                ).Select((project) => new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords));
45
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
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
55
56
57
58
59
60
61
                var project = _projectModel.GetById(Guid.Parse(id));
                if (_projectModel.CanSeeProject(user, project))
                {
                    return new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords);
                }
                else
                {
                    throw new UnauthorizedAccessException("User is not allowed to see given project Id!");
                }
62
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
63
64
        }

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        [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);
                                }
86
                                return new ResourceObject(resource.Id, resource.ExternalId, resource.Url, resource.DisplayName, new ResourceTypeObject(resourceType.Id, resourceType.DisplayName));
87
88
89
90
91
92
93
94
95
                            });
                }
                else
                {
                    throw new UnauthorizedAccessException("User cannot see resources of given project!");
                }
            }));
        }

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

114
        [HttpPost("[controller]")]
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
115
116
        public IActionResult Store()
        {
117
            return base.Ok(_authenticator.ValidateAndExecute((user) =>
118
119
            {
                ProjectObject projectObject = ObjectFactory<ProjectObject>.DeserializeFromStream(Request.Body);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
120
                var project = _projectModel.StoreFromObject(projectObject, user);
121

122
123
                if (projectObject.ParentId != null 
                    && projectObject.ParentId != new Guid()
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
124
                    && _projectModel.OwnsProject(user, _projectModel.GetById(projectObject.ParentId))) // for now, only an owner can add subprojects to projects
125
126
127
128
129
                {
                    SubProjectModel subProjectModel = new SubProjectModel();
                    subProjectModel.LinkSubProject(projectObject.ParentId, project.Id);
                }

130
131
                FireEvents((projectAction, projectEventArgs) => projectAction.OnProjectCreate(project, projectEventArgs));

132
                return new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords);
133
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
134
        }
135
136
137

        private void FireEvents(Action<IProjectAction, ProjectEventArgs> eventAction)
        {
138
            ProjectEventArgs projectEventArgs = new ProjectEventArgs(Program.Configuration, new object[0]);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
139
            foreach (var projectAction in _projectActions)
140
            {
141
                eventAction(projectAction, projectEventArgs);
142
143
            }
        }
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
144
145
    }
}