Example #1
0
        public Result <DataTableResult <UserTableModel> > GetGroupUsers(string roleId, DataTableRequest request)
        {
            ValidationResult validationResult = _dataTableValidator.Validate(request);

            if (!validationResult.IsValid)
            {
                _logger.LogWarning($"Invalid {nameof(DataTableRequest)} model");
                return(Result.Fail <DataTableResult <UserTableModel> >(ResultUtils.ToResultError(validationResult.Errors.ToList())));
            }

            BaseSpecification <RoleEntity> roleSpecification = new BaseSpecification <RoleEntity>();

            roleSpecification.AddFilter(x => x.Id == roleId);
            roleSpecification.AddFilter(x => x.Type == RoleTypes.Group);

            bool existResult = _roleRepository.Exist(roleSpecification);

            if (!existResult)
            {
                _logger.LogWarning($"GroupRole with id {roleId} does not exist");
                return(Result.Fail <DataTableResult <UserTableModel> >("no_role", "No Role"));
            }

            PaginationSpecification <GroupUserEntity, UserTableModel> baseSpecification = new PaginationSpecification <GroupUserEntity, UserTableModel>();

            baseSpecification.AddFilter(x => x.RoleId == roleId);
            baseSpecification.AddSelect(x => new UserTableModel(
                                            x.User.Id,
                                            x.User.UserName,
                                            x.Group.Name));

            if (!string.IsNullOrEmpty(request.Search))
            {
                string search = request.Search.ToUpper();

                baseSpecification.AddFilter(x =>
                                            x.User.Id.ToUpper().Contains(search) ||
                                            x.User.Email.ToUpper().Contains(search) ||
                                            x.User.UserName.ToUpper().Contains(search) ||
                                            x.User.FirstName.ToUpper().Contains(search) ||
                                            x.User.LastName.ToUpper().Contains(search));
            }
            baseSpecification.AppalyPaging(request.Start, request.Length);
            baseSpecification.AddInclude(x => x.User);

            PaginatedData <UserTableModel> paginationData = _groupUserRepository.GetPaginated(baseSpecification);

            DataTableResult <UserTableModel> result = new DataTableResult <UserTableModel>(
                draw: request.Draw,
                recordsTotal: paginationData.Count,
                recordsFilterd: paginationData.Count,
                error: null,
                data: paginationData.Data);

            return(Result.Ok(result));
        }