feature: suche
This commit is contained in:
@@ -56,4 +56,33 @@ public class FahrzeugRepository
|
||||
kommando.Parameters.AddWithValue("@fahrzeug_id", id);
|
||||
kommando.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public List<FahrzeugDTO> SucheFahrzeuge(string searchTerm)
|
||||
{
|
||||
using var datenbankVerbindung = new MySqlConnection(_connectionString);
|
||||
datenbankVerbindung.Open();
|
||||
|
||||
const string query = "SELECT id, fahrzeug_name, fahrzeug_typ FROM fahrzeuge WHERE fahrzeug_name LIKE @searchTerm OR fahrzeug_typ LIKE @searchTerm;";
|
||||
|
||||
using var kommando = new MySqlCommand(query, datenbankVerbindung);
|
||||
|
||||
kommando.Parameters.AddWithValue("@searchTerm", "%" + searchTerm + "%");
|
||||
|
||||
using var reader = kommando.ExecuteReader();
|
||||
|
||||
List<FahrzeugDTO> fahrzeugs = new();
|
||||
while (reader.Read())
|
||||
{
|
||||
var fahrzeug = new FahrzeugDTO
|
||||
{
|
||||
Id = reader.GetInt32(0),
|
||||
Name = reader.GetString(1),
|
||||
Typ = reader.GetString(2)
|
||||
};
|
||||
|
||||
fahrzeugs.Add(fahrzeug);
|
||||
}
|
||||
|
||||
return fahrzeugs;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,14 +7,23 @@ namespace FahrzeugeMVC.Controllers;
|
||||
|
||||
public class FahrzeugController : Controller
|
||||
{
|
||||
public IActionResult Index()
|
||||
public IActionResult Index(string searchTerm)
|
||||
{
|
||||
string connectionString = this.GetConnectionString();
|
||||
var respsitory = new FahrzeugRepository(connectionString);
|
||||
List<FahrzeugDTO>? fahrzeugs = respsitory.HoleAlleFahrzeuge();
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,13 @@
|
||||
|
||||
<p>Diese Fahrzeuge befinden sich derzeit in der Datenbank:</p>
|
||||
|
||||
<form asp-action="Index" asp-controller="Fahrzeug" method="get" class="mb-3">
|
||||
<div class="input-group">
|
||||
<input type="text" name="searchTerm" class="form-control" placeholder="Fahrzeug suchen..." />
|
||||
<button class="btn btn-primary" type="submit">Suchen</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<table class="table table-hover mb-0">
|
||||
<thead>
|
||||
<tr>
|
||||
|
||||
Reference in New Issue
Block a user