Dodawanie/Usuwanie

Dodawanie/Usuwanie (nie chce tu przeklinać)
This commit is contained in:
Witkopawel
2025-04-25 05:19:36 +02:00
parent 981236f6aa
commit 9151fbb6ab
6 changed files with 163 additions and 24 deletions

View File

@@ -1,7 +1,7 @@
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.13.35919.96 d17.13 VisualStudioVersion = 17.13.35919.96
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApp", "WebApp\WebApp.csproj", "{D6D76D22-3581-4984-B44E-D1C981976C0B}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApp", "WebApp\WebApp.csproj", "{D6D76D22-3581-4984-B44E-D1C981976C0B}"
EndProject EndProject

View File

@@ -0,0 +1,50 @@
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<IActionResult> Create(Event ev)
{
if (ModelState.IsValid)
{
ev.Date = DateTime.SpecifyKind(ev.Date, DateTimeKind.Utc);
_context.Events.Add(ev);
await _context.SaveChangesAsync();
return RedirectToAction("Index", "Home");
}
return View(ev);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Delete(int id)
{
var ev = await _context.Events.FindAsync(id);
if (ev != null)
{
_context.Events.Remove(ev);
await _context.SaveChangesAsync();
}
return RedirectToAction("Index", "Home");
}
}
}

View File

@@ -1,32 +1,32 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using WebApp.Data;
using WebApp.Models; using WebApp.Models;
namespace WebApp.Controllers public class HomeController : Controller
{ {
public class HomeController : Controller private readonly ILogger<HomeController> _logger;
private readonly ApplicationDbContext _context;
public HomeController(ILogger<HomeController> logger, ApplicationDbContext context)
{ {
private readonly ILogger<HomeController> _logger; _logger = logger;
_context = context;
}
public HomeController(ILogger<HomeController> logger) public IActionResult Index()
{ {
_logger = logger; var events = _context.Events.ToList(); // pobieranie danych z bazy
} return View(events); // przekazanie do widoku
}
public IActionResult Index() public IActionResult Privacy()
{ {
return View(); return View();
} }
public IActionResult Privacy() public IActionResult Error()
{ {
return View(); return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
} }
} }

View File

@@ -0,0 +1,36 @@
@model WebApp.Entities.Event
@{
ViewData["Title"] = "Create Event";
}
<h2>Create Event</h2>
<form asp-action="Create" method="post">
<div class="form-group">
<label asp-for="Name"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Place"></label>
<input asp-for="Place" class="form-control" />
<span asp-validation-for="Place" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Description"></label>
<textarea asp-for="Description" class="form-control"></textarea>
</div>
<div class="form-group">
<label asp-for="Date"></label>
<input asp-for="Date" type="datetime-local" class="form-control" />
<span asp-validation-for="Date" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
@section Scripts {
@{
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
}

View File

@@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace WebApp.Views.Events
{
public class CreateModel : PageModel
{
public void OnGet()
{
}
}
}

View File

@@ -1,8 +1,49 @@
@{ @model List<WebApp.Entities.Event>
@{
ViewData["Title"] = "Home Page"; ViewData["Title"] = "Home Page";
} }
<div class="text-center"> <div class="text-center">
<h1 class="display-4">Welcome</h1> <h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p> <p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
<a href="/Events/Create" class="btn btn-success">Create New Event</a>
</div> </div>
<hr />
<h2 class="mt-4">Lista wydarzeń</h2>
@if (Model.Any())
{
<table class="table table-bordered table-striped mt-3">
<thead>
<tr>
<th>Nazwa</th>
<th>Miejsce</th>
<th>Data</th>
<th>Akcje</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.Name</td>
<td>@item.Place</td>
<td>@item.Date.ToString("yyyy-MM-dd HH:mm")</td>
<td>
<form asp-action="Delete" asp-controller="Events" method="post" style="display:inline;">
<input type="hidden" name="id" value="@item.Id" />
<button type="submit" class="btn btn-danger" onclick="return confirm('Na pewno chcesz usunąć to wydarzenie?')">Usuń</button>
</form>
</td>
</tr>
}
</tbody>
</table>
}
else
{
<p class="mt-3">Brak wydarzeń do wyświetlenia.</p>
}