π‘ NetworkEquipmentController Documentation
π Table of Contents
- Overview & Purpose
- Key Functional Areas
- Dependencies & Technologies
- Class Structure & UML Diagram
- API Endpoints Overview
- View Models & Data Models
- Helper Methods & Utilities
- Error Handling & Return Types
- Sample Code Blocks & Usage Examples
- Conclusion
1. Overview & Purpose
π₯οΈ NetworkEquipmentController is an ASP.NET Core MVC controller responsible for managing Network Equipment assets within the IrbyImaging application. It handles CRUD operations (Create, Read, Update) plus history tracking for network devices. It inherits from BaseController, leveraging common context and configuration provided by the application.
Key Responsibilities:
- Display a searchable list of network equipment
- Show detailed information for a specific asset
- Create new equipment entries
- Edit existing entries
- View audit/history records for each asset
2. Key Functional Areas
| Action | HTTP Verb | Description | Permissions |
|---|---|---|---|
| Index | GET | List & search all network equipment | Read-Only or Admin (Policy) |
| Details | GET | Display details of a single asset by id |
Read-Only or Admin (Policy) |
| Create (GET) | GET | Show form to add new equipment | Admin (Policy) |
| Create (POST) | POST | Process creation form and save to database | Admin (Policy) |
| Edit (GET) | GET | Show form to edit an existing equipment entry | Admin (Policy) |
| Edit (POST) | POST | Process edit form & update record (with history) | Admin (Policy) |
| History | GET | View change history of a specific asset | Read-Only or Admin (Policy) |
3. Dependencies & Technologies
| Component / Namespace | Purpose |
|---|---|
| Microsoft.AspNetCore.Mvc | Base MVC framework, Controller, IActionResult |
| Microsoft.AspNetCore.Authorization | (Commented) Policies for role-based access |
| IrbyImaging.BusinessLogic | IAssetManager β handles business operations |
| IrbyImaging.Services | INetworkEquipmentMapper β maps models & VMs |
| IrbyImaging.DomainClasses | Domain types (e.g., AssetStatus) |
| IrbyImaging.Models | ViewModels (e.g., NetworkEquipmentCreateViewModel) |
| IrbyImaging.Utility | Common utilities and enums (Responses) |
| Entity Framework Core (EF Core) | Database context via IrbyContext |
| Microsoft.Extensions.Configuration | Access to app settings (IConfiguration) |
4. Class Structure & UML Diagram
classDiagram
BaseController <|-- NetworkEquipmentController
NetworkEquipmentController o-- IAssetManager
NetworkEquipmentController o-- INetworkEquipmentMapper
NetworkEquipmentController o-- IrbyContext
NetworkEquipmentController o-- IConfiguration
IAssetManager <|.. AssetManager
INetworkEquipmentMapper <|.. NetworkEquipmentMapper
Fields & Constructor:
private readonly IAssetManager _manager;
private readonly INetworkEquipmentMapper _mapper;
private readonly IrbyContext _context;
private readonly IConfiguration _configuration;
public NetworkEquipmentController(
IAssetManager manager,
INetworkEquipmentMapper mapper,
IrbyContext context,
IConfiguration configuration
) : base(context, configuration) { ... }
5. API Endpoints Overview
| Route | Verb | Parameters | Return Type | Description |
|---|---|---|---|---|
/NetworkEquipment |
GET | search (string), status (enum) |
ViewResult |
List & filter network equipment |
/NetworkEquipment/Details/{id} |
GET | id (Guid) |
ViewResult |
Show details of one equipment record |
/NetworkEquipment/Create |
GET | β | ViewResult |
Render creation form |
/NetworkEquipment/Create |
POST | NetworkEquipmentCreateViewModel |
Redirect/View |
Process and save a new record |
/NetworkEquipment/Edit/{id} |
GET | id (Guid) |
ViewResult |
Render edit form for existing record |
/NetworkEquipment/Edit/{id} |
POST | NetworkEquipmentEditViewModel |
Redirect/View |
Process updates + history log |
/NetworkEquipment/History/{id} |
GET | id (Guid) |
ViewResult |
Show audit trail of changes |
6. View Models & Data Models
| ViewModel | Purpose |
|---|---|
NetworkEquipmentListViewModel |
Data for Index view |
NetworkEquipmentDetailViewModel |
Data for Details view |
NetworkEquipmentCreateViewModel |
Binding & validation for Create form |
NetworkEquipmentEditViewModel |
Binding & validation for Edit form |
NetworkEquipmentHistoryListViewModel |
Audit entries + equipment summary in History |
Mapped domain entity:
public class NetworkEquipment
{
public Guid Id { get; set; }
public string HostName { get; set; }
public string Model { get; set; }
public AssetStatus Status { get; set; }
// ...other props...
}
7. Helper Methods & Utilities
All heavyβlifting is delegated to:
-
IAssetManager(manager)
β’GetNetworkEquipments_Async(string search)
β’GetNetworkEquipmentById_Async(Guid? id)
β’AddNewAssetWithHistory_Async(NetworkEquipment, string user)
β’UpdateAssetAndHistory_Async(NetworkEquipment, string user, string reason)
β’GetHistoryByAssetId_Async(Guid? id) -
INetworkEquipmentMapper(mapper)
β’GenerateNetworkEquipmentListViewModel(...)
β’GenerateNetworkEquipmentDetailViewModel(...)
β’GenerateNetworkEquipmentCreateViewModel()
β’CreateViewModelToNetworkEquipment(...)
β’NetworkEquipmentToEditViewModel(...)
β’EditViewModelToNetworkEquipment(...)
β’GenerateNetworkEquipmentHistoryListViewModel(...)
Common Enums & Utilities:
public enum Responses { Success, Failure }
public enum AssetStatus { Unknown, Active, Inactive, Pending }
8. Error Handling & Return Types
if (id == null) return NotFound(); // 404
if (!ModelState.IsValid) return View(vm); // Return same view with validation errors
(var response, var message) = await _manager.AddNewAssetWithHistory_Async(...);
if (response == Responses.Failure)
{
newVm.ErrorMessage = message;
return View(newVm); // Show error message on form
}
return RedirectToAction(nameof(Index)); // 302 to Index on success
NotFound()returns 404View(viewModel)returns 200 with bound modelRedirectToAction(...)returns 302
9. Sample Code Blocks & Usage Examples
9.1 Dependency Injection (Startup.cs)
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<IrbyContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("Default")));
services.AddScoped<IAssetManager, AssetManager>();
services.AddScoped<INetworkEquipmentMapper, NetworkEquipmentMapper>();
// ...authorization policies...
}
9.2 Calling the Index Action
curl "https://yourdomain.com/NetworkEquipment?search=Router&status=Active"
9.3 Simplified Razor Snippet (Index.cshtml)
@model NetworkEquipmentListViewModel
<h2>Network Equipment</h2>
<form method="get">
<input type="text" name="search" value="@Model.SearchTerm" placeholder="Search..." />
<select name="status">
@foreach(var s in Enum.GetValues(typeof(AssetStatus)))
{
<option value="@s">@s</option>
}
</select>
<button type="submit">π Search</button>
</form>
<table>
<thead>
<tr><th>Name</th><th>Model</th><th>Status</th><th>Actions</th></tr>
</thead>
<tbody>
@foreach(var item in Model.Items)
{
<tr>
<td>@item.HostName</td>
<td>@item.Model</td>
<td>@item.Status</td>
<td>
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="History" asp-route-id="@item.Id">π History</a>
</td>
</tr>
}
</tbody>
</table>
10. Conclusion
π The NetworkEquipmentController encapsulates all user interactions related to network equipment assets. By delegating business logic to IAssetManager and mapping concerns to INetworkEquipmentMapper, the controller remains lean and focused on HTTP concerns, input validation, and view rendering. This layered design promotes separation of concerns, testability, and maintainability throughout the IrbyImaging solution.
Feel free to extend authorization policies, enhance view models, or plug in new dataβsourcesβall without touching the core controller logic!
Happy coding! π