| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | |
| | | 3 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers.Base; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Какие поля заказа реально пришли с формы транзакции. |
| | | 7 | | /// Колонка, закрытая правами доступа, не рендерится, поэтому её поле приходит как null — |
| | | 8 | | /// а null для статуса, поставщика и номера заказа поставщику трактуется как «очистить значение». |
| | | 9 | | /// Список присланных полей позволяет отличить очистку от отсутствия поля в форме. |
| | | 10 | | /// </summary> |
| | | 11 | | public sealed class SubmittedOrderFields |
| | | 12 | | { |
| | 1 | 13 | | private static readonly Regex FieldKey = new( |
| | 1 | 14 | | @"^Orders\[(?<index>\d+)\]\.(?<property>[^.\[]+)", |
| | 1 | 15 | | RegexOptions.IgnoreCase | RegexOptions.Compiled); |
| | | 16 | | |
| | | 17 | | private readonly Dictionary<int, HashSet<string>> _byOrderId; |
| | | 18 | | |
| | 12 | 19 | | private SubmittedOrderFields(Dictionary<int, HashSet<string>> byOrderId) => _byOrderId = byOrderId; |
| | | 20 | | |
| | | 21 | | /// <summary>Пустой список: все заказы обрабатываются по прежним правилам.</summary> |
| | 2 | 22 | | public static SubmittedOrderFields Empty { get; } = new(new Dictionary<int, HashSet<string>>()); |
| | | 23 | | |
| | | 24 | | public static SubmittedOrderFields FromForm(IFormCollection? form) |
| | | 25 | | { |
| | 6 | 26 | | if (form == null || form.Count == 0) |
| | 1 | 27 | | return Empty; |
| | | 28 | | |
| | 5 | 29 | | var byIndex = new Dictionary<int, HashSet<string>>(); |
| | 32 | 30 | | foreach (var key in form.Keys) |
| | | 31 | | { |
| | 11 | 32 | | var match = FieldKey.Match(key); |
| | 11 | 33 | | if (!match.Success) |
| | | 34 | | continue; |
| | | 35 | | |
| | 10 | 36 | | var index = int.Parse(match.Groups["index"].Value); |
| | 10 | 37 | | if (!byIndex.TryGetValue(index, out var properties)) |
| | | 38 | | { |
| | 6 | 39 | | properties = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | 6 | 40 | | byIndex[index] = properties; |
| | | 41 | | } |
| | | 42 | | |
| | 10 | 43 | | properties.Add(match.Groups["property"].Value); |
| | | 44 | | } |
| | | 45 | | |
| | 5 | 46 | | var byOrderId = new Dictionary<int, HashSet<string>>(); |
| | 22 | 47 | | foreach (var (index, properties) in byIndex) |
| | | 48 | | { |
| | | 49 | | // Заказ опознаём по его же скрытому полю Id: индексы строк не совпадают с идентификаторами. |
| | 6 | 50 | | if (int.TryParse(form[$"Orders[{index}].Id"].FirstOrDefault(), out var orderId)) |
| | 5 | 51 | | byOrderId[orderId] = properties; |
| | | 52 | | } |
| | | 53 | | |
| | 5 | 54 | | return new SubmittedOrderFields(byOrderId); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Поля, присланные для заказа, либо null — если строки в форме не было и судить не о чем. |
| | | 59 | | /// </summary> |
| | | 60 | | public IReadOnlySet<string>? For(int orderId) => |
| | 8 | 61 | | _byOrderId.TryGetValue(orderId, out var properties) ? properties : null; |
| | | 62 | | } |