private string GetStringValue(DetectorSettingsValueEntity value) { return(GetStringValue( value.option_data_value_base64, value.detector_settings_prefab.DataType.name.FromName() )); }
public async Task <DetectorModel> SetupSettings(AuthorizedDto <DetectorDto> dto) { return(await Execute(async() => { using (UnitOfWork db = new UnitOfWork()) { IRepo <DetectorEntity> detectorRepo = db.GetRepo <DetectorEntity>(); DetectorEntity detectorEntity = await detectorRepo.FirstOrDefault(pi => pi.id == dto.Data.Id.Value); DetectorInteractionEventEntity detectorEvent = new DetectorInteractionEventEntity() { account_id = dto.Session.UserId, detector_id = detectorEntity.id, timespan = DateTime.Now, log = $"Settings setup for Detector #{detectorEntity.id} by {dto.Session.UserId}" }; await db.GetRepo <DetectorInteractionEventEntity>().Create(detectorEvent); foreach (DetectorSettingsValueDto settingsValueDto in dto.Data.SettingsValues) { if (settingsValueDto.Id.HasValue) { DetectorSettingsValueEntity settingsValueEntity = detectorEntity.DetectorSettingsValues.First( setting => setting.id == settingsValueDto.Id.Value ); DataType dataType = settingsValueEntity.detector_settings_prefab.DataType.name.FromName(); DataTypeService.CheckIsDataOfType(settingsValueDto.ValueBase64, dataType); settingsValueEntity.option_data_value_base64 = settingsValueDto.ValueBase64; } else { DetectorSettingsPrefabEntity detectorSettingsPrefab = detectorEntity.DetectorPrefab.DetectorSettingsPrefabs.FirstOrDefault( settingsPrefab => settingsPrefab.id == settingsValueDto.PrefabId.Value ); if (detectorSettingsPrefab == null) { throw new NotFoundException("DetectorSettingsPrefab"); } DataType dataType = detectorSettingsPrefab.DataType.name.FromName(); DataTypeService.CheckIsDataOfType(settingsValueDto.ValueBase64, dataType); DetectorSettingsValueEntity settingWithSameSettingPrefab = detectorEntity.DetectorSettingsValues.FirstOrDefault( setting => setting.detector_settings_prefab_id == settingsValueDto.PrefabId.Value ); if (settingWithSameSettingPrefab != null) { settingWithSameSettingPrefab.option_data_value_base64 = settingsValueDto.ValueBase64; } else { DetectorSettingsValueEntity settingsValueEntity = new DetectorSettingsValueEntity() { detector_id = detectorEntity.id, option_data_value_base64 = settingsValueDto.ValueBase64, detector_settings_prefab_id = settingsValueDto.PrefabId.Value }; detectorEntity.DetectorSettingsValues.Add(settingsValueEntity); } } } await db.Save(); return detectorEntity.ToModel <DetectorModel>(); } })); }