aboutsummaryrefslogtreecommitdiff
path: root/RhSolutions.ProductSku/ProductSku.cs
diff options
context:
space:
mode:
authorSerghei Cebotari <serghei@cebotari.ru>2023-12-08 23:14:56 +0300
committerSerghei Cebotari <serghei@cebotari.ru>2023-12-08 23:14:56 +0300
commitf448cccbebaf9c6cc92f5576cb89e8918b1beec9 (patch)
treee319a8e8e0e4f763d852ae66479f71f67311a9cd /RhSolutions.ProductSku/ProductSku.cs
parente6546254baf8130638aa0dee12f247769da4e308 (diff)
Add RhSolutions.ProductSku project
Diffstat (limited to 'RhSolutions.ProductSku/ProductSku.cs')
-rw-r--r--RhSolutions.ProductSku/ProductSku.cs194
1 files changed, 194 insertions, 0 deletions
diff --git a/RhSolutions.ProductSku/ProductSku.cs b/RhSolutions.ProductSku/ProductSku.cs
new file mode 100644
index 0000000..7e3e763
--- /dev/null
+++ b/RhSolutions.ProductSku/ProductSku.cs
@@ -0,0 +1,194 @@
+using System.Text.RegularExpressions;
+using Newtonsoft.Json;
+
+namespace RhSolutions.Models;
+public class ProductSku
+{
+ [JsonIgnore]
+ public string Id
+ {
+ get
+ {
+ return $"1{Article}{Delimiter}{Variant}";
+ }
+ set
+ {
+ var matches = GetMatches(value);
+ if (matches.Count == 0)
+ {
+ throw new ArgumentException($"Wrong Id: {value}");
+ }
+ else
+ {
+ var p = GetProducts(matches).First();
+ _article = p.Article;
+ _delimiter = p.Delimiter;
+ _variant = p.Variant;
+ }
+ }
+ }
+ private const string matchPattern = @"(?<Lead>[1\s]|^|\b)(?<Article>\d{6})(?<Delimiter>[\s13-])(?<Variant>\d{3})(\b|$)";
+ private string _article;
+ private string _variant;
+ private char _delimiter = '1';
+
+ [JsonConstructor]
+ public ProductSku(string article, string delimiter, string variant)
+ {
+ _article = article;
+ _delimiter = delimiter[0];
+ _variant = variant;
+ }
+
+ public ProductSku(string article, string variant)
+ {
+ _article = IsCorrectArticle(article) ? article
+ : throw new ArgumentException($"Wrong Article: {article}");
+ _variant = IsCorrectVariant(variant) ? variant
+ : throw new ArgumentException($"Wrong Variant: {variant}");
+ }
+ public ProductSku(string line)
+ {
+ var matches = GetMatches(line);
+ if (matches.Count == 0)
+ {
+ throw new ArgumentException($"Wrong new Sku input {line}");
+ }
+ else
+ {
+ var p = GetProducts(matches).First();
+ _article = p.Article;
+ _delimiter = p.Delimiter;
+ _variant = p.Variant;
+ }
+ }
+
+ private ProductSku(Match match)
+ {
+ _article = match.Groups["Article"].Value;
+ _delimiter = match.Groups["Delimiter"].Value switch
+ {
+ "3" => '3',
+ _ => '1'
+ };
+ _variant = match.Groups["Variant"].Value;
+ }
+ public string Article
+ {
+ get => _article;
+ set
+ {
+ _article = IsCorrectArticle(value) ? value
+ : throw new ArgumentException($"Wrong Article: {value}");
+ }
+ }
+ public string Variant
+ {
+ get => _variant;
+ set
+ {
+ _variant = IsCorrectVariant(value) ? value
+ : throw new ArgumentException($"Wrong Variant: {value}");
+ }
+ }
+
+ public char Delimiter
+ {
+ get => _delimiter;
+ set
+ {
+ if (value != '1' || value != '3')
+ {
+ throw new ArgumentException($"Wrong Delimiter: {value}");
+ }
+ else
+ {
+ _delimiter = value;
+ }
+ }
+ }
+
+ public static IEnumerable<ProductSku> Parse(string line)
+ {
+ MatchCollection matches = GetMatches(line);
+ if (matches.Count == 0)
+ {
+ return Enumerable.Empty<ProductSku>();
+ }
+ else
+ {
+ return GetProducts(matches);
+ }
+ }
+
+ public static bool TryParse(string line, out IEnumerable<ProductSku> skus)
+ {
+ MatchCollection matches = GetMatches(line);
+ if (matches.Count == 0)
+ {
+ skus = Enumerable.Empty<ProductSku>();
+ return false;
+ }
+
+ else
+ {
+ skus = GetProducts(matches);
+ return true;
+ }
+ }
+
+ private static MatchCollection GetMatches(string line)
+ {
+ return Regex.Matches(line, matchPattern);
+ }
+
+ private static IEnumerable<ProductSku> GetProducts(MatchCollection matches)
+ {
+ foreach (Match match in matches)
+ {
+ yield return new ProductSku(match);
+ }
+ }
+
+ private static bool IsCorrectArticle(string line)
+ {
+ return line != null
+ && line.Length == 6
+ && line.All(c => char.IsDigit(c));
+ }
+
+ private static bool IsCorrectVariant(string line)
+ {
+ return line != null
+ && line.Length == 3
+ && line.All(c => char.IsDigit(c));
+ }
+
+ public override string ToString()
+ {
+ return Id;
+ }
+
+ public override bool Equals(object obj)
+ {
+ if (obj == null || GetType() != obj.GetType())
+ {
+ return false;
+ }
+
+ ProductSku other = (ProductSku)obj;
+
+ return this.Article == other.Article &&
+ this.Delimiter == other.Delimiter &&
+ this.Variant == other.Variant;
+ }
+
+ public override int GetHashCode()
+ {
+ HashCode hash = new();
+ hash.Add(_article);
+ hash.Add(_variant);
+ hash.Add(_delimiter);
+ return hash.ToHashCode();
+ }
+}