πŸ“‘ ManufacturersController.md Documentation


πŸ—‚ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
    3.1. Index (List Manufacturers)
    3.2. Create (GET & POST)
    3.3. Edit (GET & POST)
  4. Data Models & View Models
  5. Helper Methods & Utilities
  6. Error Handling & Return Types
  7. API Endpoints Overview
  8. Usage Examples
  9. UML Class Diagram
  10. Conclusion

πŸ” Overview & Purpose

The ManufacturersController is an ASP.NET Core MVC controller responsible for managing manufacturer records in the IrbyImaging application. It allows administrators (optionally via an authorization policy) to:

  • List all manufacturers
  • Create new manufacturer entries
  • Edit existing manufacturer details

All data is persisted using Entity Framework Core against an IrbyContext database. This controller inherits from the application's BaseController, sharing common behaviors and configurations.


πŸ›  Dependencies & Technologies

Technology / Library Purpose
ASP.NET Core MVC (Microsoft.AspNetCore.Mvc) Web framework for routing, controllers, and views.
Entity Framework Core (Microsoft.EntityFrameworkCore) ORM for database access and migrations.
IrbyImaging.DomainClasses Domain entities (e.g., Manufacturer).
IrbyImaging.Models & IrbyImaging.Services Business logic and view models (if any).
Microsoft.Extensions.Configuration Access to appsettings and configuration.
IrbyContext (custom DbContext) Database context registering domain entities.

πŸ“Œ Key Functional Areas

3.1 Index (List Manufacturers)

  • Signature:
    public async Task<IActionResult> Index()
    
  • Description:
    Retrieves all AssetManufacturers ordered by Name and passes them to the default Index view.
  • Flow:
    1. Query _context.AssetManufacturers.OrderBy(...).
    2. Materialize with ToListAsync().
    3. Return View(list).
public async Task<IActionResult> Index()
{
    var manufacturers = await _context
        .AssetManufacturers
        .OrderBy(am => am.Name)
        .ToListAsync();

    return View(manufacturers);
}

3.2 Create (GET & POST)

GET: Display the Create Form

  • Signature:
    public IActionResult Create()
    
  • Description:
    Returns the Create view where users can fill the new manufacturer details.
public IActionResult Create()
{
    return View();
}

POST: Handle Form Submission

  • Signature:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create([Bind("Id,Name")] Manufacturer manufacturer)
    
  • Description:

    1. Validates the model.
    2. Sets default IsAvailableOption = true.
    3. Adds to context and saves changes.
    4. Redirects to Index on success.
  • Attributes:

    • [HttpPost] – Accepts only POST requests.
    • [ValidateAntiForgeryToken] – Protects against CSRF.
    • [Bind("Id,Name")] – White-lists the bound properties.
if (ModelState.IsValid)
{
    manufacturer.IsAvailableOption = true;
    _context.Add(manufacturer);
    await _context.SaveChangesAsync();
    return RedirectToAction(nameof(Index));
}
return View(manufacturer);

3.3 Edit (GET & POST)

GET: Display the Edit Form

  • Signature:
    public async Task<IActionResult> Edit(int? id)
    
  • Description:
    1. Checks for null id β†’ returns NotFound().
    2. Loads existing manufacturer with FindAsync.
    3. Returns 404 if not found; otherwise displays Edit view.
if (id == null) return NotFound();
var manufacturer = await _context.AssetManufacturers.FindAsync(id);
if (manufacturer == null) return NotFound();
return View(manufacturer);

POST: Save Edited Values

  • Signature:
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(int id, [Bind("Id,Name,IsAvailableOption")] Manufacturer manufacturer)
    
  • Description:
    1. Ensures route id matches payload manufacturer.Id.
    2. Validates ModelState.
    3. Calls _context.Update(...).
    4. Catches DbUpdateConcurrencyException and checks if the record still exists.
    5. Redirects to Index on success.
if (id != manufacturer.Id) return NotFound();

