Files
CutList/CutList.Web/Services/SupplierService.cs
AJ Isaacs 9868df162d feat: Add CutList.Web Blazor Server application
Add a new web-based frontend for cut list optimization using:
- Blazor Server with .NET 8
- Entity Framework Core with MSSQL LocalDB
- Full CRUD for Materials, Suppliers, Projects, and Cutting Tools
- Supplier stock length management for quick project setup
- Integration with CutList.Core for bin packing optimization
- Print-friendly HTML reports with efficiency statistics

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 21:56:21 -05:00

127 lines
3.6 KiB
C#

using CutList.Web.Data;
using CutList.Web.Data.Entities;
using Microsoft.EntityFrameworkCore;
namespace CutList.Web.Services;
public class SupplierService
{
private readonly ApplicationDbContext _context;
public SupplierService(ApplicationDbContext context)
{
_context = context;
}
public async Task<List<Supplier>> GetAllAsync(bool includeInactive = false)
{
var query = _context.Suppliers.AsQueryable();
if (!includeInactive)
{
query = query.Where(s => s.IsActive);
}
return await query.OrderBy(s => s.Name).ToListAsync();
}
public async Task<Supplier?> GetByIdAsync(int id)
{
return await _context.Suppliers
.Include(s => s.Stocks)
.ThenInclude(st => st.Material)
.FirstOrDefaultAsync(s => s.Id == id);
}
public async Task<Supplier> CreateAsync(Supplier supplier)
{
supplier.CreatedAt = DateTime.UtcNow;
_context.Suppliers.Add(supplier);
await _context.SaveChangesAsync();
return supplier;
}
public async Task UpdateAsync(Supplier supplier)
{
_context.Suppliers.Update(supplier);
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(int id)
{
var supplier = await _context.Suppliers.FindAsync(id);
if (supplier != null)
{
supplier.IsActive = false;
await _context.SaveChangesAsync();
}
}
// Stock management
public async Task<List<SupplierStock>> GetStocksForSupplierAsync(int supplierId)
{
return await _context.SupplierStocks
.Include(s => s.Material)
.Where(s => s.SupplierId == supplierId && s.IsActive)
.OrderBy(s => s.Material.Shape)
.ThenBy(s => s.Material.Size)
.ThenBy(s => s.LengthInches)
.ToListAsync();
}
public async Task<List<SupplierStock>> GetStocksForMaterialAsync(int materialId)
{
return await _context.SupplierStocks
.Include(s => s.Supplier)
.Where(s => s.MaterialId == materialId && s.IsActive && s.Supplier.IsActive)
.OrderBy(s => s.Supplier.Name)
.ThenBy(s => s.LengthInches)
.ToListAsync();
}
public async Task<SupplierStock?> GetStockByIdAsync(int id)
{
return await _context.SupplierStocks
.Include(s => s.Material)
.Include(s => s.Supplier)
.FirstOrDefaultAsync(s => s.Id == id);
}
public async Task<SupplierStock> AddStockAsync(SupplierStock stock)
{
_context.SupplierStocks.Add(stock);
await _context.SaveChangesAsync();
return stock;
}
public async Task UpdateStockAsync(SupplierStock stock)
{
_context.SupplierStocks.Update(stock);
await _context.SaveChangesAsync();
}
public async Task DeleteStockAsync(int id)
{
var stock = await _context.SupplierStocks.FindAsync(id);
if (stock != null)
{
stock.IsActive = false;
await _context.SaveChangesAsync();
}
}
public async Task<bool> StockExistsAsync(int supplierId, int materialId, decimal lengthInches, int? excludeId = null)
{
var query = _context.SupplierStocks.Where(s =>
s.SupplierId == supplierId &&
s.MaterialId == materialId &&
s.LengthInches == lengthInches &&
s.IsActive);
if (excludeId.HasValue)
{
query = query.Where(s => s.Id != excludeId.Value);
}
return await query.AnyAsync();
}
}