/// <summary> /// Populates the EntityViewModel object's properties from the given CommunityDetails object's properties. /// </summary> /// <param name="thisObject">Current entity view model on which the extension method is called</param> /// <param name="communityDetails">CommunityDetails model from which values to be read</param> /// <returns>Values populated EntityViewModel instance</returns> public static EntityViewModel SetValuesFrom(this EntityViewModel thisObject, CommunityDetails communityDetails) { if (communityDetails != null) { if (thisObject == null) { thisObject = new EntityViewModel(); } thisObject.Entity = communityDetails.CommunityType == CommunityTypes.Community ? EntityType.Community : EntityType.Folder; // Populate the base values using the EntityViewModel's SetValuesFrom method which take EntityDetails as input. thisObject.SetValuesFrom(communityDetails as EntityDetails); } return thisObject; }
/// <summary> /// Populates the EntityViewModel object's properties from the given EntityDetail object's properties. /// </summary> /// <param name="thisObject">Current entity view model on which the extension method is called</param> /// <param name="entityDetails">EntityDetails model from which values to be read</param> /// <returns>Values populated EntityViewModel instance</returns> public static EntityViewModel SetValuesFrom(this EntityViewModel thisObject, EntityDetails entityDetails) { if (entityDetails != null) { if (thisObject == null) { thisObject = new EntityViewModel(); } thisObject.Id = entityDetails.ID; thisObject.Name = entityDetails.Name; // Parse the category string thisObject.Category = entityDetails.CategoryID.ToEnum<int, CategoryType>(CategoryType.All); thisObject.AccessType = (AccessType)entityDetails.AccessTypeID; thisObject.ParentId = entityDetails.ParentID; thisObject.ParentName = entityDetails.ParentName; thisObject.ParentType = entityDetails.ParentType; thisObject.UserPermission = entityDetails.UserPermission; if (entityDetails.Tags != null) { thisObject.Tags = entityDetails.Tags; } thisObject.Rating = (double)entityDetails.AverageRating; thisObject.RatedPeople = entityDetails.RatedPeople; if (entityDetails.Thumbnail != null) { thisObject.ThumbnailID = entityDetails.Thumbnail.AzureID; } thisObject.Producer = entityDetails.ProducedBy; thisObject.ProducerId = entityDetails.CreatedByID; } return thisObject; }
/// <summary> /// Searches the presence of the given text in communities and contents tables (Name, description and tags fields). /// </summary> /// <param name="searchText">Text to be searched</param> /// <param name="userId">Id of the user who is accessing</param> /// <param name="pageDetails">Details about the pagination</param> /// <returns>Communities/Contents which are having the search text</returns> public async Task<IEnumerable<EntityViewModel>> SimpleSearch(string searchText, long userId, PageDetails pageDetails, SearchQueryDetails searchQueryDetails) { // Make sure pageDetails is not null this.CheckNotNull(() => new { pageDetails }); IList<EntityViewModel> searchResults = new List<EntityViewModel>(); // User Id to be used while searching. This will be used to see whether user is having permission or not. long? searchUserId = userId; if (_userRepository.GetUserRole(userId, null) == UserRole.SiteAdmin) { // Set user id as 0 for Site Administrators, so that role check will be ignored for private communities searchUserId = null; } // Gets the total communities/contents satisfying the search condition pageDetails.TotalCount = _searchViewRepository.SearchCount(searchText, searchUserId, searchQueryDetails); // Set the total pages for the search term pageDetails.TotalPages = (pageDetails.TotalCount / pageDetails.ItemsPerPage) + ((pageDetails.TotalCount % pageDetails.ItemsPerPage == 0) ? 0 : 1); // Get the skip count and take count for the given page var skipCount = (pageDetails.CurrentPage - 1) * pageDetails.ItemsPerPage; var takeCount = pageDetails.ItemsPerPage; var results = await _searchViewRepository.SearchAsync(searchText, searchUserId, skipCount, takeCount, searchQueryDetails); foreach (var entity in results) { EntityViewModel entityViewModel; if (entity.Entity == EntityType.Content.ToString()) { // In case of Content, need to create ContentViewModel instance which is expected by SearchResultView. entityViewModel = new ContentViewModel(); // This is needed to avoid the FxCop warning. var contentViewModel = entityViewModel as ContentViewModel; // Setting the properties which are specific to Contents. contentViewModel.IsLink = entity.ContentType == (int)ContentTypes.Link; contentViewModel.ContentUrl = entity.ContentType == (int)ContentTypes.Link ? entity.ContentUrl : string.Empty; contentViewModel.ContentAzureID = entity.ContentType == (int)ContentTypes.Link ? Guid.Empty : entity.ContentAzureID; } else { entityViewModel = new EntityViewModel(); } Mapper.Map(entity, entityViewModel); searchResults.Add(entityViewModel); } // TODO: Need to send the results based on relevance with following order: Title, Description, Tags and Parent. return searchResults; }