846 lines
39 KiB
C#
846 lines
39 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Manager.Helpers;
|
|
using Manager.Interfaces.DTO;
|
|
using Manager.Interfaces.Models;
|
|
using Manager.Services;
|
|
using ManagerService.Service.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.DataProtection.KeyManagement;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
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 InstanceDatabaseService _instanceService;
|
|
private SectionDatabaseService _sectionService;
|
|
private ResourceDatabaseService _resourceService;
|
|
private DeviceDatabaseService _deviceService;
|
|
private readonly ILogger<ConfigurationController> _logger;
|
|
private readonly IConfiguration _configuration;
|
|
|
|
public ConfigurationController(IConfiguration configuration, ILogger<ConfigurationController> logger, ConfigurationDatabaseService configurationService, InstanceDatabaseService instanceService, SectionDatabaseService sectionService, ResourceDatabaseService resourceService, DeviceDatabaseService deviceService)
|
|
{
|
|
_logger = logger;
|
|
_configuration = configuration;
|
|
_configurationService = configurationService;
|
|
_instanceService = instanceService;
|
|
_sectionService = sectionService;
|
|
_resourceService = resourceService;
|
|
_deviceService = deviceService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a list of all configuration (summary)
|
|
/// </summary>
|
|
/// <param name="id">id instance</param>
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(List<ConfigurationDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet]
|
|
public ObjectResult Get([FromQuery] string instanceId)
|
|
{
|
|
try
|
|
{
|
|
List<Configuration> configurations = _configurationService.GetAll(instanceId);
|
|
|
|
List<ConfigurationDTO> configurationDTOs = new List<ConfigurationDTO>();
|
|
|
|
foreach(var configuration in configurations)
|
|
{
|
|
List<string> sectionIds = _sectionService.GetAllIdsFromConfiguration(configuration.Id);
|
|
ConfigurationDTO configurationDTO = configuration.ToDTO(sectionIds);
|
|
configurationDTOs.Add(configurationDTO);
|
|
}
|
|
|
|
return new OkObjectResult(configurationDTOs.OrderBy(c => c.dateCreation));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get Confuguration list by instanceId' pincode
|
|
/// </summary>
|
|
/// <param name="pinCode">Code pin</param>
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(List<ConfigurationDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("byPin")]
|
|
public ObjectResult GetConfigurationsByPinCode([FromQuery] int pinCode)
|
|
{
|
|
try
|
|
{
|
|
Instance instance = _instanceService.GetByPinCode(pinCode);
|
|
|
|
if (instance == null)
|
|
throw new KeyNotFoundException("None instance is linked to this pin code");
|
|
|
|
List<Configuration> configurations = _configurationService.GetAll(instance.Id);
|
|
List<ConfigurationDTO> configurationDTOs = new List<ConfigurationDTO>();
|
|
foreach (var configuration in configurations)
|
|
{
|
|
List<string> sectionIds = _sectionService.GetAllIdsFromConfiguration(configuration.Id);
|
|
configurationDTOs.Add(configuration.ToDTO(sectionIds));
|
|
}
|
|
|
|
return new OkObjectResult(configurationDTOs);
|
|
}
|
|
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 async Task<ObjectResult> GetDetailAsync(string id)
|
|
{
|
|
try
|
|
{
|
|
Configuration configuration = _configurationService.GetById(id);
|
|
|
|
if (configuration == null)
|
|
throw new KeyNotFoundException("This configuration was not found");
|
|
|
|
List<string> sectionIds = _sectionService.GetAllIdsFromConfiguration(id);
|
|
|
|
if (configuration.WeatherCity != null && configuration.WeatherCity.Length >= 2 &&
|
|
(configuration.WeatherUpdatedDate == null || configuration.WeatherUpdatedDate.Value.AddHours(3) < DateTimeOffset.Now)) // Update all 4 hours
|
|
{
|
|
// Call Openweather api with token from appSettings and update result with json
|
|
var apiKey = _configuration.GetSection("OpenWeatherApiKey").Get<string>();
|
|
|
|
if (apiKey != null && apiKey.Length > 0)
|
|
{
|
|
string url = $"http://api.openweathermap.org/geo/1.0/direct?q={configuration.WeatherCity}&limit=1&appid={apiKey}";
|
|
|
|
using (HttpClient client = new HttpClient())
|
|
{
|
|
try
|
|
{
|
|
HttpResponseMessage response = await client.GetAsync(url);
|
|
response.EnsureSuccessStatusCode();
|
|
string responseBody = await response.Content.ReadAsStringAsync();
|
|
|
|
List<CityData> cities = JsonConvert.DeserializeObject<List<CityData>>(responseBody);
|
|
|
|
if (cities.Count > 0)
|
|
{
|
|
double lat = cities[0].Lat;
|
|
double lon = cities[0].Lon;
|
|
|
|
//string onecallUrl = $"https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&appid={apiKey}";
|
|
string callUrl = $"https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&units=metric&appid={apiKey}";
|
|
|
|
HttpResponseMessage callResponse = await client.GetAsync(callUrl);
|
|
callResponse.EnsureSuccessStatusCode();
|
|
string callResponseBody = await callResponse.Content.ReadAsStringAsync();
|
|
|
|
configuration.WeatherUpdatedDate = DateTimeOffset.Now;
|
|
configuration.WeatherResult = callResponseBody;
|
|
|
|
_configurationService.Update(configuration.Id, configuration);
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Aucune ville trouvée.");
|
|
}
|
|
}
|
|
catch (HttpRequestException e)
|
|
{
|
|
Console.WriteLine($"Une erreur s'est produite lors de la requête HTTP : {e.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return new OkObjectResult(configuration.ToDTO(sectionIds));
|
|
}
|
|
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.InstanceId = newConfiguration.instanceId;
|
|
configuration.Label = newConfiguration.label;
|
|
configuration.Title = new List<TranslationDTO>();
|
|
configuration.ImageId = newConfiguration.imageId;
|
|
configuration.ImageSource = newConfiguration.imageSource;
|
|
configuration.PrimaryColor = newConfiguration.primaryColor;
|
|
configuration.SecondaryColor = newConfiguration.secondaryColor;
|
|
configuration.LoaderImageId = newConfiguration.loaderImageId;
|
|
configuration.LoaderImageUrl = newConfiguration.loaderImageUrl;
|
|
configuration.WeatherCity = newConfiguration.weatherCity;
|
|
configuration.WeatherUpdatedDate = null;
|
|
configuration.WeatherResult = null;
|
|
configuration.IsDate = newConfiguration.isDate;
|
|
configuration.IsHour = newConfiguration.isHour;
|
|
|
|
configuration.Languages = _configuration.GetSection("SupportedLanguages").Get<List<string>>();
|
|
|
|
//configuration.Languages = new List<string> { "FR", "NL", "EN", "DE" }; // by default all languages
|
|
configuration.Title = LanguageInit.Init("Title", configuration.Languages);
|
|
|
|
configuration.DateCreation = DateTime.Now;
|
|
configuration.IsMobile = newConfiguration.isMobile;
|
|
configuration.IsTablet = newConfiguration.isTablet;
|
|
configuration.IsOffline = newConfiguration.isOffline;
|
|
|
|
Configuration configurationCreated = _configurationService.Create(configuration);
|
|
|
|
return new OkObjectResult(configurationCreated.ToDTO(new List<string>())); // Empty list
|
|
}
|
|
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.InstanceId = updatedConfiguration.instanceId;
|
|
configuration.Label = updatedConfiguration.label;
|
|
configuration.Title = updatedConfiguration.title;
|
|
configuration.ImageId = updatedConfiguration.imageId;
|
|
configuration.ImageSource = updatedConfiguration.imageSource;
|
|
configuration.PrimaryColor = updatedConfiguration.primaryColor;
|
|
configuration.SecondaryColor = updatedConfiguration.secondaryColor;
|
|
configuration.Languages = updatedConfiguration.languages;
|
|
configuration.IsMobile = updatedConfiguration.isMobile;
|
|
configuration.IsTablet = updatedConfiguration.isTablet;
|
|
configuration.IsOffline = updatedConfiguration.isOffline;
|
|
configuration.LoaderImageId = updatedConfiguration.loaderImageId;
|
|
configuration.LoaderImageUrl = updatedConfiguration.loaderImageUrl;
|
|
configuration.WeatherCity = updatedConfiguration.weatherCity;
|
|
if (configuration.WeatherCity != updatedConfiguration.weatherCity)
|
|
{
|
|
configuration.WeatherUpdatedDate = null;
|
|
configuration.WeatherResult = null;
|
|
}
|
|
configuration.IsDate = updatedConfiguration.isDate;
|
|
configuration.IsHour = updatedConfiguration.isHour;
|
|
|
|
Configuration configurationModified = _configurationService.Update(updatedConfiguration.id, configuration);
|
|
|
|
MqttClientService.PublishMessage($"config/{configurationModified.Id}", JsonConvert.SerializeObject(new PlayerMessageDTO() { configChanged = true }));
|
|
|
|
List<string> sectionIds = _sectionService.GetAllIdsFromConfiguration(configuration.Id);
|
|
|
|
return new OkObjectResult(configurationModified.ToDTO(sectionIds));
|
|
}
|
|
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);
|
|
|
|
// Delete config for all devices
|
|
List<Device> devices = _deviceService.GetAllWithConfig(id);
|
|
|
|
foreach (var device in devices)
|
|
{
|
|
device.Configuration = null;
|
|
device.ConfigurationId = null;
|
|
_deviceService.Update(device.Id, device);
|
|
}
|
|
|
|
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 };
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Export a configuration
|
|
/// </summary>
|
|
/// <param name="id">Id of configuration to export</param>
|
|
/// <param name="language">Language to export</param>
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(FileContentResult), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("{id}/export")]
|
|
public FileContentResult Export(string id, [FromQuery] string language)
|
|
{
|
|
try
|
|
{
|
|
if (id == null)
|
|
throw new ArgumentNullException("Configuration param is null");
|
|
|
|
Configuration configuration = _configurationService.GetById(id);
|
|
|
|
if (configuration == null)
|
|
throw new KeyNotFoundException("Configuration does not exist");
|
|
|
|
List<SectionDTO> sectionDTOs = _sectionService.GetAllFromConfiguration(configuration.Id).Select(s => s.ToDTO()).ToList();
|
|
List<ResourceDTO> resourceDTOs = new List<ResourceDTO>();
|
|
|
|
if (configuration.ImageId != null)
|
|
{
|
|
addResourceToList(resourceDTOs, configuration.ImageId);
|
|
}
|
|
|
|
if (configuration.LoaderImageId != null)
|
|
{
|
|
addResourceToList(resourceDTOs, configuration.LoaderImageId);
|
|
}
|
|
|
|
foreach (var section in sectionDTOs)
|
|
{
|
|
if (section.imageId != null) {
|
|
addResourceToList(resourceDTOs, section.imageId);
|
|
}
|
|
|
|
switch (section.type) {
|
|
case SectionType.Map:
|
|
MapDTO mapDTO = JsonConvert.DeserializeObject<MapDTO>(section.data);
|
|
if (mapDTO.iconResourceId != null)
|
|
{
|
|
addResourceToList(resourceDTOs, mapDTO.iconResourceId);
|
|
}
|
|
|
|
foreach (var point in mapDTO.points) {
|
|
foreach (var content in point.contents) {
|
|
if (content.resourceId != null)
|
|
{
|
|
addResourceToList(resourceDTOs, content.resourceId);
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var categorie in mapDTO.categories)
|
|
{
|
|
if (categorie.iconResourceId != null)
|
|
{
|
|
addResourceToList(resourceDTOs, categorie.iconResourceId);
|
|
}
|
|
}
|
|
|
|
break;
|
|
case SectionType.Slider:
|
|
SliderDTO sliderDTO = JsonConvert.DeserializeObject<SliderDTO>(section.data);
|
|
foreach (var content in sliderDTO.contents)
|
|
{
|
|
if (content.resourceId != null)
|
|
{
|
|
addResourceToList(resourceDTOs, content.resourceId);
|
|
}
|
|
}
|
|
break;
|
|
case SectionType.Quizz:
|
|
QuizzDTO quizzDTO = JsonConvert.DeserializeObject<QuizzDTO>(section.data);
|
|
foreach (var question in quizzDTO.questions)
|
|
{
|
|
if (question.label != null)
|
|
{
|
|
foreach (var questionLabel in question.label)
|
|
{
|
|
addResourceToList(resourceDTOs, questionLabel.resourceId);
|
|
}
|
|
}
|
|
if (question.imageBackgroundResourceId != null)
|
|
{
|
|
addResourceToList(resourceDTOs, question.imageBackgroundResourceId);
|
|
}
|
|
foreach (var response in question.responses)
|
|
{
|
|
if (response.label != null)
|
|
{
|
|
foreach (var responseLabel in response.label)
|
|
{
|
|
addResourceToList(resourceDTOs, responseLabel.resourceId);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (quizzDTO.bad_level != null)
|
|
{
|
|
if (quizzDTO.bad_level.label != null)
|
|
{
|
|
foreach (var badLevelLabel in quizzDTO.bad_level.label)
|
|
{
|
|
addResourceToList(resourceDTOs, badLevelLabel.resourceId);
|
|
}
|
|
}
|
|
}
|
|
if (quizzDTO.medium_level != null)
|
|
{
|
|
if (quizzDTO.medium_level.label != null)
|
|
{
|
|
foreach (var medium_levelLabel in quizzDTO.medium_level.label)
|
|
{
|
|
addResourceToList(resourceDTOs, medium_levelLabel.resourceId);
|
|
}
|
|
}
|
|
}
|
|
if (quizzDTO.good_level != null)
|
|
{
|
|
if (quizzDTO.good_level.label != null)
|
|
{
|
|
foreach (var good_levelLabel in quizzDTO.good_level.label)
|
|
{
|
|
addResourceToList(resourceDTOs, good_levelLabel.resourceId);
|
|
}
|
|
}
|
|
}
|
|
if (quizzDTO.great_level != null)
|
|
{
|
|
if (quizzDTO.great_level.label != null)
|
|
{
|
|
foreach (var great_levelLabel in quizzDTO.great_level.label)
|
|
{
|
|
addResourceToList(resourceDTOs, great_levelLabel.resourceId);
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
case SectionType.Article:
|
|
ArticleDTO articleDTO = JsonConvert.DeserializeObject<ArticleDTO>(section.data);
|
|
foreach (var content in articleDTO.contents)
|
|
{
|
|
if (content.resourceId != null)
|
|
{
|
|
addResourceToList(resourceDTOs, content.resourceId);
|
|
}
|
|
}
|
|
|
|
// If not a language is used for export in manager, if one is the myvisit app
|
|
var audios = language != null ? articleDTO.audioIds.Where(a => a.language == language) : articleDTO.audioIds;
|
|
foreach (var audio in audios)
|
|
{
|
|
if (audio.value != null)
|
|
{
|
|
addResourceToList(resourceDTOs, audio.value);
|
|
}
|
|
}
|
|
break;
|
|
case SectionType.PDF:
|
|
PdfDTO pdfDTO = JsonConvert.DeserializeObject<PdfDTO>(section.data);
|
|
if (pdfDTO.resourceId != null)
|
|
{
|
|
addResourceToList(resourceDTOs, pdfDTO.resourceId);
|
|
}
|
|
break;
|
|
case SectionType.Agenda:
|
|
AgendaDTO agendaDTO = JsonConvert.DeserializeObject<AgendaDTO>(section.data);
|
|
if (agendaDTO.resourceId != null)
|
|
{
|
|
addResourceToList(resourceDTOs, agendaDTO.resourceId);
|
|
}
|
|
break;
|
|
case SectionType.Menu:
|
|
case SectionType.Web:
|
|
case SectionType.Video:
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
ExportConfigurationDTO toDownload = configuration.ToExportDTO(sectionDTOs, resourceDTOs);
|
|
string jsonString = JsonConvert.SerializeObject(toDownload);
|
|
var fileName = $"{configuration.Label}.json";
|
|
var mimeType = "application/json";
|
|
var fileBytes = Encoding.UTF8.GetBytes(jsonString);
|
|
return new FileContentResult(fileBytes, mimeType)
|
|
{
|
|
FileDownloadName = fileName
|
|
};
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return null;
|
|
//return new BadRequestObjectResult(ex.Message) { };
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return null;
|
|
//return new NotFoundObjectResult(ex.Message) { };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return null;
|
|
//return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Import a configuration
|
|
/// </summary>
|
|
/// <param name="exportConfiguration">Configuration to import</param>
|
|
[ProducesResponseType(typeof(string), 202)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 409)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPost("import")]
|
|
public ObjectResult Import([FromBody] ExportConfigurationDTO exportConfiguration)
|
|
{
|
|
try
|
|
{
|
|
if (exportConfiguration == null)
|
|
throw new ArgumentNullException("File to import is null");
|
|
|
|
Configuration configuration = _configurationService.GetById(exportConfiguration.id);
|
|
if (configuration != null)
|
|
throw new InvalidOperationException("Configuration already exist in the system");
|
|
|
|
configuration = new Configuration();
|
|
configuration.Id = exportConfiguration.id;
|
|
configuration.InstanceId = exportConfiguration.instanceId;
|
|
configuration.Label = exportConfiguration.label;
|
|
configuration.Title = exportConfiguration.title;
|
|
configuration.ImageId = exportConfiguration.imageId;
|
|
configuration.ImageSource = exportConfiguration.imageSource;
|
|
|
|
if (configuration.ImageId != null)
|
|
{
|
|
createResource(exportConfiguration.resources.Where(r => r.id == configuration.ImageId).FirstOrDefault());
|
|
}
|
|
|
|
configuration.DateCreation = exportConfiguration.dateCreation;
|
|
configuration.PrimaryColor = exportConfiguration.primaryColor;
|
|
configuration.SecondaryColor = exportConfiguration.secondaryColor;
|
|
configuration.Languages = exportConfiguration.languages;
|
|
configuration.IsMobile = exportConfiguration.isMobile;
|
|
configuration.IsTablet = exportConfiguration.isTablet;
|
|
configuration.IsOffline = exportConfiguration.isOffline;
|
|
configuration.LoaderImageId = exportConfiguration.loaderImageId;
|
|
configuration.LoaderImageUrl = exportConfiguration.loaderImageUrl;
|
|
|
|
if (configuration.LoaderImageId != null)
|
|
{
|
|
createResource(exportConfiguration.resources.Where(r => r.id == configuration.LoaderImageId).FirstOrDefault());
|
|
}
|
|
|
|
configuration.WeatherCity = exportConfiguration.weatherCity;
|
|
configuration.IsDate = exportConfiguration.isDate;
|
|
configuration.IsHour = exportConfiguration.isHour;
|
|
|
|
_configurationService.Create(configuration);
|
|
|
|
foreach (var section in exportConfiguration.sections.Where(s => !_sectionService.IsExist(s.id)))
|
|
{
|
|
Section newSection = new Section();
|
|
newSection.Id = section.id;
|
|
newSection.InstanceId = section.instanceId;
|
|
newSection.Label = section.label;
|
|
newSection.Title = section.title;
|
|
newSection.Description = section.description;
|
|
newSection.Order = section.order.GetValueOrDefault(); // if one day we can use same section in multiple configuration, need to change that
|
|
newSection.Type = section.type;
|
|
newSection.ImageId = section.imageId;
|
|
newSection.ImageSource = section.imageSource;
|
|
newSection.ConfigurationId = section.configurationId;
|
|
newSection.IsSubSection = section.isSubSection;
|
|
newSection.ParentId = section.parentId;
|
|
newSection.Data = section.data;
|
|
newSection.DateCreation = section.dateCreation;
|
|
newSection.IsBeacon = section.isBeacon;
|
|
newSection.BeaconId = section.beaconId;
|
|
newSection.Latitude = section.latitude;
|
|
newSection.Longitude = section.longitude;
|
|
newSection.MeterZoneGPS = section.meterZoneGPS;
|
|
|
|
if (newSection.ImageId != null)
|
|
{
|
|
createResource(exportConfiguration.resources.Where(r => r.id == newSection.ImageId).FirstOrDefault());
|
|
}
|
|
|
|
_sectionService.Create(newSection);
|
|
|
|
switch (section.type)
|
|
{
|
|
case SectionType.Map:
|
|
MapDTO mapDTO = JsonConvert.DeserializeObject<MapDTO>(section.data);
|
|
if (mapDTO.iconResourceId != null)
|
|
{
|
|
createResource(exportConfiguration.resources.Where(r => r.id == mapDTO.iconResourceId).FirstOrDefault());
|
|
}
|
|
|
|
foreach (var point in mapDTO.points)
|
|
{
|
|
foreach (var content in point.contents)
|
|
{
|
|
if (content.resourceId != null)
|
|
{
|
|
createResource(exportConfiguration.resources.Where(r => r.id == content.resourceId).FirstOrDefault());
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var categorie in mapDTO.categories)
|
|
{
|
|
if (categorie.iconResourceId != null)
|
|
{
|
|
createResource(exportConfiguration.resources.Where(r => r.id == categorie.iconResourceId).FirstOrDefault());
|
|
}
|
|
}
|
|
|
|
break;
|
|
case SectionType.Slider:
|
|
SliderDTO sliderDTO = JsonConvert.DeserializeObject<SliderDTO>(section.data);
|
|
foreach (var content in sliderDTO.contents)
|
|
{
|
|
if (content.resourceId != null)
|
|
{
|
|
createResource(exportConfiguration.resources.Where(r => r.id == content.resourceId).FirstOrDefault());
|
|
}
|
|
}
|
|
break;
|
|
case SectionType.Quizz:
|
|
QuizzDTO quizzDTO = JsonConvert.DeserializeObject<QuizzDTO>(section.data);
|
|
foreach (var question in quizzDTO.questions)
|
|
{
|
|
if (question.label != null)
|
|
{
|
|
foreach (var questionLabel in question.label)
|
|
{
|
|
createResource(exportConfiguration.resources.Where(r => r.id == questionLabel.resourceId).FirstOrDefault());
|
|
}
|
|
}
|
|
if (question.imageBackgroundResourceId != null)
|
|
{
|
|
createResource(exportConfiguration.resources.Where(r => r.id == question.imageBackgroundResourceId).FirstOrDefault());
|
|
}
|
|
foreach (var response in question.responses)
|
|
{
|
|
if (response.label != null)
|
|
{
|
|
foreach (var responseLabel in response.label)
|
|
{
|
|
createResource(exportConfiguration.resources.Where(r => r.id == responseLabel.resourceId).FirstOrDefault());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (quizzDTO.bad_level != null)
|
|
{
|
|
if(quizzDTO.bad_level.label != null)
|
|
{
|
|
foreach (var balLevelLabel in quizzDTO.bad_level.label)
|
|
{
|
|
createResource(exportConfiguration.resources.Where(r => r.id == balLevelLabel.resourceId).FirstOrDefault());
|
|
}
|
|
}
|
|
}
|
|
if (quizzDTO.medium_level != null)
|
|
{
|
|
if (quizzDTO.medium_level.label != null)
|
|
{
|
|
foreach (var medium_levelLabel in quizzDTO.medium_level.label)
|
|
{
|
|
createResource(exportConfiguration.resources.Where(r => r.id == medium_levelLabel.resourceId).FirstOrDefault());
|
|
}
|
|
}
|
|
}
|
|
if (quizzDTO.good_level != null)
|
|
{
|
|
if (quizzDTO.good_level.label != null)
|
|
{
|
|
foreach (var good_levelLabel in quizzDTO.good_level.label)
|
|
{
|
|
createResource(exportConfiguration.resources.Where(r => r.id == good_levelLabel.resourceId).FirstOrDefault());
|
|
}
|
|
}
|
|
}
|
|
if (quizzDTO.great_level != null)
|
|
{
|
|
if (quizzDTO.great_level.label != null)
|
|
{
|
|
foreach (var great_levelLabel in quizzDTO.great_level.label)
|
|
{
|
|
createResource(exportConfiguration.resources.Where(r => r.id == great_levelLabel.resourceId).FirstOrDefault());
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
case SectionType.Article:
|
|
ArticleDTO articleDTO = JsonConvert.DeserializeObject<ArticleDTO>(section.data);
|
|
foreach (var content in articleDTO.contents)
|
|
{
|
|
if (content.resourceId != null)
|
|
{
|
|
createResource(exportConfiguration.resources.Where(r => r.id == content.resourceId).FirstOrDefault());
|
|
}
|
|
}
|
|
break;
|
|
case SectionType.Menu:
|
|
case SectionType.Web:
|
|
case SectionType.Video:
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
return new ObjectResult("The configuration has been successfully imported") { StatusCode = 202 };
|
|
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { };
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new NotFoundObjectResult(ex.Message) { };
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return new ConflictObjectResult(ex.Message) { };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
private void createResource(ResourceDTO resourceExport)
|
|
{
|
|
if (resourceExport != null)
|
|
{
|
|
Resource resource = new Resource();
|
|
resource.Id = resourceExport.id;
|
|
resource.InstanceId = resourceExport.instanceId;
|
|
resource.Type = resourceExport.type;
|
|
resource.Label = resourceExport.label;
|
|
resource.DateCreation = resourceExport.dateCreation;
|
|
//resource.Data = resourceExport.data;
|
|
|
|
if (!_resourceService.IsExist(resourceExport.id))
|
|
_resourceService.Create(resource);
|
|
}
|
|
}
|
|
|
|
private List<ResourceDTO> addResourceToList(List<ResourceDTO> resourceDTOs, string resourceId) {
|
|
if (!resourceDTOs.Select(r => r.id).Contains(resourceId)) {
|
|
Resource resource = _resourceService.GetById(resourceId);
|
|
|
|
if (resource != null && !resourceDTOs.Any(r => r.id == resource.Id)) {
|
|
resourceDTOs.Add(resource.ToDTO());
|
|
}
|
|
}
|
|
return resourceDTOs;
|
|
}
|
|
}
|
|
}
|