115 lines
3.2 KiB
C#
115 lines
3.2 KiB
C#
using FahrzeugDatenBank;
|
|
using FahrzeugeMVC.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Reflection;
|
|
|
|
namespace FahrzeugeMVC.Controllers;
|
|
|
|
public class FahrzeugController : Controller
|
|
{
|
|
private readonly IKonfigurationsleser _konfigurationsleser;
|
|
|
|
public FahrzeugController(IKonfigurationsleser konfigurationsleser)
|
|
{
|
|
_konfigurationsleser = konfigurationsleser;
|
|
}
|
|
|
|
public IActionResult Index(string searchTerm)
|
|
{
|
|
string connectionString = this.GetConnectionString();
|
|
var repository = new FahrzeugRepository(connectionString);
|
|
|
|
List<FahrzeugDTO> fahrzeugs;
|
|
|
|
if (!string.IsNullOrEmpty(searchTerm))
|
|
{
|
|
fahrzeugs = repository.SucheFahrzeuge(searchTerm);
|
|
}
|
|
else
|
|
{
|
|
fahrzeugs = repository.HoleAlleFahrzeuge();
|
|
}
|
|
|
|
var model = new FahrzeugListeModel(fahrzeugs);
|
|
return View(model);
|
|
}
|
|
|
|
public string GetConnectionString()
|
|
{
|
|
return _konfigurationsleser.LiesDBVerebindung();
|
|
}
|
|
|
|
[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 Aktualisieren(int id)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
string connectionString = this.GetConnectionString();
|
|
var repository = new FahrzeugRepository(connectionString);
|
|
var fahrzeug = repository.GetFahrzeugByID(id);
|
|
var model = new FahrzeugAktualisierenModel();
|
|
model.Id = fahrzeug.Id;
|
|
model.Type = fahrzeug.Typ;
|
|
model.Name = fahrzeug.Name;
|
|
return View(model);
|
|
}
|
|
else
|
|
{
|
|
return View();
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public IActionResult Aktualisieren(FahrzeugAktualisierenModel model)
|
|
{
|
|
if (ModelState.IsValid && !string.IsNullOrEmpty(model.Name) && !string.IsNullOrEmpty(model.Type))
|
|
{
|
|
string connectionString = this.GetConnectionString();
|
|
var repository = new FahrzeugRepository(connectionString);
|
|
int id = (int)model.Id;
|
|
repository.AktualisiereFahrzeug(id, 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));
|
|
}
|
|
}
|