Пример #1
0
        public async Task <IActionResult> SearchAsync(int page, UserLearningFilterModel filterModel)
        {
            filterModel = filterModel ?? throw new ArgumentNullException(nameof(filterModel));
            this.logger.LogInformation("User Learning modules search- HTTP Post Call initiated.");
            this.RecordEvent("User Learning module search- HTTP Post call initiated.", RequestType.Initiated);

            if (page < 0)
            {
                this.logger.LogError("User Learning module search- HTTP Post call Failed, parameter pageCount is less than zero.");
                this.RecordEvent("User Learning module search- HTTP Post call Failed.", RequestType.Failed);
                return(this.BadRequest($"Parameter {nameof(page)} cannot be less than zero."));
            }

            var skipRecords = page * Constants.LazyLoadPerPagePostCount;

            try
            {
                IEnumerable <LearningModule> learningModules = new List <LearningModule>();

                if (filterModel.IsSaved)
                {
                    learningModules = await this.unitOfWork.UserLearningModuleRepository.GetUserSavedModulesAsync(filterModel, skipRecords, Constants.LazyLoadPerPagePostCount);
                }
                else
                {
                    learningModules = await this.unitOfWork.LearningModuleRepository.GetUserModulesAsync(filterModel, Constants.LazyLoadPerPagePostCount, skipRecords);
                }

                // Post userId and user display name.
                var userAADObjectIds = learningModules.Select(resource => resource.CreatedBy).Distinct().Select(userObjectId => userObjectId.ToString());
                Dictionary <Guid, string> idToNameMap = new Dictionary <Guid, string>();
                if (userAADObjectIds.Any())
                {
                    idToNameMap = await this.usersService.GetUserDisplayNamesAsync(this.UserObjectId.ToString(), this.Request.Headers["Authorization"].ToString(), userAADObjectIds);
                }

                var moduleWithVotesAndResources = this.unitOfWork.LearningModuleRepository.GetModulesWithVotesAndResources(learningModules);

                var learningModuleDetails = this.learningModuleMapper.MapToViewModels(
                    moduleWithVotesAndResources,
                    this.UserObjectId,
                    idToNameMap);

                this.logger.LogInformation("User Learning module search- HTTP Post Call succeeded.");
                this.RecordEvent("User Learning module search- HTTP Post call succeeded.", RequestType.Succeeded);

                return(this.Ok(learningModuleDetails));
            }
            catch (Exception ex)
            {
                this.RecordEvent("User Learning module search- HTTP Post call failed.", RequestType.Failed);
                this.logger.LogError(ex, "User Learning module search- HTTP Post call failed.");
                throw;
            }
        }
Пример #2
0
        /// <summary>
        /// Get module based on page count, records to skip and provided filters.
        /// </summary>
        /// <param name="filterModel">Filter model to get search data.</param>
        /// <param name="skip">Number of modules to be skipped to fetch next set of module.</param>
        /// <param name="count">Number of modules to be fetched from database.</param>
        /// <returns>Returns collection of filtered entities.</returns>
        public async Task <IEnumerable <LearningModule> > GetUserSavedModulesAsync(UserLearningFilterModel filterModel, int skip, int count)
        {
            filterModel = filterModel ?? throw new ArgumentNullException(nameof(filterModel));

            var userLearningModuleEntities = this.context.Set <UserLearningModule>()
                                             .Include(x => x.LearningModule).ThenInclude(x => x.Subject)
                                             .Include(x => x.LearningModule).ThenInclude(x => x.Grade)
                                             .Include(x => x.LearningModule).ThenInclude(x => x.LearningModuleTag).ThenInclude(p => p.Tag);

            var query = userLearningModuleEntities.Where(x => x.UserId == filterModel.UserObjectId);

            if (!string.IsNullOrEmpty(filterModel.SearchText))
            {
#pragma warning disable CA1307 // Ignoring StringComparison as EF handles the string comparison while building SQL query from LINQ expression. In case of explicit StringComparison addition, then it fails the SQL query execution with error.// Ignoring StringComparison as EF handles the string comparison while building SQL query from LINQ expression. In case of explicit StringComparison addition, then it fails the SQL query execution with error.
                query = query.Where(x => x.LearningModule.Title.Contains(filterModel.SearchText));
#pragma warning restore CA1307 // Specify StringComparison
            }

            var learningModules = await query.OrderByDescending(x => x.LearningModule.UpdatedOn).Skip(skip).Take(count).AsNoTracking().ToListAsync().ConfigureAwait(false);

            return(learningModules.Select(x => x.LearningModule));
        }