From 470ce72b6a2da833a7798e60dbe82b6d137db48d Mon Sep 17 00:00:00 2001 From: Witkopawel Date: Wed, 30 Apr 2025 12:40:59 +0200 Subject: [PATCH] Wersja probna powinno cos dzialac --- WebApp/Controllers/EventApiController.cs | 89 ++++++++++++++++++++++++ WebApp/Controllers/EventsController.cs | 50 ------------- WebApp/Program.cs | 1 + WebApp/Views/Events/Create.cshtml | 41 ----------- WebApp/Views/Events/Create.cshtml.cs | 12 ---- WebApp/ts/eventDelete.ts | 33 ++++----- WebApp/ts/eventList.ts | 41 +++++++++++ WebApp/wwwroot/create.html | 51 ++++++++++++++ WebApp/wwwroot/css/style.css | 37 ++++++++++ WebApp/wwwroot/index.html | 25 +++++++ WebApp/wwwroot/js/eventDelete.js | 44 ++++++------ WebApp/wwwroot/js/eventList.js | 49 +++++++++++++ 12 files changed, 332 insertions(+), 141 deletions(-) create mode 100644 WebApp/Controllers/EventApiController.cs delete mode 100644 WebApp/Controllers/EventsController.cs delete mode 100644 WebApp/Views/Events/Create.cshtml delete mode 100644 WebApp/Views/Events/Create.cshtml.cs create mode 100644 WebApp/ts/eventList.ts create mode 100644 WebApp/wwwroot/create.html create mode 100644 WebApp/wwwroot/css/style.css create mode 100644 WebApp/wwwroot/index.html create mode 100644 WebApp/wwwroot/js/eventList.js diff --git a/WebApp/Controllers/EventApiController.cs b/WebApp/Controllers/EventApiController.cs new file mode 100644 index 0000000..6ac3d51 --- /dev/null +++ b/WebApp/Controllers/EventApiController.cs @@ -0,0 +1,89 @@ +using Microsoft.AspNetCore.Mvc; +using System.Linq.Expressions; +using System.Reflection.Metadata; +using WebApp.Data; +using WebApp.Entities; + +namespace WebApp.Controllers.Api +{ + [ApiController] + [Route("api/events")] + public class EventsApiController : ControllerBase + { + private readonly ApplicationDbContext _context; + + public EventsApiController(ApplicationDbContext context) + { + _context = context; + } + + // GET: /api/events + [HttpGet] + public IActionResult GetAll() + { + var events = _context.Events.ToList(); + return Ok(events); + } + + // GET: /api/events/5 + [HttpGet("{id}")] + public IActionResult GetById(int id) + { + var ev = _context.Events.Find(id); + if (ev == null) + return NotFound(); + + return Ok(ev); + } + + // POST: /api/events + [HttpPost] + public IActionResult Create([FromBody] Event ev) + { + if (!ModelState.IsValid) + return BadRequest(ModelState); + + ev.EventDate = DateTime.SpecifyKind(ev.EventDate, DateTimeKind.Utc); + + _context.Events.Add(ev); + _context.SaveChanges(); + + return CreatedAtAction(nameof(GetById), new { id = ev.EventId }, ev); + } + + // PUT: /api/events/5 + [HttpPut("{id}")] + public IActionResult Update(int id, [FromBody] Event updated) + { + if (id != updated.EventId) + return BadRequest("ID w URL nie zgadza się z obiektem."); + + var ev = _context.Events.Find(id); + if (ev == null) + return NotFound(); + + ev.Title = updated.Title; + ev.Description = updated.Description; + ev.Location = updated.Location; + ev.EventDate = updated.EventDate; + ev.OrganisationId = updated.OrganisationId; + + _context.SaveChanges(); + return NoContent(); + } + + // DELETE: /api/events/5 + [HttpDelete("{id}")] + public IActionResult Delete(int id) + { + var ev = _context.Events.Find(id); + if (ev == null) + return NotFound(); + + _context.Events.Remove(ev); + _context.SaveChanges(); + + return NoContent(); + } + } +} diff --git a/WebApp/Controllers/EventsController.cs b/WebApp/Controllers/EventsController.cs deleted file mode 100644 index 1672365..0000000 --- a/WebApp/Controllers/EventsController.cs +++ /dev/null @@ -1,50 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using WebApp.Data; -using WebApp.Entities; - -namespace WebApp.Controllers -{ - public class EventsController : Controller - { - private readonly ApplicationDbContext _context; - - public EventsController(ApplicationDbContext context) - { - _context = context; - } - - public IActionResult Create() - { - return View(); - } - - [HttpPost] - [ValidateAntiForgeryToken] - public async Task Create(Event ev) - { - if (ModelState.IsValid) - { - ev.EventDate = DateTime.SpecifyKind(ev.EventDate, DateTimeKind.Utc); - _context.Events.Add(ev); - await _context.SaveChangesAsync(); - - return RedirectToAction("Index", "Home"); - } - return View(ev); - } - - [HttpPost] - [ValidateAntiForgeryToken] - public async Task Delete(int id) - { - var ev = await _context.Events.FindAsync(id); - if (ev != null) - { - _context.Events.Remove(ev); - await _context.SaveChangesAsync(); - } - return RedirectToAction("Index", "Home"); - } - - } -} diff --git a/WebApp/Program.cs b/WebApp/Program.cs index b02fc6c..2ff8e98 100644 --- a/WebApp/Program.cs +++ b/WebApp/Program.cs @@ -30,6 +30,7 @@ else } app.UseHttpsRedirection(); +app.UseDefaultFiles(); app.UseStaticFiles(); app.UseRouting(); diff --git a/WebApp/Views/Events/Create.cshtml b/WebApp/Views/Events/Create.cshtml deleted file mode 100644 index cc034d3..0000000 --- a/WebApp/Views/Events/Create.cshtml +++ /dev/null @@ -1,41 +0,0 @@ -@model WebApp.Entities.Event - -@{ - ViewData["Title"] = "Create Event"; -} - -

Create Event

- -
-
- - - -
-
- - - -
-
- - -
-
- - - -
-
- - - -
- -
- -@section Scripts { - @{ - await Html.RenderPartialAsync("_ValidationScriptsPartial"); - } -} diff --git a/WebApp/Views/Events/Create.cshtml.cs b/WebApp/Views/Events/Create.cshtml.cs deleted file mode 100644 index 9a8d4fa..0000000 --- a/WebApp/Views/Events/Create.cshtml.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.RazorPages; - -namespace WebApp.Views.Events -{ - public class CreateModel : PageModel - { - public void OnGet() - { - } - } -} diff --git a/WebApp/ts/eventDelete.ts b/WebApp/ts/eventDelete.ts index a16246e..82eb724 100644 --- a/WebApp/ts/eventDelete.ts +++ b/WebApp/ts/eventDelete.ts @@ -1,24 +1,25 @@ document.addEventListener("DOMContentLoaded", () => { - const deleteButtons = document.querySelectorAll(".delete-btn"); + document.body.addEventListener("click", async (e) => { + const target = e.target as HTMLElement; - deleteButtons.forEach(button => { - button.addEventListener("click", async () => { - const id = (button as HTMLButtonElement).dataset.id; - if (!id) return; + if (!target.matches(".delete-btn")) return; - const confirmed = confirm("Na pewno chcesz usunąć to wydarzenie?"); - if (!confirmed) return; + const id = target.getAttribute("data-id"); + if (!id) return; - const response = await fetch(`/api/events/${id}`, { - method: "DELETE" - }); + const confirmed = confirm("Na pewno chcesz usunąć to wydarzenie?"); + if (!confirmed) return; - if (response.ok) { - const row = button.closest("tr"); - if (row) row.remove(); - } else { - alert("Nie udało się usunąć wydarzenia."); - } + const response = await fetch(`/api/events/${id}`, { + + method: "DELETE" }); + + if (response.ok) { + const card = target.closest(".col"); + if (card) card.remove(); + } else { + alert("Nie udało się usunąć wydarzenia."); + } }); }); diff --git a/WebApp/ts/eventList.ts b/WebApp/ts/eventList.ts new file mode 100644 index 0000000..5149baa --- /dev/null +++ b/WebApp/ts/eventList.ts @@ -0,0 +1,41 @@ +document.addEventListener("DOMContentLoaded", async () => { + const container = document.getElementById("eventList"); + if (!container) return; + + try { + const res = await fetch("/api/events"); + if (!res.ok) throw new Error("Błąd pobierania wydarzeń"); + + const events = await res.json(); + + if (events.length === 0) { + container.innerHTML = "

Brak wydarzeń do wyświetlenia.

"; + return; + } + + for (const ev of events) { + const card = document.createElement("div"); + card.className = "col"; + card.innerHTML = ` +
+
+
${ev.title}
+

+ Miejsce: ${ev.location}
+ Data: ${new Date(ev.eventDate).toLocaleString()}
+ Organizacja ID: ${ev.organisationId} +

+ +
+
+ `; + + container.appendChild(card); + } + } catch (err) { + container.innerHTML = `

Błąd ładowania danych.

`; + console.error(err); + } +}); diff --git a/WebApp/wwwroot/create.html b/WebApp/wwwroot/create.html new file mode 100644 index 0000000..3ab5d52 --- /dev/null +++ b/WebApp/wwwroot/create.html @@ -0,0 +1,51 @@ + + + + + Nowe wydarzenie + + + + + + + +
+

Utwórz wydarzenie

+ +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+ +
+ + + +
+ + + + + + + + diff --git a/WebApp/wwwroot/css/style.css b/WebApp/wwwroot/css/style.css new file mode 100644 index 0000000..83783d3 --- /dev/null +++ b/WebApp/wwwroot/css/style.css @@ -0,0 +1,37 @@ +.button { + border-radius: 4px; + background-color: #f4511e; + color: #FFFFFF; + text-align: center; + font-size: 28px; + padding: 20px 30px; + cursor: pointer; + position: relative; + transition: all 0.5s; + text-transform: uppercase; + border: none; +} + + .button:hover { + background-color: rgb(189, 0, 0); + } + + .button span:first-child { + padding-left: 10px; + transition: all 0.5s; + } + + .button:hover span:first-child { + padding-left: 0px; + } + + .button span:last-child { + opacity: 0; + transition: all 0.5s; + padding-left: 0px; + } + + .button:hover span:last-child { + opacity: 1; + padding-left: 10px; + } diff --git a/WebApp/wwwroot/index.html b/WebApp/wwwroot/index.html new file mode 100644 index 0000000..94cade8 --- /dev/null +++ b/WebApp/wwwroot/index.html @@ -0,0 +1,25 @@ + + + + + Lista wydarzeń + + + + + +
+
+

📅 Wydarzenia

+ + Dodaj nowe +
+ +
+
+
+ + + + + + diff --git a/WebApp/wwwroot/js/eventDelete.js b/WebApp/wwwroot/js/eventDelete.js index d8dc896..43b9ee6 100644 --- a/WebApp/wwwroot/js/eventDelete.js +++ b/WebApp/wwwroot/js/eventDelete.js @@ -9,26 +9,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; document.addEventListener("DOMContentLoaded", () => { - const deleteButtons = document.querySelectorAll(".delete-btn"); - deleteButtons.forEach(button => { - button.addEventListener("click", () => __awaiter(void 0, void 0, void 0, function* () { - const id = button.dataset.id; - if (!id) - return; - const confirmed = confirm("Na pewno chcesz usunąć to wydarzenie?"); - if (!confirmed) - return; - const response = yield fetch(`/api/events/${id}`, { - method: "DELETE" - }); - if (response.ok) { - const row = button.closest("tr"); - if (row) - row.remove(); - } - else { - alert("Nie udało się usunąć wydarzenia."); - } - })); - }); + document.body.addEventListener("click", (e) => __awaiter(void 0, void 0, void 0, function* () { + const target = e.target; + if (!target.matches(".delete-btn")) + return; + const id = target.getAttribute("data-id"); + if (!id) + return; + const confirmed = confirm("Na pewno chcesz usunąć to wydarzenie?"); + if (!confirmed) + return; + const response = yield fetch(`/api/events/${id}`, { + method: "DELETE" + }); + if (response.ok) { + const card = target.closest(".col"); + if (card) + card.remove(); + } + else { + alert("Nie udało się usunąć wydarzenia."); + } + })); }); diff --git a/WebApp/wwwroot/js/eventList.js b/WebApp/wwwroot/js/eventList.js new file mode 100644 index 0000000..2262335 --- /dev/null +++ b/WebApp/wwwroot/js/eventList.js @@ -0,0 +1,49 @@ +"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()); + }); +}; +document.addEventListener("DOMContentLoaded", () => __awaiter(void 0, void 0, void 0, function* () { + const container = document.getElementById("eventList"); + if (!container) + return; + try { + const res = yield fetch("/api/events"); + if (!res.ok) + throw new Error("Błąd pobierania wydarzeń"); + const events = yield res.json(); + if (events.length === 0) { + container.innerHTML = "

Brak wydarzeń do wyświetlenia.

"; + return; + } + for (const ev of events) { + const card = document.createElement("div"); + card.className = "col"; + card.innerHTML = ` +
+
+
${ev.title}
+

+ Miejsce: ${ev.location}
+ Data: ${new Date(ev.eventDate).toLocaleString()}
+ Organizacja ID: ${ev.organisationId} +

+ +
+
+ `; + container.appendChild(card); + } + } + catch (err) { + container.innerHTML = `

Błąd ładowania danych.

`; + console.error(err); + } +}));