189 lines
7.1 KiB
C#
189 lines
7.1 KiB
C#
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.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<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.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 };
|
|
}
|
|
}
|
|
|
|
/// <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();
|
|
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 };
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <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.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 };
|
|
}
|
|
}
|
|
|
|
/// <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 };
|
|
}
|
|
}
|
|
}
|
|
}
|