ResourceTypeController.cs 2.72 KB
Newer Older
1 2 3
using Coscine.Api.Project.Models;
using Coscine.Api.Project.ReturnObjects;
using Coscine.ApiCommons;
4
using Microsoft.AspNetCore.Authorization;
5 6 7 8 9 10
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;

namespace Coscine.Api.Project.Controllers
{
11
    [Authorize]
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    public class ResourceTypeController : Controller
    {
        private readonly Authenticator _authenticator;
        private readonly ResourceTypeModel _resourceTypeModel;

        public ResourceTypeController()
        {
            _authenticator = new Authenticator(this, Program.Configuration);
            _resourceTypeModel = new ResourceTypeModel();
        }


        [Route("[controller]")]
        public IActionResult Index()
        {
27
                return Json(_resourceTypeModel.GetAll()
28 29 30 31 32 33 34 35
                    .Select((resourceType) => new ResourceTypeObject(resourceType.Id, resourceType.DisplayName, resourceType.Enabled)));
        }

        [Route("[controller]/enabled")]
        public IActionResult GetEnabledResourceTypes()
        {
            return Json(_resourceTypeModel.GetAllWhere((resourceType) => (resourceType.Enabled == true))
                .Select((resourceType) => new ResourceTypeObject(resourceType.Id, resourceType.DisplayName, resourceType.Enabled)));
36 37
        }

38 39 40
        [Route("[controller]/{id}/fields")]
        public IActionResult Fields(string id)
        {
41
            var resourceType = _resourceTypeModel.GetById(Guid.Parse(id));
42

43 44
                if (resourceType.DisplayName == "s3")
                {
45
                    return Json(Type.GetType("Coscine.Api.Project.ReturnObjects.S3ResourceTypeObject").GetProperties()
46 47
                            .Where((property) => property.Name != "Id")
                            .Select((property) => property.Name)
48
                            .ToList());
49 50
                }
                else if (resourceType.DisplayName == "rds")
51
                {
52
                    return Json(Type.GetType("Coscine.Api.Project.ReturnObjects.RDSResourceTypeObject").GetProperties()
53 54
                            .Where((property) => property.Name != "Id")
                            .Select((property) => property.Name)
55
                            .ToList());
56 57 58
                }
                else if(resourceType.DisplayName == "gitlab")
                {
59
                    return Json(Type.GetType("Coscine.Api.Project.ReturnObjects.GitlabResourceTypeObject").GetProperties()
60 61
                            .Where((property) => property.Name != "Id")
                            .Select((property) => property.Name)
62
                            .ToList());
63 64 65 66 67 68
                }
                else
                {
                    throw new ArgumentException("Invalid Resource Type!");
                }
        }
69
    }
70
}