if (ModelState.IsValid)
{
    try
    {
        _context.Update(manufacturer);
        await _context.SaveChangesAsync();
    }
    catch (DbUpdateConcurrencyException)
    {
        if (!ManufacturerExists(manufacturer.Id))
            return NotFound();
        else
            throw;  // Bubble up unexpected concurrency issues
    }
    return RedirectToAction(nameof(Index));
}
return View(manufacturer);

πŸ”§ Data Models & View Models

Class Properties
Manufacturer - int Id- string Name- bool IsAvailableOption

Note: The domain class Manufacturer likely resides in IrbyImaging.DomainClasses. It maps to a database table AssetManufacturers via EF Core.


πŸ›  Helper Methods & Utilities

/// <summary>Check if a manufacturer exists by ID.</summary>
private bool ManufacturerExists(int id)
{
    return _context.AssetManufacturers.Any(e => e.Id == id);
}
  • Purpose:
    Quickly determines if an entity is present in the DB.
  • Usage:
    Used in the Edit (POST) action to differentiate between a concurrency conflict or a genuine deletion.

⚠️ Error Handling & Return Types

Scenario Return Type Description
Successful view rendering View(...) Renders Razor view with model data.
Invalid model / form submission errors View(manufacturer) Re-displays form with validation errors.
Entity not found (null id or missing) NotFound() (404) Returns HTTP 404.
Concurrency conflict on update NotFound() / rethrow exception 404 if deleted; otherwise bubble up.
Successful create/edit RedirectToAction(nameof(Index)) Redirects to list page.

🌐 API Endpoints Overview

Although this is an MVC controller returning views, we can summarize the routes:

HTTP Verb Route Action Description
GET /Manufacturers Index List all manufacturers
GET /Manufacturers/Create Create Show the new-manufacturer form
POST /Manufacturers/Create Create Process the create form
GET /Manufacturers/Edit/{id} Edit Show the edit-manufacturer form
POST /Manufacturers/Edit/{id} Edit Process the edit form

Tip: Attribute routing or conventional routing (in Startup.cs or Program.cs) drives these URLs.


πŸ“š Usage Examples

  1. Viewing all manufacturers
    Navigate to:

    GET https://<your-domain>/Manufacturers
    
  2. Adding a new manufacturer

    • GET the form:
      GET https://<your-domain>/Manufacturers/Create
      
    • POST the data:
      <form method="post" action="/Manufacturers/Create">
        <input name="Name" />
        <button type="submit">Save</button>
      </form>
      
  3. Editing an existing manufacturer

    • GET the edit page:
      GET https://<your-domain>/Manufacturers/Edit/5
      
    • POST updated data:
      <form method="post" action="/Manufacturers/Edit/5">
        <input type="hidden" name="Id" value="5" />
        <input name="Name" value="Updated Name" />
        <input type="checkbox" name="IsAvailableOption" checked />
        <button type="submit">Update</button>
      </form>
      

πŸ“Š UML Class Diagram

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     BaseController         β”‚
│────────────────────────────│
β”‚ + protected readonly IrbyContext _context    β”‚
β”‚ + protected readonly IConfiguration _config   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              β”‚ Inherits
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  ManufacturersController      β”‚
│───────────────────────────────│
β”‚ - IrbyContext _context        β”‚
β”‚ - IConfiguration _configurationβ”‚
│───────────────────────────────│
β”‚ + Task<IActionResult> Index() β”‚
β”‚ + IActionResult Create()      β”‚
β”‚ + Task<IActionResult> Create(Manufacturer)  β”‚
β”‚ + Task<IActionResult> Edit(int?)            β”‚
β”‚ + Task<IActionResult> Edit(int, Manufacturer) β”‚
β”‚ - bool ManufacturerExists(int)              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🏁 Conclusion

The ManufacturersController provides a simple CRUD interface over the Manufacturer domain entity, leveraging ASP.NET Core MVC patterns and Entity Framework Core for data access. Key highlights include:

  • Secure form submissions with anti-forgery tokens.
  • Model binding and validation via ModelState.
  • Graceful error handling for not-found and concurrency issues.
  • Separation of concerns by inheriting from BaseController.

This documentation should give developers a clear understanding of how to maintain, extend, or integrate manufacturer management within the IrbyImaging application. Happy coding! πŸš€