🖥️ IrbyImaging.Controllers.ComputersController Documentation
📑 Table of Contents
- Overview & Purpose
- Key Functional Areas
- Dependencies & Technologies
- API Endpoints Overview
- View Models & Data Models
- Helper Methods & Utilities
- Error Handling & Return Types
- Sample Code Blocks & Usage Examples
- UML Diagram
- Conclusion
1. Overview & Purpose
ComputersController is an ASP.NET Core MVC controller within the IrbyImaging.Controllers namespace responsible for managing Computer assets. It extends from BaseController and offers the standard CRUD (Create, Read, Update, Delete) operations plus a History view.
-
Primary Responsibilities
- List all computers
- Show details of a single computer
- Create new computer assets
- Edit existing computer assets
- Display change history of a computer asset
-
Design Patterns
- Dependency Injection for service abstractions (
IAssetManager,IComputerMapper,IrbyContext,IConfiguration) - ViewModel Mapping to decouple domain objects from UI models
- Action Filters (
[ValidateAntiForgeryToken]) for security
- Dependency Injection for service abstractions (
2. Key Functional Areas
| Area | Description |
|---|---|
| Index (List) | Displays searchable list of computers |
| Details | Shows detailed information for one computer asset |
| Create (GET/POST) | Renders and processes the creation form |
| Edit (GET/POST) | Renders and processes the edit form |
| History | Displays asset-change history (audit trail) |
3. Dependencies & Technologies
| Dependency | Purpose |
|---|---|
| Microsoft.AspNetCore.Mvc | MVC controller base classes and attributes |
| Microsoft.AspNetCore.Authorization | Authorization policies (commented-out in this file) |
| IrbyImaging.BusinessLogic | IAssetManager – Business logic for asset operations |
| IrbyImaging.Services | IComputerMapper – Maps between domain & view models |
| IrbyImaging.DomainClasses | Domain entities (e.g., Computer) |
| IrbyImaging.Models | ViewModel definitions |
| IrbyImaging.Utility | Utility classes (e.g., Responses enum) |
| Microsoft.Extensions.Configuration | App configuration |
Entity Framework Core (IrbyContext) |
Direct data context for advanced queries |
4. API Endpoints Overview
| Action | Route | HTTP Method | Description | Parameters |
|---|---|---|---|---|
| Index | /Computers |
GET | List all computers (search + filter by status) | string search, AssetStatus status |
| Details | /Computers/Details/{id} |
GET | View details of a computer by GUID | Guid? id |
| Create (GET) | /Computers/Create |
GET | Display form to create new computer | — |
| Create (POST) | /Computers/Create |
POST | Process the creation form | ComputerCreateViewModel viewModel |
| Edit (GET) | /Computers/Edit/{id} |
GET | Display form to edit existing computer | Guid? id |
| Edit (POST) | /Computers/Edit/{id} |
POST | Process the edit form | Guid id, ComputerEditViewModel viewModel |
| History | /Computers/History/{id} |
GET | Show audit history of a computer asset | Guid? id, AssetStatus status, string search |
5. View Models & Data Models
5.1 Domain / Data Models
- Computer (
IrbyImaging.DomainClasses)
Represents the computer asset with properties likeId,Make,Model,OwnerId,Status, etc. - AssetStatus (
IrbyImaging.Utility)
Enum for asset states: e.g.,Active,InRepair,Retired, etc.
5.2 View Models
| ViewModel | Purpose |
|---|---|
| ComputerListViewModel | Contains a list of computers and filtering criteria |
| ComputerDetailViewModel | Detailed fields for a single computer view |
| ComputerCreateViewModel | Fields required to create a new computer |
| ComputerEditViewModel | Fields required to edit an existing computer |
| ComputerHistoryListViewModel | Holds history records and nested computer detail |
Note: All mapping between Domain ↔ ViewModel is handled by
IComputerMapper.
6. Helper Methods & Utilities
- _manager (
IAssetManager)GetComputers_Async(string search)GetComputerById_Async(Guid? id)AddNewAssetWithHistory_Async(Computer, string userName)UpdateAssetAndHistory_Async(Computer, string userName, string reason)GetHistoryByAssetId_Async(Guid? id)
- _mapper (
IComputerMapper)GenerateComputerListViewModel(...)GenerateComputerDetailViewModel(...)GenerateComputerCreateViewModel()CreateViewModelToComputer(...)ComputerToEditViewModel(...)EditViewModelToComputer(...)GenerateComputerHistoryListViewModel(...)
- BaseController
- Provides shared context, configuration, user/session utilities
7. Error Handling & Return Types
- All actions return IActionResult.
- 404 NotFound() when:
id == null- No record found for a given
id
- Model Validation:
- Checks
ModelState.IsValidbefore processing POST requests.
- Checks
- Failure Responses:
AddNewAssetWithHistory_AsyncandUpdateAssetAndHistory_Asyncreturn a(Responses, string)tuple.- On
Responses.Failure, the controller re-displays the form with an ErrorMessage.
8. Sample Code Blocks & Usage Examples
8.1 Listing Computers (Index)
// GET: /Computers?search=laptop&status=Active
public async Task<IActionResult> Index(string search, AssetStatus status)
{
// 1. Fetch domain models
var computers = await _manager.GetComputers_Async(search);
// 2. Map to ViewModel
var viewModel = _mapper.GenerateComputerListViewModel(computers, status);
// 3. Pass data to the view
ViewBag.dataSource = viewModel.Computers.ToList();
return View(viewModel);
}
8.2 Creating a New Computer
// GET: Show blank form
public IActionResult Create()
{
var vm = _mapper.GenerateComputerCreateViewModel();
return View(vm);
}
// POST: Process form
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Create(ComputerCreateViewModel vm)
{
if (!ModelState.IsValid)
return View(vm);
var computer = _mapper.CreateViewModelToComputer(vm);
(var result, var msg) = await _manager.AddNewAssetWithHistory_Async(computer, User.Identity.Name);
if (result == Responses.Failure)
{
vm.ErrorMessage = msg;
return View(vm);
}
return RedirectToAction(nameof(Index));
}
9. UML Diagram
+----------------------------------+
| BaseController |
|----------------------------------|
| - Context: IrbyContext |
| - Configuration: IConfiguration |
+-----------------^----------------+
|
+----------------------------------+
| ComputersController |
|----------------------------------|
| - _manager: IAssetManager |
| - _mapper: IComputerMapper |
| - _context: IrbyContext |
| - _configuration: IConfiguration |
+----------------------------------+
| + Index(...) : IActionResult |
| + Details(...) |
| + Create() / Create(vm) |
| + Edit(id) / EditComputer(id,vm) |
| + History(...) |
+----------------------------------+
10. Conclusion
🎯 ComputersController orchestrates the presentation logic for computer assets by:
- Delegating data access and business rules to
IAssetManager. - Transforming between domain models and view models via
IComputerMapper. - Ensuring robust validation, error handling, and history auditing.
This design promotes separation of concerns, testability, and scalability in the IrbyImaging application. The patterns demonstrated here can be extended to other asset types (e.g., Printers, Monitors, etc.) by following the same controller + manager + mapper structure. 🚀
Happy Coding! 🛠️