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 Mqtt.Client.AspNetCore.Services; using Newtonsoft.Json; 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 _logger; public ConfigurationController(ILogger logger, ConfigurationDatabaseService configurationService) { _logger = logger; _configurationService = configurationService; } /// /// Get a list of all configuration (summary) /// [AllowAnonymous] [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 500)] [HttpGet] public ObjectResult Get() { try { List configurations = _configurationService.GetAll(); return new OkObjectResult(configurations.Select(r => r.ToDTO())); } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Get a specific display configuration /// /// id configuration [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 }; } } /// /// Create a new configuration /// /// New configuration info [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 { "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 }; } } /// /// Update a configuration /// /// Configuration to update [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); MqttClientService.PublishMessage($"config/{configurationModified.Id}", JsonConvert.SerializeObject(new PlayerMessageDTO() { configChanged = true })); 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 }; } } /// /// Delete a configuration /// /// Id of configuration 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("Configuration param is null"); if (!_configurationService.IsExist(id)) throw new KeyNotFoundException("Configuration does not exist"); _configurationService.Remove(id); MqttClientService.PublishMessage($"config/{id}", JsonConvert.SerializeObject(new PlayerMessageDTO() { configChanged = true, isDeleted = true })); 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 }; } } } }