Change Sku to RhSolutions.Api implementation

This commit is contained in:
Sergey Chebotar 2022-12-22 14:52:54 +03:00
parent 8ac3234473
commit f267b27375
2 changed files with 87 additions and 37 deletions

View File

@ -2,6 +2,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using RhSolutions.Models; using RhSolutions.Models;
using System.Linq;
namespace RhSolutions.Controllers namespace RhSolutions.Controllers
{ {
@ -56,9 +57,9 @@ namespace RhSolutions.Controllers
{ {
object current = cells[row, column]; object current = cells[row, column];
if (Sku.TryParse(current.ToString(), out Sku rauSku)) if (Sku.TryParse(current.ToString(), out var rauSku))
{ {
sku = rauSku.ToString(); sku = rauSku.FirstOrDefault().ToString() ?? null;
} }
else if (current.GetType() == typeof(string) else if (current.GetType() == typeof(string)

View File

@ -1,11 +1,15 @@
using System.Text.RegularExpressions; using System.Collections.Generic;
using System.Linq;
using System;
using System.Text.RegularExpressions;
namespace RhSolutions.Models namespace RhSolutions.Models
{ {
internal class Sku public class Sku
{ {
public string Article { get; private set; } private const string matchPattern = @"([1\D]|\b)(?<Article>\d{6})([1\s-]|)(?<Variant>\d{3})\b";
public string Variant { get; private set; } private string _article;
private string _variant;
public Sku(string article, string variant) public Sku(string article, string variant)
{ {
@ -13,49 +17,94 @@ namespace RhSolutions.Models
Variant = variant; Variant = variant;
} }
public static bool TryParse(string line, out Sku rehauSku) public string Id
{ {
Match match; get
match = Regex.Match(line, @"\b[1]\d{6}[1]\d{3}\b");
if (match.Success)
{ {
string sku = match.Value.Substring(1, 6); return $"1{Article}1{Variant}";
string variant = match.Value.Substring(8, 3); }
rehauSku = new Sku(sku, variant); set
return true; {
if (TryParse(value, out IEnumerable<Sku> skus))
{
if (skus.Count() > 1)
{
throw new ArgumentException($"More than one valid sku detected: {value}");
}
else
{
this.Article = skus.First().Article;
this.Variant = skus.First().Variant;
}
}
else
{
throw new ArgumentException($"Invalid sku input: {value}");
}
}
}
public string Article
{
get
{
return _article;
}
set
{
if (value == null || value.Length != 6 || value.Where(c => char.IsDigit(c)).Count() != 6)
{
throw new ArgumentException($"Wrong Article: {Article}");
}
else
{
_article = value;
}
}
}
public string Variant
{
get
{
return _variant;
}
set
{
if (value == null || value.Length != 3 || value.Where(c => char.IsDigit(c)).Count() != 3)
{
throw new ArgumentException($"Wrong Variant: {Variant}");
}
else _variant = value;
}
}
public static IEnumerable<Sku> GetValidSkus(string line)
{
MatchCollection matches = Regex.Matches(line, matchPattern);
if (matches.Count == 0)
{
yield break;
}
else
{
foreach (Match m in matches)
{
yield return new Sku(m.Groups["Article"].Value, m.Groups["Variant"].Value);
}
}
} }
match = Regex.Match(line, @"\b\d{6}\D\d{3}\b"); public static bool TryParse(string line, out IEnumerable<Sku> skus)
if (match.Success)
{ {
string sku = match.Value.Substring(0, 6); MatchCollection matches = Regex.Matches(line, matchPattern);
string variant = match.Value.Substring(7, 3); if (matches.Count == 0)
rehauSku = new Sku(sku, variant);
return true;
}
match = Regex.Match(line, @"\b\d{9}\b");
if (match.Success)
{ {
string sku = match.Value.Substring(0, 6); skus = Enumerable.Empty<Sku>();
string variant = match.Value.Substring(6, 3); return false;
rehauSku = new Sku(sku, variant);
return true;
}
match = Regex.Match(line, @"\b\d{6}\b");
if (match.Success)
{
string sku = match.Value.Substring(0, 6);
string variant = "001";
rehauSku = new Sku(sku, variant);
return true;
} }
else else
{ {
rehauSku = null; skus = GetValidSkus(line);
return false; return true;
} }
} }