diff --git a/WebApp/DTOs/EventSearchDto.cs b/WebApp/DTOs/EventSearchDto.cs new file mode 100644 index 0000000..c1cf6cb --- /dev/null +++ b/WebApp/DTOs/EventSearchDto.cs @@ -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? EventSkills, // obecnie nie dotyczy + ICollection? EventRegistrations // obecnie nie dotyczy +); diff --git a/WebApp/DTOs/EventSummaryDto.cs b/WebApp/DTOs/EventSummaryDto.cs index ae18f96..5835f40 100644 --- a/WebApp/DTOs/EventSummaryDto.cs +++ b/WebApp/DTOs/EventSummaryDto.cs @@ -7,6 +7,7 @@ namespace WebApp.DTOs; public record class EventSummaryDto( int EventId, [Required] string Organisation, + [Required] int OrganisationId, [Required] [StringLength(50)] string Title, [StringLength(500)] string Description, [Required] [StringLength(100)] string Location, diff --git a/WebApp/Endpoints/EventsEndpoints.cs b/WebApp/Endpoints/EventsEndpoints.cs index 048825c..87b6939 100644 --- a/WebApp/Endpoints/EventsEndpoints.cs +++ b/WebApp/Endpoints/EventsEndpoints.cs @@ -63,6 +63,10 @@ namespace WebApp.Endpoints // Wyzeruj EventRegistrations, ponieważ nie są to dane, // które powinniśmy przyjmować bez zgody wolontariuszy! 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); await dbContext.SaveChangesAsync(); @@ -135,6 +139,46 @@ namespace WebApp.Endpoints 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 SearchResults = []; + + List 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; } } diff --git a/WebApp/Mapping/EventMapping.cs b/WebApp/Mapping/EventMapping.cs index 8abf804..e617cbe 100644 --- a/WebApp/Mapping/EventMapping.cs +++ b/WebApp/Mapping/EventMapping.cs @@ -39,6 +39,7 @@ public static class EventMapping return new EventSummaryDto( myEvent.EventId, myEvent.Organisation!.Name, + myEvent.OrganisationId, myEvent.Title, myEvent.Description, myEvent.Location,