4 Commits

Author SHA1 Message Date
50a4c24660 Merge remote-tracking branch 'origin/master' into DtoBuilders 2025-06-02 00:38:24 +02:00
271bf84467 feat: show slightly more information in event list view
instead of event title and organizer, we now also show place and date
2025-06-02 00:37:47 +02:00
AleksDw
fd97b2c2d9 fix apply button when loged off 2025-06-02 00:17:43 +02:00
42fd94e5ac feat: replace manual event search in favor of dto builders 2025-06-01 23:55:43 +02:00
12 changed files with 188 additions and 138 deletions

View File

@@ -12,8 +12,8 @@ public record class EventSummaryDto {
[StringLength(500)] public string Description { get; set; } [StringLength(500)] public string Description { get; set; }
[Required] [StringLength(100)] public string Location { get; set; } [Required] [StringLength(100)] public string Location { get; set; }
[Required] public DateTime? EventDate { get; set; } [Required] public DateTime? EventDate { get; set; }
public ICollection<EventSkill> EventSkills { get; set; } public ICollection<SkillSummaryDto> EventSkills { get; set; }
public ICollection<EventRegistration> EventRegistrations { get; set; } public ICollection<EventRegistrationDto> EventRegistrations { get; set; }
}; };

View File

@@ -229,7 +229,7 @@ namespace WebApp.Endpoints
.Include(vs => vs.Skill) .Include(vs => vs.Skill)
.Select(vs => new .Select(vs => new
{ {
skillId = vs.Skill.SkillId, skillId = vs.Skill!.SkillId,
skillName = vs.Skill.Name skillName = vs.Skill.Name
}) })
.ToListAsync(); .ToListAsync();

View File

