/// <summary> /// Updates existing autopilot rules /// </summary> /// <param name="autopilotRuleModels"></param> private void UpdateAutopilotRulesRepository(IReadOnlyCollection <UpdateAutopilotRuleModel> autopilotRuleModels) { if (autopilotRuleModels == null || !autopilotRuleModels.Any()) { return; } var autopilotRules = _autopilotRuleRepository .GetAll() .GroupBy(r => r.UniqueRuleKey) .ToDictionary(r => r.Key, r => r.ToList()); foreach (var ruleModel in autopilotRuleModels) { var rulesToUpdate = !autopilotRules.ContainsKey(ruleModel.UniqueRuleKey) ? null : autopilotRules[ruleModel.UniqueRuleKey]; if (rulesToUpdate == null || !rulesToUpdate.Any()) { continue; } // update for each flexibility level foreach (var rule in rulesToUpdate) { rule.Enabled = ruleModel.Enabled; _autopilotRuleRepository.Update(rule); } } }
/// <summary> /// Updates autopilot rules according to the rules repository changes /// </summary> /// <param name="ruleTypeId"></param> /// <param name="newRules"></param> /// <param name="deletedRules"></param> private void UpdateAutopilotRulesRepository(int ruleTypeId, IReadOnlyCollection <int> newRules, IReadOnlyCollection <int> deletedRules) { var flexibilityLevels = _flexibilityLevelRepository.GetAll().ToList(); // delete autopilot rules if (deletedRules != null && deletedRules.Any()) { var autopilotRulesToDelete = _autopilotRuleRepository.GetAll() .Where(rt => rt.RuleTypeId == ruleTypeId && deletedRules.Contains(rt.RuleId)).ToList(); if (autopilotRulesToDelete.Any()) { _autopilotRuleRepository.Delete(autopilotRulesToDelete.Select(r => r.Id)); } } // add new autopilot rules if (newRules != null && newRules.Any()) { foreach (var ruleId in newRules) { foreach (var flexibilityLevel in flexibilityLevels) { var autopilotRule = AutopilotRule.Create(flexibilityLevel.Id, ruleId, ruleTypeId); _autopilotRuleRepository.Add(autopilotRule); } } } _autopilotRuleRepository.SaveChanges(); }
/// <summary> /// Builds instance of <see cref="AutopilotSettingsModel"/> model using <see cref="AutopilotSettings"/> /// </summary> /// <param name="autopilotSettings"></param> /// <param name="autopilotRuleRepository"></param> /// <param name="ruleRepository"></param> /// <param name="ruleTypeRepository"></param> /// <returns></returns> public static AutopilotSettingsModel MapToAutopilotSettingsModel(AutopilotSettings autopilotSettings, IAutopilotRuleRepository autopilotRuleRepository, IRuleRepository ruleRepository, IRuleTypeRepository ruleTypeRepository, IMapper mapper) { // get only autopilot enabled rule types with rules var ruleTypeModels = MapToRuleTypeModel(ruleTypeRepository.GetAll(true).ToList(), ruleRepository, mapper); var rules = ruleTypeModels.SelectMany(r => r.Rules).ToList(); // one autopilot rule per internal rule var autopilotRulesGrouped = autopilotRuleRepository.GetAll() .GroupBy(r => r.UniqueRuleKey) .Select(r => r.First()) .ToList(); return(mapper.Map <AutopilotSettingsModel>(Tuple.Create(autopilotSettings, autopilotRulesGrouped, rules, ruleTypeModels))); }
public IEnumerable <AutopilotRule> GetAll() => _repository.GetAll();