mirror of
https://github.com/GCMatters/hermes.git
synced 2026-02-04 05:30: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,7 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.Intrinsics.Arm;
|
||||
using WebApp.Data;
|
||||
using WebApp.DTOs;
|
||||
using WebApp.Entities;
|
||||
@@ -40,14 +42,17 @@ namespace WebApp.Endpoints
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
});
|
||||
|
||||
|
||||
|
||||
// GET /events/1
|
||||
group.MapGet("/{id}",
|
||||
group.MapGet("/{id}",
|
||||
async (int id, ApplicationDbContext dbContext, HttpContext httpContext, GeneralUseHelpers guhf) =>
|
||||
{
|
||||
Event? Eve = await dbContext.Events.FindAsync(id);
|
||||
|
||||
Event? Eve = await dbContext
|
||||
.Events
|
||||
.Include(e => e.Organisation)
|
||||
.FirstOrDefaultAsync(e => e.EventId == id);
|
||||
if (Eve is null) return Results.NotFound();
|
||||
|
||||
// Sprawdź, czy token należy do organizacji, a jeżeli tak, to do której.
|
||||
@@ -55,16 +60,14 @@ namespace WebApp.Endpoints
|
||||
Organisation? org = await guhf.GetOrganisationFromToken(token);
|
||||
|
||||
// Jeśli token należy do organizacji, która utworzyła to wydarzenie,
|
||||
// to zwróć także EventRegistrations. W przeciwnym razie usuń to pole
|
||||
// przed jego wysłaniem!
|
||||
if (org is null || org.OrganisationId != Eve.OrganisationId) Eve.EventRegistrations = [];
|
||||
// to zwróć także EventRegistrations. W przeciwnym razie niech będzie to
|
||||
// puste pole.
|
||||
List<EventDetailsDto> result = await guhf.BuildDetailedEventsDto(
|
||||
dbContext,
|
||||
(org is not null && Eve.Organisation == org)
|
||||
);
|
||||
|
||||
// DLACZEGO?
|
||||
Eve.Organisation = await guhf.GetOrganisationFromId(Eve.OrganisationId);
|
||||
|
||||
EventDetailsDto EveDto = Eve.ToEventDetailsDto();
|
||||
|
||||
return Results.Ok(EveDto); //EventDetailsDto
|
||||
return Results.Ok(result.FirstOrDefault(e => e.EventId == id));
|
||||
})
|
||||
.WithName(GetEventEndpointName);
|
||||
|
||||
@@ -214,6 +217,9 @@ namespace WebApp.Endpoints
|
||||
|
||||
// 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());
|
||||
|
||||
@@ -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