manager-service/ManagerService/Controllers/ConfigurationController.cs

209 lines
7.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Manager.Interfaces.DTO;
using Manager.Interfaces.Models;
using Manager.Services;
using ManagerService.Service.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("Configuration", Description = "Configuration management")]
public class ConfigurationController : ControllerBase
{
private ConfigurationDatabaseService _configurationService;
private readonly ILogger<ConfigurationController> _logger;
public ConfigurationController(ILogger<ConfigurationController> logger, ConfigurationDatabaseService configurationService)
{
_logger = logger;
_configurationService = configurationService;
}
/// <summary>
/// Get a list of all configuration (summary)
/// </summary>
[ProducesResponseType(typeof(List<ConfigurationDTO>), 200)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet]
public ObjectResult Get()
{
try
{
List<Configuration> configurations = _configurationService.GetAll();
return new OkObjectResult(configurations.Select(r => r.ToDTO()));
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary>
/// Get a specific display configuration
/// </summary>
/// <param name="id">id configuration</param>
[AllowAnonymous]
[ProducesResponseType(typeof(ConfigurationDTO), 200)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet("{id}")]
public ObjectResult GetDetail(string id)
{
try
{
Configuration configuration = _configurationService.GetById(id);
if (configuration == null)
throw new KeyNotFoundException("This configuration was not found");
return new OkObjectResult(configuration.ToDTO());
}
catch (KeyNotFoundException ex)
{
return new NotFoundObjectResult(ex.Message) {};
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary>
/// Create a new configuration
/// </summary>
/// <param name="newConfiguration">New configuration info</param>
[ProducesResponseType(typeof(ConfigurationDTO), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 409)]
[ProducesResponseType(typeof(string), 500)]
[HttpPost]
public ObjectResult Create([FromBody] ConfigurationDTO newConfiguration)
{
try
{
if (newConfiguration == null)
throw new ArgumentNullException("Configuration param is null");
// Todo add some verification ?
Configuration configuration = new Configuration();
configuration.Label = newConfiguration.Label;
configuration.PrimaryColor = newConfiguration.PrimaryColor;
configuration.SecondaryColor = newConfiguration.SecondaryColor;
configuration.Languages = new List<string> { "FR", "NL", "EN", "DE" }; // by default all languages
configuration.DateCreation = DateTime.Now;
Configuration configurationCreated = _configurationService.Create(configuration);
return new OkObjectResult(configurationCreated.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 a configuration
/// </summary>
/// <param name="updatedConfiguration">Configuration to update</param>
[ProducesResponseType(typeof(ConfigurationDTO), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpPut]
public ObjectResult Update([FromBody] ConfigurationDTO updatedConfiguration)
{
try
{
if (updatedConfiguration == null)
throw new ArgumentNullException("configuration param is null");
Configuration configuration = _configurationService.GetById(updatedConfiguration.Id);
if (configuration == null)
throw new KeyNotFoundException("Configuration does not exist");
// Todo add some verification ?
configuration.Label = updatedConfiguration.Label;
configuration.PrimaryColor = updatedConfiguration.PrimaryColor;
configuration.SecondaryColor = updatedConfiguration.SecondaryColor;
configuration.Languages = updatedConfiguration.Languages;
Configuration configurationModified = _configurationService.Update(updatedConfiguration.Id, configuration);
return new OkObjectResult(configurationModified.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 a configuration
/// </summary>
/// <param name="id">Id of configuration 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("Configuration param is null");
if (!_configurationService.IsExist(id))
throw new KeyNotFoundException("Configuration does not exist");
_configurationService.Remove(id);
return new ObjectResult("The configuration 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 };
}
}
}
}