Adds CRUD methods for managing material stock lengths, including duplicate checking and quantity tracking. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
123 lines
3.5 KiB
C#
123 lines
3.5 KiB
C#
using CutList.Web.Data;
|
|
using CutList.Web.Data.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace CutList.Web.Services;
|
|
|
|
public class MaterialService
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
public static readonly string[] CommonShapes =
|
|
{
|
|
"Round Tube",
|
|
"Square Tube",
|
|
"Rectangular Tube",
|
|
"Angle",
|
|
"Channel",
|
|
"Flat Bar",
|
|
"Round Bar",
|
|
"Square Bar",
|
|
"I-Beam",
|
|
"Pipe"
|
|
};
|
|
|
|
public MaterialService(ApplicationDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<List<Material>> GetAllAsync(bool includeInactive = false)
|
|
{
|
|
var query = _context.Materials.AsQueryable();
|
|
if (!includeInactive)
|
|
{
|
|
query = query.Where(m => m.IsActive);
|
|
}
|
|
return await query.OrderBy(m => m.Shape).ThenBy(m => m.Size).ToListAsync();
|
|
}
|
|
|
|
public async Task<Material?> GetByIdAsync(int id)
|
|
{
|
|
return await _context.Materials.FindAsync(id);
|
|
}
|
|
|
|
public async Task<Material> CreateAsync(Material material)
|
|
{
|
|
material.CreatedAt = DateTime.UtcNow;
|
|
_context.Materials.Add(material);
|
|
await _context.SaveChangesAsync();
|
|
return material;
|
|
}
|
|
|
|
public async Task UpdateAsync(Material material)
|
|
{
|
|
material.UpdatedAt = DateTime.UtcNow;
|
|
_context.Materials.Update(material);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task DeleteAsync(int id)
|
|
{
|
|
var material = await _context.Materials.FindAsync(id);
|
|
if (material != null)
|
|
{
|
|
material.IsActive = false;
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public async Task<bool> ExistsAsync(string shape, string size, int? excludeId = null)
|
|
{
|
|
var query = _context.Materials.Where(m => m.Shape == shape && m.Size == size && m.IsActive);
|
|
if (excludeId.HasValue)
|
|
{
|
|
query = query.Where(m => m.Id != excludeId.Value);
|
|
}
|
|
return await query.AnyAsync();
|
|
}
|
|
|
|
// Stock Length methods
|
|
public async Task<List<MaterialStockLength>> GetStockLengthsAsync(int materialId)
|
|
{
|
|
return await _context.MaterialStockLengths
|
|
.Where(s => s.MaterialId == materialId && s.IsActive)
|
|
.OrderBy(s => s.LengthInches)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<MaterialStockLength> AddStockLengthAsync(MaterialStockLength stockLength)
|
|
{
|
|
_context.MaterialStockLengths.Add(stockLength);
|
|
await _context.SaveChangesAsync();
|
|
return stockLength;
|
|
}
|
|
|
|
public async Task UpdateStockLengthAsync(MaterialStockLength stockLength)
|
|
{
|
|
_context.MaterialStockLengths.Update(stockLength);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task DeleteStockLengthAsync(int id)
|
|
{
|
|
var stockLength = await _context.MaterialStockLengths.FindAsync(id);
|
|
if (stockLength != null)
|
|
{
|
|
_context.MaterialStockLengths.Remove(stockLength);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public async Task<bool> StockLengthExistsAsync(int materialId, decimal lengthInches, int? excludeId = null)
|
|
{
|
|
var query = _context.MaterialStockLengths
|
|
.Where(s => s.MaterialId == materialId && s.LengthInches == lengthInches && s.IsActive);
|
|
if (excludeId.HasValue)
|
|
{
|
|
query = query.Where(s => s.Id != excludeId.Value);
|
|
}
|
|
return await query.AnyAsync();
|
|
}
|
|
}
|