Added Files
This commit is contained in:
@@ -0,0 +1,231 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PepApi.Core.Configuration;
|
||||
using PepApi.Core.Models;
|
||||
using PepLib;
|
||||
using PepLib.Data;
|
||||
using Plate = PepApi.Core.Models.Plate;
|
||||
|
||||
namespace PepApi.Core.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("nests")]
|
||||
public class NestsController : ControllerBase
|
||||
{
|
||||
private readonly PepDB _db;
|
||||
private readonly string _nestDirectory;
|
||||
|
||||
public NestsController(PepDB db, IOptions<PepSettings> settings)
|
||||
{
|
||||
_db = db;
|
||||
_nestDirectory = settings.Value.NestDirectory;
|
||||
}
|
||||
|
||||
[HttpGet("{year:int:regex(^\\d{{4}}$)}")]
|
||||
public async Task<ActionResult<List<NestSummary>>> GetNests(int year, [FromQuery] NestFilterData? filter)
|
||||
{
|
||||
if (year == 0)
|
||||
year = DateTime.Now.Year;
|
||||
|
||||
var dir = Path.Combine(_nestDirectory, year.ToString());
|
||||
|
||||
if (!Directory.Exists(dir))
|
||||
{
|
||||
return Ok(new List<NestSummary>());
|
||||
}
|
||||
|
||||
var nestHeaders = await _db.NestHeaders
|
||||
.Where(n => n.DateProgrammed != null && n.DateProgrammed.Value.Year == year)
|
||||
.ToListAsync();
|
||||
|
||||
var nestSummaries = nestHeaders.Select(ConvertFrom).ToList();
|
||||
|
||||
if (filter != null)
|
||||
{
|
||||
return Ok(filter.Apply(nestSummaries.AsQueryable()).ToList());
|
||||
}
|
||||
|
||||
return Ok(nestSummaries);
|
||||
}
|
||||
|
||||
private static NestSummary ConvertFrom(NestHeader nestHeader)
|
||||
{
|
||||
return new NestSummary
|
||||
{
|
||||
Comments = nestHeader.Comments,
|
||||
Customer = nestHeader.CustomerName,
|
||||
DateCreated = nestHeader.DateProgrammed!.Value,
|
||||
DateLastModified = nestHeader.ModifiedDate!.Value,
|
||||
HasErrors = !nestHeader.Errors.IsNullOrWhiteSpace(),
|
||||
MaterialGrade = nestHeader.MatGrade,
|
||||
MaterialNumber = int.Parse(nestHeader.Material ?? "0"),
|
||||
Name = nestHeader.NestName,
|
||||
Notes = nestHeader.Remarks,
|
||||
ProgrammedBy = nestHeader.Programmer,
|
||||
Revision = nestHeader.UserDefined1,
|
||||
Status = PepHelper.GetStatus(nestHeader.Status),
|
||||
Application = PepHelper.GetApplication(nestHeader.Application)
|
||||
};
|
||||
}
|
||||
|
||||
// Get nest by name - finds most recent if year not specified
|
||||
[HttpGet("{nestName}")]
|
||||
public async Task<ActionResult<NestDetails>> GetNestInfo(string nestName, [FromQuery] int? year = null)
|
||||
{
|
||||
var nestFile = year.HasValue
|
||||
? GetNestPath(nestName, year.Value)
|
||||
: await FindMostRecentNestAsync(nestName);
|
||||
|
||||
if (nestFile == null || !System.IO.File.Exists(nestFile))
|
||||
return NotFound(new { message = "Nest not found" });
|
||||
|
||||
var details = await GetNestDetailsAsync(nestFile);
|
||||
return Ok(details);
|
||||
}
|
||||
|
||||
// Get nest by year and name (backward compatibility)
|
||||
[HttpGet("{year:int:regex(^\\d{{4}}$)}/{nestName}")]
|
||||
public async Task<ActionResult<NestDetails>> GetNestInfoByYear(int year, string nestName)
|
||||
{
|
||||
var nestFile = GetNestPath(nestName, year);
|
||||
|
||||
if (!System.IO.File.Exists(nestFile))
|
||||
return NotFound(new { message = "Nest not found" });
|
||||
|
||||
var details = await GetNestDetailsAsync(nestFile);
|
||||
return Ok(details);
|
||||
}
|
||||
|
||||
// Download nest file - finds most recent if year not specified
|
||||
[HttpGet("{nestName}/download")]
|
||||
public async Task<IActionResult> DownloadFile(string nestName, [FromQuery] int? year = null)
|
||||
{
|
||||
var filePath = year.HasValue
|
||||
? GetNestPath(nestName, year.Value)
|
||||
: await FindMostRecentNestAsync(nestName);
|
||||
|
||||
if (filePath == null || !System.IO.File.Exists(filePath))
|
||||
return NotFound(new { message = "Nest not found" });
|
||||
|
||||
var bytes = System.IO.File.ReadAllBytes(filePath);
|
||||
var fileName = Path.GetFileName(filePath);
|
||||
var mimeType = "application/octet-stream";
|
||||
|
||||
return File(bytes, mimeType, fileName);
|
||||
}
|
||||
|
||||
// Download nest file by year (backward compatibility)
|
||||
[HttpGet("{year:int:regex(^\\d{{4}}$)}/{nestName}/download")]
|
||||
public IActionResult DownloadFileByYear(int year, string nestName)
|
||||
{
|
||||
var filePath = GetNestPath(nestName, year);
|
||||
|
||||
if (!System.IO.File.Exists(filePath))
|
||||
return NotFound(new { message = "Nest not found" });
|
||||
|
||||
var bytes = System.IO.File.ReadAllBytes(filePath);
|
||||
var fileName = Path.GetFileName(filePath);
|
||||
var mimeType = "application/octet-stream";
|
||||
|
||||
return File(bytes, mimeType, fileName);
|
||||
}
|
||||
|
||||
// Get plates - finds most recent if year not specified
|
||||
[HttpGet("{nestName}/plates")]
|
||||
public async Task<ActionResult<List<Plate>>> GetPlates(string nestName, [FromQuery] int? year = null)
|
||||
{
|
||||
var nestFile = year.HasValue
|
||||
? GetNestPath(nestName, year.Value)
|
||||
: await FindMostRecentNestAsync(nestName);
|
||||
|
||||
if (nestFile == null || !System.IO.File.Exists(nestFile))
|
||||
return NotFound(new { message = "Nest not found" });
|
||||
|
||||
var nest = Nest.Load(nestFile);
|
||||
var plates = PepHelper.GetPlates(nest);
|
||||
var combined = PepHelper.CombineLikePlates(plates);
|
||||
|
||||
return Ok(combined);
|
||||
}
|
||||
|
||||
// Get plates by year (backward compatibility)
|
||||
[HttpGet("{year:int:regex(^\\d{{4}}$)}/{nestName}/plates")]
|
||||
public ActionResult<List<Plate>> GetPlatesByYear(int year, string nestName)
|
||||
{
|
||||
var nestFile = GetNestPath(nestName, year);
|
||||
|
||||
if (!System.IO.File.Exists(nestFile))
|
||||
return NotFound(new { message = "Nest not found" });
|
||||
|
||||
var nest = Nest.Load(nestFile);
|
||||
var plates = PepHelper.GetPlates(nest);
|
||||
var combined = PepHelper.CombineLikePlates(plates);
|
||||
|
||||
return Ok(combined);
|
||||
}
|
||||
|
||||
private async Task<string?> FindMostRecentNestAsync(string nestName)
|
||||
{
|
||||
// Query database for most recent nest by name
|
||||
var mostRecent = await _db.NestHeaders
|
||||
.Where(n => n.NestName.ToUpper() == nestName.ToUpper() && n.DateProgrammed != null)
|
||||
.OrderByDescending(n => n.DateProgrammed)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
if (mostRecent == null)
|
||||
return null;
|
||||
|
||||
var year = mostRecent.DateProgrammed!.Value.Year;
|
||||
return GetNestPath(nestName, year);
|
||||
}
|
||||
|
||||
private string GetNestPath(string nestName, int year)
|
||||
{
|
||||
var fileName = nestName + ".zip";
|
||||
var filePath = Path.Combine(_nestDirectory, year.ToString(), fileName);
|
||||
|
||||
if (System.IO.File.Exists(filePath))
|
||||
return filePath;
|
||||
|
||||
fileName = nestName + ".pep";
|
||||
filePath = Path.Combine(_nestDirectory, year.ToString(), fileName);
|
||||
|
||||
return filePath;
|
||||
}
|
||||
|
||||
private async Task<NestDetails> GetNestDetailsAsync(string nestFilePath)
|
||||
{
|
||||
var nest = Nest.Load(nestFilePath);
|
||||
var dir = Path.GetDirectoryName(nestFilePath) + "\\";
|
||||
var name = Path.GetFileNameWithoutExtension(nestFilePath).ToUpper();
|
||||
|
||||
var info = await _db.NestHeaders
|
||||
.FirstOrDefaultAsync(n => n.NestName.ToUpper() == name && dir == n.Path);
|
||||
|
||||
if (info == null)
|
||||
throw new Exception("Nest header not found in database");
|
||||
|
||||
var details = new NestDetails
|
||||
{
|
||||
Name = info.NestName,
|
||||
DateCreated = info.DateProgrammed!.Value,
|
||||
Status = PepHelper.GetStatus(info.Status),
|
||||
DateLastModified = info.ModifiedDate!.Value,
|
||||
Material = PepHelper.GetMaterial(nest),
|
||||
Plates = PepHelper.GetPlates(nest),
|
||||
Parts = PepHelper.GetParts(nest),
|
||||
NumberOfTestSquares = PepHelper.GetTestSquareCount(nest),
|
||||
AreTestSquaresOutOfSequence = !PepHelper.AreTestSquaresInCorrectOrder(nest),
|
||||
Customer = info.CustomerName,
|
||||
Comments = info.Comments,
|
||||
HasErrors = !info.Errors.IsNullOrWhiteSpace(),
|
||||
Notes = info.Remarks,
|
||||
ProgrammedBy = info.Programmer,
|
||||
Revision = info.UserDefined1,
|
||||
Application = PepHelper.GetApplication(info.Application)
|
||||
};
|
||||
|
||||
return details;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user