|
| 1 | +// Copyright (c) Source Tree Solutions, LLC. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | +// Author: Joe Audette |
| 4 | +// Created: 2017-07-13 |
| 5 | +// Last Modified: 2020-12-16 - jk |
| 6 | +// |
| 7 | + |
| 8 | +using cloudscribe.Core.Models; |
| 9 | +using cloudscribe.Core.Storage.EFCore.Common; |
| 10 | +using cloudscribe.Kvp.Models; |
| 11 | +using cloudscribe.Pagination.Models; |
| 12 | +using Microsoft.EntityFrameworkCore; |
| 13 | +using System; |
| 14 | +using System.Collections.Generic; |
| 15 | +using System.Linq; |
| 16 | +using System.Threading; |
| 17 | +using System.Threading.Tasks; |
| 18 | + |
| 19 | +namespace cloudscribe.Kvp.Storage.EFCore.Common |
| 20 | +{ |
| 21 | + public class KvpUserSearchQueries : IKvpUserSearchQueries |
| 22 | + { |
| 23 | + public KvpUserSearchQueries(IKvpDbContextFactory kvpContextFactory, |
| 24 | + ICoreDbContextFactory coreDbContextFactory) |
| 25 | + { |
| 26 | + _kvpContextFactory = kvpContextFactory; |
| 27 | + _coreDbContextFactory = coreDbContextFactory; |
| 28 | + } |
| 29 | + |
| 30 | + private readonly IKvpDbContextFactory _kvpContextFactory; |
| 31 | + private readonly ICoreDbContextFactory _coreDbContextFactory; |
| 32 | + |
| 33 | + private Dictionary<string, List<string>> kvpMatches = new Dictionary<string, List<string>>(); |
| 34 | + |
| 35 | + public async Task<PagedResult<IUserInfo>> GetUserAdminSearchPage( |
| 36 | + Guid siteId, |
| 37 | + int pageNumber, |
| 38 | + int pageSize, |
| 39 | + string searchInput, |
| 40 | + int sortMode, |
| 41 | + List<string> searchableKvpKeys, |
| 42 | + CancellationToken cancellationToken = default(CancellationToken)) |
| 43 | + { |
| 44 | + //sortMode: 0 = DisplayName asc, 1 = JoinDate desc, 2 = Last, First |
| 45 | + |
| 46 | + if (searchInput == null) searchInput = string.Empty; |
| 47 | + |
| 48 | + //allows user to enter multiple words (e.g. to allow full name search) |
| 49 | + var searchTerms = searchInput.Trim().ToUpper().Split(" "); |
| 50 | + |
| 51 | + int offset = (pageSize * pageNumber) - pageSize; |
| 52 | + |
| 53 | + string searchInputUpper = searchInput.Trim().ToUpper(); |
| 54 | + |
| 55 | + // get a list of user IDs from KVPs that match each search input term |
| 56 | + // provided the KVP is configured searchable |
| 57 | + using (var dbKvpContext = _kvpContextFactory.Create()) |
| 58 | + { |
| 59 | + foreach (var term in searchTerms) |
| 60 | + { |
| 61 | + if (!string.IsNullOrWhiteSpace(term)) |
| 62 | + { |
| 63 | + var usersMatchingTerm = dbKvpContext.KvpItems |
| 64 | + .Where (x => searchableKvpKeys.Contains(x.Key.ToUpper())) |
| 65 | + .Where (x => x.SetId.ToUpper().Equals(siteId.ToString().ToUpper())) |
| 66 | + .Where (x => x.Value.ToUpper().Contains(term)) |
| 67 | + .Select(x => x.SubSetId.ToUpper()).Distinct().ToList(); |
| 68 | + |
| 69 | + kvpMatches.Add(term, usersMatchingTerm); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + using (var dbContext = _coreDbContextFactory.CreateContext()) |
| 75 | + { |
| 76 | + var query = dbContext.Users.Where(x => x.SiteId == siteId); |
| 77 | + foreach (var term in searchTerms) |
| 78 | + { |
| 79 | + if (!string.IsNullOrWhiteSpace(term)) |
| 80 | + { |
| 81 | + // Note each term is already in upper case |
| 82 | + query = query.Where(x => |
| 83 | + x.NormalizedEmail.Contains(term) |
| 84 | + || x.NormalizedUserName.Contains(term) |
| 85 | + || (x.FirstName != null && x.FirstName.ToUpper().Contains(term)) |
| 86 | + || (x.LastName != null && x.LastName.ToUpper().Contains(term)) |
| 87 | + || x.DisplayName.ToUpper().Contains(term) |
| 88 | + || kvpMatches[term].Contains(x.Id.ToString().ToUpper()) |
| 89 | + ); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + query = query.Distinct(); |
| 94 | + |
| 95 | + switch (sortMode) |
| 96 | + { |
| 97 | + case 2: |
| 98 | + query = query.OrderBy(sl => sl.LastName).ThenBy(s2 => s2.FirstName).AsQueryable(); |
| 99 | + break; |
| 100 | + case 1: |
| 101 | + query = query.OrderByDescending(sl => sl.CreatedUtc).AsQueryable(); |
| 102 | + break; |
| 103 | + case 0: |
| 104 | + default: |
| 105 | + query = query.OrderBy(sl => sl.DisplayName).AsQueryable(); |
| 106 | + break; |
| 107 | + } |
| 108 | + |
| 109 | + var data = await query |
| 110 | + .AsNoTracking() |
| 111 | + .Skip(offset) |
| 112 | + .Take(pageSize) |
| 113 | + .ToListAsync<IUserInfo>(cancellationToken) |
| 114 | + .ConfigureAwait(false); |
| 115 | + |
| 116 | + return new PagedResult<IUserInfo> |
| 117 | + { |
| 118 | + Data = data, |
| 119 | + PageNumber = pageNumber, |
| 120 | + PageSize = pageSize, |
| 121 | + TotalItems = await CountUsersForAdminSearch(siteId, searchTerms, cancellationToken).ConfigureAwait(false) |
| 122 | + }; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + public async Task<int> CountUsersForAdminSearch( |
| 128 | + Guid siteId, |
| 129 | + string[] searchTerms, |
| 130 | + CancellationToken cancellationToken = default(CancellationToken)) |
| 131 | + { |
| 132 | + using (var dbContext = _coreDbContextFactory.CreateContext()) |
| 133 | + { |
| 134 | + var query = dbContext.Users.Where(x => x.SiteId == siteId); |
| 135 | + |
| 136 | + foreach (var term in searchTerms) |
| 137 | + { |
| 138 | + if (!string.IsNullOrWhiteSpace(term)) |
| 139 | + { |
| 140 | + query = query.Where(x => |
| 141 | + x.NormalizedEmail.Contains(term) |
| 142 | + || x.NormalizedUserName.Contains(term) |
| 143 | + || (x.FirstName != null && x.FirstName.ToUpper().Contains(term)) |
| 144 | + || (x.LastName != null && x.LastName .ToUpper().Contains(term)) |
| 145 | + || x.DisplayName.ToUpper().Contains(term) |
| 146 | + || kvpMatches[term].Contains(x.Id.ToString().ToUpper()) |
| 147 | + ); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + query = query.Distinct(); |
| 152 | + |
| 153 | + return await query.CountAsync().ConfigureAwait(false); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments