refactoring the code.
This commit is contained in:
@ -29,8 +29,9 @@ namespace Oqtane.Repository
|
||||
public virtual DbSet<Language> Language { get; set; }
|
||||
public virtual DbSet<Visitor> Visitor { get; set; }
|
||||
public virtual DbSet<UrlMapping> UrlMapping { get; set; }
|
||||
public virtual DbSet<SearchDocument> SearchDocument { get; set; }
|
||||
public virtual DbSet<SearchDocumentProperty> SearchDocumentProperty { get; set; }
|
||||
public virtual DbSet<SearchDocumentTag> SearchDocumentTag { get; set; }
|
||||
public virtual DbSet<SearchContent> SearchContent { get; set; }
|
||||
public virtual DbSet<SearchContentProperty> SearchContentProperty { get; set; }
|
||||
public virtual DbSet<SearchContentWords> SearchContentWords { get; set; }
|
||||
public virtual DbSet<SearchContentWordSource> SearchContentWordSource { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Oqtane.Models;
|
||||
|
||||
namespace Oqtane.Repository
|
||||
{
|
||||
public interface ISearchContentRepository
|
||||
{
|
||||
Task<IEnumerable<SearchContent>> GetSearchContentListAsync(SearchQuery searchQuery);
|
||||
SearchContent AddSearchContent(SearchContent searchContent);
|
||||
void DeleteSearchContent(int searchContentId);
|
||||
void DeleteSearchContent(string entityName, int entryId);
|
||||
void DeleteSearchContent(string uniqueKey);
|
||||
void DeleteAllSearchContent();
|
||||
|
||||
SearchContentWordSource GetSearchContentWordSource(string word);
|
||||
SearchContentWordSource AddSearchContentWordSource(SearchContentWordSource wordSource);
|
||||
|
||||
IEnumerable<SearchContentWords> GetWords(int searchContentId);
|
||||
SearchContentWords AddSearchContentWords(SearchContentWords word);
|
||||
SearchContentWords UpdateSearchContentWords(SearchContentWords word);
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Oqtane.Models;
|
||||
|
||||
namespace Oqtane.Repository
|
||||
{
|
||||
public interface ISearchDocumentRepository
|
||||
{
|
||||
Task<IEnumerable<SearchDocument>> GetSearchDocumentsAsync(SearchQuery searchQuery);
|
||||
SearchDocument AddSearchDocument(SearchDocument searchDocument);
|
||||
void DeleteSearchDocument(int searchDocumentId);
|
||||
void DeleteSearchDocument(string indexerName, int entryId);
|
||||
void DeleteSearchDocument(string uniqueKey);
|
||||
void DeleteAllSearchDocuments();
|
||||
}
|
||||
}
|
167
Oqtane.Server/Repository/SearchContentRepository.cs
Normal file
167
Oqtane.Server/Repository/SearchContentRepository.cs
Normal file
@ -0,0 +1,167 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Oqtane.Models;
|
||||
using Oqtane.Shared;
|
||||
|
||||
namespace Oqtane.Repository
|
||||
{
|
||||
public class SearchContentRepository : ISearchContentRepository
|
||||
{
|
||||
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;
|
||||
|
||||
public SearchContentRepository(IDbContextFactory<TenantDBContext> dbContextFactory)
|
||||
{
|
||||
_dbContextFactory = dbContextFactory;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<SearchContent>> GetSearchContentListAsync(SearchQuery searchQuery)
|
||||
{
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
var searchContentList = db.SearchContent.AsNoTracking()
|
||||
.Include(i => i.Properties)
|
||||
.Where(i => i.SiteId == searchQuery.SiteId && i.IsActive);
|
||||
|
||||
if (searchQuery.EntityNames != null && searchQuery.EntityNames.Any())
|
||||
{
|
||||
searchContentList = searchContentList.Where(i => searchQuery.EntityNames.Contains(i.EntityName));
|
||||
}
|
||||
|
||||
if (searchQuery.BeginModifiedTimeUtc != DateTime.MinValue)
|
||||
{
|
||||
searchContentList = searchContentList.Where(i => i.ModifiedTime >= searchQuery.BeginModifiedTimeUtc);
|
||||
}
|
||||
|
||||
if (searchQuery.EndModifiedTimeUtc != DateTime.MinValue)
|
||||
{
|
||||
searchContentList = searchContentList.Where(i => i.ModifiedTime <= searchQuery.EndModifiedTimeUtc);
|
||||
}
|
||||
|
||||
if (searchQuery.Properties != null && searchQuery.Properties.Any())
|
||||
{
|
||||
foreach (var property in searchQuery.Properties)
|
||||
{
|
||||
searchContentList = searchContentList.Where(i => i.Properties.Any(p => p.Name == property.Key && p.Value == property.Value));
|
||||
}
|
||||
}
|
||||
|
||||
var filteredContentList = new List<SearchContent>();
|
||||
if (!string.IsNullOrEmpty(searchQuery.Keywords))
|
||||
{
|
||||
foreach (var keyword in SearchUtils.GetKeywordsList(searchQuery.Keywords))
|
||||
{
|
||||
filteredContentList.AddRange(await searchContentList.Where(i => i.Words.Any(w => w.WordSource.Word.StartsWith(keyword))).ToListAsync());
|
||||
}
|
||||
}
|
||||
|
||||
return filteredContentList.DistinctBy(i => i.UniqueKey);
|
||||
}
|
||||
|
||||
public SearchContent AddSearchContent(SearchContent searchContent)
|
||||
{
|
||||
using var context = _dbContextFactory.CreateDbContext();
|
||||
context.SearchContent.Add(searchContent);
|
||||
|
||||
if(searchContent.Properties != null && searchContent.Properties.Any())
|
||||
{
|
||||
foreach(var property in searchContent.Properties)
|
||||
{
|
||||
property.SearchContentId = searchContent.SearchContentId;
|
||||
context.SearchContentProperty.Add(property);
|
||||
}
|
||||
}
|
||||
|
||||
context.SaveChanges();
|
||||
|
||||
return searchContent;
|
||||
}
|
||||
|
||||
public void DeleteSearchContent(int searchContentId)
|
||||
{
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
var searchContent = db.SearchContent.Find(searchContentId);
|
||||
db.SearchContent.Remove(searchContent);
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
public void DeleteSearchContent(string entityName, int entryId)
|
||||
{
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
var searchContent = db.SearchContent.FirstOrDefault(i => i.EntityName == entityName && i.EntityId == entryId);
|
||||
if(searchContent != null)
|
||||
{
|
||||
db.SearchContent.Remove(searchContent);
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteSearchContent(string uniqueKey)
|
||||
{
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
var searchContent = db.SearchContent.FirstOrDefault(i => (i.EntityName + ":" + i.EntityId) == uniqueKey);
|
||||
if (searchContent != null)
|
||||
{
|
||||
db.SearchContent.Remove(searchContent);
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteAllSearchContent()
|
||||
{
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
db.SearchContent.RemoveRange(db.SearchContent);
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
public SearchContentWordSource GetSearchContentWordSource(string word)
|
||||
{
|
||||
if(string.IsNullOrEmpty(word))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
return db.SearchContentWordSource.FirstOrDefault(i => i.Word == word);
|
||||
}
|
||||
|
||||
public SearchContentWordSource AddSearchContentWordSource(SearchContentWordSource wordSource)
|
||||
{
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
|
||||
db.SearchContentWordSource.Add(wordSource);
|
||||
db.SaveChanges();
|
||||
|
||||
return wordSource;
|
||||
}
|
||||
|
||||
public IEnumerable<SearchContentWords> GetWords(int searchContentId)
|
||||
{
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
return db.SearchContentWords
|
||||
.Include(i => i.WordSource)
|
||||
.Where(i => i.SearchContentId == searchContentId).ToList();
|
||||
}
|
||||
|
||||
public SearchContentWords AddSearchContentWords(SearchContentWords word)
|
||||
{
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
|
||||
db.SearchContentWords.Add(word);
|
||||
db.SaveChanges();
|
||||
|
||||
return word;
|
||||
}
|
||||
|
||||
public SearchContentWords UpdateSearchContentWords(SearchContentWords word)
|
||||
{
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
|
||||
db.Entry(word).State = EntityState.Modified;
|
||||
db.SaveChanges();
|
||||
|
||||
return word;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,136 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Oqtane.Models;
|
||||
using Oqtane.Shared;
|
||||
|
||||
namespace Oqtane.Repository
|
||||
{
|
||||
public class SearchDocumentRepository : ISearchDocumentRepository
|
||||
{
|
||||
private readonly IDbContextFactory<TenantDBContext> _dbContextFactory;
|
||||
|
||||
public SearchDocumentRepository(IDbContextFactory<TenantDBContext> dbContextFactory)
|
||||
{
|
||||
_dbContextFactory = dbContextFactory;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<SearchDocument>> GetSearchDocumentsAsync(SearchQuery searchQuery)
|
||||
{
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
var documents = db.SearchDocument.AsNoTracking()
|
||||
.Include(i => i.Properties)
|
||||
.Include(i => i.Tags)
|
||||
.Where(i => i.SiteId == searchQuery.SiteId);
|
||||
|
||||
if (searchQuery.Sources != null && searchQuery.Sources.Any())
|
||||
{
|
||||
documents = documents.Where(i => searchQuery.Sources.Contains(i.IndexerName));
|
||||
}
|
||||
|
||||
if (searchQuery.BeginModifiedTimeUtc != DateTime.MinValue)
|
||||
{
|
||||
documents = documents.Where(i => i.ModifiedTime >= searchQuery.BeginModifiedTimeUtc);
|
||||
}
|
||||
|
||||
if (searchQuery.EndModifiedTimeUtc != DateTime.MinValue)
|
||||
{
|
||||
documents = documents.Where(i => i.ModifiedTime <= searchQuery.EndModifiedTimeUtc);
|
||||
}
|
||||
|
||||
if (searchQuery.Tags != null && searchQuery.Tags.Any())
|
||||
{
|
||||
foreach (var tag in searchQuery.Tags)
|
||||
{
|
||||
documents = documents.Where(i => i.Tags.Any(t => t.Tag == tag));
|
||||
}
|
||||
}
|
||||
|
||||
if (searchQuery.Properties != null && searchQuery.Properties.Any())
|
||||
{
|
||||
foreach (var property in searchQuery.Properties)
|
||||
{
|
||||
documents = documents.Where(i => i.Properties.Any(p => p.Name == property.Key && p.Value == property.Value));
|
||||
}
|
||||
}
|
||||
|
||||
var filteredDocuments = new List<SearchDocument>();
|
||||
if (!string.IsNullOrEmpty(searchQuery.Keywords))
|
||||
{
|
||||
foreach (var keyword in SearchUtils.GetKeywordsList(searchQuery.Keywords))
|
||||
{
|
||||
filteredDocuments.AddRange(await documents.Where(i => i.Title.Contains(keyword) || i.Description.Contains(keyword) || i.Body.Contains(keyword)).ToListAsync());
|
||||
}
|
||||
}
|
||||
|
||||
return filteredDocuments.DistinctBy(i => i.UniqueKey);
|
||||
}
|
||||
|
||||
public SearchDocument AddSearchDocument(SearchDocument searchDocument)
|
||||
{
|
||||
using var context = _dbContextFactory.CreateDbContext();
|
||||
context.SearchDocument.Add(searchDocument);
|
||||
|
||||
if(searchDocument.Properties != null && searchDocument.Properties.Any())
|
||||
{
|
||||
foreach(var property in searchDocument.Properties)
|
||||
{
|
||||
property.SearchDocumentId = searchDocument.SearchDocumentId;
|
||||
context.SearchDocumentProperty.Add(property);
|
||||
}
|
||||
}
|
||||
|
||||
if (searchDocument.Tags != null && searchDocument.Tags.Any())
|
||||
{
|
||||
foreach (var tag in searchDocument.Tags)
|
||||
{
|
||||
tag.SearchDocumentId = searchDocument.SearchDocumentId;
|
||||
context.SearchDocumentTag.Add(tag);
|
||||
}
|
||||
}
|
||||
|
||||
context.SaveChanges();
|
||||
|
||||
return searchDocument;
|
||||
}
|
||||
|
||||
public void DeleteSearchDocument(int searchDocumentId)
|
||||
{
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
var searchDocument = db.SearchDocument.Find(searchDocumentId);
|
||||
db.SearchDocument.Remove(searchDocument);
|
||||
db.SaveChanges();
|
||||
}
|
||||
|
||||
public void DeleteSearchDocument(string indexerName, int entryId)
|
||||
{
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
var searchDocument = db.SearchDocument.FirstOrDefault(i => i.IndexerName == indexerName && i.EntryId == entryId);
|
||||
if(searchDocument != null)
|
||||
{
|
||||
db.SearchDocument.Remove(searchDocument);
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteSearchDocument(string uniqueKey)
|
||||
{
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
var searchDocument = db.SearchDocument.FirstOrDefault(i => (i.IndexerName + ":" + i.EntryId) == uniqueKey);
|
||||
if (searchDocument != null)
|
||||
{
|
||||
db.SearchDocument.Remove(searchDocument);
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteAllSearchDocuments()
|
||||
{
|
||||
using var db = _dbContextFactory.CreateDbContext();
|
||||
db.SearchDocument.RemoveRange(db.SearchDocument);
|
||||
db.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user