shoc-backend/SeaHaven.Services/Helpers/WorkOrderIdSet.cs

39 lines
1.3 KiB
C#
Raw Normal View History

using System.Globalization;
namespace SeaHaven.Services.Helpers
{
/// <summary>
/// The board's exact work-order id filter: comma-separated positive integers, deduplicated,
/// at most <see cref="MaxCount"/> distinct ids.
/// </summary>
public static class WorkOrderIdSet
{
public const int MaxCount = 200;
/// <summary>
/// Null when <paramref name="raw"/> is absent or blank. Throws <see cref="ArgumentException"/>
/// (a 400 on the board) for any token that is not a positive integer, or too many ids.
/// </summary>
public static IReadOnlyList<int>? ParseOrThrow(string? raw)
{
if (string.IsNullOrWhiteSpace(raw))
return null;
var ids = new List<int>();
var seen = new HashSet<int>();
foreach (var token in raw.Split(','))
{
if (!int.TryParse(token.Trim(), NumberStyles.None, CultureInfo.InvariantCulture, out var id) || id <= 0)
throw new ArgumentException("ids must be a comma-separated list of positive work order ids.");
if (seen.Add(id))
ids.Add(id);
}
if (ids.Count > MaxCount)
throw new ArgumentException($"ids accepts at most {MaxCount} work orders.");
return ids;
}
}
}