commit 6933a41eb54e59134cf08d6aaf7a83352a56a4f7
parent a02dfd53bf98991c49d32d49ac34871af97bc02e
Author: William Lindholm <a22willi@student.his.se>
Date: Wed, 11 Oct 2023 09:19:29 +0200
Created TableObjectBuilder to dynamically create tables with advanced parameters.
Diffstat:
4 files changed, 109 insertions(+), 16 deletions(-)
diff --git a/MVC-databaskonstruktion/Controllers/AgentsController.cs b/MVC-databaskonstruktion/Controllers/AgentsController.cs
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using MVC_databaskonstruktion.Models;
+using MVC_databaskonstruktion.Utils;
using System.Data;
namespace MVC_databaskonstruktion.Controllers
@@ -16,13 +17,14 @@ namespace MVC_databaskonstruktion.Controllers
public IActionResult Index()
{
- GetAgents();
+ BuildAgentTables();
return View();
}
- private void GetAgents()
+ private void BuildAgentTables()
{
_agentsModel = new AgentsModel(this._configuration);
+
ViewBag.FieldAgents = _agentsModel.GetFieldAgents();
ViewBag.GroupLeaders = _agentsModel.GetGroupLeaders();
ViewBag.Managers = _agentsModel.GetManagers();
diff --git a/MVC-databaskonstruktion/Models/AgentsModel.cs b/MVC-databaskonstruktion/Models/AgentsModel.cs
@@ -1,39 +1,50 @@
-using MySql.Data.MySqlClient;
-using Org.BouncyCastle.Security;
+using MVC_databaskonstruktion.Utils;
using System.Data;
namespace MVC_databaskonstruktion.Models
{
public class AgentsModel
{
- private IConfiguration _configuration;
- private string connectionString;
- private DatabaseRepository _databaseRepository;
+ private IConfiguration _configuration { get; set;}
+ private DatabaseRepository _databaseRepository { get; set; }
+ private TableObjectBuilder _tableBuilder { get; set;}
public AgentsModel(IConfiguration configuration)
{
_configuration = configuration;
_databaseRepository = new DatabaseRepository(_configuration);
+ _tableBuilder = new TableObjectBuilder();
}
- public DataTable GetAgents()
- {
- return _databaseRepository.GetTable("Agent");
+ public TableObject GetAgents()
+ {
+ return _tableBuilder.SetControllerName("Agents")
+ .SetDeleteTable("Agent")
+ .SetDataTable(_databaseRepository.GetTable("Agent"))
+ .SetPrimaryKeys(new List<string> { "AgentId" })
+ .SetRedirect("Index")
+ .Build();
}
- public DataTable GetFieldAgents()
+ public TableObject GetFieldAgents()
{
- return _databaseRepository.GetTable("FieldAgents");
+ return _tableBuilder
+ .SetDataTable(_databaseRepository.GetTable("FieldAgents"))
+ .Build();
}
- public DataTable GetGroupLeaders()
+ public TableObject GetGroupLeaders()
{
- return _databaseRepository.GetTable("GroupLeaders");
+ return _tableBuilder
+ .SetDataTable(_databaseRepository.GetTable("GroupLeaders"))
+ .Build();
}
- public DataTable GetManagers()
+ public TableObject GetManagers()
{
- return _databaseRepository.GetTable("Managers");
+ return _tableBuilder
+ .SetDataTable(_databaseRepository.GetTable("Managers"))
+ .Build();
}
}
}
diff --git a/MVC-databaskonstruktion/Utils/TableObject.cs b/MVC-databaskonstruktion/Utils/TableObject.cs
@@ -0,0 +1,13 @@
+using System.Data;
+
+namespace MVC_databaskonstruktion.Utils
+{
+ public struct TableObject
+ {
+ public DataTable DataSet;
+ public string ControllerName;
+ public string? DeleteTable;
+ public List<string>? PrimaryKeys;
+ public string? Redirect;
+ }
+}
diff --git a/MVC-databaskonstruktion/Utils/TableObjectBuilder.cs b/MVC-databaskonstruktion/Utils/TableObjectBuilder.cs
@@ -0,0 +1,67 @@
+using System.Data;
+
+namespace MVC_databaskonstruktion.Utils
+{
+ public class TableObjectBuilder
+ {
+ private DataTable _dataTable;
+ private string _controllerName;
+ private string? _deleteTable;
+ private List<string>? _primaryKeys;
+ private string? _redirect;
+
+ public TableObjectBuilder(DataTable dataTable)
+ {
+ _dataTable = dataTable;
+ }
+ public TableObjectBuilder()
+ {
+ //empty constructor if you want to set the DataTable later
+ }
+
+ public TableObjectBuilder SetControllerName(string controllerName)
+ {
+ _controllerName = controllerName;
+ return this;
+ }
+
+ public TableObjectBuilder SetPrimaryKeys(List<string> primaryKeys)
+ {
+ _primaryKeys = primaryKeys;
+ return this;
+ }
+
+ public TableObjectBuilder SetDeleteTable(string tableName)
+ {
+ _deleteTable = tableName;
+ return this;
+ }
+
+ public TableObjectBuilder SetDataTable(DataTable dataTable)
+ {
+ _dataTable = dataTable;
+ return this;
+ }
+
+ public TableObjectBuilder SetRedirect(string redirect)
+ {
+ _redirect = redirect;
+ return this;
+ }
+
+ public TableObject Build()
+ {
+ if (_dataTable == null)
+ throw new NullReferenceException("DataTable is null.");
+
+ return new TableObject
+ {
+ DataSet = this._dataTable,
+ ControllerName = this._controllerName,
+ DeleteTable = this._deleteTable,
+ PrimaryKeys = this._primaryKeys,
+ Redirect = this._redirect
+ };
+ }
+ }
+}