400 lines
17 KiB
C#
400 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
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("ApplicationInstance", Description = "Application instance management")]
|
|
public class ApplicationInstanceController : ControllerBase
|
|
{
|
|
private readonly MyInfoMateDbContext _myInfoMateDbContext;
|
|
|
|
private InstanceDatabaseService _instanceService;
|
|
private UserDatabaseService _userService;
|
|
private readonly ILogger<ApplicationInstanceController> _logger;
|
|
private readonly ProfileLogic _profileLogic;
|
|
IHexIdGeneratorService idService = new HexIdGeneratorService();
|
|
|
|
public ApplicationInstanceController(ILogger<ApplicationInstanceController> logger, InstanceDatabaseService instanceService, UserDatabaseService userService, ProfileLogic profileLogic, MyInfoMateDbContext myInfoMateDbContext)
|
|
{
|
|
_logger = logger;
|
|
_instanceService = instanceService;
|
|
_userService = userService;
|
|
_profileLogic = profileLogic;
|
|
_myInfoMateDbContext = myInfoMateDbContext;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a list of all applicationInstance (summary)
|
|
/// </summary>
|
|
/// <param name="id">instance id</param>
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(List<ApplicationInstanceDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet]
|
|
public ObjectResult Get([FromQuery] string instanceId)
|
|
{
|
|
try
|
|
{
|
|
List<ApplicationInstance> applicationInstances = _myInfoMateDbContext.ApplicationInstances.Include(s => s.SectionEvent).Where(ai => ai.InstanceId == instanceId).ToList();
|
|
|
|
return new OkObjectResult(applicationInstances.Select(ai => ai.ToDTO(_myInfoMateDbContext)).OrderBy(c => c.appType));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a new application instance
|
|
/// </summary>
|
|
/// <param name="newApplicationInstanceDTO">New application instance info</param>
|
|
[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().FromDTO(newApplicationInstanceDTO);
|
|
applicationInstance.Id = idService.GenerateHexId();
|
|
|
|
_myInfoMateDbContext.ApplicationInstances.Add(applicationInstance);
|
|
|
|
_myInfoMateDbContext.SaveChanges();
|
|
|
|
return new OkObjectResult(applicationInstance.ToDTO(_myInfoMateDbContext));
|
|
}
|
|
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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update an application instance
|
|
/// </summary>
|
|
/// <param name="updatedApplicationInstanceDTO">application instance to update</param>
|
|
[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.Include(s => s.SectionEvent).FirstOrDefault(ai => ai.Id == updatedApplicationInstanceDTO.id);
|
|
|
|
if (applicationInstance == null)
|
|
throw new KeyNotFoundException("application instance does not exist");
|
|
|
|
applicationInstance = applicationInstance.FromDTO(updatedApplicationInstanceDTO);
|
|
_myInfoMateDbContext.SaveChanges();
|
|
|
|
return new OkObjectResult(applicationInstance.ToDTO(_myInfoMateDbContext));
|
|
}
|
|
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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete an application instance
|
|
/// </summary>
|
|
/// <param name="id">Id of application instance to delete</param>
|
|
[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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all application Link
|
|
/// </summary>
|
|
/// <param name="applicationInstanceId">application instance id</param>
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(List<AppConfigurationLinkDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("{applicationInstanceId}/application-link")]
|
|
public ObjectResult GetAllApplicationLinkFromApplicationInstance(string applicationInstanceId)
|
|
{
|
|
try
|
|
{
|
|
List<AppConfigurationLink> appConfigurationLinks = _myInfoMateDbContext.AppConfigurationLinks
|
|
.Include(acl => acl.Configuration)
|
|
.Include(acl => acl.Device)
|
|
.Where(acl => acl.ApplicationInstanceId == applicationInstanceId)
|
|
.ToList();
|
|
return new OkObjectResult(appConfigurationLinks.Select(acl => acl.ToDTO()).OrderBy(acl => acl.order).ToList());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a new application Link to specified application instance
|
|
/// </summary>
|
|
/// <param name="applicationInstanceId">Application instance id</param>
|
|
/// <param name="appConfigurationLinkDTO">Info to link config and application instance</param>
|
|
[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");
|
|
|
|
// Check if already linked
|
|
if (_myInfoMateDbContext.AppConfigurationLinks.Any(acl => acl.ConfigurationId == appConfigurationLinkDTO.configurationId && acl.ApplicationInstanceId == applicationInstanceId))
|
|
return new ConflictObjectResult("This configuration is already linked to this applicationInstance");
|
|
|
|
// Todo add some verification ?
|
|
AppConfigurationLink appConfigurationLink = new AppConfigurationLink().FromDTO(appConfigurationLinkDTO);
|
|
appConfigurationLink.Id = idService.GenerateHexId();
|
|
appConfigurationLink.Order = _myInfoMateDbContext.AppConfigurationLinks.Count(acl => acl.ApplicationInstanceId == applicationInstanceId);
|
|
|
|
_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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update an app configuration link
|
|
/// </summary>
|
|
/// <param name="appConfigurationLinkDTO">App configuration link to update</param>
|
|
[ProducesResponseType(typeof(AppConfigurationLinkDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPut("application-link")]
|
|
public ObjectResult UpdateApplicationLink([FromBody] AppConfigurationLinkDTO appConfigurationLinkDTO)
|
|
{
|
|
try
|
|
{
|
|
if (appConfigurationLinkDTO == null)
|
|
throw new ArgumentNullException("appConfigurationLink param is null");
|
|
|
|
AppConfigurationLink appConfigurationLink = _myInfoMateDbContext.AppConfigurationLinks.Include(acl => acl.Configuration).Include(acl => acl.Device).FirstOrDefault(acl => acl.Id == appConfigurationLinkDTO.id);
|
|
|
|
if (appConfigurationLink == null)
|
|
throw new KeyNotFoundException("appConfigurationLink does not exist");
|
|
|
|
appConfigurationLink = appConfigurationLink.FromDTO(appConfigurationLinkDTO);
|
|
_myInfoMateDbContext.SaveChanges();
|
|
|
|
return new OkObjectResult(appConfigurationLink.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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update app configuration links order
|
|
/// </summary>
|
|
/// <param name="appConfigurationLinkDTO">App configuration link to update</param>
|
|
[ProducesResponseType(typeof(List<AppConfigurationLinkDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPut("application-link/order")]
|
|
public ObjectResult UpdateApplicationLinkOrder([FromBody] List<AppConfigurationLinkDTO> appConfigurationLinkDTOs)
|
|
{
|
|
try
|
|
{
|
|
if (appConfigurationLinkDTOs == null)
|
|
throw new ArgumentNullException("appConfigurationLink param is null");
|
|
|
|
List<AppConfigurationLink> appConfigurationLinks = _myInfoMateDbContext.AppConfigurationLinks.Include(acl => acl.Configuration).Include(acl => acl.Device).Where(acl => appConfigurationLinkDTOs.Select(acld => acld.id).Contains(acl.Id)).ToList();
|
|
|
|
if (appConfigurationLinks.Count != appConfigurationLinkDTOs.Count)
|
|
throw new KeyNotFoundException("Length does not match");
|
|
|
|
foreach (var appConfigurationLinkDTO in appConfigurationLinkDTOs) {
|
|
var appConf = appConfigurationLinks.FirstOrDefault(acl => acl.Id == appConfigurationLinkDTO.id);
|
|
appConf.Order = appConfigurationLinkDTO.order;
|
|
}
|
|
_myInfoMateDbContext.SaveChanges();
|
|
|
|
return new OkObjectResult(appConfigurationLinks.Select(acl => acl.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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove configuration from instance
|
|
/// </summary>
|
|
/// <param name="applicationInstanceId">Application instance id</param>
|
|
/// <param name="appConfigurationLinkId">AppConfiguration id</param>
|
|
[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 };
|
|
}
|
|
}
|
|
}
|
|
}
|