using Coscine.Api.Project.Models; using Coscine.Api.Project.ReturnObjects; using Coscine.ApiCommons; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System; using System.Linq; namespace Coscine.Api.Project.Controllers { [Authorize] 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() { return Json(_resourceTypeModel.GetAll() .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))); } [Route("[controller]/{id}/fields")] public IActionResult Fields(string id) { var resourceType = _resourceTypeModel.GetById(Guid.Parse(id)); if (resourceType.DisplayName == "s3") { return Json(Type.GetType("Coscine.Api.Project.ReturnObjects.S3ResourceTypeObject").GetProperties() .Where((property) => property.Name != "Id") .Select((property) => property.Name) .ToList()); } else if (resourceType.DisplayName == "rds") { return Json(Type.GetType("Coscine.Api.Project.ReturnObjects.RDSResourceTypeObject").GetProperties() .Where((property) => property.Name != "Id") .Select((property) => property.Name) .ToList()); } else if(resourceType.DisplayName == "gitlab") { return Json(Type.GetType("Coscine.Api.Project.ReturnObjects.GitlabResourceTypeObject").GetProperties() .Where((property) => property.Name != "Id") .Select((property) => property.Name) .ToList()); } else { throw new ArgumentException("Invalid Resource Type!"); } } } }