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.Collections.Generic;
using RhSolutions.Models;
using System.Linq;
namespace RhSolutions.Controllers
{
@ -56,9 +57,9 @@ namespace RhSolutions.Controllers
{
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)

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
{
internal class Sku
public class Sku
{
public string Article { get; private set; }
public string Variant { get; private set; }
private const string matchPattern = @"([1\D]|\b)(?<Article>\d{6})([1\s-]|)(?<Variant>\d{3})\b";
private string _article;
private string _variant;
public Sku(string article, string variant)
{
@ -13,49 +17,94 @@ namespace RhSolutions.Models
Variant = variant;
}
public static bool TryParse(string line, out Sku rehauSku)
public string Id
{
Match match;
match = Regex.Match(line, @"\b[1]\d{6}[1]\d{3}\b");
if (match.Success)
get
{
string sku = match.Value.Substring(1, 6);
string variant = match.Value.Substring(8, 3);
rehauSku = new Sku(sku, variant);
return true;
return $"1{Article}1{Variant}";
}
set
{
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");
if (match.Success)
public static bool TryParse(string line, out IEnumerable<Sku> skus)
{
string sku = match.Value.Substring(0, 6);
string variant = match.Value.Substring(7, 3);
rehauSku = new Sku(sku, variant);
return true;
}
match = Regex.Match(line, @"\b\d{9}\b");
if (match.Success)
MatchCollection matches = Regex.Matches(line, matchPattern);
if (matches.Count == 0)
{
string sku = match.Value.Substring(0, 6);
string variant = match.Value.Substring(6, 3);
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;
skus = Enumerable.Empty<Sku>();
return false;
}
else
{
rehauSku = null;
return false;
skus = GetValidSkus(line);
return true;
}
}