Files
csharp-uebung/FahrzeugeMVC/Controllers/FahrzeugController.cs

62 lines
1.7 KiB
C#

using FahrzeugDatenBank;
using FahrzeugeMVC.Models;
using Microsoft.AspNetCore.Mvc;
using System.Reflection;
namespace FahrzeugeMVC.Controllers;
public class FahrzeugController : Controller
{
public IActionResult Index()
{
string connectionString = this.GetConnectionString();
var respsitory = new FahrzeugRepository(connectionString);
List<FahrzeugDTO>? fahrzeugs = respsitory.HoleAlleFahrzeuge();
var model = new FahrzeugListeModel(fahrzeugs);
return View(model);
}
public string GetConnectionString()
{
return "Server=localhost;User ID=admin;Password=admin;Database=FahrzeugDB";
}
[HttpGet]
public IActionResult Einfuegen()
{
var model = new FahrzeugEinfugenModel();
return View(model);
}
[HttpPost]
public IActionResult Einfuegen(FahrzeugEinfugenModel model)
{
if (ModelState.IsValid && !string.IsNullOrEmpty(model.Name) && !string.IsNullOrEmpty(model.Type))
{
string connectionString = this.GetConnectionString();
var repository = new FahrzeugRepository(connectionString);
repository.FuegeFahrzeugEin(model.Name, model.Type);
return RedirectToAction(nameof(Index));
}
else
{
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));
}
}