📑 MonitorsController Documentation
Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Controller Responsibilities
- Routing & API Endpoints
- Key Functional Areas
- 5.1 Index (List)
- 5.2 Details
- 5.3 Create (GET & POST)
- 5.4 Edit (GET & POST)
- 5.5 History
- View Models & Data Flow
- Error Handling & Return Types
- Third‑party Integrations & Utilities
- Sample Usage & Code Snippets
- UML Class Diagram
- Conclusion
🌟 1. Overview & Purpose
The MonitorsController lives under
IrbyImaging/Controllers/MonitorsController.cs
and is part of the IrbyImaging.UI ASP.NET Core MVC application. Its primary function is to provide CRUD (Create, Read, Update) plus historical tracking for Monitor assets in the system.
Key Goals
- Serve HTML views for listing, detailing, creating, editing, and viewing history of monitor assets.
- Coordinate between the AssetManager (business logic), MonitorMapper (DTO ↔ Model mapping), and the Entity Framework IrbyContext.
- Handle input validation, authorization, and routing logic in a consistent way by inheriting from BaseController.
🔧 2. Dependencies & Technologies
| Layer | Technology / Library |
|---|---|
| Framework | ASP.NET Core MVC |
| Data Access | Entity Framework Core (IrbyContext) |
| Business Logic | IAssetManager service |
| Mapping | IMonitorMapper |
| Configuration | Microsoft.Extensions.Configuration |
| Authorization | Microsoft.AspNetCore.Authorization |
| Misc. Utility | Custom IrbyImaging.Utility |
Namespaces Used
using IrbyImaging.BusinessLogic;
using IrbyImaging.Utility;
using IrbyImaging.Models;
using IrbyImaging.Services;
using IrbyImaging.DomainClasses;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
🎯 3. Controller Responsibilities
- List all monitors with optional search and status filters.
- Display details of a single monitor.
- Render/Create forms for adding new monitors.
- Render/Edit forms for updating existing monitors.
- View History of changes for a given monitor asset.
It strictly handles HTTP requests and delegates data operations to services and mapping to mappers.
🚦 4. Routing & API Endpoints
By convention, the routes are under /Monitors/:
| HTTP Method | Route | Action | Description |
|---|---|---|---|
| GET | /Monitors |
Index | List & filter monitors |
| GET | /Monitors/Details/{id} |
Details | Show a monitor’s details |
| GET | /Monitors/Create |
Create 🎨 | Show “add new monitor” form |
| POST | /Monitors/Create |
Create 💾 | Process creation of new monitor |
| GET | /Monitors/Edit/{id} |
Edit ✏️ | Show “edit monitor” form |
| POST | /Monitors/Edit/{id} |
Edit 💾 | Process monitor update |
| GET | /Monitors/History/{id} |
History | Show full change history |
Note: Authorization attributes are commented out but illustrate intent:
[Authorize(Policy = "ReadOnlyOrAdminPolicy")]on the controller[Authorize(Policy = "AdminPolicy")]on Create/Edit actions
🛠️ 5. Key Functional Areas
5.1 Index (List)
public async Task<IActionResult> Index(string search, AssetStatus status)
{
var monitors = await _manager.GetMonitors_Async(search);
var viewModel = _mapper.GenerateMonitorListViewModel(monitors, status);
return View(viewModel);
}
- Purpose: Display a pageable/filterable list of monitors.
- Inputs:
search: Free-text searchstatus:AssetStatusenum for filtering by state
- Process: Calls
GetMonitors_Async, maps toMonitorListViewModel. - Return:
View(viewModel)→ Views/Monitors/Index.cshtml
5.2 Details
public async Task<IActionResult> Details(Guid? id)
{
if (id == null) return NotFound();
var monitor = await _manager.GetMonitorById_Async(id);
if (monitor == null) return NotFound();
var viewModel = _mapper.GenerateMonitorDetailViewModel(monitor);
return View(viewModel);
}
- Purpose: Show detailed information for a single monitor.
- Error Cases: Null
idor no record →404 NotFound. - Mapping: Domain model →
MonitorDetailViewModel.
5.3 Create (GET & POST)
GET Create
public IActionResult Create()
{
var viewModel = _mapper.GenerateMonitorCreateViewModel();
return View(viewModel);
}
- Renders empty form using defaults from
GenerateMonitorCreateViewModel.
POST Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(MonitorCreateViewModel viewModel)
{
if (!ModelState.IsValid)
return View(viewModel);
var monitor = _mapper.CreateViewModelToMonitor(viewModel);
(var response, var message) =
await _manager.AddNewAssetWithHistory_Async(monitor, User.Identity.Name);
if (response == Responses.Failure)
{
var newVM = _mapper.GenerateMonitorCreateViewModel();
newVM.ErrorMessage = message;
return View(newVM);
}
return RedirectToAction(nameof(Index));
}
- Validation:
ModelState.IsValid - Service Call: Adds both asset & history record
- Error Handling: Returns form with
ErrorMessageon failure
5.4 Edit (GET & POST)
GET Edit
public async Task<IActionResult> Edit(Guid? id)
{
if (id == null) return NotFound();
var monitor = await _manager.GetMonitorById_Async(id);
if (monitor == null) return NotFound();
var vm = _mapper.MonitorToEditViewModel(monitor);
return View(vm);
}
POST Edit
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(Guid id, MonitorEditViewModel viewModel)
{
if (id != viewModel.Id) return NotFound();
if (!ModelState.IsValid) return View(viewModel);
var monitor = _mapper.EditViewModelToMonitor(viewModel);
(var response, var message) =
await _manager.UpdateAssetAndHistory_Async(monitor, User.Identity.Name, viewModel.Reason);
if (response == Responses.Failure)
{
var newVM = _mapper.MonitorToEditViewModel(monitor);
newVM.ErrorMessage = message;
return View(newVM);
}
return RedirectToAction(nameof(Index));
}
- Additional Field:
viewModel.Reasonfor history notes - Error Handling: Similar to Create
5.5 History
public async Task<IActionResult> History(Guid? id)
{
if (id == null) return NotFound();
var histories = await _manager.GetHistoryByAssetId_Async(id);
if (histories == null) return NotFound();
var historyVM = _mapper.GenerateMonitorHistoryListViewModel(histories);
var monitor = await _manager.GetMonitorById_Async(id);
if (monitor == null) return NotFound();
historyVM.MonitorsList = _mapper.GenerateMonitorDetailViewModel(monitor);
return View(historyVM);
}
- Purpose: Show a chronological list of changes along with current monitor details
- Combined ViewModel:
MonitorHistoryListViewModel- Embeds
MonitorDetailViewModelinMonitorsList
📦 6. View Models & Data Flow
| Domain Model | ViewModel | Purpose |
|---|---|---|
Monitor |
MonitorListViewModel |
Listing w/ filtering |
Monitor |
MonitorDetailViewModel |
Single-item details display |
Monitor |
MonitorCreateViewModel |
Form data + dropdown defaults |
Monitor |
MonitorEditViewModel |
Pre-filled form + change reason |
History |
MonitorHistoryListViewModel |
List of change records + detail |
Data always flows:
Controller → Mapper → ViewModel → View
ViewModel → Mapper → Domain Model → Manager → Database
🚨 7. Error Handling & Return Types
- 404 (NotFound): Whenever
idis null or record doesn’t exist - Model Validation: Uses
ModelState.IsValidto redisplay form - Service Failure: Service returns
(Responses.Failure, message)tuple- On failure, repopulate form with
ErrorMessage
- On failure, repopulate form with
- Success: Redirect to
Index(PRG pattern)
🔗 8. Third-party Integrations & Utilities
- Entity Framework Core via
IrbyContext - ASP.NET Core Identity (for
User.Identity.Name) - Custom Utility
IrbyImaging.Utility.Responsesenum - AutoMapper‑Style pattern implemented in
IMonitorMapper
🚀 9. Sample Usage & Code Snippets
Dependency Injection (Startup.cs)
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<IrbyContext>(...);
services.AddScoped<IAssetManager, AssetManager>();
services.AddScoped<IMonitorMapper, MonitorMapper>();
services.AddControllersWithViews();
}
Calling the List Endpoint (cURL)
curl "https://localhost:5001/Monitors?search=DELL&status=InUse"
Posting a New Monitor (HTML form)
<form asp-action="Create" method="post">
<input asp-for="SerialNumber" />
<input asp-for="Manufacturer" />
<input type="submit" value="Add Monitor" />
</form>
📐 10. UML Class Diagram
classDiagram
MonitorsController <|-- BaseController
MonitorsController : - IAssetManager _manager
MonitorsController : - IMonitorMapper _mapper
MonitorsController : - IrbyContext _context
MonitorsController : - IConfiguration _configuration
MonitorsController : + Index(string, AssetStatus)
MonitorsController : + Details(Guid?)
MonitorsController : + Create()
MonitorsController : + Create(MonitorCreateViewModel)
MonitorsController : + Edit(Guid?)
MonitorsController : + Edit(Guid, MonitorEditViewModel)
MonitorsController : + History(Guid?)
MonitorsController --> IAssetManager
MonitorsController --> IMonitorMapper
MonitorsController --> IrbyContext
MonitorsController --> IConfiguration
🏁 11. Conclusion
The MonitorsController provides a clean, MVC‑style interface for managing Monitor assets within the application. It leverages:
- Separation of Concerns: Controllers for HTTP/Views, Managers for business logic, Mappers for DTO conversion.
- Robust Error Handling: NotFound responses, ModelState checks, service-level failure captures.
- Consistency: Aligns all asset controllers under a common pattern for ease of maintenance.
With this documentation, you should be able to understand, extend, and integrate the Monitors functionality effectively. Happy coding! 🚀