public async Task ChangeRoleAssignmentAsync(bool value, int id) { AppAccess model = ExpandModel; if (value) { var roles = await QueryAllRolesAsync().ConfigureAwait(false); var role = roles.SingleOrDefault(e => e.Id == id); if (role != null) { model.AddManyItem(role); await DataAccess.UpdateAsync(model).ConfigureAwait(false); } } else { var item = model.ManyItems.SingleOrDefault(e => e.Id == id); if (item != null) { model.RemoveManyItem(item); await DataAccess.UpdateAsync(model).ConfigureAwait(false); } } await LoadRolesAsync(model).ConfigureAwait(false); }
public override async Task <IAppAccess> InsertAsync(IAppAccess entity) { entity.CheckArgument(nameof(entity)); entity.OneItem.CheckArgument(nameof(entity.OneItem)); entity.ManyItems.CheckArgument(nameof(entity.ManyItems)); var result = new AppAccess(); result.OneEntity.CopyProperties(entity.OneItem); await oneEntityController.InsertAsync(result.OneEntity).ConfigureAwait(false); foreach (var item in entity.ManyItems) { var role = new Role(); #pragma warning disable IDE0017 // Simplify object initialization var joinRole = new IdentityXRole(); #pragma warning restore IDE0017 // Simplify object initialization joinRole.Identity = result.OneEntity; if (item.Id == 0) { item.Designation = RoleController.ClearRoleDesignation(item.Designation); var qryItem = await manyEntityController.ExecuteFirstOrDefaultAsync(e => e.Designation.Equals(item.Designation)) .ConfigureAwait(false); if (qryItem != null) { role.CopyProperties(qryItem); joinRole.RoleId = role.Id; } else { role.CopyProperties(item); await manyEntityController.InsertAsync(role).ConfigureAwait(false); joinRole.Role = role; } } else { var qryItem = await manyEntityController.GetByIdAsync(item.Id).ConfigureAwait(false); if (qryItem != null) { role.CopyProperties(qryItem); } joinRole.RoleId = role.Id; } await IdentityXRoleController.InsertAsync(joinRole).ConfigureAwait(false); result.AddManyItem(role); } return(result); }
protected override async Task LoadDetailsAsync(AppAccess entity, int masterId) { entity.ClearManyItems(); var query = await IdentityXRoleController.ExecuteWhereAsync(p => p.IdentityId == masterId) .ConfigureAwait(false); foreach (var item in query) { var role = await ManyEntityController.GetByIdAsync(item.RoleId).ConfigureAwait(false); if (role != null) { entity.AddManyItem(role); } } }
public override async Task <IAppAccess> UpdateAsync(IAppAccess entity) { entity.CheckArgument(nameof(entity)); entity.OneItem.CheckArgument(nameof(entity.OneItem)); entity.ManyItems.CheckArgument(nameof(entity.ManyItems)); var accessRoles = new List <Role>(); foreach (var item in entity.ManyItems) { var role = new Role(); item.Designation = RoleController.ClearRoleDesignation(item.Designation); role.CopyProperties(item); if (role.Id == 0) { var qyrRole = await manyEntityController.ExecuteFirstOrDefaultAsync(e => e.Designation.Equals(item.Designation)) .ConfigureAwait(false); if (qyrRole != null) { role.CopyProperties(qyrRole); } } accessRoles.Add(role); } //Delete all costs that are no longer included in the list. var identityXRoles = await IdentityXRoleController.ExecuteQueryAllEntitiesAsync(e => e.IdentityId == entity.Id).ConfigureAwait(false); foreach (var item in identityXRoles) { var stillHasTheRole = accessRoles.Any(i => i.Id == item.RoleId); if (stillHasTheRole == false) { await IdentityXRoleController.DeleteAsync(item.Id).ConfigureAwait(false); } } var result = new AppAccess(); var firstEntity = await OneEntityController.UpdateAsync(entity.OneItem).ConfigureAwait(false); result.OneItem.CopyProperties(firstEntity); foreach (var accessRole in accessRoles) { var role = new Role(); var joinRole = new IdentityXRole(); role.Id = accessRole.Id; joinRole.IdentityId = firstEntity.Id; if (accessRole.Id == 0) { role.CopyProperties(accessRole); await manyEntityController.InsertAsync(role).ConfigureAwait(false); joinRole.Role = role; } else { var qryRole = await manyEntityController.GetByIdAsync(role.Id).ConfigureAwait(false); if (qryRole != null) { role.CopyProperties(qryRole); joinRole.RoleId = role.Id; } } var identityXRole = identityXRoles.SingleOrDefault(e => e.IdentityId == joinRole.IdentityId && e.RoleId == joinRole.RoleId); if (identityXRole == null) { await IdentityXRoleController.InsertAsync(joinRole).ConfigureAwait(false); } result.AddManyItem(role); } return(result); }