Ignore:
Timestamp:
10/27/21 19:32:04 (3 years ago)
Author:
Стојков Марко <mst@…>
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.
Message:

Merged feature/search-questions into dev

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/FinkiChattery/FinkiChattery.Persistence/Repositories/Implementations/QuestionRepo.cs

    r466d1ac r7899209  
    22using FinkiChattery.Persistence.Models;
    33using FinkiChattery.Persistence.Repositories.Contracts;
     4using Microsoft.Data.SqlClient;
    45using Microsoft.EntityFrameworkCore;
    56using System;
     
    78using System.Linq;
    89using System.Linq.Expressions;
     10using System.Text.RegularExpressions;
    911using System.Threading.Tasks;
    1012
     
    110112            return questionDto;
    111113        }
     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        }
    112146    }
    113147}
Note: See TracChangeset for help on using the changeset viewer.