@@ -20,27 +20,24 @@ namespace WebApp.Endpoints
// GET /events // GET /events
group.MapGet("/", group.MapGet("/",
async (ApplicationDbContext dbContext, HttpContext httpContext) => async (ApplicationDbContext dbContext, HttpContext httpContext, GeneralUseHelpers guhf) =>
{ {
var sort = httpContext.Request.Query["sort"].ToString(); // Sprawdź, czy lista powinna by posortowana rosnąco. Domyślnie: malejąco.
IOrderedQueryable<Event> res; var sort = httpContext.Request.Query["sort"].ToString().ToUpper();
var r = dbContext.Events
.Include(Eve => Eve.Organisation);
if (sort is not null && sort.ToUpper() == "ASC") // Sprawdź, czy token należy do organizacji, a jeżeli tak, to do której.
{ Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
res = r.OrderBy(Eve => Eve.EventId); Organisation? org = await guhf.GetOrganisationFromToken(token);
}
else List<EventSummaryDto> result = await guhf.BuildSummaryEventsDto(
{ dbContext,
res = r.OrderByDescending(Eve => Eve.EventId); org,
} (sort == "ASC")
);
return Results.Ok(result);
return await res
.Select(Eve => Eve.ToEventSummaryDto()) //EventSummaryDto
.AsNoTracking()
.ToListAsync();
}); });
@@ -64,7 +61,7 @@ namespace WebApp.Endpoints
// puste pole. // puste pole.
List<EventDetailsDto> result = await guhf.BuildDetailedEventsDto( List<EventDetailsDto> result = await guhf.BuildDetailedEventsDto(
dbContext, dbContext,
(org is not null && Eve.Organisation == org) org
); );
return Results.Ok(result.FirstOrDefault(e => e.EventId == id)); return Results.Ok(result.FirstOrDefault(e => e.EventId == id));
@@ -158,20 +155,14 @@ namespace WebApp.Endpoints
{ {
// Uzyskaj organizację z tokenu // Uzyskaj organizację z tokenu
var sort = httpContext.Request.Query["sort"].ToString(); var sort = httpContext.Request.Query["sort"].ToString().ToUpper();
Token? token = await guhf.GetTokenFromHTTPContext(httpContext); Token? token = await guhf.GetTokenFromHTTPContext(httpContext);
Organisation? org = await guhf.GetOrganisationFromToken(token); Organisation? org = await guhf.GetOrganisationFromToken(token);
List<EventSummaryDto> SearchCandidates = await guhf.BuildSummaryEventsDto(dbContext, org, sort == "ASC");
List<EventSummaryDto> SearchResults = []; List<EventSummaryDto> SearchResults = [];
List<Event> AllEvents = await dbContext.Events.ToListAsync(); foreach(EventSummaryDto e in SearchCandidates)
if (sort is null || sort.ToUpper() != "ASC")
{
AllEvents.Reverse(); // aby wyświetlało od najnowszych wydarzeń
}
foreach(Event e in AllEvents)
{ {
bool matchFound = true; bool matchFound = true;
// Logika wyszukiwania // Logika wyszukiwania
@@ -210,19 +201,12 @@ namespace WebApp.Endpoints
// Uwaga! Zanim to zrobisz, sprawdź, czy użytkownik // Uwaga! Zanim to zrobisz, sprawdź, czy użytkownik
// jest twórcą danego wydarzenia! Jeżeli nim nie jest, // jest twórcą danego wydarzenia! Jeżeli nim nie jest,
// wyzeruj EventRegistrations! // wyzeruj EventRegistrations!
if (org is null || e.Organisation != org) if (org is null || e.OrganisationId != org.OrganisationId)
{ {
e.EventRegistrations.Clear(); e.EventRegistrations.Clear();
} }
// UWAGA! TO NIE POWINNO TAK DZIAŁAĆ! if (matchFound) SearchResults.Add(e);
// 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());
} }
return Results.Ok(SearchResults); return Results.Ok(SearchResults);

View File

@@ -36,12 +36,7 @@ public class GeneralUseHelpers
User? user = await GetUserFromToken(t); User? user = await GetUserFromToken(t);
if (user is not null && user.IsOrganisation) if (user is not null && user.IsOrganisation)
{ {
Organisation? org = await _context.Organisations.FirstOrDefaultAsync(o => o.UserId == t.UserId); Organisation? org = await _context.Organisations.FirstOrDefaultAsync(o => o.UserId == t!.UserId);
if (org is null)
{
Console.WriteLine("!!!");
}
return org; return org;
} }
@@ -114,36 +109,26 @@ public class GeneralUseHelpers
return words.Any(word => word.Contains(searchTerm, StringComparison.OrdinalIgnoreCase)); return words.Any(word => word.Contains(searchTerm, StringComparison.OrdinalIgnoreCase));
} }
public async Task<List<EventDetailsDto>> BuildDetailedEventsDto(ApplicationDbContext context, bool includeEventRegistrations = false) public async Task<List<EventDetailsDto>> BuildDetailedEventsDto(
ApplicationDbContext context,
Organisation? org,
bool sortAscending = false)
{ {
// https://khalidabuhakmeh.com/ef-core-and-aspnet-core-cycle-issue-and-solution // 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, // 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 // to zwróć także EventRegistrations. W przeciwnym razie niech będzie to
// puste pole. // 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 IQueryable<EventDetailsDto> result_iq = context
.Events .Events
.Select(e => new EventDetailsDto .Select(e => new EventDetailsDto
{ {
EventId = e.EventId, EventId = e.EventId,
OrganisationId = e.OrganisationId, OrganisationId = e.OrganisationId,
OrganisationName = e.Organisation.Name, OrganisationName = e.Organisation!.Name,
Title = e.Title, Title = e.Title,
Description = e.Description, Description = e.Description ?? "",
Location = e.Location, Location = e.Location,
EventDate = e.EventDate, EventDate = e.EventDate,
EventSkills = e EventSkills = e
@@ -151,11 +136,69 @@ public class GeneralUseHelpers
.Select(es => new SkillSummaryDto .Select(es => new SkillSummaryDto
{ {
SkillId = es.SkillId, SkillId = es.SkillId,
SkillName = es.Skill.Name SkillName = es.Skill!.Name
}).ToList(), }).ToList(),
EventRegistrations = ERs EventRegistrations = e.Organisation == org ?
}).ToListAsync(); e.EventRegistrations
.Select(er => new EventRegistrationDto
{
EventId = er.EventId,
UserId = er.UserId,
UserName = er.User!.FirstName + " " + er.User.LastName,
RegisteredAt = er.RegisteredAt
}).ToList() : null!
});
return result; if (sortAscending) result_iq = result_iq.OrderBy(e => e.EventId);
else result_iq = result_iq.OrderByDescending(e => e.EventId);
return await result_iq.ToListAsync();
} }
public async Task<List<EventSummaryDto>> BuildSummaryEventsDto(
ApplicationDbContext context,
Organisation? org,
bool sortAscending = 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.
IQueryable<EventSummaryDto> result_iq = context
.Events
.Select(e => new EventSummaryDto
{
EventId = e.EventId,
OrganisationId = e.OrganisationId,
Organisation = 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 = e.Organisation == org ?
e.EventRegistrations
.Select(er => new EventRegistrationDto
{
EventId = er.EventId,
UserId = er.UserId,
UserName = er.User!.FirstName + " " + er.User.LastName,
RegisteredAt = er.RegisteredAt
}).ToList() : null!
});
if (sortAscending) result_iq = result_iq.OrderBy(e => e.EventId);
else result_iq = result_iq.OrderByDescending(e => e.EventId);
return await result_iq.ToListAsync();
}
} }

