using System; using System.Collections.Generic; using System.Linq; using Manager.Helpers; using Manager.Services; using ManagerService.Data; using ManagerService.DTOs; using ManagerService.Helpers; using ManagerService.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using NSwag.Annotations; namespace ManagerService.Controllers { [Authorize] // TODO Add ROLES (Roles = "Admin") [ApiController, Route("api/[controller]")] [OpenApiTag("Instance", Description = "Application instance management")] public class ApplicationInstanceController : ControllerBase { private readonly MyInfoMateDbContext _myInfoMateDbContext; private InstanceDatabaseService _instanceService; private UserDatabaseService _userService; private readonly ILogger _logger; private readonly ProfileLogic _profileLogic; IHexIdGeneratorService idService = new HexIdGeneratorService(); public ApplicationInstanceController(ILogger logger, InstanceDatabaseService instanceService, UserDatabaseService userService, ProfileLogic profileLogic, MyInfoMateDbContext myInfoMateDbContext) { _logger = logger; _instanceService = instanceService; _userService = userService; _profileLogic = profileLogic; _myInfoMateDbContext = myInfoMateDbContext; } /// /// Get a list of all applicationInstance (summary) /// /// instance id [AllowAnonymous] [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 500)] [HttpGet] public ObjectResult Get([FromQuery] string instanceId) { try { List applicationInstances = _myInfoMateDbContext.ApplicationInstances.Where(ai => ai.InstanceId == instanceId).ToList(); return new OkObjectResult(applicationInstances.Select(ai => ai.ToDTO()).OrderBy(c => c.appType)); } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Create a new application instance /// /// New application instance info [ProducesResponseType(typeof(ApplicationInstanceDTO), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 409)] [ProducesResponseType(typeof(string), 500)] [HttpPost] public ObjectResult Create([FromBody] ApplicationInstanceDTO newApplicationInstanceDTO) { try { if (newApplicationInstanceDTO == null) throw new ArgumentNullException("Application instance param is null"); // Todo add some verification ? ApplicationInstance applicationInstance = new ApplicationInstance(); applicationInstance.FromDTO(newApplicationInstanceDTO); applicationInstance.Id = idService.GenerateHexId(); _myInfoMateDbContext.ApplicationInstances.Add(applicationInstance); _myInfoMateDbContext.SaveChanges(); return new OkObjectResult(applicationInstance.ToDTO()); } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) { }; } catch (InvalidOperationException ex) { return new ConflictObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Update an application instance /// /// application instance to update [ProducesResponseType(typeof(ApplicationInstanceDTO), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpPut] public ObjectResult Update([FromBody] ApplicationInstanceDTO updatedApplicationInstanceDTO) { try { if (updatedApplicationInstanceDTO == null) throw new ArgumentNullException("application instance param is null"); ApplicationInstance applicationInstance = _myInfoMateDbContext.ApplicationInstances.FirstOrDefault(ai => ai.Id == updatedApplicationInstanceDTO.id); if (applicationInstance == null) throw new KeyNotFoundException("application instance does not exist"); applicationInstance.FromDTO(updatedApplicationInstanceDTO); _myInfoMateDbContext.SaveChanges(); return new OkObjectResult(applicationInstance.ToDTO()); } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) {}; } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) {}; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Delete an application instance /// /// Id of application instance to delete [ProducesResponseType(typeof(string), 202)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpDelete("{id}")] public ObjectResult Delete(string id) { try { if (id == null) throw new ArgumentNullException("application instance param is null"); ApplicationInstance applicationInstance = _myInfoMateDbContext.ApplicationInstances.FirstOrDefault(ai => ai.Id == id); if (applicationInstance == null) throw new KeyNotFoundException("application instance does not exist"); //_instanceService.Remove(id); _myInfoMateDbContext.ApplicationInstances.Remove(applicationInstance); _myInfoMateDbContext.SaveChanges(); return new ObjectResult("The application instance has been deleted") { StatusCode = 202 }; } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) { }; } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Create a new application Link to specified application instance /// /// Application instance id /// Info to link config and application instance [ProducesResponseType(typeof(AppConfigurationLinkDTO), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 409)] [ProducesResponseType(typeof(string), 500)] [HttpPost("{applicationInstanceId}/application-link")] public ObjectResult AddConfigurationToApplicationInstance(string applicationInstanceId, [FromBody] AppConfigurationLinkDTO appConfigurationLinkDTO) { try { if (applicationInstanceId == null) throw new ArgumentNullException("Application instance param is null"); if (appConfigurationLinkDTO == null) throw new ArgumentNullException("Configuration param is null"); ApplicationInstance applicationInstance = _myInfoMateDbContext.ApplicationInstances.Include(ai => ai.Configurations).FirstOrDefault(ai => ai.Id == applicationInstanceId); if (applicationInstance == null) throw new KeyNotFoundException("This application instance was not found"); Configuration configuration = _myInfoMateDbContext.Configurations.FirstOrDefault(c => c.Id == appConfigurationLinkDTO.configurationId); if (configuration == null) throw new KeyNotFoundException("This configuration was not found"); // Todo add some verification ? AppConfigurationLink appConfigurationLink = new AppConfigurationLink(); appConfigurationLink.Id = idService.GenerateHexId(); appConfigurationLink.FromDTO(appConfigurationLinkDTO); _myInfoMateDbContext.AppConfigurationLinks.Add(appConfigurationLink); _myInfoMateDbContext.SaveChanges(); return new OkObjectResult(appConfigurationLink.ToDTO()); } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) { }; } catch (InvalidOperationException ex) { return new ConflictObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Remove configuration from instance /// /// Application instance id /// AppConfiguration id [ProducesResponseType(typeof(string), 202)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpDelete("{applicationInstanceId}/application-link/{appConfigurationLinkId}")] public ObjectResult DeleteAppConfigurationLink(string applicationInstanceId, string appConfigurationLinkId) { try { if (applicationInstanceId == null) throw new ArgumentNullException("application instance param is null"); if (appConfigurationLinkId == null) throw new ArgumentNullException("Configuration param is null"); ApplicationInstance applicationInstance = _myInfoMateDbContext.ApplicationInstances.FirstOrDefault(ai => ai.Id == applicationInstanceId); if (applicationInstance == null) throw new KeyNotFoundException("application instance does not exist"); AppConfigurationLink appConfigurationLink = _myInfoMateDbContext.AppConfigurationLinks.FirstOrDefault(acl => acl.Id == appConfigurationLinkId); if (appConfigurationLink == null) throw new KeyNotFoundException("This app configuration link was not found"); _myInfoMateDbContext.AppConfigurationLinks.Remove(appConfigurationLink); _myInfoMateDbContext.SaveChanges(); return new ObjectResult("The app configuration link has been deleted") { StatusCode = 202 }; } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) { }; } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } } }