mirror of
https://github.com/GCMatters/hermes.git
synced 2026-02-04 13:40:13 +01:00
fix: check POST data for validity
feat: also introduces a template for search
This commit is contained in:
16
WebApp/DTOs/EventSearchDto.cs
Normal file
16
WebApp/DTOs/EventSearchDto.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using WebApp.Entities;
|
||||||
|
|
||||||
|
namespace WebApp.DTOs;
|
||||||
|
|
||||||
|
// Output values in JSON file
|
||||||
|
public record class EventSearchDto
|
||||||
|
(
|
||||||
|
int? OrganisationId,
|
||||||
|
string? Title,
|
||||||
|
string? Description,
|
||||||
|
string? Location,
|
||||||
|
DateTime? EventDate,
|
||||||
|
ICollection<EventSkill>? EventSkills, // obecnie nie dotyczy
|
||||||
|
ICollection<EventRegistration>? EventRegistrations // obecnie nie dotyczy
|
||||||
|
);
|
||||||
@@ -7,6 +7,7 @@ namespace WebApp.DTOs;
|
|||||||
public record class EventSummaryDto(
|
public record class EventSummaryDto(
|
||||||
int EventId,
|
int EventId,
|
||||||
[Required] string Organisation,
|
[Required] string Organisation,
|
||||||
|
[Required] int OrganisationId,
|
||||||
[Required] [StringLength(50)] string Title,
|
[Required] [StringLength(50)] string Title,
|
||||||
[StringLength(500)] string Description,
|
[StringLength(500)] string Description,
|
||||||
[Required] [StringLength(100)] string Location,
|
[Required] [StringLength(100)] string Location,
|
||||||
|
|||||||
@@ -63,6 +63,10 @@ namespace WebApp.Endpoints
|
|||||||
// Wyzeruj EventRegistrations, ponieważ nie są to dane,
|
// Wyzeruj EventRegistrations, ponieważ nie są to dane,
|
||||||
// które powinniśmy przyjmować bez zgody wolontariuszy!
|
// które powinniśmy przyjmować bez zgody wolontariuszy!
|
||||||
Eve.EventRegistrations = [];
|
Eve.EventRegistrations = [];
|
||||||
|
Eve.OrganisationId = org.OrganisationId;
|
||||||
|
|
||||||
|
// Na wszelki wypadek, gdyby użytkownik wciskał nam kit :D
|
||||||
|
if (newEvent.OrganisationId is not null && newEvent.OrganisationId != org.OrganisationId) return Results.StatusCode(418);
|
||||||
|
|
||||||
dbContext.Events.Add(Eve);
|
dbContext.Events.Add(Eve);
|
||||||
await dbContext.SaveChangesAsync();
|
await dbContext.SaveChangesAsync();
|
||||||
@@ -135,6 +139,46 @@ namespace WebApp.Endpoints
|
|||||||
return Results.NoContent();
|
return Results.NoContent();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// POST /events/search
|
||||||
|
group.MapPost("/search/",
|
||||||
|
async (EventSearchDto query, ApplicationDbContext dbContext, HttpContext httpContext, GeneralUseHelpers guhf) =>
|
||||||
|
{
|
||||||
|
|
||||||
|
// Uzyskaj organizację z tokenu
|
||||||
|
Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
|
||||||
|
Organisation? org = await guhf.GetOrganisationFromToken(token);
|
||||||
|
List<EventSummaryDto> SearchResults = [];
|
||||||
|
|
||||||
|
List<Event> AllEvents = await dbContext.Events.ToListAsync();
|
||||||
|
|
||||||
|
foreach(Event e in AllEvents)
|
||||||
|
{
|
||||||
|
// Logika wyszukiwania
|
||||||
|
// Sprawdź wszystkie pola z EventSearchDto, np.
|
||||||
|
if (query.OrganisationId is not null)
|
||||||
|
{
|
||||||
|
// Sprawdź, czy Event należy do query.OrganisationId.
|
||||||
|
}
|
||||||
|
|
||||||
|
// ...
|
||||||
|
|
||||||
|
// Jeśli Event jest tym, czego szuka użytkownik,
|
||||||
|
// dodaj go do listy SearchResults.
|
||||||
|
//
|
||||||
|
// 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)
|
||||||
|
{
|
||||||
|
e.EventRegistrations.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
SearchResults.Add(e.ToEventSummaryDto());
|
||||||
|
}
|
||||||
|
|
||||||
|
return Results.Ok(SearchResults);
|
||||||
|
});
|
||||||
|
|
||||||
return group;
|
return group;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ public static class EventMapping
|
|||||||
return new EventSummaryDto(
|
return new EventSummaryDto(
|
||||||
myEvent.EventId,
|
myEvent.EventId,
|
||||||
myEvent.Organisation!.Name,
|
myEvent.Organisation!.Name,
|
||||||
|
myEvent.OrganisationId,
|
||||||
myEvent.Title,
|
myEvent.Title,
|
||||||
myEvent.Description,
|
myEvent.Description,
|
||||||
myEvent.Location,
|
myEvent.Location,
|
||||||
|
|||||||
Reference in New Issue
Block a user