From a8d706bf97fdf2f9a36d3d0eb41b284a382c5b86 Mon Sep 17 00:00:00 2001 From: Witkopawel Date: Mon, 2 Jun 2025 07:11:30 +0200 Subject: [PATCH] Calendar Calendar that show all events that we joined --- .../Endpoints/EventRegistrationEndpoints.cs | 21 ++++- WebApp/ts/calendar.ts | 39 ++++++++++ WebApp/ts/eventCreate.js | 61 --------------- WebApp/wwwroot/calendar.html | 77 +++++++++++++++++++ WebApp/wwwroot/index.html | 2 +- WebApp/wwwroot/js/calendar.js | 46 +++++++++++ WebApp/wwwroot/modify.html | 2 +- WebApp/wwwroot/view.html | 2 +- 8 files changed, 185 insertions(+), 65 deletions(-) create mode 100644 WebApp/ts/calendar.ts delete mode 100644 WebApp/ts/eventCreate.js create mode 100644 WebApp/wwwroot/calendar.html create mode 100644 WebApp/wwwroot/js/calendar.js diff --git a/WebApp/Endpoints/EventRegistrationEndpoints.cs b/WebApp/Endpoints/EventRegistrationEndpoints.cs index bb45476..130d139 100644 --- a/WebApp/Endpoints/EventRegistrationEndpoints.cs +++ b/WebApp/Endpoints/EventRegistrationEndpoints.cs @@ -1,4 +1,4 @@ -using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.EntityFrameworkCore; using System.Security.Cryptography; @@ -128,7 +128,26 @@ namespace WebApp.Endpoints return Results.Json(new { success = true }); }); + group.MapGet("/registered", + async (ApplicationDbContext dbContext, HttpContext httpContext, GeneralUseHelpers guhf) => + { + Token? token = await guhf.GetTokenFromHTTPContext(httpContext); + User? user = await guhf.GetUserFromToken(token); + if (user is null || user.IsOrganisation) + return Results.Json(new { success = false, error_msg = "Unauthorized or organisations cannot register." }); + + var events = await dbContext.EventRegistrations + .Where(r => r.UserId == user.UserId) + .Select(r => new { + r.Event.EventId, + r.Event.Title, + r.Event.EventDate + }) + .ToListAsync(); + + return Results.Json(events); + }); return group; } } diff --git a/WebApp/ts/calendar.ts b/WebApp/ts/calendar.ts new file mode 100644 index 0000000..9960864 --- /dev/null +++ b/WebApp/ts/calendar.ts @@ -0,0 +1,39 @@ +async function getRegisteredEvents(): Promise { + const res = await fetch("/api/events/registered"); + if (!res.ok) throw new Error("Couldn't load joined events"); + + const data = await res.json(); + return data.map((ev: any) => ({ + title: ev.title, + start: ev.eventDate, + url: `/view.html?event=${ev.eventId}` + })); +} + + +document.addEventListener("DOMContentLoaded", async () => { + const calendarEl = document.getElementById("calendar") as HTMLElement; + if (!calendarEl) return; + + const events = await getRegisteredEvents(); + + const calendar = new (window as any).FullCalendar.Calendar(calendarEl, { + initialView: 'dayGridMonth', + headerToolbar: { + left: 'prev,next today', + center: 'title', + right: 'dayGridMonth,timeGridWeek,listWeek' + }, + themeSystem: 'bootstrap5', + events: events, + eventClick: function (info: any) { + if (info.event.url) { + window.location.href = info.event.url; + info.jsEvent.preventDefault(); + } + } + }); + + calendar.render(); +}); + diff --git a/WebApp/ts/eventCreate.js b/WebApp/ts/eventCreate.js deleted file mode 100644 index 2013e47..0000000 --- a/WebApp/ts/eventCreate.js +++ /dev/null @@ -1,61 +0,0 @@ -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -console.log("TypeScript działa!"); -function createEvent() { - return __awaiter(this, void 0, void 0, function* () { - // Pobieranie danych z formularza - const title = document.getElementById('title').value; - const location = document.getElementById('location').value; - const description = document.getElementById('description').value; - const eventDateRaw = document.getElementById('eventDate').value; - const organisationIdRaw = document.getElementById('organisationId').value; - // Walidacja prostych pól - if (!title || !location || !eventDateRaw || !organisationIdRaw) { - alert("Uzupełnij wszystkie wymagane pola!"); - return; - } - const eventDate = new Date(eventDateRaw).toISOString(); - const organisationId = parseInt(organisationIdRaw); - const payload = { - title, - location, - description, - eventDate, - organisationId - }; - try { - const response = yield fetch('/api/events', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(payload) - }); - if (!response.ok) { - const errorText = yield response.text(); - throw new Error(errorText); - } - alert("Wydarzenie zostało utworzone!"); - window.location.href = "/"; // Przekierowanie do strony głównej - } - catch (error) { - console.error("Błąd podczas tworzenia:", error); - alert("Nie udało się utworzyć wydarzenia: " + error); - } - }); -} -document.addEventListener("DOMContentLoaded", () => { - const saveBtn = document.getElementById("saveBtn"); - if (saveBtn) { - saveBtn.addEventListener("click", (e) => { - e.preventDefault(); - createEvent(); - }); - } -}); diff --git a/WebApp/wwwroot/calendar.html b/WebApp/wwwroot/calendar.html new file mode 100644 index 0000000..16df166 --- /dev/null +++ b/WebApp/wwwroot/calendar.html @@ -0,0 +1,77 @@ + + + + + Kalendarz Wydarzeń + + + + + + + + + +
+ + + + +
+ +
+ + + + +
+
+ + +
+
+
+

Mój Kalendarz

+
+
+
+
+
+ + + + + + + diff --git a/WebApp/wwwroot/index.html b/WebApp/wwwroot/index.html index 9dd961e..ac98f43 100644 --- a/WebApp/wwwroot/index.html +++ b/WebApp/wwwroot/index.html @@ -28,7 +28,7 @@
- +
Calendar
diff --git a/WebApp/wwwroot/js/calendar.js b/WebApp/wwwroot/js/calendar.js new file mode 100644 index 0000000..d9c1045 --- /dev/null +++ b/WebApp/wwwroot/js/calendar.js @@ -0,0 +1,46 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +function getRegisteredEvents() { + return __awaiter(this, void 0, void 0, function* () { + const res = yield fetch("/api/events/registered"); + if (!res.ok) + throw new Error("Couldn't load joined events"); + const data = yield res.json(); + return data.map((ev) => ({ + title: ev.title, + start: ev.eventDate, + url: `/view.html?event=${ev.eventId}` + })); + }); +} +document.addEventListener("DOMContentLoaded", () => __awaiter(void 0, void 0, void 0, function* () { + const calendarEl = document.getElementById("calendar"); + if (!calendarEl) + return; + const events = yield getRegisteredEvents(); + const calendar = new window.FullCalendar.Calendar(calendarEl, { + initialView: 'dayGridMonth', + headerToolbar: { + left: 'prev,next today', + center: 'title', + right: 'dayGridMonth,timeGridWeek,listWeek' + }, + themeSystem: 'bootstrap5', + events: events, + eventClick: function (info) { + if (info.event.url) { + window.location.href = info.event.url; + info.jsEvent.preventDefault(); + } + } + }); + calendar.render(); +})); diff --git a/WebApp/wwwroot/modify.html b/WebApp/wwwroot/modify.html index 15455ad..6067d2a 100644 --- a/WebApp/wwwroot/modify.html +++ b/WebApp/wwwroot/modify.html @@ -31,7 +31,7 @@
- +
Calendar
diff --git a/WebApp/wwwroot/view.html b/WebApp/wwwroot/view.html index 0c56a6c..1165d8b 100644 --- a/WebApp/wwwroot/view.html +++ b/WebApp/wwwroot/view.html @@ -30,7 +30,7 @@