feat: replace manual event search in favor of dto builders

This commit is contained in:
2025-06-01 23:55:43 +02:00
parent 07128948b0
commit 42fd94e5ac
6 changed files with 118 additions and 94 deletions

View File

@@ -20,27 +20,24 @@ namespace WebApp.Endpoints
// GET /events
group.MapGet("/",
async (ApplicationDbContext dbContext, HttpContext httpContext) =>
async (ApplicationDbContext dbContext, HttpContext httpContext, GeneralUseHelpers guhf) =>
{
var sort = httpContext.Request.Query["sort"].ToString();
IOrderedQueryable<Event> res;
var r = dbContext.Events
.Include(Eve => Eve.Organisation);
// Sprawdź, czy lista powinna by posortowana rosnąco. Domyślnie: malejąco.
var sort = httpContext.Request.Query["sort"].ToString().ToUpper();
if (sort is not null && sort.ToUpper() == "ASC")
{
res = r.OrderBy(Eve => Eve.EventId);
}
else
{
res = r.OrderByDescending(Eve => Eve.EventId);
}
// Sprawdź, czy token należy do organizacji, a jeżeli tak, to do której.
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
Organisation? org = await guhf.GetOrganisationFromToken(token);
List<EventSummaryDto> result = await guhf.BuildSummaryEventsDto(
dbContext,
org,
(sort == "ASC")
);
return Results.Ok(result);
return await res
.Select(Eve => Eve.ToEventSummaryDto()) //EventSummaryDto
.AsNoTracking()
.ToListAsync();
});
@@ -64,7 +61,7 @@ namespace WebApp.Endpoints
// puste pole.
List<EventDetailsDto> result = await guhf.BuildDetailedEventsDto(
dbContext,
(org is not null && Eve.Organisation == org)
org
);
return Results.Ok(result.FirstOrDefault(e => e.EventId == id));
@@ -158,20 +155,14 @@ namespace WebApp.Endpoints
{
// Uzyskaj organizację z tokenu
var sort = httpContext.Request.Query["sort"].ToString();
var sort = httpContext.Request.Query["sort"].ToString().ToUpper();
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
Organisation? org = await guhf.GetOrganisationFromToken(token);
List<EventSummaryDto> SearchCandidates = await guhf.BuildSummaryEventsDto(dbContext, org, sort == "ASC");
List<EventSummaryDto> SearchResults = [];
List<Event> AllEvents = await dbContext.Events.ToListAsync();
if (sort is null || sort.ToUpper() != "ASC")
{
AllEvents.Reverse(); // aby wyświetlało od najnowszych wydarzeń
}
foreach(Event e in AllEvents)
foreach(EventSummaryDto e in SearchCandidates)
{
bool matchFound = true;
// Logika wyszukiwania
@@ -184,19 +175,19 @@ namespace WebApp.Endpoints
if (query.TitleOrDescription is not null)
{
var TitleMatch = guhf.SearchString(e.Title, query.TitleOrDescription);
var TitleMatch = guhf.SearchString(e.Title, query.TitleOrDescription);
var DescMatch = guhf.SearchString(e.Description, query.TitleOrDescription);
if (!TitleMatch && !DescMatch) matchFound = false;
}
//Zakres dat do wyszukiwania
if(query.EventDateFrom is not null)
// Zakres dat do wyszukiwania
if (query.EventDateFrom is not null)
{
if (e.EventDate < query.EventDateFrom) matchFound = false;
}
if(query.EventDateTo is not null)
if (query.EventDateTo is not null)
{
if (e.EventDate > query.EventDateTo) matchFound = false;
}
@@ -210,19 +201,12 @@ namespace WebApp.Endpoints
// Uwaga! Zanim to zrobisz, sprawdź, czy użytkownik
// jest twórcą danego wydarzenia! Jeżeli nim nie jest,
// wyzeruj EventRegistrations!
if (org is null || e.Organisation != org)
if (org is null || e.OrganisationId != org.OrganisationId)
{
e.EventRegistrations.Clear();
}
// UWAGA! TO NIE POWINNO TAK DZIAŁAĆ!
// KTOKOLWIEK WIDZIAŁ, KTOKOLWIEK WIE CZEMU Organisation JEST null?
//
// Odpowiedź: Bo pobieramy dane bez .Include(e => e.Organisation),
// co zapobiega masie innych problemów, m.in. rekurencyjnym importom.
e.Organisation = await guhf.GetOrganisationFromId(e.OrganisationId);
if (matchFound) SearchResults.Add(e.ToEventSummaryDto());
if (matchFound) SearchResults.Add(e);
}
return Results.Ok(SearchResults);