public Role AddOrUpdateRole(Role role) { if (role == null) { throw new ArgumentNullException("role"); } var sourceEntry = role.ToDataModel(); using (var repository = _platformRepository()) using(var changeTracker = GetChangeTracker(repository)) { AddOrUpdatePermissions(repository, role.Permissions); var targetEntry = repository.Roles.Include(r => r.RolePermissions) .FirstOrDefault(r => r.Id == role.Id); if (targetEntry == null) { repository.Add(sourceEntry); } else { changeTracker.Attach(targetEntry); sourceEntry.Patch(targetEntry); } CommitChanges(repository); } var result = GetRole(sourceEntry.Id); return result; }
public Role AddOrUpdateRole(Role role) { if (role == null) { throw new ArgumentNullException("role"); } var sourceEntry = role.ToDataModel(); using (var repository = _platformRepository()) using(var changeTracker = GetChangeTracker(repository)) { var targetEntry = repository.GetRoleById(role.Id); //Create not exist permissions if(role.Permissions != null) { var permissionIds = role.Permissions.Select(x => x.Id).ToArray(); var alreadyExistPermissionIds = repository.Permissions.Where(x => permissionIds.Contains(x.Id)) .Select(x => x.Id) .ToArray(); var notExistPermissionIds = permissionIds.Except(alreadyExistPermissionIds).ToArray(); foreach(var notExistPermissionId in notExistPermissionIds) { var permission = role.Permissions.First(x => x.Id == notExistPermissionId).ToDataModel(); repository.Add(permission); } } if (targetEntry == null) { repository.Add(sourceEntry); } else { changeTracker.Attach(targetEntry); sourceEntry.Patch(targetEntry); } CommitChanges(repository); } var result = GetRole(sourceEntry.Id); return result; }