From 20c35b2c8db43760b9ed1ed453112c0cd06f018e Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Thu, 6 Mar 2025 18:21:42 +0100 Subject: [PATCH] Configurations controller (to be tested) + wip for subsection --- .../Controllers/ConfigurationController.cs | 73 +++++++++++-------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/ManagerService/Controllers/ConfigurationController.cs b/ManagerService/Controllers/ConfigurationController.cs index 46b6621..305830d 100644 --- a/ManagerService/Controllers/ConfigurationController.cs +++ b/ManagerService/Controllers/ConfigurationController.cs @@ -60,7 +60,7 @@ namespace ManagerService.Controllers foreach(var configuration in configurations) { - List sectionIds = _myInfoMateDbContext.Sections.Where(c => c.ConfigurationId == configuration.Id).Select(c => c.Id).ToList(); + List sectionIds = _myInfoMateDbContext.Sections.Where(s => s.ConfigurationId == configuration.Id && !s.IsSubSection).Select(s => s.Id).ToList(); ConfigurationDTO configurationDTO = configuration.ToDTO(sectionIds); configurationDTOs.Add(configurationDTO); } @@ -86,16 +86,16 @@ namespace ManagerService.Controllers { try { - Instance instance = _instanceService.GetByPinCode(pinCode); + Instance instance = _myInfoMateDbContext.Instances.FirstOrDefault(i => i.PinCode == pinCode); if (instance == null) throw new KeyNotFoundException("None instance is linked to this pin code"); - List configurations = _configurationService.GetAll(instance.Id); + List configurations = _myInfoMateDbContext.Configurations.Where(c => c.InstanceId == instance.Id).ToList(); List configurationDTOs = new List(); foreach (var configuration in configurations) { - List sectionIds = _sectionService.GetAllIdsFromConfiguration(configuration.Id); + List sectionIds = _myInfoMateDbContext.Sections.Where(s => s.ConfigurationId == configuration.Id && !s.IsSubSection).Select(s => s.Id).ToList(); configurationDTOs.Add(configuration.ToDTO(sectionIds)); } @@ -120,16 +120,16 @@ namespace ManagerService.Controllers { try { - Configuration configuration = _configurationService.GetById(id); + Configuration configuration = _myInfoMateDbContext.Configurations.FirstOrDefault(c => c.Id == id); if (configuration == null) throw new KeyNotFoundException("This configuration was not found"); - List sectionIds = _sectionService.GetAllIdsFromConfiguration(id); + List sectionIds = _myInfoMateDbContext.Sections.Where(s => s.ConfigurationId == id && !s.IsSubSection).Select(s => s.Id).ToList(); 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) { @@ -168,9 +168,10 @@ namespace ManagerService.Controllers weatherDTO.updatedDate = DateTimeOffset.Now; 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 { @@ -249,9 +250,10 @@ namespace ManagerService.Controllers configuration.IsTablet = newConfiguration.isTablet; 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())); // Empty list + return new OkObjectResult(configuration.ToDTO(new List())); // Empty list } catch (ArgumentNullException ex) { @@ -284,7 +286,7 @@ namespace ManagerService.Controllers if (updatedConfiguration == 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) throw new KeyNotFoundException("Configuration does not exist"); @@ -315,13 +317,14 @@ namespace ManagerService.Controllers configuration.RoundedValue = updatedConfiguration.roundedValue; 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 })); - - List sectionIds = _sectionService.GetAllIdsFromConfiguration(configuration.Id); + MqttClientService.PublishMessage($"config/{configuration.Id}", JsonConvert.SerializeObject(new PlayerMessageDTO() { configChanged = true })); - return new OkObjectResult(configurationModified.ToDTO(sectionIds)); + List 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) { @@ -354,19 +357,22 @@ namespace ManagerService.Controllers if (id == 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"); - _configurationService.Remove(id); + _myInfoMateDbContext.Remove(configuration); // Delete config for all devices - List devices = _deviceService.GetAllWithConfig(id); + List devices = _myInfoMateDbContext.Devices.Where(d => d.ConfigurationId == id).ToList(); foreach (var device in devices) { device.Configuration = 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 })); @@ -407,12 +413,12 @@ namespace ManagerService.Controllers if (id == 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) throw new KeyNotFoundException("Configuration does not exist"); - List sectionDTOs = _sectionService.GetAllFromConfigurationEvenSubsection(configuration.Id).Select(s => s.ToDTO()).ToList(); + List sectionDTOs = _myInfoMateDbContext.Sections.Where(s => s.ConfigurationId == configuration.Id).Select(s => s.ToDTO()).ToList(); List resourceDTOs = new List(); if (configuration.ImageId != null) @@ -634,7 +640,7 @@ namespace ManagerService.Controllers if (exportConfiguration == 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) throw new InvalidOperationException("Configuration already exist in the system"); @@ -673,9 +679,12 @@ namespace ManagerService.Controllers configuration.RoundedValue = exportConfiguration.roundedValue; 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(); newSection.Id = section.id; @@ -703,7 +712,8 @@ namespace ManagerService.Controllers createResource(exportConfiguration.resources.Where(r => r.id == newSection.ImageId).FirstOrDefault()); } - _sectionService.Create(newSection); + _myInfoMateDbContext.Sections.Add(newSection); + //_sectionService.Create(newSection); switch (section.type) { @@ -867,14 +877,17 @@ namespace ManagerService.Controllers resource.DateCreation = resourceExport.dateCreation; //resource.Data = resourceExport.data; - if (!_resourceService.IsExist(resourceExport.id)) - _resourceService.Create(resource); + var resourceInDb = _myInfoMateDbContext.Resources.FirstOrDefault(r => r.Id == resourceExport.id); + + if (resourceInDb == null) + _myInfoMateDbContext.Resources.Add(resource); + //_resourceService.Create(resource); } } private List addResourceToList(List resourceDTOs, string 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)) { resourceDTOs.Add(resource.ToDTO());