Expose error message using ProducesResponseType

This commit is contained in:
Mike Phares 2024-06-18 13:36:30 -07:00
parent e7b721fdb3
commit fe3292133e
4 changed files with 50 additions and 24 deletions

View File

@ -3,6 +3,13 @@ namespace OI.Metrology.Shared.DataModels;
public record WaferCounter(DateTime DateTime, public record WaferCounter(DateTime DateTime,
string DateTimeFormatted, string DateTimeFormatted,
string EquipmentId, string EquipmentId,
string Message,
string Text, string Text,
int Total, int Total,
string SlotMap); string SlotMap)
{
public static WaferCounter GetWaferCounter(string message) =>
new(DateTime.MinValue, string.Empty, string.Empty, message, string.Empty, 0, string.Empty);
}

View File

@ -4,6 +4,6 @@ public interface IWaferCounterRepository
{ {
string? GetSlotMap(string line1, string line2); string? GetSlotMap(string line1, string line2);
DataModels.WaferCounter GetLastQuantityAndSlotMap(string area, string waferSize); DataModels.WaferCounter? GetLastQuantityAndSlotMap(string area, string waferSize);
} }

View File

@ -12,7 +12,16 @@ public class WaferCounterController : Controller, IWaferCounterController<IActio
public WaferCounterController(IWaferCounterRepository waferCounterRepository) => public WaferCounterController(IWaferCounterRepository waferCounterRepository) =>
_WaferCounterRepository = waferCounterRepository; _WaferCounterRepository = waferCounterRepository;
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[HttpGet("{waferSize}/last-quantity-and-slot-map")] [HttpGet("{waferSize}/last-quantity-and-slot-map")]
public IActionResult GetLastQuantityAndSlotMap(string area, string waferSize) => public IActionResult GetLastQuantityAndSlotMap(string area, string waferSize)
Json(_WaferCounterRepository.GetLastQuantityAndSlotMap(area, 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);
}
} }

View File

@ -1,7 +1,7 @@
using OI.Metrology.Wafer.Counter.Models;
using OI.Metrology.Shared.DataModels; using OI.Metrology.Shared.DataModels;
using OI.Metrology.Shared.Models; using OI.Metrology.Shared.Models;
using OI.Metrology.Shared.Models.Stateless; using OI.Metrology.Shared.Models.Stateless;
using OI.Metrology.Wafer.Counter.Models;
using System.Globalization; using System.Globalization;
namespace OI.Metrology.Wafer.Counter.Repository; namespace OI.Metrology.Wafer.Counter.Repository;
@ -145,38 +145,48 @@ public class WaferCounterRepository : IWaferCounterRepository
return results; return results;
} }
private static WaferCounter GetLastQuantityAndSlotMap(string waferSize, HttpClient httpClient, NginxFileSystemSortable nginxFileSystemSortable) private static WaferCounter? GetLastQuantityAndSlotMap(string waferSize, HttpClient httpClient, NginxFileSystemSortable nginxFileSystemSortable)
{ {
WaferCounter result; WaferCounter? result;
string text = string.Empty;
Task<string> value = httpClient.GetStringAsync(nginxFileSystemSortable.Uri); Task<string> value = httpClient.GetStringAsync(nginxFileSystemSortable.Uri);
value.Wait(); value.Wait();
string[] lines = value.Result.Split("\r\n"); string[] lines = value.Result.Split("\r\n");
if (lines.Length <= 1) if (lines.Length < 3)
throw new Exception("Incomplete file length!"); result = WaferCounter.GetWaferCounter("Incomplete file length!");
string[] segments = nginxFileSystemSortable.Name.Split('-'); else
Record record = GetRecord(lines[0], lines[1]); {
string equipmentId = segments.Length <= 1 ? nginxFileSystemSortable.Name : segments[1].Split('.')[0]; string text = string.Empty;
if (string.IsNullOrEmpty(record.SlotMap) || record.SlotMap.Length != 25) string[] segments = nginxFileSystemSortable.Name.Split('-');
throw new Exception("Wrong length for slot-map!"); Record record = GetRecord(lines[^3], lines[^2]);
if (record.Total != record.Check) string equipmentId = segments.Length <= 1 ? nginxFileSystemSortable.Name : segments[1].Split('.')[0];
throw new Exception($"Checksum has failed. {record.Total} != {record.Check}"); if (string.IsNullOrEmpty(record.SlotMap) || record.SlotMap.Length != 25)
result = new(nginxFileSystemSortable.DateTime, nginxFileSystemSortable.DateTime.ToString("yyyy-MM-dd hh:mm tt"), $"WC{waferSize}{equipmentId}", text, record.Total, record.SlotMap); 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; 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); Uri waferSizeUri = GetWaferSizeUri(area, waferSize);
HttpClient httpClient = _HttpClientFactory.CreateClient(); HttpClient httpClient = _HttpClientFactory.CreateClient();
string waferSizeDirectory = GetWaferSizeDirectory(area, waferSize, destination: false); string waferSizeDirectory = GetWaferSizeDirectory(area, waferSize, destination: false);
List<NginxFileSystemSortable> nginxFileSystemSortableCollection = GetNginxFileSystemSortableCollection(httpClient, waferSizeUri); List<NginxFileSystemSortable> nginxFileSystemSortableCollection = GetNginxFileSystemSortableCollection(httpClient, waferSizeUri);
if (nginxFileSystemSortableCollection.Count < 1) if (nginxFileSystemSortableCollection.Count < 1)
throw new Exception("No files!"); result = WaferCounter.GetWaferCounter("No files!");
result = GetLastQuantityAndSlotMap(waferSize, httpClient, nginxFileSystemSortableCollection[0]); else
for (int i = 0; i < nginxFileSystemSortableCollection.Count; i++) {
MoveFile(waferSizeDirectory, nginxFileSystemSortableCollection[i]); result = GetLastQuantityAndSlotMap(waferSize, httpClient, nginxFileSystemSortableCollection[0]);
for (int i = 0; i < nginxFileSystemSortableCollection.Count; i++)
MoveFile(waferSizeDirectory, nginxFileSystemSortableCollection[i]);
}
return result; return result;
} }