fix: offload building DTOs to GUHF

DTO building allows for fully returning correct event's
skills and registrations
This commit is contained in:
2025-06-01 20:33:58 +02:00
parent 426288d728
commit efb71b24d3
10 changed files with 182 additions and 72 deletions

View File

@@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using WebApp.Data;
using WebApp.DTOs;
using WebApp.Entities;
namespace WebApp.Endpoints;
@@ -112,4 +113,49 @@ public class GeneralUseHelpers
// Sprawdza, czy któreś ze słów pasuje (nawet częściowo) do searchTerm
return words.Any(word => word.Contains(searchTerm, StringComparison.OrdinalIgnoreCase));
}
public async Task<List<EventDetailsDto>> BuildDetailedEventsDto(ApplicationDbContext context, bool includeEventRegistrations = false)
{
// https://khalidabuhakmeh.com/ef-core-and-aspnet-core-cycle-issue-and-solution
// Jeśli token należy do organizacji, która utworzyła to wydarzenie,
// to zwróć także EventRegistrations. W przeciwnym razie niech będzie to
// puste pole.
ICollection<EventRegistrationDto> ERs = new List<EventRegistrationDto>();
if (includeEventRegistrations)
{
ERs = await context
.EventRegistrations
.Select(er => new EventRegistrationDto
{
EventId = er.EventId,
UserId = er.UserId,
UserName = er.User.FirstName + " " + er.User.LastName,
RegisteredAt = er.RegisteredAt
}).ToListAsync();
}
List<EventDetailsDto> result = await context
.Events
.Select(e => new EventDetailsDto
{
EventId = e.EventId,
OrganisationId = e.OrganisationId,
OrganisationName = e.Organisation.Name,
Title = e.Title,
Description = e.Description,
Location = e.Location,
EventDate = e.EventDate,
EventSkills = e
.EventSkills
.Select(es => new SkillSummaryDto
{
SkillId = es.SkillId,
SkillName = es.Skill.Name
}).ToList(),
EventRegistrations = ERs
}).ToListAsync();
return result;
}
}