public async Task <bool> UpdatePolicySet(PolicySet policySet) { if (policySet == null) { throw new ArgumentException("Набор политик не задан"); } var existed = await _policySetDbContext.PolicySets.FirstOrDefaultAsync(x => x.PolicySetId == policySet.PolicySetId); if (existed == null) { throw new Exception($"Набора политик {policySet.PolicySetId} не существует"); } existed.GroupId = policySet.GroupId; existed.LoginId = policySet.LoginId; existed.PolicyId = policySet.PolicyId; existed.Selected = policySet.Selected; existed.PolicyParam = policySet.PolicyParam; var updated = await(_policySetDbContext as DbContext).SaveChangesAsync(); return(updated > 0); }
public async Task <bool> UpdatePolicySetsForLogin(Guid loginId, PolicySet[] policySetList) { if (loginId == Guid.Empty) { throw new ArgumentException("Не задан логин"); } if (policySetList == null || !policySetList.Any()) { throw new ArgumentException("Пустой набор политик"); } var nativeDbContext = _policySetDbContext as DbContext; using (var transaction = await nativeDbContext.Database.BeginTransactionAsync()) { try { var groupId = (await _policySetDbContext.Logins.FirstOrDefaultAsync(x => x.LoginId == loginId)) .GroupId; var groupPolicySet = _policySetDbContext.PolicySets.Where(x => x.GroupId == groupId); var policySetForLoginInTbl = _policySetDbContext.PolicySets.Where(x => x.LoginId == loginId); for (var i = 0; i < policySetList.Length; i++) { var containedGroup = await groupPolicySet.FirstOrDefaultAsync(x => x.PolicyId == policySetList[i].PolicyId); var containedInTbl = await policySetForLoginInTbl.FirstOrDefaultAsync(x => x.PolicyId == policySetList[i].PolicyId); if (policySetList[i].Selected.HasValue && policySetList[i].Selected.Value) { if (containedGroup == null) { if (containedInTbl != null) { containedInTbl.PolicyId = policySetList[i].PolicyId; containedInTbl.LoginId = policySetList[i].LoginId; containedInTbl.Selected = policySetList[i].Selected; containedInTbl.PolicyParam = policySetList[i].PolicyParam; } else { var newPolicySet = new PolicySet { PolicySetId = Guid.NewGuid(), PolicyId = policySetList[i].PolicyId, LoginId = policySetList[i].LoginId, Selected = policySetList[i].Selected, PolicyParam = policySetList[i].PolicyParam }; _policySetDbContext.PolicySets.Add(newPolicySet); } } else { if (containedInTbl == null) { continue; } var deletedPolicySet = await _policySetDbContext.PolicySets.FirstOrDefaultAsync(x => x.PolicySetId == containedInTbl.PolicySetId); if (deletedPolicySet != null) { _policySetDbContext.PolicySets.Remove(deletedPolicySet); } } } else { if (containedGroup == null) { if (containedInTbl == null) { continue; } var deletedPolicySet = await _policySetDbContext.PolicySets.FirstOrDefaultAsync(x => x.PolicySetId == containedInTbl.PolicySetId); if (deletedPolicySet != null) { _policySetDbContext.PolicySets.Remove(deletedPolicySet); } } else { if (containedInTbl != null) { containedInTbl.PolicyId = policySetList[i].PolicyId; containedInTbl.LoginId = policySetList[i].LoginId; containedInTbl.Selected = policySetList[i].Selected; containedInTbl.PolicyParam = policySetList[i].PolicyParam; } else { var newPolicySet = new PolicySet { PolicySetId = Guid.NewGuid(), PolicyId = policySetList[i].PolicyId, LoginId = policySetList[i].LoginId, Selected = policySetList[i].Selected, PolicyParam = policySetList[i].PolicyParam }; _policySetDbContext.PolicySets.Add(newPolicySet); } } } } var result = await nativeDbContext.SaveChangesAsync() == policySetList.Length; if (result) { await transaction.CommitAsync(); } return(result); } catch (Exception) { await transaction.RollbackAsync(); throw; } } }