commit 80e8417c6c672a67e34963cc1d6f9c3cb81c3dc0
parent ad26e7f91b70cf06489e1600652587f8d5b25a0b
Author: William Lindholm <william_lindholm@outlook.com>
Date: Sun, 15 Oct 2023 01:13:08 +0200
Added Operations page.
Diffstat:
9 files changed, 198 insertions(+), 5 deletions(-)
diff --git a/MVC-databaskonstruktion/Controllers/IncidentsController.cs b/MVC-databaskonstruktion/Controllers/IncidentsController.cs
@@ -18,6 +18,7 @@ namespace MVC_databaskonstruktion.Controllers
public IActionResult Index(string IncidentName = "")
{
ViewBag.Table = _incidentsModel.GetIncidents(IncidentName);
+ ViewBag.Modal = _incidentsModel.CreateIncidentModal();
return View();
}
@@ -52,5 +53,33 @@ namespace MVC_databaskonstruktion.Controllers
return RedirectToAction("Index");
}
+
+ public IActionResult Create(string IncidentName, int IncidentNumber, string RegionName, int Terrain, string Location)
+ {
+ try
+ {
+ _incidentsModel.CreateIncident(IncidentName, IncidentNumber, RegionName, Terrain, Location);
+ }
+ catch (MySqlException e)
+ {
+ switch (e.Number)
+ {
+ case 1062:
+ TempData["ErrorMessage"] = "Incident already exists!";
+ break;
+ case 1406:
+ TempData["ErrorMessage"] = "Data too long!";
+ break;
+ case 3819:
+ TempData["ErrorMessage"] = "Invalid Data!";
+ break;
+ default:
+ TempData["ErrorMessage"] = $"Something went wrong: {e.Number}";
+ break;
+ }
+ }
+
+ return RedirectToAction("Index");
+ }
}
}
diff --git a/MVC-databaskonstruktion/Controllers/OperationsController.cs b/MVC-databaskonstruktion/Controllers/OperationsController.cs
@@ -0,0 +1,51 @@
+using Microsoft.AspNetCore.Mvc;
+using MVC_databaskonstruktion.Models;
+using MySql.Data.MySqlClient;
+using System.Numerics;
+
+namespace MVC_databaskonstruktion.Controllers
+{
+ public class OperationsController : Controller
+ {
+ private readonly IConfiguration _configuration;
+ private OperationsModel _operationsModel { get; set; }
+
+ public OperationsController(IConfiguration configuration)
+ {
+ this._configuration = configuration;
+ _operationsModel = new OperationsModel(_configuration);
+ }
+
+ public IActionResult Index()
+ {
+ ViewBag.Table = _operationsModel.GetOperations();
+
+ return View();
+ }
+
+ public IActionResult Delete(string table, string IncidentName, string IncidentNumber, string OperationName, DateTime StartDate)
+ {
+ try
+ {
+ _operationsModel.DeleteOperation(table, IncidentName, IncidentNumber, OperationName, StartDate);
+ }
+ 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
@@ -1,4 +1,5 @@
using MVC_databaskonstruktion.Utils;
+using System.Runtime.InteropServices;
namespace MVC_databaskonstruktion.Models
{
@@ -42,5 +43,36 @@ namespace MVC_databaskonstruktion.Models
.SetDataTable(_databaseRepository.GetTable($"SELECT * FROM Operation WHERE IncidentName = '{IncidentName}' AND IncidentNumber = '{IncidentNumber}';"))
.Build();
}
+
+ public ModalContext CreateIncidentModal()
+ {
+ var TerrainSelection = _databaseRepository.GetColumnAsDropdown("SELECT TerrainCode FROM Terrain;");
+
+ var modalBuilder = new ModalBuilder()
+ .SetTitle("Create Incident")
+ .SetIdentifier("createIncidentModal")
+ .SetAction("Create", "Incidents")
+ .AddInput("IncidentName", "IncidentName", "normal", "IncidentName")
+ .AddInput("IncidentNumber", "IncidentNumber", "normal", "IncidentNumber")
+ .AddInput("RegionName", "RegionName", "normal", "RegionName")
+ .AddInput("Terrain", "Terrain", "dropdown", "", TerrainSelection)
+ .AddInput("Location", "Location", "normal", "Location");
+
+ return modalBuilder.Build();
+ }
+
+ public void CreateIncident(string IncidentName, int IncidentNumber, string RegionName, int Terrain, string Location)
+ {
+ var IncidentData = new List<KeyValuePair<string, object>>
+ {
+ new KeyValuePair<string, object>("IncidentName", IncidentName),
+ new KeyValuePair<string, object>("IncidentNumber", IncidentNumber),
+ new KeyValuePair<string, object>("RegionName", RegionName),
+ new KeyValuePair<string, object>("Terrain", Terrain),
+ new KeyValuePair<string, object>("Location", Location)
+ };
+
+ _databaseRepository.CreateRow("Incident", IncidentData);
+ }
}
}
diff --git a/MVC-databaskonstruktion/Models/OperationsModel.cs b/MVC-databaskonstruktion/Models/OperationsModel.cs
@@ -0,0 +1,48 @@
+using MVC_databaskonstruktion.Utils;
+
+namespace MVC_databaskonstruktion.Models
+{
+ public class OperationsModel
+ {
+
+ private IConfiguration _configuration { get; set; }
+ private DatabaseRepository _databaseRepository { get; set; }
+ private TableObjectBuilder _tableBuilder { get; set; }
+
+ public OperationsModel(IConfiguration configuration)
+ {
+ _configuration = configuration;
+ _databaseRepository = new DatabaseRepository(_configuration);
+ _tableBuilder = new TableObjectBuilder()
+ .SetPrimaryKeys(new List<string> { "IncidentName", "IncidentNumber", "OperationName", "StartDate" })
+ .SetDeleteTable("Operation");
+ }
+
+ public TableObject GetOperations()
+ {
+ return _tableBuilder
+ .SetDataTable(_databaseRepository.GetTable("Operation"))
+ .Build();
+ }
+
+ public TableObject GetOperations(string IncidentName, int IncidentNumber)
+ {
+ return _tableBuilder
+ .SetDataTable(_databaseRepository.GetTable($"SELECT * FROM Operation WHERE IncidentName = '{IncidentName}' AND IncidentNumber = '{IncidentNumber}';"))
+ .Build();
+ }
+
+ public void DeleteOperation(string table, string IncidentName, string IncidentNumber, string OperationName, DateTime StartDate)
+ {
+ List<KeyValuePair<string, string>> conditions = new List<KeyValuePair<string, string>>
+ {
+ new KeyValuePair<string, string>("IncidentName", IncidentName),
+ new KeyValuePair<string, string>("IncidentNumber", IncidentNumber),
+ new KeyValuePair<string, string>("OperationName", OperationName),
+ new KeyValuePair<string, string>("StartDate", StartDate.ToString("yyyy-M-d"))
+ };
+
+ _databaseRepository.DeleteRow(table, conditions);
+ }
+ }
+}
diff --git a/MVC-databaskonstruktion/Utils/DatabaseRepository.cs b/MVC-databaskonstruktion/Utils/DatabaseRepository.cs
@@ -1,4 +1,5 @@
-using MySql.Data.MySqlClient;
+using Microsoft.AspNetCore.Mvc.Rendering;
+using MySql.Data.MySqlClient;
using System.Data;
namespace MVC_databaskonstruktion.Utils
@@ -29,6 +30,23 @@ namespace MVC_databaskonstruktion.Utils
}
}
+ public List<SelectListItem> GetColumnAsDropdown(string query)
+ {
+ var table = GetTable(query);
+ var dropdown = new List<SelectListItem>();
+
+ foreach (DataRow row in table.Rows)
+ {
+ dropdown.Add(new SelectListItem
+ {
+ Value = row[0].ToString(),
+ Text = row[0].ToString()
+ });
+ }
+
+ return dropdown;
+ }
+
public void CreateRow(string table, List<KeyValuePair<string, object>> data)
{
if (string.IsNullOrEmpty(table) || data == null || data.Count == 0)
diff --git a/MVC-databaskonstruktion/Views/Incidents/Details.cshtml b/MVC-databaskonstruktion/Views/Incidents/Details.cshtml
@@ -4,4 +4,4 @@
}
<h3>Operations in Incident</h3>
-<partial name="_TablePartial" model="ViewBag.Operations" />
+<partial name="_TablePartial" model="ViewBag.Operations" />
+\ No newline at end of file
diff --git a/MVC-databaskonstruktion/Views/Incidents/Index.cshtml b/MVC-databaskonstruktion/Views/Incidents/Index.cshtml
@@ -7,4 +7,9 @@
<hr />
<h3>Incidents</h3>
-<partial name="_TablePartial" model="ViewBag.Table" />
-\ No newline at end of file
+<partial name="_TablePartial" model="ViewBag.Table" />
+<hr />
+
+<label>Create Incident:</label>
+<br />
+<partial name="_ModalPartial" model="ViewBag.Modal" />
+\ No newline at end of file
diff --git a/MVC-databaskonstruktion/Views/Operations/index.cshtml b/MVC-databaskonstruktion/Views/Operations/index.cshtml
@@ -0,0 +1,8 @@
+
+@{
+ ViewData["Title"] = "index";
+}
+
+<h3>Operations</h3>
+<partial name="_TablePartial" model="ViewBag.Table" />
+
diff --git a/MVC-databaskonstruktion/Views/Shared/_Layout.cshtml b/MVC-databaskonstruktion/Views/Shared/_Layout.cshtml
@@ -2,7 +2,8 @@
var navLinks = new List<(string DisplayName, string Controller, string Action)>
{
("Agents", "Agents", "Index"),
- ("Incidents", "Incidents", "Index")
+ ("Incidents", "Incidents", "Index"),
+ ("Operations", "Operations", "Index")
};
}