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);
        }
        public override async Task <IAppAccess> InsertAsync(IAppAccess entity)
        {
            entity.CheckArgument(nameof(entity));
            entity.Identity.CheckArgument(nameof(entity.Identity));
            entity.Roles.CheckArgument(nameof(entity.Roles));

            var result = new AppAccess();

            result.IdentityEntity.CopyProperties(entity.Identity);
            result.IdentityEntity.PasswordHash = AccountManager.CalculateHash(result.IdentityEntity.Password);
            await identityController.InsertAsync(result.IdentityEntity).ConfigureAwait(false);

            foreach (var item in entity.Roles)
            {
                var role     = new Role();
                var joinRole = new IdentityXRole();

                joinRole.Identity = result.IdentityEntity;
                if (item.Id == 0)
                {
                    item.Designation = RoleController.ClearRoleDesignation(item.Designation);

                    var qryItem = roleController.ExecuteQuery(e => e.Designation.Equals(item.Designation)).FirstOrDefault();

                    if (qryItem != null)
                    {
                        role.CopyProperties(qryItem);
                        joinRole.RoleId = role.Id;
                    }
                    else
                    {
                        role.CopyProperties(item);
                        await roleController.InsertAsync(role).ConfigureAwait(false);

                        joinRole.Role = role;
                    }
                }
                else
                {
                    var qryItem = await roleController.GetByIdAsync(item.Id).ConfigureAwait(false);

                    if (qryItem != null)
                    {
                        role.CopyProperties(qryItem);
                    }
                    joinRole.RoleId = role.Id;
                }
                await identityXroleController.InsertAsync(joinRole).ConfigureAwait(false);

                result.RoleEntities.Add(role);
            }
            return(result);
        }
Ejemplo n.º 3
0
        public override async Task <IAppAccess> UpdateAsync(IAppAccess entity)
        {
            entity.CheckArgument(nameof(entity));
            entity.FirstItem.CheckArgument(nameof(entity.FirstItem));
            entity.SecondItems.CheckArgument(nameof(entity.SecondItems));

            var accessRoles = entity.SecondItems.Select(i =>
            {
                var entity = new Role();

                i.Designation = RoleController.ClearRoleDesignation(i.Designation);
                if (entity.Id == 0)
                {
                    var role = ManyEntityController.ExecuteQuery(e => e.Designation.Equals(i.Designation))
                               .FirstOrDefault();
                    if (role != null)
                    {
                        entity.CopyProperties(role);
                    }
                }
                return(entity);
            });

            //Delete all costs that are no longer included in the list.
            var identityXRoles = await IdentityXRoleController.ExecuteQueryAllAsync(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.FirstItem).ConfigureAwait(false);

            result.FirstItem.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.AddSecondItem(role);
            }
            return(result);
        }
        public override async Task <IAppAccess> UpdateAsync(IAppAccess entity)
        {
            entity.CheckArgument(nameof(entity));
            entity.Identity.CheckArgument(nameof(entity.Identity));
            entity.Roles.CheckArgument(nameof(entity.Roles));

            //Delete all costs that are no longer included in the list.
            var identXRoles = identityXroleController.ExecuteQuery(e => e.IdentityId == entity.Identity.Id).ToList();

            foreach (var item in identXRoles)
            {
                var tmpItem = entity.Roles.SingleOrDefault(i => i.Id == item.RoleId);

                if (tmpItem == null)
                {
                    await identityXroleController.DeleteAsync(item.Id).ConfigureAwait(false);
                }
            }

            var result   = new AppAccess();
            var identity = await identityController.UpdateAsync(entity.Identity).ConfigureAwait(false);

            foreach (var item in entity.Roles)
            {
                var role     = new Role();
                var joinRole = new IdentityXRole();

                role.Id             = item.Id;
                joinRole.IdentityId = identity.Id;
                if (item.Id == 0)
                {
                    item.Designation = RoleController.ClearRoleDesignation(role.Designation);
                    var qryItem = roleController.ExecuteQuery(e => e.Designation.Equals(item.Designation))
                                  .FirstOrDefault();

                    if (qryItem != null)
                    {
                        role.CopyProperties(qryItem);
                        joinRole.RoleId = role.Id;
                    }
                    else
                    {
                        role.CopyProperties(item);
                        await roleController.InsertAsync(role).ConfigureAwait(false);

                        joinRole.Role = role;
                    }
                }
                else
                {
                    var qryItem = await roleController.GetByIdAsync(role.Id).ConfigureAwait(false);

                    if (qryItem != null)
                    {
                        role.CopyProperties(qryItem);
                        joinRole.RoleId = role.Id;
                    }
                }
                var tmpItem = identXRoles.SingleOrDefault(e => e.IdentityId == joinRole.IdentityId && e.RoleId == joinRole.RoleId);

                if (tmpItem == null)
                {
                    await identityXroleController.InsertAsync(joinRole).ConfigureAwait(false);
                }
                result.RoleEntities.Add(role);
            }
            return(result);
        }