πŸ–¨οΈ 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

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Class Diagram (UML)
  4. Key Functional Areas (Action Methods)
  5. View Models & Data Flow
  6. Error Handling & Return Types
  7. Sample Usage Examples
  8. 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:

  1. _manager.GetPrinters_Async(search) retrieves domain Printer objects.
  2. _mapper.GeneratePrinterListViewModel(printers, status) maps to PrinterListViewModel.
  3. 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:

  1. id == null β†’ NotFound().
  2. _manager.GetPrinterById_Async(id) β†’ domain Printer.
  3. If null β†’ NotFound().
  4. Map to PrinterDetailViewModel and View(viewModel).

3. Create

GET: Display Form

Route: GET /Printers/Create
Signature:

public IActionResult Create()
  • Simply returns a blank PrinterCreateViewModel via _mapper.GeneratePrinterCreateViewModel().

POST: Submit Form

Route: POST /Printers/Create
Attributes: [HttpPost], [ValidateAntiForgeryToken]
Signature:

public async Task<IActionResult> Create(PrinterCreateViewModel viewModel)

Workflow:

  1. ModelState.IsValid check – if invalid, redisplay form.
  2. Map viewModel β†’ domain Printer.
  3. _manager.AddNewAssetWithHistory_Async(printer, User.Identity.Name) returns (Responses, message).
  4. On Failure, repopulate VM with ErrorMessage and show form again.
  5. 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 to PrinterEditViewModel, returns view.

POST: Submit Edits

Route: POST /Printers/Edit/{id}
Attributes: [HttpPost], [ValidateAntiForgeryToken]
Signature:

public async Task<IActionResult> Edit(Guid id, PrinterEditViewModel viewModel)

Workflow:

  1. Check id == viewModel.Id else NotFound().
  2. ModelState.IsValid β†’ if false, return view.
  3. Map VM β†’ domain Printer.
  4. _manager.UpdateAssetAndHistory_Async(printer, user, viewModel.Reason).
  5. On Failure, repopulate VM, show error.
  6. 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:

  1. Validate id, fetch List<History> via _manager.GetHistoryByAssetId_Async(id).
  2. Map to PrinterHistoryListViewModel.
  3. Also fetch printer details and embed in VM.
  4. 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 id parameters.
  • ModelState Validation: On invalid inputs, the same view is re-rendered with validation messages.
  • Operation Failure: Business layer returns Responses.Failure plus an error message; displayed in ViewModel’s ErrorMessage property.
  • Success: Redirects to Index on successful Create/Edit.

πŸ’‘ Sample Usage Examples

  1. List all printers

    GET https://yourapp.com/Printers
    
  2. Search printers with β€œHP” in name

    GET https://yourapp.com/Printers?search=HP
    
  3. View details of a specific printer

    GET https://yourapp.com/Printers/Details/3fa85f64-5717-4562-b3fc-2c963f66afa6
    
  4. Create a new printer (JSON payload)

    POST /Printers/Create
    Content-Type: application/x-www-form-urlencoded
    
    Name=OfficePrinter&Model=HP-LaserJet+M503&Location=HQ
    
  5. 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!