MVC-databaskonstruktion

Log | Files | Refs | README

commit a82767dceebe3bf2abb45677ac17948896f562d3
parent a0e84ba293e60aa4e0112ea23795f47e02d10e23
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Sat, 14 Oct 2023 22:55:50 +0200

Added Incidents page.

Diffstat:
AMVC-databaskonstruktion/Controllers/IncidentsController.cs | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
AMVC-databaskonstruktion/Models/IncidentsModel.cs | 38++++++++++++++++++++++++++++++++++++++
AMVC-databaskonstruktion/Views/Incidents/Index.cshtml | 11+++++++++++
MMVC-databaskonstruktion/Views/Shared/_Layout.cshtml | 2+-
4 files changed, 102 insertions(+), 1 deletion(-)

diff --git a/MVC-databaskonstruktion/Controllers/IncidentsController.cs b/MVC-databaskonstruktion/Controllers/IncidentsController.cs @@ -0,0 +1,52 @@ +using Microsoft.AspNetCore.Mvc; +using MVC_databaskonstruktion.Models; +using MVC_databaskonstruktion.Utils; +using MySql.Data.MySqlClient; +using System.Diagnostics; + +namespace MVC_databaskonstruktion.Controllers +{ + public class IncidentsController : Controller + { + private readonly IConfiguration _configuration; + private IncidentsModel _incidentsModel { get; set; } + + public IncidentsController(IConfiguration configuration) + { + this._configuration = configuration; + _incidentsModel = new IncidentsModel(_configuration); + } + + public IActionResult Index(string IncidentName = "") + { + ViewBag.Table = _incidentsModel.GetIncidents(IncidentName); + + return View(); + } + + public IActionResult Delete(string table, string incidentName, string incidentNumber) + { + try + { + _incidentsModel.DeleteIncident(table, incidentName, incidentNumber); + } + catch (MySqlException e) + { + switch (e.Number) + { + case 1451: + TempData["ErrorMessage"] = "Foreign Key Constraint Failed!"; + break; + case 1452: + TempData["ErrorMessage"] = "Foreign Key Constraint Failed!"; + break; + default: + TempData["ErrorMessage"] = $"Something went wrong: {e.Number}"; + break; + } + } + + return RedirectToAction("Index"); + } + } +} diff --git a/MVC-databaskonstruktion/Models/IncidentsModel.cs b/MVC-databaskonstruktion/Models/IncidentsModel.cs @@ -0,0 +1,38 @@ +using MVC_databaskonstruktion.Utils; + +namespace MVC_databaskonstruktion.Models +{ + public class IncidentsModel + { + private IConfiguration _configuration { get; set; } + private DatabaseRepository _databaseRepository { get; set; } + private TableObjectBuilder _tableBuilder { get; set; } + + public IncidentsModel(IConfiguration configuration) + { + _configuration = configuration; + _databaseRepository = new DatabaseRepository(_configuration); + _tableBuilder = new TableObjectBuilder() + .SetPrimaryKeys(new List<string> { "IncidentName", "IncidentNumber" }) + .SetDeleteTable("Incident"); + } + + public TableObject GetIncidents(string searchQuery) + { + return _tableBuilder + .SetDataTable(_databaseRepository.GetTable($"SELECT * FROM Incident WHERE IncidentName LIKE '%{searchQuery}%'")) + .Build(); + } + + public void DeleteIncident(string table, string incidentName, string incidentNumber) + { + List<KeyValuePair<string, string>> conditions = new List<KeyValuePair<string, string>> + { + new KeyValuePair<string, string>("IncidentName", incidentName), + new KeyValuePair<string, string>("IncidentNumber", incidentNumber) + }; + + _databaseRepository.DeleteRow(table, conditions); + } + } +} diff --git a/MVC-databaskonstruktion/Views/Incidents/Index.cshtml b/MVC-databaskonstruktion/Views/Incidents/Index.cshtml @@ -0,0 +1,10 @@ +<form action="@Url.Action("index")" method="get" class="mt-1"> + <label>Search for Incident:</label> + <br /> + <input type="text" name="IncidentName" placeholder="Search" class="form-control d-inline-block" style="vertical-align: middle; width: auto;" /> + <button type="submit" class="btn btn-primary ml-2">Search</button> +</form> +<hr /> + +<h3>Incidents</h3> +<partial name="_TablePartial" model="ViewBag.Table" /> +\ No newline at end of file diff --git a/MVC-databaskonstruktion/Views/Shared/_Layout.cshtml b/MVC-databaskonstruktion/Views/Shared/_Layout.cshtml @@ -2,7 +2,7 @@ var navLinks = new List<(string DisplayName, string Controller, string Action)> { ("Agents", "Agents", "Index"), - ("Incidents", "Incident", "Index") + ("Incidents", "Incidents", "Index") }; }