πŸ“„ OperatingSystemsController Documentation


Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Controller Hierarchy
  4. API Endpoints Overview
  5. Key Functional Areas
  6. View Models & Data Models
  7. Helper Methods & Utilities
  8. Error Handling & Return Types
  9. UML Class Diagram
  10. Sample Code Blocks & Usage Examples
  11. Conclusion

1. Overview & Purpose

The OperatingSystemsController is an ASP.NET Core MVC controller responsible for managing CRUD (Create, Read, Update, Delete) operations on Operating System entities within the IrbyImaging application. It provides administrative interfaces to:

  • πŸ“‹ List all available operating systems
  • βž• Add a new operating system
  • ✏️ Edit an existing operating system

All operations leverage Entity Framework Core for database access through the injected IrbyContext.


2. Dependencies & Technologies

Technology / Package Purpose
Microsoft.AspNetCore.Mvc Core MVC framework (controllers, actions, views)
Microsoft.EntityFrameworkCore ORM for database access
Microsoft.Extensions.Configuration Configuration management (e.g., connection strings)
IrbyImaging.DomainClasses Domain models (e.g., OperatingSystem)
IrbyImaging.Utility / Services (Imported but not directly used here)
IrbyImaging.BusinessLogic (Imported but not directly used here)

3. Controller Hierarchy

BaseController
   └── OperatingSystemsController
  • BaseController
    • Provides shared behavior (authentication, logging, etc.)
  • OperatingSystemsController
    • Inherits from BaseController
    • Manages OperatingSystem entities

4. API Endpoints Overview

Action HTTP Route Purpose
Index GET /OperatingSystems/Index Display sorted list of operating systems
Create (Form) GET /OperatingSystems/Create Show form to add a new OS
Create (Submit) POST /OperatingSystems/Create Persist new OS; redirect to Index on success
Edit (Form) GET /OperatingSystems/Edit/{id} Show form to edit an existing OS
Edit (Submit) POST /OperatingSystems/Edit/{id} Update OS; handle concurrency; redirect

5. Key Functional Areas

  1. Listing

    • public async Task<IActionResult> Index()
    • Retrieves all OS records, orders by Name, and passes to the view.
  2. Creation

    • GET: public IActionResult Create()
      • Returns an empty form view.
    • POST:
      [HttpPost]
      [ValidateAntiForgeryToken]
      public async Task<IActionResult> Create(
        [Bind("Id,Name")] OperatingSystem operatingSystem)
      
      • Sets IsAvailableOption = true by default.
      • Saves new record; redirects to Index.
  3. Editing

    • GET:
      public async Task<IActionResult> Edit(int? id)
      
      • Loads OS by id; returns 404 NotFound if missing.
    • POST:
      [HttpPost]
      [ValidateAntiForgeryToken]
      public async Task<IActionResult> Edit(
        int id,
        [Bind("Id,Name,IsAvailableOption")] OperatingSystem operatingSystem)
      
      • Verifies route id matches model Id.
      • Attempts update; catches DbUpdateConcurrencyException.

6. View Models & Data Models

OperatingSystem (DomainClasses)

public class OperatingSystem
{
    public int    Id                 { get; set; }
    public string Name               { get; set; }
    public bool   IsAvailableOption  { get; set; }
}
  • Id: Primary key
  • Name: Display name of OS
  • IsAvailableOption: Indicates if OS should appear in dropdowns

7. Helper Methods & Utilities

  • OperatingSystemExists(int id)
    • Checks for existence in the database:
      private bool OperatingSystemExists(int id) 
        => _context.OperatingSystems.Any(e => e.Id == id);
      

8. Error Handling & Return Types

Scenario Handling
Missing id on GET Edit Returns NotFound() (404)
OS not found in DB Returns NotFound() (404)
ModelState invalid (POST) Returns same View with validation msgs
Concurrency conflict on Update Catches DbUpdateConcurrencyException,
re-checks existence, rethrows or 404

All action methods return IActionResult, enabling view rendering or HTTP status results.


9. UML Class Diagram

classDiagram
    BaseController <|-- OperatingSystemsController
    OperatingSystemsController : - IrbyContext _context
    OperatingSystemsController : - IConfiguration _configuration
    OperatingSystemsController : + Index() : Task<IActionResult>
    OperatingSystemsController : + Create() : IActionResult
    OperatingSystemsController : + Create(OperatingSystem) : Task<IActionResult>
    OperatingSystemsController : + Edit(int?) : Task<IActionResult>
    OperatingSystemsController : + Edit(int, OperatingSystem) : Task<IActionResult>
    OperatingSystemsController : - OperatingSystemExists(int) : bool
    OperatingSystem : + int Id
    OperatingSystem : + string Name
    OperatingSystem : + bool IsAvailableOption

10. Sample Code Blocks & Usage Examples

10.1 Retrieving the List (Index)

// URL: GET /OperatingSystems/Index
public async Task<IActionResult> Index()
{
    var osList = await _context
        .OperatingSystems
        .OrderBy(os => os.Name)
        .ToListAsync();
    return View(osList);
}

Razor View Snippet (Index.cshtml):

@model IEnumerable<OperatingSystem>

<h2>Operating Systems</h2>
<table>
  <thead><tr><th>Name</th><th>Available?</th><th>Actions</th></tr></thead>
  <tbody>
    @foreach(var os in Model) {
      <tr>
        <td>@os.Name</td>
        <td>@os.IsAvailableOption</td>
        <td>
          <a asp-action="Edit" asp-route-id="@os.Id">Edit</a>
        </td>
      </tr>
    }
  </tbody>
</table>
<a asp-action="Create">Add New OS</a>

10.2 Creating a New OS

<!-- Create.cshtml -->
@model OperatingSystem

<form asp-action="Create" method="post">
  <div>
    <label>Name</label>
    <input asp-for="Name" />
    <span asp-validation-for="Name"></span>
  </div>
  <button type="submit">Save</button>
</form>

10.3 Editing an OS

<!-- Edit.cshtml -->
@model OperatingSystem

<form asp-action="Edit" method="post">
  <input type="hidden" asp-for="Id" />
  <div>
    <label>Name</label>
    <input asp-for="Name" />
  </div>
  <div>
    <label>Available</label>
    <input type="checkbox" asp-for="IsAvailableOption" />
  </div>
  <button type="submit">Update</button>
</form>

11. Conclusion

The OperatingSystemsController offers a straightforward CRUD interface for managing operating systems in the IrbyImaging application. Leveraging ASP.NET Core MVC patterns and Entity Framework Core, it ensures:

  • Clean separation of concerns
  • Secure form submissions via anti-forgery tokens
  • Robust error and concurrency handling

πŸš€ Administrators can easily list, add, and update OS options, ensuring the application’s hardware/software compatibility lists remain up-to-date.