diff options
Diffstat (limited to 'Services')
| -rw-r--r-- | Services/SearchService.cs | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/Services/SearchService.cs b/Services/SearchService.cs index 8a109ea..e8e497d 100644 --- a/Services/SearchService.cs +++ b/Services/SearchService.cs @@ -7,10 +7,17 @@ public interface ISearchService { } public class SearchService : ISearchService { + private ITagService tagService; + private IDbContextFactory<HBContext> dbFactory; - public SearchService(IDbContextFactory<HBContext> dbFactory) => - this.dbFactory = dbFactory; + public SearchService( + IDbContextFactory<HBContext> dbFactory, + ITagService tagService) { + + this.tagService = tagService; + this.dbFactory = dbFactory; + } public Media[] Search(string query) { var db = dbFactory.CreateDbContext(); @@ -18,29 +25,32 @@ public class SearchService : ISearchService { query = query.ToLower(); var matchedTag = db.TagDefinitions - .FirstOrDefault(td => td.Name.ToLower() == query)?.Guid; + .FirstOrDefault(td => td.Name.ToLower() == query); - Guid[] guids; + int[] tags; if(matchedTag is not null) { - guids = new[] { (Guid) matchedTag }; + tags = tagService + .TagsThatImply(matchedTag) + .Select(td => td.ObjectId) + .ToArray(); } else { - guids = db.TagDefinitions + // TODO: expand scope to all tags that imply + tags = db.TagDefinitions .Where(td => td.Name.ToLower().Contains(query)) - .Select(td => td.Guid) + .Select(td => td.ObjectId) .ToArray(); } return db.Media .Include(m => m.Tags) - .ThenInclude(m => m.TagDefinition) .AsEnumerable() - .Where(m => m.Tags.IntersectBy(guids, t => t.TagDefinition.Guid).Any()) + .Where(m => m.Tags.IntersectBy(tags, t => t.TagDefinitionId).Any()) .Concat(db.Media .Where(m => (m.ShortDescription != null && m.ShortDescription.ToLower().Contains(query)) || (m.LongDescription != null && m.LongDescription.ToLower().Contains(query)))) - .DistinctBy(m => m.Guid) + .DistinctBy(m => m.ObjectId) .ToArray(); } } |
