Aufgrund einer Wartung wird GitLab am 26.10. zwischen 8:00 und 9:00 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to maintenance, GitLab will be temporarily unavailable on 26.10. between 8:00 and 9:00 am.

ProjectController.cs 4.12 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
26
27
28
29
30
31
32
33
34
35
36
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ProjectModel projectModel = new ProjectModel();
                return projectModel.GetAllWhere((project) =>
                {
                    return (from projectRole in project.ProjectRolesProjectIdIds
                            where projectRole.User == user
                            && projectRole.Role.DisplayName == "Owner"
                            select projectRole).Any();
                }).Select((project) =>
                {
                    return new ProjectObject(project.Id, project.Description, project.DisplayName, project.Organization);
                });
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
37
38
39
40
41
42
        }

        //[Route("[controller]/greet/{username}")] would also work, but would take all commands
        [HttpGet("[controller]/{id}")]
        public IActionResult Get(string id)
        {
43
44
45
46
47
48
            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
49
50
51
52
53
54
        }

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

71
        [HttpPost("[controller]")]
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
72
73
        public IActionResult Store()
        {
74
75
76
77
78
79
80
            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
81
82
        }

83
        [HttpPost("[controller]/{id}/resource/{resource_id}")]
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
84
85
        public IActionResult AddResource(string id, string resource_id)
        {
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ProjectModel projectModel = new ProjectModel();
                var project = projectModel.GetById(Guid.Parse(id));
                ResourceModel resourceModel = new ResourceModel();
                var resource = resourceModel.GetById(Guid.Parse(resource_id));
                if (projectModel.OwnsProject(user, project) && resourceModel.OwnsResource(user, resource))
                {
                    projectModel.AddResource(project, resource);
                    return true;
                }
                else
                {
                    throw new NotAuthorizedException("User is not authorized to add resource to project!");
                }
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
102
103
104
        }
    }
}