48 lines
1.3 KiB
C#
48 lines
1.3 KiB
C#
using FahrzeugDatenBank;
|
|
using FahrzeugeMVC.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|