diff --git a/FahrzeugDatenBank/FahrzeugRepository.cs b/FahrzeugDatenBank/FahrzeugRepository.cs index 0fbd40a..52a91ca 100644 --- a/FahrzeugDatenBank/FahrzeugRepository.cs +++ b/FahrzeugDatenBank/FahrzeugRepository.cs @@ -45,4 +45,15 @@ public class FahrzeugRepository kommando.Parameters.AddWithValue("@fahrzeug_typ", fahrzeugTyp); kommando.ExecuteNonQuery(); } + + public void LoescheFahrzeug(int id) + { + using var datenbankVerbindung = new MySqlConnection(_connectionString); + datenbankVerbindung.Open(); + + const string query = "DELETE FROM fahrzeuge WHERE id = @fahrzeug_id;"; + using var kommando = new MySqlCommand(query, datenbankVerbindung); + kommando.Parameters.AddWithValue("@fahrzeug_id", id); + kommando.ExecuteNonQuery(); + } } diff --git a/FahrzeugeMVC/Controllers/FahrzeugController.cs b/FahrzeugeMVC/Controllers/FahrzeugController.cs index 9a514bb..4f56dc8 100644 --- a/FahrzeugeMVC/Controllers/FahrzeugController.cs +++ b/FahrzeugeMVC/Controllers/FahrzeugController.cs @@ -1,6 +1,7 @@ using FahrzeugDatenBank; using FahrzeugeMVC.Models; using Microsoft.AspNetCore.Mvc; +using System.Reflection; namespace FahrzeugeMVC.Controllers; @@ -44,4 +45,17 @@ public class FahrzeugController : Controller return View(model); } } + + [HttpGet] + public IActionResult Loesche(int id) + { + if (ModelState.IsValid) + { + string connectionString = this.GetConnectionString(); + var repository = new FahrzeugRepository(connectionString); + repository.LoescheFahrzeug(id); + } + + return RedirectToAction(nameof(Index)); + } } diff --git a/FahrzeugeMVC/Views/Fahrzeug/Index.cshtml b/FahrzeugeMVC/Views/Fahrzeug/Index.cshtml index 06ffd4f..b8b148e 100644 --- a/FahrzeugeMVC/Views/Fahrzeug/Index.cshtml +++ b/FahrzeugeMVC/Views/Fahrzeug/Index.cshtml @@ -17,6 +17,9 @@