public async Task <ActionResult> PostAssignFunctionalitiesToRole([FromBody] FunctionalityRoleAssignmentRequest functionalityRoleAssignmentRequest)
        {
            var dbTransaction = await _dataContext.Database.BeginTransactionAsync();

            var result = await _roleManagementRepository.AssignFunctionalitiesToRole(functionalityRoleAssignmentRequest);

            if (result.StatusCode == Utils.Success)
            {
                result.ObjectValue = _mapper.Map <List <FunctionalityRoleResponse> >(((List <FunctionalityRole>)result.ObjectValue));
                await dbTransaction.CommitAsync();

                return(StatusCode(StatusCodes.Status200OK, result));
            }
            else
            {
                await dbTransaction.RollbackAsync();

                return(StatusCode(StatusCodes.Status400BadRequest, result));
            }
        }
예제 #2
0
        public async Task <ReturnResponse> AssignFunctionalitiesToRole(FunctionalityRoleAssignmentRequest functionalityRoleAssignmentRequest)
        {
            if (functionalityRoleAssignmentRequest == null)
            {
                return(new ReturnResponse()
                {
                    StatusCode = Utils.ObjectNull,
                    StatusMessage = Utils.StatusMessageObjectNull
                });
            }

            var functionalityRoles = new List <FunctionalityRole>();
            var role = await _roleManager.FindByIdAsync(Convert.ToString(functionalityRoleAssignmentRequest.RoleId));

            if (role == null)
            {
                return(new ReturnResponse()
                {
                    StatusCode = Utils.NotFound,
                    StatusMessage = Utils.StatusMessageNotFound
                });
            }

            //DELETE THE OLD FUNCTIONALITIES TIED TO THE ROLE
            var functionalityRolesToDelete = await _dataContext.FunctionalityRole.Where(a => a.RoleId == functionalityRoleAssignmentRequest.RoleId).ToListAsync();

            var deletionResult = _globalRepository.Delete(functionalityRolesToDelete);

            if (!deletionResult)
            {
                return(new ReturnResponse()
                {
                    StatusCode = Utils.NotSucceeded,
                    StatusMessage = Utils.StatusMessageNotSucceeded
                });
            }

            foreach (var r in functionalityRoleAssignmentRequest.FunctionalityIds)
            {
                var functionality = await _dataContext.Functionality.Where(a => a.FunctionalityId == r).FirstOrDefaultAsync();

                if (functionality == null)
                {
                    return(new ReturnResponse()
                    {
                        StatusCode = Utils.NotFound,
                        StatusMessage = Utils.StatusMessageNotFound
                    });
                }

                functionalityRoles.Add(new FunctionalityRole()
                {
                    RoleId            = role.Id,
                    RoleName          = role.Name,
                    FunctionalityId   = functionality.FunctionalityId,
                    FunctionalityName = functionality.FunctionalityName
                });
            }

            var result = await _globalRepository.Add(functionalityRoles);

            if (result)
            {
                var saved = await _globalRepository.SaveAll();

                if (saved.HasValue)
                {
                    if (!saved.Value)
                    {
                        return(new ReturnResponse()
                        {
                            StatusCode = Utils.SaveNoRowAffected,
                            StatusMessage = Utils.StatusMessageSaveNoRowAffected
                        });
                    }

                    return(new ReturnResponse()
                    {
                        StatusCode = Utils.Success,
                        StatusMessage = Utils.StatusMessageSuccess,
                        ObjectValue = functionalityRoles
                    });
                }

                return(new ReturnResponse()
                {
                    StatusCode = Utils.SaveError,
                    StatusMessage = Utils.StatusMessageSaveError
                });
            }

            return(new ReturnResponse()
            {
                StatusCode = Utils.NotSucceeded
            });
        }