ProjectController.cs 4.58 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
10
using Microsoft.AspNetCore.Mvc;
using System;
11
using System.Collections.Generic;
12
using System.Linq;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
13
14
15
16
17
18

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

        public ProjectController()
        {
24
            _authenticator = new Authenticator(this, Program.Configuration);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
25
            _projectActions = new List<IProjectAction>()
26
27
28
29
            {
                new PIDAction(),
                new SharePointSiteAction()
            };
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
30
            _projectModel = new ProjectModel();
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()
L. Ellenbeck's avatar
L. Ellenbeck committed
43
                ).Select((project) => new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords));
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
55
56
57
58
59
60
                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!");
                }
61
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
62
63
64
65
66
        }

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

82
        [HttpPost("[controller]")]
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
83
84
        public IActionResult Store()
        {
85
            return base.Ok(_authenticator.ValidateAndExecute((user) =>
86
87
            {
                ProjectObject projectObject = ObjectFactory<ProjectObject>.DeserializeFromStream(Request.Body);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
88
                var project = _projectModel.StoreFromObject(projectObject, user);
89

90
91
                if (projectObject.ParentId != null 
                    && projectObject.ParentId != new Guid()
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
92
                    && _projectModel.OwnsProject(user, _projectModel.GetById(projectObject.ParentId))) // for now, only an owner can add subprojects to projects
93
94
95
96
97
                {
                    SubProjectModel subProjectModel = new SubProjectModel();
                    subProjectModel.LinkSubProject(projectObject.ParentId, project.Id);
                }

98
99
                FireEvents((projectAction, projectEventArgs) => projectAction.OnProjectCreate(project, projectEventArgs));

100
                return new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords);
101
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
102
        }
103
104
105

        private void FireEvents(Action<IProjectAction, ProjectEventArgs> eventAction)
        {
106
            ProjectEventArgs projectEventArgs = new ProjectEventArgs(Program.Configuration, new object[0]);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
107
            foreach (var projectAction in _projectActions)
108
            {
109
                eventAction(projectAction, projectEventArgs);
110
111
            }
        }
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
112
113
    }
}