From fe3292133ec1d40429361e00de5bc0af1f481e0a Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Tue, 18 Jun 2024 13:36:30 -0700 Subject: [PATCH] Expose error message using ProducesResponseType --- Shared/DataModels/WaferCounter.cs | 9 +++- .../Stateless/IWaferCounterRepository.cs | 2 +- .../ApiControllers/WaferCounterController.cs | 13 ++++- .../Repositories/WaferCounterRepository.cs | 50 +++++++++++-------- 4 files changed, 50 insertions(+), 24 deletions(-) diff --git a/Shared/DataModels/WaferCounter.cs b/Shared/DataModels/WaferCounter.cs index 7b16013..fe1ec43 100644 --- a/Shared/DataModels/WaferCounter.cs +++ b/Shared/DataModels/WaferCounter.cs @@ -3,6 +3,13 @@ namespace OI.Metrology.Shared.DataModels; public record WaferCounter(DateTime DateTime, string DateTimeFormatted, string EquipmentId, + string Message, string Text, int Total, - string SlotMap); \ No newline at end of file + string SlotMap) +{ + + public static WaferCounter GetWaferCounter(string message) => + new(DateTime.MinValue, string.Empty, string.Empty, message, string.Empty, 0, string.Empty); + +} \ No newline at end of file diff --git a/Shared/Models/Stateless/IWaferCounterRepository.cs b/Shared/Models/Stateless/IWaferCounterRepository.cs index 6e82d90..791e571 100644 --- a/Shared/Models/Stateless/IWaferCounterRepository.cs +++ b/Shared/Models/Stateless/IWaferCounterRepository.cs @@ -4,6 +4,6 @@ public interface IWaferCounterRepository { string? GetSlotMap(string line1, string line2); - DataModels.WaferCounter GetLastQuantityAndSlotMap(string area, string waferSize); + DataModels.WaferCounter? GetLastQuantityAndSlotMap(string area, string waferSize); } \ No newline at end of file diff --git a/Wafer-Counter/ApiControllers/WaferCounterController.cs b/Wafer-Counter/ApiControllers/WaferCounterController.cs index f610166..e99229a 100644 --- a/Wafer-Counter/ApiControllers/WaferCounterController.cs +++ b/Wafer-Counter/ApiControllers/WaferCounterController.cs @@ -12,7 +12,16 @@ public class WaferCounterController : Controller, IWaferCounterController _WaferCounterRepository = waferCounterRepository; + [ProducesResponseType(StatusCodes.Status400BadRequest)] [HttpGet("{waferSize}/last-quantity-and-slot-map")] - public IActionResult GetLastQuantityAndSlotMap(string area, string waferSize) => - Json(_WaferCounterRepository.GetLastQuantityAndSlotMap(area, waferSize)); + public IActionResult GetLastQuantityAndSlotMap(string area, string waferSize) + { + Shared.DataModels.WaferCounter? waferCounter = _WaferCounterRepository.GetLastQuantityAndSlotMap(area, waferSize); + if (waferCounter is null) + return this.BadRequest(); + else if (!string.IsNullOrEmpty(waferCounter.Message)) + return this.BadRequest(waferCounter.Message); + else + return Json(waferCounter); + } } \ No newline at end of file diff --git a/Wafer-Counter/Repositories/WaferCounterRepository.cs b/Wafer-Counter/Repositories/WaferCounterRepository.cs index 4a74770..d350f43 100644 --- a/Wafer-Counter/Repositories/WaferCounterRepository.cs +++ b/Wafer-Counter/Repositories/WaferCounterRepository.cs @@ -1,7 +1,7 @@ -using OI.Metrology.Wafer.Counter.Models; using OI.Metrology.Shared.DataModels; using OI.Metrology.Shared.Models; using OI.Metrology.Shared.Models.Stateless; +using OI.Metrology.Wafer.Counter.Models; using System.Globalization; namespace OI.Metrology.Wafer.Counter.Repository; @@ -145,38 +145,48 @@ public class WaferCounterRepository : IWaferCounterRepository return results; } - private static WaferCounter GetLastQuantityAndSlotMap(string waferSize, HttpClient httpClient, NginxFileSystemSortable nginxFileSystemSortable) + private static WaferCounter? GetLastQuantityAndSlotMap(string waferSize, HttpClient httpClient, NginxFileSystemSortable nginxFileSystemSortable) { - WaferCounter result; - string text = string.Empty; + WaferCounter? result; Task value = httpClient.GetStringAsync(nginxFileSystemSortable.Uri); value.Wait(); string[] lines = value.Result.Split("\r\n"); - if (lines.Length <= 1) - throw new Exception("Incomplete file length!"); - string[] segments = nginxFileSystemSortable.Name.Split('-'); - Record record = GetRecord(lines[0], lines[1]); - string equipmentId = segments.Length <= 1 ? nginxFileSystemSortable.Name : segments[1].Split('.')[0]; - if (string.IsNullOrEmpty(record.SlotMap) || record.SlotMap.Length != 25) - throw new Exception("Wrong length for slot-map!"); - if (record.Total != record.Check) - throw new Exception($"Checksum has failed. {record.Total} != {record.Check}"); - result = new(nginxFileSystemSortable.DateTime, nginxFileSystemSortable.DateTime.ToString("yyyy-MM-dd hh:mm tt"), $"WC{waferSize}{equipmentId}", text, record.Total, record.SlotMap); + if (lines.Length < 3) + result = WaferCounter.GetWaferCounter("Incomplete file length!"); + else + { + string text = string.Empty; + string[] segments = nginxFileSystemSortable.Name.Split('-'); + Record record = GetRecord(lines[^3], lines[^2]); + string equipmentId = segments.Length <= 1 ? nginxFileSystemSortable.Name : segments[1].Split('.')[0]; + if (string.IsNullOrEmpty(record.SlotMap) || record.SlotMap.Length != 25) + result = WaferCounter.GetWaferCounter("Wrong length for slot-map!"); + else + { + if (record.Total != record.Check) + result = WaferCounter.GetWaferCounter($"Checksum has failed. {record.Total} != {record.Check}"); + else + result = new(nginxFileSystemSortable.DateTime, nginxFileSystemSortable.DateTime.ToString("yyyy-MM-dd hh:mm tt"), $"WC{waferSize}{equipmentId}", string.Empty, text, record.Total, record.SlotMap); + } + } return result; } - WaferCounter IWaferCounterRepository.GetLastQuantityAndSlotMap(string area, string waferSize) + WaferCounter? IWaferCounterRepository.GetLastQuantityAndSlotMap(string area, string waferSize) { - WaferCounter result; + WaferCounter? result; Uri waferSizeUri = GetWaferSizeUri(area, waferSize); HttpClient httpClient = _HttpClientFactory.CreateClient(); string waferSizeDirectory = GetWaferSizeDirectory(area, waferSize, destination: false); List nginxFileSystemSortableCollection = GetNginxFileSystemSortableCollection(httpClient, waferSizeUri); if (nginxFileSystemSortableCollection.Count < 1) - throw new Exception("No files!"); - result = GetLastQuantityAndSlotMap(waferSize, httpClient, nginxFileSystemSortableCollection[0]); - for (int i = 0; i < nginxFileSystemSortableCollection.Count; i++) - MoveFile(waferSizeDirectory, nginxFileSystemSortableCollection[i]); + result = WaferCounter.GetWaferCounter("No files!"); + else + { + result = GetLastQuantityAndSlotMap(waferSize, httpClient, nginxFileSystemSortableCollection[0]); + for (int i = 0; i < nginxFileSystemSortableCollection.Count; i++) + MoveFile(waferSizeDirectory, nginxFileSystemSortableCollection[i]); + } return result; }