public static PolicySetDataContract FromTblPolicySetToPolicySetDataContract(tbl_policy_set policySetTblData) { if (policySetTblData.id < 1 || policySetTblData.id_policy < 1) { return(null); } return(new PolicySetDataContract { PolicySetId = policySetTblData.id, PolicyId = policySetTblData.id_policy, LoginId = policySetTblData.id_login, GroupId = policySetTblData.id_group, Selected = policySetTblData.selected, PolicyParam = policySetTblData.policy_param }); }
public Result <object> UpdatePolicySetForLogin(long loginId, PolicySetDataContract[] policySetList) { var result = new Result <object>(); using (var transaction = new TransactionScope()) { try { if (loginId < 1) { throw new Exception("Не выбран логин"); } using (var ctx = new PolicyProjectEntities()) { var groupId = ctx.tbl_login.FirstOrDefault(x => x.id == loginId).id_group; var groupPolicySet = ctx.tbl_policy_set.Where(x => x.id_group == groupId); var policySetForLoginInTbl = ctx.tbl_policy_set.Where(x => x.id_login == loginId); long newPolicySetId = 0; for (var i = 0; i < policySetList.Length; i++) { var containedGroup = groupPolicySet.FirstOrDefault(x => x.id_policy == policySetList[i].PolicyId); var containedInTbl = policySetForLoginInTbl.FirstOrDefault(x => x.id_policy == policySetList[i].PolicyId); if (policySetList[i].Selected.HasValue && policySetList[i].Selected.Value) { if (containedGroup == null) { if (containedInTbl != null) { containedInTbl.id_policy = policySetList[i].PolicyId; containedInTbl.id_login = policySetList[i].LoginId; containedInTbl.selected = policySetList[i].Selected; containedInTbl.policy_param = policySetList[i].PolicyParam; } else { if (newPolicySetId == 0) { newPolicySetId = ctx.tbl_policy_set.Any() ? ctx.tbl_policy_set.Max(x => x.id) + 1 : 1; } else { newPolicySetId++; } var newPolicySet = new tbl_policy_set { id = newPolicySetId, id_policy = policySetList[i].PolicyId, id_login = policySetList[i].LoginId, selected = policySetList[i].Selected, policy_param = policySetList[i].PolicyParam }; ctx.tbl_policy_set.Add(newPolicySet); } } else { if (containedInTbl == null) { continue; } var deletedPolicySet = ctx.tbl_policy_set.FirstOrDefault(x => x.id == containedInTbl.id); ctx.tbl_policy_set.Remove(deletedPolicySet); } } else { if (containedGroup == null) { if (containedInTbl == null) { continue; } var deletedPolicySet = ctx.tbl_policy_set.FirstOrDefault(x => x.id == containedInTbl.id); ctx.tbl_policy_set.Remove(deletedPolicySet); } else { if (containedInTbl != null) { containedInTbl.id_policy = policySetList[i].PolicyId; containedInTbl.id_login = policySetList[i].LoginId; containedInTbl.selected = policySetList[i].Selected; containedInTbl.policy_param = policySetList[i].PolicyParam; } else { if (newPolicySetId == 0) { newPolicySetId = ctx.tbl_policy_set.Any() ? ctx.tbl_policy_set.Max(x => x.id) + 1 : 1; } else { newPolicySetId++; } var newPolicySet = new tbl_policy_set { id = newPolicySetId, id_policy = policySetList[i].PolicyId, id_login = policySetList[i].LoginId, selected = policySetList[i].Selected, policy_param = policySetList[i].PolicyParam }; ctx.tbl_policy_set.Add(newPolicySet); } } } } result.BoolRes = ctx.SaveChanges() == policySetList.Length; if (result.BoolRes) { transaction.Complete(); } } } catch (Exception ex) { transaction.Dispose(); result.BoolRes = false; result.ErrorRes = string.Concat("Ошибка сохранения набора политик логина. ", ex.Message); } } return(result); }