mirror of
https://github.com/GCMatters/hermes.git
synced 2026-02-04 13:40:13 +01:00
feat: implement basic search functionality with partial text matches
This commit is contained in:
@@ -7,8 +7,7 @@ namespace WebApp.DTOs;
|
|||||||
public record class EventSearchDto
|
public record class EventSearchDto
|
||||||
(
|
(
|
||||||
int? OrganisationId,
|
int? OrganisationId,
|
||||||
string? Title,
|
string? TitleOrDescription,
|
||||||
string? Description,
|
|
||||||
string? Location,
|
string? Location,
|
||||||
DateTime? EventDate,
|
DateTime? EventDate,
|
||||||
ICollection<EventSkill>? EventSkills, // obecnie nie dotyczy
|
ICollection<EventSkill>? EventSkills, // obecnie nie dotyczy
|
||||||
|
|||||||
@@ -159,11 +159,20 @@ namespace WebApp.Endpoints
|
|||||||
|
|
||||||
foreach(Event e in AllEvents)
|
foreach(Event e in AllEvents)
|
||||||
{
|
{
|
||||||
|
bool matchFound = true;
|
||||||
// Logika wyszukiwania
|
// Logika wyszukiwania
|
||||||
// Sprawdź wszystkie pola z EventSearchDto, np.
|
// Sprawdź wszystkie pola z EventSearchDto, np.
|
||||||
if (query.OrganisationId is not null)
|
if (query.OrganisationId is not null)
|
||||||
{
|
{
|
||||||
// Sprawdź, czy Event należy do query.OrganisationId.
|
// Sprawdź, czy Event należy do query.OrganisationId.
|
||||||
|
if (e.OrganisationId != query.OrganisationId) matchFound = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (query.TitleOrDescription is not null)
|
||||||
|
{
|
||||||
|
var TitleMatch = guhf.SearchString(e.Title, query.TitleOrDescription);
|
||||||
|
var DescMatch = guhf.SearchString(e.Description, query.TitleOrDescription);
|
||||||
|
if (!TitleMatch && !DescMatch) matchFound = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
@@ -179,7 +188,11 @@ namespace WebApp.Endpoints
|
|||||||
e.EventRegistrations.Clear();
|
e.EventRegistrations.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
SearchResults.Add(e.ToEventSummaryDto());
|
// UWAGA! TO NIE POWINNO TAK DZIAŁAĆ!
|
||||||
|
// KTOKOLWIEK WIDZIAŁ, KTOKOLWIEK WIE CZEMU Organisation JEST null?
|
||||||
|
e.Organisation = await guhf.GetOrganisationFromId(e.OrganisationId);
|
||||||
|
|
||||||
|
if (matchFound) SearchResults.Add(e.ToEventSummaryDto());
|
||||||
}
|
}
|
||||||
|
|
||||||
return Results.Ok(SearchResults);
|
return Results.Ok(SearchResults);
|
||||||
|
|||||||
@@ -48,6 +48,12 @@ public class GeneralUseHelpers
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async public Task<Organisation?> GetOrganisationFromId(int id)
|
||||||
|
{
|
||||||
|
Organisation? org = await _context.Organisations.FirstOrDefaultAsync(o => o.OrganisationId == id);
|
||||||
|
return org;
|
||||||
|
}
|
||||||
|
|
||||||
public string? GetTokenStrFromHTTPContext(HttpContext httpContext)
|
public string? GetTokenStrFromHTTPContext(HttpContext httpContext)
|
||||||
{
|
{
|
||||||
var cookies = httpContext.Request.Cookies;
|
var cookies = httpContext.Request.Cookies;
|
||||||
@@ -87,4 +93,17 @@ public class GeneralUseHelpers
|
|||||||
_context.Tokens.Remove(token);
|
_context.Tokens.Remove(token);
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool SearchString(string? text, string searchTerm)
|
||||||
|
{
|
||||||
|
// Zwraca fałsz jeśli tekst jest pusty.
|
||||||
|
// (Brak tekstu nie wpływa na wynik wyszukiwania).
|
||||||
|
if (text is null) return false;
|
||||||
|
|
||||||
|
// Zamienia tekst na słowa
|
||||||
|
var words = text.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
|
// Sprawdza, czy któreś ze słów pasuje (nawet częściowo) do searchTerm
|
||||||
|
return words.Any(word => word.Contains(searchTerm, StringComparison.OrdinalIgnoreCase));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user