ProjectController.cs 3.03 KB
Newer Older
1
2
3
4
5
6
7
using Coscine.Api.Project.Exceptions;
using Coscine.Api.Project.Factories;
using Coscine.Api.Project.Models;
using Coscine.Api.Project.ReturnObjects;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

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

        public ProjectController()
        {
            _authenticator = new Authenticator(this);
        }

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

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

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

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