π¨οΈ PrintersController Documentation
Welcome to the comprehensive documentation for the PrintersController class in the IrbyImaging.UI ASP.NET Core MVC application. This guide will walk you through everything you need to know to understand, use, and extend this controller.
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Class Diagram (UML)
- Key Functional Areas (Action Methods)
- View Models & Data Flow
- Error Handling & Return Types
- Sample Usage Examples
- Conclusion
π Overview & Purpose
PrintersController is an ASP.NET Core MVC controller responsible for managing Printer Assets within the IrbyImaging application. It provides CRUD (Create, Read, Update) operations and asset history tracking for printers.
Key responsibilities:
- Display a list of printers (
Index) - Show detailed information (
Details) - Create new printer assets (
Create) - Edit existing printer assets (
Edit) - View change history of a printer (
History)
This controller inherits from BaseController, which supplies common DbContext and Configuration functionality.
βοΈ Dependencies & Technologies
| Dependency | Purpose |
|---|---|
Microsoft.AspNetCore.Mvc |
MVC framework components |
Microsoft.AspNetCore.Authorization |
Authorization attributes |
Microsoft.Extensions.Configuration |
Access to configuration (appsettings) |
IrbyImaging.BusinessLogic |
IAssetManager for data operations |
IrbyImaging.Services |
IPrinterMapper for mapping domain/models |
IrbyImaging.DomainClasses |
EF Core entities (e.g., Printer) |
IrbyImaging.Models |
ViewModel classes (e.g., PrinterCreateViewModel) |
IrbyImaging.Utility |
Shared utilities and enums (e.g., Responses) |
IrbyContext |
Entity Framework Core DB context |
π Class Diagram (UML)
classDiagram
BaseController <|-- PrintersController
PrintersController o-- IAssetManager : _manager
PrintersController o-- IPrinterMapper : _mapper
PrintersController o-- IrbyContext : _context
PrintersController o-- IConfiguration : _configuration
interface IAssetManager {
+Task<List<Printer>> GetPrinters_Async(string)
+Task<Printer> GetPrinterById_Async(Guid?)
+Task<Responses, string> AddNewAssetWithHistory_Async(Printer, string)
+Task<Responses, string> UpdateAssetAndHistory_Async(Printer, string, string)
+Task<List<History>> GetHistoryByAssetId_Async(Guid?)
}
interface IPrinterMapper {
+PrinterListViewModel GeneratePrinterListViewModel(List<Printer>, AssetStatus)
+PrinterDetailViewModel GeneratePrinterDetailViewModel(Printer)
+PrinterCreateViewModel GeneratePrinterCreateViewModel()
+PrinterCreateViewModel CreateViewModelToPrinter(PrinterCreateViewModel)
+PrinterEditViewModel PrinterToEditViewModel(Printer)
+Printer EditViewModelToPrinter(PrinterEditViewModel)
+PrinterHistoryListViewModel GeneratePrinterHistoryListViewModel(List<History>)
}
π Key Functional Areas (Action Methods)
1. Index
Route: GET /Printers?search={search}&status={status}
Purpose: Lists printers, optionally filtered by search term and status.
Signature:
public async Task<IActionResult> Index(string search, AssetStatus status)
Workflow:
_manager.GetPrinters_Async(search)retrieves domainPrinterobjects._mapper.GeneratePrinterListViewModel(printers, status)maps toPrinterListViewModel.- Returns
View(viewModel).
| Parameter | Type | Description |
|---|---|---|
| search | string |
Partial match on printer name/ID |
| status | AssetStatus |
Enum filter (e.g., Active, Inactive) |
2. Details
Route: GET /Printers/Details/{id}
Purpose: Shows detailed info for a single printer.
Signature:
public async Task<IActionResult> Details(Guid? id)
Workflow:
id == nullβNotFound()._manager.GetPrinterById_Async(id)β domainPrinter.- If
nullβNotFound(). - Map to
PrinterDetailViewModelandView(viewModel).
3. Create
GET: Display Form
Route: GET /Printers/Create
Signature:
public IActionResult Create()
- Simply returns a blank
PrinterCreateViewModelvia_mapper.GeneratePrinterCreateViewModel().
POST: Submit Form
Route: POST /Printers/Create
Attributes: [HttpPost], [ValidateAntiForgeryToken]
Signature:
public async Task<IActionResult> Create(PrinterCreateViewModel viewModel)
Workflow:
ModelState.IsValidcheck β if invalid, redisplay form.- Map
viewModelβ domainPrinter. _manager.AddNewAssetWithHistory_Async(printer, User.Identity.Name)returns(Responses, message).- On
Failure, repopulate VM withErrorMessageand show form again. - On success, redirect to
Index.
4. Edit
GET: Display Edit Form
Route: GET /Printers/Edit/{id}
Signature:
public async Task<IActionResult> Edit(Guid? id)
- Validates
id, fetches printer, maps toPrinterEditViewModel, returns view.
POST: Submit Edits
Route: POST /Printers/Edit/{id}
Attributes: [HttpPost], [ValidateAntiForgeryToken]
Signature:
public async Task<IActionResult> Edit(Guid id, PrinterEditViewModel viewModel)
Workflow:
- Check
id == viewModel.IdelseNotFound(). ModelState.IsValidβ if false, return view.- Map VM β domain
Printer. _manager.UpdateAssetAndHistory_Async(printer, user, viewModel.Reason).- On
Failure, repopulate VM, show error. - On success, redirect to
Index.
5. History
Route: GET /Printers/History/{id}
Purpose: Displays change history for a printer asset.
Signature:
public async Task<IActionResult> History(Guid? id)
Workflow:
- Validate
id, fetchList<History>via_manager.GetHistoryByAssetId_Async(id). - Map to
PrinterHistoryListViewModel. - Also fetch printer details and embed in VM.
- Return
View(viewModel).
π View Models & Data Flow
flowchart LR
UIForms -->|POST| PrintersController
PrintersController -->|Map VM->Domain| IPrinterMapper
IPrinterMapper --> IrbyContext
IrbyContext -->|CRUD| SQLServer[(Database)]
SQLServer -->|Data| IAssetManager
IAssetManager -->|Domain Objects| PrintersController
PrintersController -->|Map Domain->VM| IPrinterMapper
PrintersController -->|Return| UIViews
Key ViewModels:
| ViewModel | Usage |
|---|---|
PrinterListViewModel |
Index listing |
PrinterDetailViewModel |
Details page |
PrinterCreateViewModel |
Create form |
PrinterEditViewModel |
Edit form |
PrinterHistoryListViewModel |
History page |
β οΈ Error Handling & Return Types
- 404 NotFound(): Missing or invalid
idparameters. - ModelState Validation: On invalid inputs, the same view is re-rendered with validation messages.
- Operation Failure: Business layer returns
Responses.Failureplus an error message; displayed in ViewModelβsErrorMessageproperty. - Success: Redirects to
Indexon successful Create/Edit.
π‘ Sample Usage Examples
-
List all printers
GET https://yourapp.com/Printers -
Search printers with βHPβ in name
GET https://yourapp.com/Printers?search=HP -
View details of a specific printer
GET https://yourapp.com/Printers/Details/3fa85f64-5717-4562-b3fc-2c963f66afa6 -
Create a new printer (JSON payload)
POST /Printers/Create Content-Type: application/x-www-form-urlencoded Name=OfficePrinter&Model=HP-LaserJet+M503&Location=HQ -
Edit an existing printer
POST /Printers/Edit/3fa85f64-5717-4562-b3fc-2c963f66afa6 Content-Type: application/x-www-form-urlencoded Id=3fa85f64-5717-4562-b3fc-2c963f66afa6 Name=NewName&Model=HP-M608&Location=RemoteSite&Reason=UpdatedModel
β Conclusion
The PrintersController orchestrates all printer-asset related workflows:
- Listing printers with optional filtering
- Viewing details & history
- Creating and editing assets with audit history
By adhering to MVC best practices (separation of concerns, DI, ViewModel mapping) it is both maintainable and extensible.
Feel free to explore the related services (IAssetManager) and mappers (IPrinterMapper) to customize behavior, add new features, or integrate additional data sources! π
Generated with β€οΈ by the IrbyImaging Docs Team!