mirror of
https://github.com/GCMatters/hermes.git
synced 2026-02-04 13:40:13 +01:00
fix: offload building DTOs to GUHF
DTO building allows for fully returning correct event's skills and registrations
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user