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.

ResourceController.cs 4.43 KB
Newer Older
1
2
3
4
5
using Coscine.Api.Project.Exceptions;
using Coscine.Api.Project.Factories;
using Coscine.Api.Project.Models;
using Coscine.Api.Project.ReturnObjects;
using Microsoft.AspNetCore.Mvc;
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

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

        [Route("[controller]")]
        public IActionResult Index()
        {
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ResourceModel resourceModel = new ResourceModel(); ;
                return resourceModel.GetAllWhere((resource) =>
                {
                    return (from projectResource in resource.ProjectResourceResourceIdIds
                            where (from projectRole in projectResource.Project.ProjectRolesProjectIdIds
                                   where projectRole.User == user
                                   && projectRole.Role.DisplayName == "Owner"
                                   select projectRole).Any()
                            select projectResource).Any();
                }).Select((resource) =>
                {
                    return new ResourceObject(resource.Id, resource.ExternalId, resource.Url, new ResourceTypeObject(resource.Type.Id, resource.Type.DisplayName));
                });
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
42
43
44
45
46
47
48
        }


        //[Route("[controller]/greet/{username}")] would also work, but would take all commands
        [HttpGet("[controller]/{id}")]
        public IActionResult Get(string id)
        {
49
50
51
52
53
54
55
56
57
58
59
60
61
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ResourceModel resourceModel = new ResourceModel();
                var resource = resourceModel.GetById(Guid.Parse(id));
                if (resourceModel.OwnsResource(user, resource))
                {
                    return new ResourceObject(resource.Id, resource.ExternalId, resource.Url, new ResourceTypeObject(resource.Type.Id, resource.Type.DisplayName));
                }
                else
                {
                    throw new NotAuthorizedException("User does not own resource!");
                }
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
62
63
64
65
66
67
        }

        //[Route("[controller]/greet/{username}")] would also work, but would take all commands
        [HttpPost("[controller]/{id}")]
        public IActionResult Update(string id)
        {
68
69
70
71
72
73
74
75
76
77
78
79
80
81
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ResourceObject resourceObject = ObjectFactory<ResourceObject>.DeserializeFromStream(Request.Body);
                ResourceModel resourceModel = new ResourceModel();
                var resource = resourceModel.GetById(Guid.Parse(id));
                if (resourceModel.OwnsResource(user, resource))
                {
                    return resourceModel.Update(resource);
                }
                else
                {
                    throw new NotAuthorizedException("The user is not authorized to perform an update on the selected resource!");
                }
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
82
83
        }

84
85
        [HttpPost("[controller]/project/{project_id}")]
        public IActionResult StoreToProject(string project_id)
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
86
        {
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                ResourceObject resourceObject = ObjectFactory<ResourceObject>.DeserializeFromStream(Request.Body);

                ProjectModel projectModel = new ProjectModel();
                var project = projectModel.GetById(Guid.Parse(project_id));
                if (projectModel.OwnsProject(user, project))
                {
                    ResourceModel resourceModel = new ResourceModel();
                    var resource = resourceModel.StoreFromObject(resourceObject);

                    projectModel.AddResource(project, resource);

                    return new ResourceObject(resource.Id, resource.ExternalId, resource.Url, new ResourceTypeObject(resource.Type.Id, resource.Type.DisplayName));
                }
                else
                {
                    throw new NotAuthorizedException("The user is not authorized to add a new resource to the selected project!");
                }                
            }));
Benedikt Heinrichs's avatar
Benedikt Heinrichs committed
107
108
109
110
        }

    }
}