View File

@@ -34,16 +34,30 @@ public static class EventMapping
public static EventSummaryDto ToEventSummaryDto(this Event myEvent) public static EventSummaryDto ToEventSummaryDto(this Event myEvent)
{ {
List<SkillSummaryDto> ssdto = new List<SkillSummaryDto>();
List<EventRegistrationDto> erdto = new List<EventRegistrationDto>();
foreach (EventSkill es in myEvent.EventSkills)
{
ssdto.Add(es.ToSkillSummaryDto());
}
foreach (EventRegistration er in myEvent.EventRegistrations)
{
erdto.Add(er.ToEventRegistrationDto());
}
return new EventSummaryDto { return new EventSummaryDto {
EventId = myEvent.EventId, EventId = myEvent.EventId,
Organisation = myEvent.Organisation!.Name, Organisation = myEvent.Organisation!.Name,
OrganisationId = myEvent.OrganisationId, OrganisationId = myEvent.OrganisationId,
Title = myEvent.Title, Title = myEvent.Title,
Description = myEvent.Description, Description = myEvent.Description ?? "",
Location = myEvent.Location, Location = myEvent.Location,
EventDate = myEvent.EventDate, EventDate = myEvent.EventDate,
EventSkills = myEvent.EventSkills, EventSkills = ssdto,
EventRegistrations = myEvent.EventRegistrations EventRegistrations = erdto
}; };
} }
public static EventSummaryNoErDto ToEventSummaryNoErDto(this Event myEvent) public static EventSummaryNoErDto ToEventSummaryNoErDto(this Event myEvent)
@@ -54,7 +68,7 @@ public static class EventMapping
myEvent.Organisation!.Name, myEvent.Organisation!.Name,
myEvent.OrganisationId, myEvent.OrganisationId,
myEvent.Title, myEvent.Title,
myEvent.Description, myEvent.Description ?? "",
myEvent.Location, myEvent.Location,
myEvent.EventDate, myEvent.EventDate,
myEvent.EventSkills myEvent.EventSkills
@@ -67,7 +81,7 @@ public static class EventMapping
return new EventRegistrationDto { return new EventRegistrationDto {
EventId = myER.EventId, EventId = myER.EventId,
UserId = myER.UserId, UserId = myER.UserId,
UserName = myER.User.FirstName + " " + myER.User.LastName, UserName = myER.User!.FirstName + " " + myER.User!.LastName,
RegisteredAt = myER.RegisteredAt RegisteredAt = myER.RegisteredAt
}; };
} }
@@ -90,9 +104,9 @@ public static class EventMapping
return new EventDetailsDto { return new EventDetailsDto {
EventId = myEvent.EventId, EventId = myEvent.EventId,
OrganisationId = myEvent.OrganisationId, OrganisationId = myEvent.OrganisationId,
OrganisationName = myEvent.Organisation.Name, OrganisationName = myEvent.Organisation!.Name,
Title = myEvent.Title, Title = myEvent.Title,
Description = myEvent.Description, Description = myEvent.Description ?? "",
Location = myEvent.Location, Location = myEvent.Location,
EventDate = myEvent.EventDate, EventDate = myEvent.EventDate,
EventSkills = ssdto, EventSkills = ssdto,

View File

@@ -1,17 +0,0 @@
using WebApp.DTOs;
using WebApp.Entities;
namespace WebApp.Mapping
{
public static class EventRegistrationMapping
{
public static EventRegistrationDto ToEventRegistrationDto(this EventRegistration er)
{
return new EventRegistrationDto(
er.EventId,
er.UserId,
er.RegisteredAt
);
}
}
}

View File

@@ -1,4 +1,4 @@
import { getEvent, getMyAccount, unhideElementById } from './generalUseHelpers.js'; import { getEvent, getMyAccount, unhideElementById } from './generalUseHelpers.js';
var isAscending: boolean = false; var isAscending: boolean = false;
@@ -57,10 +57,17 @@ async function loadEvents(org_id: number, evs?: Promise<any>) {
//card.innerHTML = ` //card.innerHTML = `
// <span>${ev.title}</span>` // <span>${ev.title}</span>`
// Do odkomentowania kiedy widok podglądu wydarzeń będzie gotowy // Do odkomentowania kiedy widok podglądu wydarzeń będzie gotowy
let formattedDate: string = new Intl.DateTimeFormat('en-US', {
weekday: 'long', // "Monday"
year: 'numeric', // "2023"
month: 'long', // "December"
day: 'numeric' // "1"
}).format(ev.eventDate);
console.log(formattedDate);
card.innerHTML = ` card.innerHTML = `
<span> <span>
<a href="/view.html?event=${ev.eventId}" style="color: #2898BD">${ev.title}</a> <a href="/view.html?event=${ev.eventId}" style="color: #2898BD">${ev.title}</a>
<p style="margin: 0">${ev.organisation}</p> <p style="margin: 0">${ev.organisation} | ${ev.location} | ${formattedDate}</p>
</span>` </span>`
if (org_id == ev.organisationId) { if (org_id == ev.organisationId) {
card.innerHTML += ` card.innerHTML += `

View File

@@ -59,6 +59,7 @@ document.addEventListener("DOMContentLoaded", async () => {
unhideElementById(document, "removeBtn"); unhideElementById(document, "removeBtn");
} else if (org_id == -1) { } else if (org_id == -1) {
// Użytkownik jest wolontariuszem // Użytkownik jest wolontariuszem
try {
const registeredIds = await getMyRegisteredEventIds(); const registeredIds = await getMyRegisteredEventIds();
const isRegistered = registeredIds.includes(Number(eventId)); const isRegistered = registeredIds.includes(Number(eventId));
@@ -67,6 +68,12 @@ document.addEventListener("DOMContentLoaded", async () => {
} else { } else {
unhideElementById(document, "applyBtn"); unhideElementById(document, "applyBtn");
} }
} catch {
unhideElementById(document, "applyBtn");
(applyBtn as HTMLButtonElement).textContent = "log in to apply";
(applyBtn as HTMLButtonElement).disabled = true;
}
} }
unhideElementById(document, "mainContainer"); unhideElementById(document, "mainContainer");

View File

@@ -36,7 +36,7 @@ export async function getEvent(id: string): Promise<EventData> {
export async function getMyAccount(): Promise<MyAccount> { export async function getMyAccount(): Promise<MyAccount> {
const res = await fetch("/api/auth/my_account"); const res = await fetch("/api/auth/my_account");
if (!res.ok) { if (!res.ok) {
throw Error("U<EFBFBD>ytkownik niezalogowany!"); throw Error("Użytkownik niezalogowany!");
} }
const data = await res.json(); const data = await res.json();
return data; return data;

View File

@@ -59,13 +59,18 @@ function loadEvents(org_id, evs) {
for (const ev of events) { for (const ev of events) {
const card = document.createElement("div"); const card = document.createElement("div");
card.className = "event-card filled"; card.className = "event-card filled";
//card.innerHTML = `
// <span>${ev.title}</span>` let formattedDate = new Intl.DateTimeFormat('en-US', {
// Do odkomentowania kiedy widok podglądu wydarzeń będzie gotowy weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric' // "1"
}).format(new Date(ev.eventDate));
card.innerHTML = ` card.innerHTML = `
<span> <span>
<a href="/view.html?event=${ev.eventId}" style="color: #2898BD">${ev.title}</a> <a href="/view.html?event=${ev.eventId}" style="color: #2898BD">${ev.title}</a>
<p style="margin: 0">${ev.organisation}</p> <p style="margin: 0">👥 ${ev.organisation} | 📍 ${ev.location} | 📅 ${formattedDate}</p>
</span>`; </span>`;
if (org_id == ev.organisationId) { if (org_id == ev.organisationId) {
card.innerHTML += ` card.innerHTML += `

View File

@@ -65,6 +65,7 @@ document.addEventListener("DOMContentLoaded", () => __awaiter(void 0, void 0, vo
} }
else if (org_id == -1) { else if (org_id == -1) {
// Użytkownik jest wolontariuszem // Użytkownik jest wolontariuszem
try {
const registeredIds = yield getMyRegisteredEventIds(); const registeredIds = yield getMyRegisteredEventIds();
const isRegistered = registeredIds.includes(Number(eventId)); const isRegistered = registeredIds.includes(Number(eventId));
if (isRegistered) { if (isRegistered) {
@@ -74,6 +75,12 @@ document.addEventListener("DOMContentLoaded", () => __awaiter(void 0, void 0, vo
unhideElementById(document, "applyBtn"); unhideElementById(document, "applyBtn");
} }
} }
catch (_b) {
unhideElementById(document, "applyBtn");
applyBtn.textContent = "log in to apply";
applyBtn.disabled = true;
}
}
unhideElementById(document, "mainContainer"); unhideElementById(document, "mainContainer");
} }
if (modifyBtn) { if (modifyBtn) {
@@ -107,33 +114,9 @@ document.addEventListener("DOMContentLoaded", () => __awaiter(void 0, void 0, vo
} }
if (applyBtn) { if (applyBtn) {
applyBtn.addEventListener("click", (e) => __awaiter(void 0, void 0, void 0, function* () { applyBtn.addEventListener("click", (e) => __awaiter(void 0, void 0, void 0, function* () {
var _b;
try {
const response = yield fetch(`/api/events/join/${eventId}`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
});
const result = yield response.json();
if (result.success) {
window.location.href = `/view.html?event=${eventId}`;
}
else {
alert(`Error: ${(_b = result.error_msg) !== null && _b !== void 0 ? _b : "Unknown error occurred."}`);
}
}
catch (error) {
console.error("Failed to apply:", error);
alert("Failed to apply.");
}
}));
}
if (leaveBtn) {
leaveBtn.addEventListener("click", (e) => __awaiter(void 0, void 0, void 0, function* () {
var _c; var _c;
try { try {
const response = yield fetch(`/api/events/leave/${eventId}`, { const response = yield fetch(`/api/events/join/${eventId}`, {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json" "Content-Type": "application/json"
@@ -147,6 +130,30 @@ document.addEventListener("DOMContentLoaded", () => __awaiter(void 0, void 0, vo
alert(`Error: ${(_c = result.error_msg) !== null && _c !== void 0 ? _c : "Unknown error occurred."}`); alert(`Error: ${(_c = result.error_msg) !== null && _c !== void 0 ? _c : "Unknown error occurred."}`);
} }
} }
catch (error) {
console.error("Failed to apply:", error);
alert("Failed to apply.");
}
}));
}
if (leaveBtn) {
leaveBtn.addEventListener("click", (e) => __awaiter(void 0, void 0, void 0, function* () {
var _d;
try {
const response = yield fetch(`/api/events/leave/${eventId}`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
});
const result = yield response.json();
if (result.success) {
window.location.href = `/view.html?event=${eventId}`;
}
else {
alert(`Error: ${(_d = result.error_msg) !== null && _d !== void 0 ? _d : "Unknown error occurred."}`);
}
}
catch (error) { catch (error) {
console.error("Failed to leave:", error); console.error("Failed to leave:", error);
alert("Failed to leave."); alert("Failed to leave.");

View File

@@ -29,7 +29,7 @@ export function getMyAccount() {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const res = yield fetch("/api/auth/my_account"); const res = yield fetch("/api/auth/my_account");
if (!res.ok) { if (!res.ok) {
throw Error("U<EFBFBD>ytkownik niezalogowany!"); throw Error("Użytkownik niezalogowany!");
} }
const data = yield res.json(); const data = yield res.json();
return data; return data;