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

namespace Coscine.Api.Project.Controllers
{
    public class ProjectController : Controller
    {
        private readonly Authenticator _authenticator;

        public ProjectController()
        {
18
            _authenticator = new Authenticator(this, Program.Configuration);
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
19
20
21
22
23
        }

        [Route("[controller]")]
        public IActionResult Index()
        {
24
25
26
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ProjectModel projectModel = new ProjectModel();
27
28
                return projectModel.GetAllWhere((project) =>                
                    (from projectRole in project.ProjectRolesProjectIdIds
29
30
                            where projectRole.User == user
                            && projectRole.Role.DisplayName == "Owner"
31
32
                            select projectRole).Any()
                ).Select((project) =>
33
                {
34
                    return new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords);
35
36
                });
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
37
38
39
40
41
        }

        [HttpGet("[controller]/{id}")]
        public IActionResult Get(string id)
        {
42
43
44
45
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ProjectModel projectModel = new ProjectModel();
                var project = projectModel.GetById(Guid.Parse(id));
46
                return new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords);
47
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
48
49
50
51
52
        }

        [HttpPost("[controller]/{id}")]
        public IActionResult Update(string id)
        {
53
54
55
56
57
58
59
            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))
                {
60
                    return projectModel.UpdateByObject(project, projectObject);
61
62
63
                }
                else
                {
64
                    throw new NotAuthorizedException("The user is not authorized to perform an update on the selected project!");
65
66
                }
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
67
68
        }

69
        [HttpPost("[controller]")]
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
70
71
        public IActionResult Store()
        {
72
73
74
75
76
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ProjectObject projectObject = ObjectFactory<ProjectObject>.DeserializeFromStream(Request.Body);
                ProjectModel projectModel = new ProjectModel();
                var project = projectModel.StoreFromObject(projectObject, user);
77
                return new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization, project.StartDate, project.EndDate, project.Keywords);
78
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
79
80
81
        }
    }
}