π ManufacturersController.md Documentation
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
3.1. Index (List Manufacturers)
3.2. Create (GET & POST)
3.3. Edit (GET & POST) - Data Models & View Models
- Helper Methods & Utilities
- Error Handling & Return Types
- API Endpoints Overview
- Usage Examples
- UML Class Diagram
- 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 allAssetManufacturersordered byNameand passes them to the default Index view. - Flow:
- Query
_context.AssetManufacturers.OrderBy(...). - Materialize with
ToListAsync(). - Return
View(list).
- Query
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:
- Validates the model.
- Sets default
IsAvailableOption = true. - Adds to context and saves changes.
- 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:
- Checks for
nullid β returnsNotFound(). - Loads existing manufacturer with
FindAsync. - Returns 404 if not found; otherwise displays Edit view.
- Checks for
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:
- Ensures route
idmatches payloadmanufacturer.Id. - Validates
ModelState. - Calls
_context.Update(...). - Catches
DbUpdateConcurrencyExceptionand checks if the record still exists. - Redirects to Index on success.
- Ensures route
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
Manufacturerlikely resides inIrbyImaging.DomainClasses. It maps to a database tableAssetManufacturersvia 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.csorProgram.cs) drives these URLs.
π Usage Examples
-
Viewing all manufacturers
Navigate to:GET https://<your-domain>/Manufacturers -
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>
- GET the form:
-
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>
- GET the edit page:
π 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! π