ProjectController.cs 4.56 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 Microsoft.AspNetCore.Mvc;
10
using Microsoft.SharePoint;
11
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;
20
        private readonly List<IProjectAction> projectActions;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
21
22
23

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

        [Route("[controller]")]
        public IActionResult Index()
        {
35
36
37
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ProjectModel projectModel = new ProjectModel();
38
39
                return projectModel.GetAllWhere((project) =>                
                    (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
52
53
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ProjectModel projectModel = new ProjectModel();
                var project = projectModel.GetById(Guid.Parse(id));
54
                return new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords);
55
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
56
57
58
59
60
        }

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

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

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

88
                return new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords);
89
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
90
        }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

        private void FireEvents(Action<IProjectAction, ProjectEventArgs> eventAction)
        {
            var sharePointSite = Request.Headers["Referer"];

            SPUserToken systemAccount = SPUserToken.SystemAccount;
            using (SPSite site = new SPSite(sharePointSite, systemAccount))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPContext context = SPContext.GetContext(web);
                    object[] args = new object[]
                    {
                        context
                    };
                    ProjectEventArgs projectEventArgs = new ProjectEventArgs(Program.Configuration, args);
                    foreach (var projectAction in projectActions)
                    {
                        eventAction(projectAction, projectEventArgs);
                    }
                }
            }
        }
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
114
115
    }
}