Changeset 7899209 for src/FinkiChattery/FinkiChattery.Persistence/Repositories/Implementations/QuestionRepo.cs
- Timestamp:
- 10/27/21 19:32:04 (3 years ago)
- Branches:
- dev
- Children:
- 6b0fbbe, 79ae621
- Parents:
- 466d1ac (diff), a3b5f34 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/FinkiChattery/FinkiChattery.Persistence/Repositories/Implementations/QuestionRepo.cs
r466d1ac r7899209 2 2 using FinkiChattery.Persistence.Models; 3 3 using FinkiChattery.Persistence.Repositories.Contracts; 4 using Microsoft.Data.SqlClient; 4 5 using Microsoft.EntityFrameworkCore; 5 6 using System; … … 7 8 using System.Linq; 8 9 using System.Linq.Expressions; 10 using System.Text.RegularExpressions; 9 11 using System.Threading.Tasks; 10 12 … … 110 112 return questionDto; 111 113 } 114 115 public async Task<List<QuestionPreviewDto>> SearchQuestions(string searchText, IEnumerable<Guid> categories) 116 { 117 var search = Regex.Replace(searchText, "[\\\\/:*?\"<>\\]\\[|&'`~^=%,(){}_\\-]", " ") 118 .Split(" ".ToArray(), StringSplitOptions.RemoveEmptyEntries) 119 .Select(c => $"\"{c}*\""); 120 121 var searchString = string.Join(" AND ", search); 122 123 var rawQuery = (IQueryable<Question>) 124 DbSet.FromSqlRaw(@" 125 SELECT [q].[Id], [q].[Uid], [q].[Title], [q].[Views], [q].[AnswersCount], [q].[CreatedOn] 126 FROM [dbo].[Question] AS [q] 127 INNER JOIN CONTAINSTABLE(Question, Search, @searchString, 30) ccontains ON [q].[Id] = ccontains.[KEY]", 128 new SqlParameter("searchString", searchString)) 129 .Include(x => x.QuestionCategories).ThenInclude(x => x.Category); 130 131 if (categories.Any()) 132 { 133 rawQuery = rawQuery.Where(x => x.QuestionCategories.Any(y => categories.Contains(y.Category.Uid))); 134 } 135 136 return await rawQuery 137 .Select(x => new QuestionPreviewDto(x.Id, 138 x.Uid, 139 x.Title, 140 x.Views, 141 x.AnswersCount, 142 x.CreatedOn, 143 x.QuestionCategories.Select(y => new QuestionPreviewCategoryDto(y.Id, y.Uid, y.Category.Name)))) 144 .ToListAsync(); 145 } 112 146 } 113 147 }
Note:
See TracChangeset
for help on using the changeset viewer.