207 lines
7.6 KiB
Plaintext
207 lines
7.6 KiB
Plaintext
@page "/search"
|
|
@page "/search/{SearchTerms}"
|
|
|
|
@using Microsoft.EntityFrameworkCore
|
|
@using Microsoft.Extensions.Logging
|
|
@using MudBlazor
|
|
@using NpgsqlTypes
|
|
@using OpenArchival.DataAccess
|
|
@using Persic
|
|
@using Npgsql.EntityFrameworkCore.PostgreSQL
|
|
@using System.Linq
|
|
@using System.Linq.Expressions
|
|
|
|
@using OpenArchival.Blazor.ArtifactGroupingDisplay
|
|
|
|
<MudAutocomplete FullWidth="true"
|
|
AutoFocus="string.IsNullOrEmpty(SearchTerms)"
|
|
Placeholder="Search"
|
|
T="string"
|
|
Variant="Variant.Outlined"
|
|
AdornmentIcon="@Icons.Material.Filled.Search"
|
|
Class="mt-5"
|
|
@bind-Text="@SearchTerms"
|
|
OnKeyDown="HandleSearchKeyDown" />
|
|
|
|
<MudExpansionPanel Text="Filter...">
|
|
<MudText Typo="Typo.caption"></MudText>
|
|
<MudDivider></MudDivider>
|
|
|
|
<MudRadioGroup T="SearchFilterType" @bind-SelectedOption="_selectedFilter">
|
|
<MudRadio Option="SearchFilterType.All" T="SearchFilterType">All</MudRadio>
|
|
<MudRadio Option="SearchFilterType.Tags" T="SearchFilterType">Tags</MudRadio>
|
|
<MudRadio Option="SearchFilterType.Defects" T="SearchFilterType">Defects</MudRadio>
|
|
<MudRadio Option="SearchFilterType.ListedNames" T="SearchFilterType">Listed Names</MudRadio>
|
|
<MudRadio Option="SearchFilterType.Title" T="SearchFilterType">Title</MudRadio>
|
|
<MudRadio Option="SearchFilterType.Description" T="SearchFilterType">Description</MudRadio>
|
|
<MudRadio Option="SearchFilterType.Filenames" T="SearchFilterType">Filenames</MudRadio>
|
|
<MudRadio Option="SearchFilterType.ArtifactTranscriptions" T="SearchFilterType">Artifact Transcriptions</MudRadio>
|
|
</MudRadioGroup>
|
|
|
|
</MudExpansionPanel>
|
|
@if (_totalResults > 0)
|
|
{
|
|
<MudGrid Justify="Justify.FlexStart" Class="mt-1 ml-1 mb-2">
|
|
<MudText Typo="Typo.subtitle2" Class="my-2">@_totalResults results found</MudText>
|
|
<MudButton Class="ml-1" StartIcon="@Icons.Material.Filled.Clear" OnClick="OnClearResults" Variant="Variant.Filled" Color="Color.Primary" Size="Size.Small">Clear</MudButton>
|
|
</MudGrid>
|
|
}
|
|
@if (_artifactGroupings.Count > 0)
|
|
{
|
|
<MudGrid>
|
|
@foreach (var grouping in _artifactGroupings)
|
|
{
|
|
<MudItem xs="12" sm="6" md="4" lg="4">
|
|
<ArtifactGroupingSearchResult ArtifactGrouping="grouping"></ArtifactGroupingSearchResult>
|
|
</MudItem>
|
|
}
|
|
</MudGrid>
|
|
|
|
<MudPaper Class="d-flex justify-center py-2 mt-4" Elevation="0">
|
|
<MudPagination Count="_totalPages" Page="_currentPage" SelectedPageChanged="OnPageChangedAsync" />
|
|
</MudPaper>
|
|
}
|
|
else
|
|
{
|
|
@foreach (SearchPageSliderEntry entry in _sliderEntries)
|
|
{
|
|
<SearchPageSlider SliderEntry="entry"></SearchPageSlider>
|
|
}
|
|
}
|
|
|
|
@inject IDbContextFactory<ApplicationDbContext> ContextFactory;
|
|
@inject ILogger<SearchArchive> Logger;
|
|
@inject NavigationManager NavigationManager;
|
|
@code {
|
|
public enum SearchFilterType
|
|
{
|
|
All,
|
|
Tags,
|
|
Defects,
|
|
ListedNames,
|
|
Title,
|
|
Description,
|
|
Filenames,
|
|
ArtifactTranscriptions
|
|
}
|
|
|
|
[Parameter]
|
|
public string SearchTerms { get; set; } = "";
|
|
|
|
private SearchFilterType _selectedFilter = SearchFilterType.All;
|
|
private List<ArtifactGrouping> _artifactGroupings { get; set; } = [];
|
|
private int _currentPage { get; set; } = 1;
|
|
private int _totalPages { get; set; } = 0;
|
|
private int _totalResults { get; set; } = 0;
|
|
private const int PageSize = 20;
|
|
|
|
private List<SearchPageSliderEntry> _sliderEntries { get; set; } = [];
|
|
|
|
// Field to store the current filter logic
|
|
private Expression<Func<ArtifactGrouping, bool>> _currentFilterPredicate;
|
|
|
|
protected override async Task OnParametersSetAsync()
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(SearchTerms))
|
|
{
|
|
await PerformSearchAsync();
|
|
}
|
|
|
|
await using var context = await ContextFactory.CreateDbContextAsync();
|
|
_sliderEntries = await context.SearchPageSliderEntries.Include(e=>e.FilterTags).ToListAsync();
|
|
}
|
|
|
|
private async Task HandleSearchKeyDown(Microsoft.AspNetCore.Components.Web.KeyboardEventArgs args)
|
|
{
|
|
if (args.Key != "Enter" || string.IsNullOrWhiteSpace(SearchTerms))
|
|
{
|
|
return;
|
|
}
|
|
|
|
NavigationManager.NavigateTo($"/search/{SearchTerms}");
|
|
await PerformSearchAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the search predicate, calculates total results, and loads the first page.
|
|
/// </summary>
|
|
private async Task PerformSearchAsync()
|
|
{
|
|
// Determine which filter expression to use based on the radio button selection
|
|
switch (_selectedFilter)
|
|
{
|
|
case SearchFilterType.Tags:
|
|
_currentFilterPredicate = x => x.TagsSearchVector.Matches(SearchTerms);
|
|
break;
|
|
case SearchFilterType.Title:
|
|
_currentFilterPredicate = x => x.TitleSearchVector.Matches(SearchTerms);
|
|
break;
|
|
case SearchFilterType.Description:
|
|
_currentFilterPredicate = x => x.DescriptionSearchVector.Matches(SearchTerms);
|
|
break;
|
|
case SearchFilterType.Defects:
|
|
_currentFilterPredicate = x => x.DefectsSearchVector.Matches(SearchTerms);
|
|
break;
|
|
case SearchFilterType.Filenames:
|
|
_currentFilterPredicate = x => x.FilenamesSearchVector.Matches(SearchTerms);
|
|
break;
|
|
case SearchFilterType.ArtifactTranscriptions:
|
|
_currentFilterPredicate = x => x.FileContentSearchVector.Matches(SearchTerms);
|
|
break;
|
|
case SearchFilterType.ListedNames:
|
|
_currentFilterPredicate = x => x.ListedNamesSearchVector.Matches(SearchTerms);
|
|
break;
|
|
case SearchFilterType.All:
|
|
default:
|
|
_currentFilterPredicate = x => x.AllSearchVector.Matches(SearchTerms);
|
|
break;
|
|
}
|
|
|
|
// Get the total count using the chosen filter
|
|
await using var context = await ContextFactory.CreateDbContextAsync();
|
|
_totalResults = await context.ArtifactGroupings.Where(_currentFilterPredicate).CountAsync();
|
|
_totalPages = (int)Math.Ceiling(_totalResults / (double)PageSize);
|
|
|
|
// Load the first page with the chosen filter
|
|
await LoadPageAsync(1);
|
|
}
|
|
|
|
private async Task OnPageChangedAsync(int page)
|
|
{
|
|
await LoadPageAsync(page);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fetches a specific page of data from the database using the currently set filter.
|
|
/// </summary>
|
|
private async Task LoadPageAsync(int page)
|
|
{
|
|
_currentPage = page;
|
|
|
|
if (_currentFilterPredicate == null) // Don't run if no search has been performed
|
|
{
|
|
return;
|
|
}
|
|
|
|
await using var context = await ContextFactory.CreateDbContextAsync();
|
|
|
|
// The query uses the dynamically set filter predicate
|
|
_artifactGroupings = await context.ArtifactGroupings
|
|
.Where(_currentFilterPredicate)
|
|
.Include(x => x.ChildArtifactEntries)
|
|
.ThenInclude(x => x.Files)
|
|
.OrderBy(x => x.Id)
|
|
.Skip((_currentPage - 1) * PageSize)
|
|
.Take(PageSize)
|
|
.ToListAsync();
|
|
|
|
StateHasChanged();
|
|
}
|
|
private void OnClearResults(Microsoft.AspNetCore.Components.Web.MouseEventArgs args)
|
|
{
|
|
_totalResults = 0;
|
|
_artifactGroupings.Clear();
|
|
|
|
StateHasChanged();
|
|
}
|
|
} |