Configurations controller (to be tested) + wip for subsection

This commit is contained in:
Thomas Fransolet 2025-03-06 18:21:42 +01:00
parent 8c18825c5d
commit 20c35b2c8d

View File

@ -60,7 +60,7 @@ namespace ManagerService.Controllers
foreach(var configuration in configurations) foreach(var configuration in configurations)
{ {
List<string> sectionIds = _myInfoMateDbContext.Sections.Where(c => c.ConfigurationId == configuration.Id).Select(c => c.Id).ToList(); List<string> sectionIds = _myInfoMateDbContext.Sections.Where(s => s.ConfigurationId == configuration.Id && !s.IsSubSection).Select(s => s.Id).ToList();
ConfigurationDTO configurationDTO = configuration.ToDTO(sectionIds); ConfigurationDTO configurationDTO = configuration.ToDTO(sectionIds);
configurationDTOs.Add(configurationDTO); configurationDTOs.Add(configurationDTO);
} }
@ -86,16 +86,16 @@ namespace ManagerService.Controllers
{ {
try try
{ {
Instance instance = _instanceService.GetByPinCode(pinCode); Instance instance = _myInfoMateDbContext.Instances.FirstOrDefault(i => i.PinCode == pinCode);
if (instance == null) if (instance == null)
throw new KeyNotFoundException("None instance is linked to this pin code"); throw new KeyNotFoundException("None instance is linked to this pin code");
List<Configuration> configurations = _configurationService.GetAll(instance.Id); List<Configuration> configurations = _myInfoMateDbContext.Configurations.Where(c => c.InstanceId == instance.Id).ToList();
List<ConfigurationDTO> configurationDTOs = new List<ConfigurationDTO>(); List<ConfigurationDTO> configurationDTOs = new List<ConfigurationDTO>();
foreach (var configuration in configurations) foreach (var configuration in configurations)
{ {
List<string> sectionIds = _sectionService.GetAllIdsFromConfiguration(configuration.Id); List<string> sectionIds = _myInfoMateDbContext.Sections.Where(s => s.ConfigurationId == configuration.Id && !s.IsSubSection).Select(s => s.Id).ToList();
configurationDTOs.Add(configuration.ToDTO(sectionIds)); configurationDTOs.Add(configuration.ToDTO(sectionIds));
} }
@ -120,16 +120,16 @@ namespace ManagerService.Controllers
{ {
try try
{ {
Configuration configuration = _configurationService.GetById(id); Configuration configuration = _myInfoMateDbContext.Configurations.FirstOrDefault(c => c.Id == id);
if (configuration == null) if (configuration == null)
throw new KeyNotFoundException("This configuration was not found"); throw new KeyNotFoundException("This configuration was not found");
List<string> sectionIds = _sectionService.GetAllIdsFromConfiguration(id); List<string> sectionIds = _myInfoMateDbContext.Sections.Where(s => s.ConfigurationId == id && !s.IsSubSection).Select(s => s.Id).ToList();
try try
{ {
var weatherSections = _sectionService.GetAllWeatherSectionsFromConfiguration(id); var weatherSections = _myInfoMateDbContext.Sections.Where(s => s.ConfigurationId == id && s.Type == SectionType.Weather && !s.IsSubSection).ToList(); // TODO TEST
foreach (var weatherSection in weatherSections) foreach (var weatherSection in weatherSections)
{ {
@ -168,9 +168,10 @@ namespace ManagerService.Controllers
weatherDTO.updatedDate = DateTimeOffset.Now; weatherDTO.updatedDate = DateTimeOffset.Now;
weatherDTO.result = callResponseBody; weatherDTO.result = callResponseBody;
weatherSection.Data = JsonConvert.SerializeObject(weatherDTO); weatherSection.Data = JsonConvert.SerializeObject(weatherDTO); // TODO update
_sectionService.Update(weatherSection.Id, weatherSection); _myInfoMateDbContext.SaveChanges();
//_sectionService.Update(weatherSection.Id, weatherSection);
} }
else else
{ {
@ -249,9 +250,10 @@ namespace ManagerService.Controllers
configuration.IsTablet = newConfiguration.isTablet; configuration.IsTablet = newConfiguration.isTablet;
configuration.IsOffline = newConfiguration.isOffline; configuration.IsOffline = newConfiguration.isOffline;
Configuration configurationCreated = _configurationService.Create(configuration); _myInfoMateDbContext.Configurations.Add(configuration);
//Configuration configurationCreated = _configurationService.Create(configuration);
return new OkObjectResult(configurationCreated.ToDTO(new List<string>())); // Empty list return new OkObjectResult(configuration.ToDTO(new List<string>())); // Empty list
} }
catch (ArgumentNullException ex) catch (ArgumentNullException ex)
{ {
@ -284,7 +286,7 @@ namespace ManagerService.Controllers
if (updatedConfiguration == null) if (updatedConfiguration == null)
throw new ArgumentNullException("configuration param is null"); throw new ArgumentNullException("configuration param is null");
Configuration configuration = _configurationService.GetById(updatedConfiguration.id); Configuration configuration = _myInfoMateDbContext.Configurations.FirstOrDefault(c => c.Id == updatedConfiguration.id);
if (configuration == null) if (configuration == null)
throw new KeyNotFoundException("Configuration does not exist"); throw new KeyNotFoundException("Configuration does not exist");
@ -315,13 +317,14 @@ namespace ManagerService.Controllers
configuration.RoundedValue = updatedConfiguration.roundedValue; configuration.RoundedValue = updatedConfiguration.roundedValue;
configuration.ScreenPercentageSectionsMainPage = updatedConfiguration.screenPercentageSectionsMainPage; configuration.ScreenPercentageSectionsMainPage = updatedConfiguration.screenPercentageSectionsMainPage;
Configuration configurationModified = _configurationService.Update(updatedConfiguration.id, configuration); //Configuration configurationModified = _configurationService.Update(updatedConfiguration.id, configuration);
_myInfoMateDbContext.SaveChanges();
MqttClientService.PublishMessage($"config/{configurationModified.Id}", JsonConvert.SerializeObject(new PlayerMessageDTO() { configChanged = true })); MqttClientService.PublishMessage($"config/{configuration.Id}", JsonConvert.SerializeObject(new PlayerMessageDTO() { configChanged = true }));
List<string> sectionIds = _sectionService.GetAllIdsFromConfiguration(configuration.Id);
return new OkObjectResult(configurationModified.ToDTO(sectionIds)); List<string> sectionIds = _myInfoMateDbContext.Sections.Where(s => s.ConfigurationId == configuration.Id && !s.IsSubSection).Select(s => s.Id).ToList(); // _sectionService.GetAllIdsFromConfiguration(configuration.Id);
return new OkObjectResult(configuration.ToDTO(sectionIds));
} }
catch (ArgumentNullException ex) catch (ArgumentNullException ex)
{ {
@ -354,19 +357,22 @@ namespace ManagerService.Controllers
if (id == null) if (id == null)
throw new ArgumentNullException("Configuration param is null"); throw new ArgumentNullException("Configuration param is null");
if (!_configurationService.IsExist(id)) var configuration = _myInfoMateDbContext.Configurations.FirstOrDefault(c => c.Id == id);
if (configuration == null)
throw new KeyNotFoundException("Configuration does not exist"); throw new KeyNotFoundException("Configuration does not exist");
_configurationService.Remove(id); _myInfoMateDbContext.Remove(configuration);
// Delete config for all devices // Delete config for all devices
List<Device> devices = _deviceService.GetAllWithConfig(id); List<Device> devices = _myInfoMateDbContext.Devices.Where(d => d.ConfigurationId == id).ToList();
foreach (var device in devices) foreach (var device in devices)
{ {
device.Configuration = null; device.Configuration = null;
device.ConfigurationId = null; device.ConfigurationId = null;
_deviceService.Update(device.Id, device); _myInfoMateDbContext.SaveChanges();
//_deviceService.Update(device.Id, device);
} }
MqttClientService.PublishMessage($"config/{id}", JsonConvert.SerializeObject(new PlayerMessageDTO() { configChanged = true, isDeleted = true })); MqttClientService.PublishMessage($"config/{id}", JsonConvert.SerializeObject(new PlayerMessageDTO() { configChanged = true, isDeleted = true }));
@ -407,12 +413,12 @@ namespace ManagerService.Controllers
if (id == null) if (id == null)
throw new ArgumentNullException("Configuration param is null"); throw new ArgumentNullException("Configuration param is null");
Configuration configuration = _configurationService.GetById(id); Configuration configuration = _myInfoMateDbContext.Configurations.FirstOrDefault(c => c.Id == id);
if (configuration == null) if (configuration == null)
throw new KeyNotFoundException("Configuration does not exist"); throw new KeyNotFoundException("Configuration does not exist");
List<SectionDTO> sectionDTOs = _sectionService.GetAllFromConfigurationEvenSubsection(configuration.Id).Select(s => s.ToDTO()).ToList(); List<SectionDTO> sectionDTOs = _myInfoMateDbContext.Sections.Where(s => s.ConfigurationId == configuration.Id).Select(s => s.ToDTO()).ToList();
List<ResourceDTO> resourceDTOs = new List<ResourceDTO>(); List<ResourceDTO> resourceDTOs = new List<ResourceDTO>();
if (configuration.ImageId != null) if (configuration.ImageId != null)
@ -634,7 +640,7 @@ namespace ManagerService.Controllers
if (exportConfiguration == null) if (exportConfiguration == null)
throw new ArgumentNullException("File to import is null"); throw new ArgumentNullException("File to import is null");
Configuration configuration = _configurationService.GetById(exportConfiguration.id); Configuration configuration = _myInfoMateDbContext.Configurations.FirstOrDefault(c => c.Id == exportConfiguration.id);
if (configuration != null) if (configuration != null)
throw new InvalidOperationException("Configuration already exist in the system"); throw new InvalidOperationException("Configuration already exist in the system");
@ -673,9 +679,12 @@ namespace ManagerService.Controllers
configuration.RoundedValue = exportConfiguration.roundedValue; configuration.RoundedValue = exportConfiguration.roundedValue;
configuration.ScreenPercentageSectionsMainPage = exportConfiguration.screenPercentageSectionsMainPage; configuration.ScreenPercentageSectionsMainPage = exportConfiguration.screenPercentageSectionsMainPage;
_configurationService.Create(configuration); _myInfoMateDbContext.Configurations.Add(configuration);
//_configurationService.Create(configuration);
foreach (var section in exportConfiguration.sections.Where(s => !_sectionService.IsExist(s.id))) var sectionsAlreadyInDB = _myInfoMateDbContext.Sections.Where(s => !exportConfiguration.sections.Select(s => s.id).Contains(s.Id)).Select(s => s.Id).ToList();
// TODO TEST
foreach (var section in exportConfiguration.sections.Where(s => !sectionsAlreadyInDB.Contains(s.id)))
{ {
Section newSection = new Section(); Section newSection = new Section();
newSection.Id = section.id; newSection.Id = section.id;
@ -703,7 +712,8 @@ namespace ManagerService.Controllers
createResource(exportConfiguration.resources.Where(r => r.id == newSection.ImageId).FirstOrDefault()); createResource(exportConfiguration.resources.Where(r => r.id == newSection.ImageId).FirstOrDefault());
} }
_sectionService.Create(newSection); _myInfoMateDbContext.Sections.Add(newSection);
//_sectionService.Create(newSection);
switch (section.type) switch (section.type)
{ {
@ -867,14 +877,17 @@ namespace ManagerService.Controllers
resource.DateCreation = resourceExport.dateCreation; resource.DateCreation = resourceExport.dateCreation;
//resource.Data = resourceExport.data; //resource.Data = resourceExport.data;
if (!_resourceService.IsExist(resourceExport.id)) var resourceInDb = _myInfoMateDbContext.Resources.FirstOrDefault(r => r.Id == resourceExport.id);
_resourceService.Create(resource);
if (resourceInDb == null)
_myInfoMateDbContext.Resources.Add(resource);
//_resourceService.Create(resource);
} }
} }
private List<ResourceDTO> addResourceToList(List<ResourceDTO> resourceDTOs, string resourceId) { private List<ResourceDTO> addResourceToList(List<ResourceDTO> resourceDTOs, string resourceId) {
if (!resourceDTOs.Select(r => r.id).Contains(resourceId)) { if (!resourceDTOs.Select(r => r.id).Contains(resourceId)) {
Resource resource = _resourceService.GetById(resourceId); Resource resource = _myInfoMateDbContext.Resources.FirstOrDefault(r => r.Id == resourceId);
if (resource != null && !resourceDTOs.Any(r => r.id == resource.Id)) { if (resource != null && !resourceDTOs.Any(r => r.id == resource.Id)) {
resourceDTOs.Add(resource.ToDTO()); resourceDTOs.Add(resource.ToDTO());