// Directory Structure for Clean Architecture
// 1. InventoryManagement.sln (Solution File)
// 2. InventoryManagement.Domain (Core Layer)
// 3. InventoryManagement.Application (Application Logic)
// 4. InventoryManagement.Infrastructure (Database and External Systems)
// 5. InventoryManagement.API (API Layer)
// InventoryManagement.Domain
// ------------------------------------------
// Project: InventoryManagement.Domain (Class Library project to be created in Visual Studio 2022)
namespace InventoryManagement.Domain.Entities
{
public class InventoryItem
{
public int Id { get; set; } // Primary Key (Auto Increment)
public string Name { get; set; }
public string Brand { get; set; }
public string VendorId { get; set; } // Vendor Identifier or Model Number
public int Quantity { get; set; }
public decimal Cost { get; set; }
public string Description { get; set; }
public string MenSize { get; set; }
public string WomenSize { get; set; }
[Url]
public string PictureUrl { get; set; } // URL for Picture
}
}
// InventoryManagement.Application
// ------------------------------------------
// Project: InventoryManagement.Application (Class Library project to be created in Visual Studio 2022)
using InventoryManagement.Domain.Entities;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace InventoryManagement.Application.Interfaces
{
public interface IInventoryService
{
Task<IEnumerable<InventoryItem>> GetAllItemsAsync();
Task<InventoryItem> GetItemByIdAsync(int id);
Task AddItemAsync(InventoryItem item);
Task UpdateItemAsync(InventoryItem item);
Task DeleteItemAsync(int id);
}
}
// InventoryManagement.Infrastructure
// ------------------------------------------
// Project: InventoryManagement.Infrastructure (Class Library project to be created in Visual Studio 2022)
// File: InventoryDbContext.cs
using InventoryManagement.Domain.Entities;
using Microsoft.EntityFrameworkCore;
namespace InventoryManagement.Infrastructure.Persistence
{
public class InventoryDbContext : DbContext
{
public InventoryDbContext(DbContextOptions<InventoryDbContext> options) : base(options) { }
public DbSet<InventoryItem> InventoryItems { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<InventoryItem>().HasData(
new InventoryItem
{
Id = -1," // Assign a negative value to avoid collision
Name = "Adidas Soccer Cleats",
Brand = "Adidas",
VendorId = "AD-001",
Quantity = 50,
Cost = 79.99M,
Description = "High-quality Adidas soccer cleats for performance on the field.",
MenSize = "8-12",
WomenSize = "9-13",
PictureUrl = "https://example.com/adidas-soccer-cleats.jpg"
}
);
}
}
}
// File: InventoryService.cs
using InventoryManagement.Application.Interfaces;
using InventoryManagement.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace InventoryManagement.Infrastructure.Persistence
{
public class InventoryService : IInventoryService
{
private readonly InventoryDbContext _context;
public InventoryService(InventoryDbContext context)
{
_context = context;
}
public async Task<IEnumerable<InventoryItem>> GetAllItemsAsync()
{
return await _context.InventoryItems.ToListAsync();
}
public async Task<InventoryItem> GetItemByIdAsync(int id)
{
return await _context.InventoryItems.FindAsync(id);
}
public async Task AddItemAsync(InventoryItem item)
{
await _context.InventoryItems.AddAsync(item);
await _context.SaveChangesAsync();
}
// InventoryManagement.API
// ------------------------------------------
// Project: InventoryManagement.API (ASP.NET Core Web API project to be created in Visual Studio 2022)
// File: InventoryController.cs
using InventoryManagement.Application.Interfaces;
using InventoryManagement.Domain.Entities;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace InventoryManagement.API.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class InventoryController : ControllerBase
{
private readonly IInventoryService _inventoryService;
public InventoryController(IInventoryService inventoryService)
{
_inventoryService = inventoryService;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<InventoryItem>>> GetAllItems()
{
var items = await _inventoryService.GetAllItemsAsync();
return Ok(items);
}
[HttpGet("{id}")]
public async Task<ActionResult<InventoryItem>> GetItemById(int id)
{
var item = await _inventoryService.GetItemByIdAsync(id);
if (item == null)
{
return NotFound();
}
return Ok(item);
}
[HttpPost]
public async Task<ActionResult> AddItem([FromBody] InventoryItem item)
{
await _inventoryService.AddItemAsync(item);
return CreatedAtAction(nameof(GetItemById), new { id = item.Id }, item);
}
[HttpPut("{id}")]
public async Task<ActionResult> UpdateItem(int id, [FromBody] InventoryItem item)
{
if (id != item.Id)
{
return BadRequest();
}
await _inventoryService.UpdateItemAsync(item);
return NoContent();
}
[HttpDelete("{id}")]
public async Task<ActionResult> DeleteItem(int id)
{
await _inventoryService.DeleteItemAsync(id);
return NoContent();
}
}
}
// Project Reference Dependencies
// ------------------------------------------
// 1. InventoryManagement.Domain:
// - No project references are required.
//
// 2. InventoryManagement.Application:
// - Add reference to InventoryManagement.Domain.
//
// 3. InventoryManagement.Infrastructure:
// - Add references to InventoryManagement.Domain and InventoryManagement.Application.
//
// 4. InventoryManagement.API:
// - Add references to InventoryManagement.Application and InventoryManagement.Infrastructure.
// NuGet Package Installation
// ------------------------------------------
// To install the necessary NuGet packages via the Package Manager Console:
// 1. Open Visual Studio 2022.
// 2. Go to Tools > NuGet Package Manager > Package Manager Console.
// 3. Run the following commands for each project:
//
// - InventoryManagement.Infrastructure:
// Install-Package Microsoft.EntityFrameworkCore -ProjectName InventoryManagement.Infrastructure
// Install-Package Microsoft.EntityFrameworkCore.Tools -ProjectName InventoryManagement.Infrastructure
// Install-Package Npgsql.EntityFrameworkCore.PostgreSQL -ProjectName InventoryManagement.Infrastructure
//
// - InventoryManagement.API:
// Install-Package Swashbuckle.AspNetCore -ProjectName InventoryManagement.API
// Database Creation
// ------------------------------------------
// To create the PostgreSQL database:
// 1. Ensure PostgreSQL is installed and running.
// 2. Open a terminal and connect to your PostgreSQL server:
// psql -U yourusername
// 3. Create the database using the following command:
// CREATE DATABASE "InventoryDb";
// 4. Make sure the connection string in appsettings.json matches your PostgreSQL server configuration.
// 5. Run the application to apply migrations and create tables automatically.
// Applying Migrations
// ------------------------------------------
// To apply migrations and update the database:
// 1. Open Visual Studio 2022.
// 2. Go to Tools > NuGet Package Manager > Package Manager Console.
// 3. Select the Default Project as "InventoryManagement.Infrastructure".
// 4. Run the following commands:
// - Add-Migration InitialCreate
// - Update-Database
// 5. This will create the necessary migration files and apply the changes to the database.
// Visual Studio 2022 Configuration
// ------------------------------------------
// To build this solution in Visual Studio 2022:
// 1. Open Visual Studio 2022.
// 2. Select "Open a project or solution" and navigate to the "InventoryManagement.sln" file.
// 3. Create new blank projects for "InventoryManagement.Domain", "InventoryManagement.Application", "InventoryManagement.Infrastructure", and "InventoryManagement.API".
// 4. Add the respective code for each project as provided above.
// 5. Make sure that you have .NET 8 SDK installed.
// 6. Set "InventoryManagement.API" as the startup project.
// 7. Build the solution by clicking "Build" -> "Build Solution".
// 8. Run the API by pressing F5 or selecting "Debug" -> "Start Debugging".
