From 66ff51e014ddc06ece813d0634d0d1b561c2b396 Mon Sep 17 00:00:00 2001 From: Docker Date: Wed, 5 Nov 2025 03:15:57 -0800 Subject: [PATCH] Add project files. --- hszf-z2-gyakorlas.sln | 25 +++ hszf-z2-gyakorlas/Program.cs | 190 +++++++++++++++++++++ hszf-z2-gyakorlas/hszf-z2-gyakorlas.csproj | 28 +++ hszf-z2-gyakorlas/products.xml | 164 ++++++++++++++++++ 4 files changed, 407 insertions(+) create mode 100644 hszf-z2-gyakorlas.sln create mode 100644 hszf-z2-gyakorlas/Program.cs create mode 100644 hszf-z2-gyakorlas/hszf-z2-gyakorlas.csproj create mode 100644 hszf-z2-gyakorlas/products.xml diff --git a/hszf-z2-gyakorlas.sln b/hszf-z2-gyakorlas.sln new file mode 100644 index 0000000..70e581f --- /dev/null +++ b/hszf-z2-gyakorlas.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36623.8 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hszf-z2-gyakorlas", "hszf-z2-gyakorlas\hszf-z2-gyakorlas.csproj", "{EF0023E3-A1F1-4755-944B-3DED4BAE47EC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {EF0023E3-A1F1-4755-944B-3DED4BAE47EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EF0023E3-A1F1-4755-944B-3DED4BAE47EC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EF0023E3-A1F1-4755-944B-3DED4BAE47EC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EF0023E3-A1F1-4755-944B-3DED4BAE47EC}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {15B28962-E262-496E-B7F8-DC1EBB7434E9} + EndGlobalSection +EndGlobal diff --git a/hszf-z2-gyakorlas/Program.cs b/hszf-z2-gyakorlas/Program.cs new file mode 100644 index 0000000..7c98a84 --- /dev/null +++ b/hszf-z2-gyakorlas/Program.cs @@ -0,0 +1,190 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Internal; +using Newtonsoft.Json; +using System; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Reflection; +using System.Text.Json.Serialization; +using System.Xml.Linq; +using System.Xml.Serialization; + +namespace hszf_z2_gyakorlas +{ + public class category + { + [Key] + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + public string Name { get; set; } + [Required] + public virtual ICollection Products { get; set; } + + public category(string name, List products) + { + this.Name = name; + this.Products = products; + } + + public category() + { + Products = new HashSet(); + } + + } + + public class product + { + public product(string sku, string name, int price) + { + this.Sku = sku; + this.Name = name; + this.Price = price; + } + + public product() + { + + } + + [Key] + [RequiredNonEmpty] + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + public string Sku { get; set; } + public string Name { get; set; } + [CorrectPrice(10, 20000)] + public int Price { get; set; } + } + + public class RequiredNonEmpty : Attribute { + + } + + public class CorrectPrice : Attribute + { + int minValue; + int maxValue; + + public CorrectPrice(int minValue, int maxValue) + { + this.minValue = minValue; + this.maxValue = maxValue; + } + + public int MinValue { get => minValue; set => minValue = value; } + public int MaxValue { get => maxValue; set => maxValue = value; } + } + + + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + string[] sorok = File.ReadAllLines("products.xml"); + + XDocument doc = XDocument.Load("products.xml"); + List categories = new List(); + foreach (var item in doc.Descendants("Categories").Descendants("Category")) + { + string nev = item.Element("Name").Value; + List array2 = new List(); + foreach (var item2 in item.Descendants("Products").Descendants("Product")) + { + array2.Add(new product(item2.Element("Sku").Value, item2.Element("Name").Value, int.Parse(item2.Element("Price").Value))); + } + categories.Add(new category(nev, array2)); + + } + + Console.WriteLine(Validate.Valid(categories)); + + Console.WriteLine(Validate.ValidPrice(categories)); + + string connStr = @"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=persondb;Integrated Security=True;MultipleActiveResultSets=true"; + + + CategoryDbContext ctx = new CategoryDbContext(); + + foreach (var item in categories.Select(x => new category(x.Name, x.Products.Where(x => x.Price > 10000).ToList()))) + { + ctx.Categories.Add(item); + } + ctx.SaveChanges(); + + string asd = JsonConvert.SerializeObject(categories, Formatting.Indented); + Console.WriteLine(asd); + File.WriteAllLines("categories.json", [asd]); + + + List categories1 = JsonConvert.DeserializeObject>(asd); + Console.WriteLine((categories1[1].Products).ToList()[0].Name); + } + } + + public class Validate + { + public static bool Valid(List categories) + { + foreach (var category in categories) + { + foreach (var item in category.Products) + { + Type t = item.GetType(); + PropertyInfo[]? propertyInfos = t.GetProperties(); + if (propertyInfos is not null) + { + foreach (PropertyInfo propertyInfo in propertyInfos) + { + if (propertyInfo.GetCustomAttribute() is not null && (propertyInfo.GetValue(item) == null || propertyInfo.GetValue(item) == "")) + { + return false; + } + } + } + } + } + return true; + } + + public static bool ValidPrice(List categories) + { + foreach (var category in categories) + { + foreach (var item in category.Products) + { + Type t = item.GetType(); + PropertyInfo[]? propertyInfos = t.GetProperties(); + if (propertyInfos is not null) + { + foreach (PropertyInfo propertyInfo in propertyInfos) + { + CorrectPrice? correctPrice = propertyInfo.GetCustomAttribute(); + if (correctPrice is not null && ((int)propertyInfo.GetValue(item) > correctPrice.MaxValue || (int)propertyInfo.GetValue(item) < correctPrice.MinValue)) + { + return false; + } + } + } + } + } + return true; + } + } + + public class CategoryDbContext : DbContext + { + public DbSet Categories { get; set; } + public CategoryDbContext() + { + Database.EnsureDeleted(); + Database.EnsureCreated(); + } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + string connStr = @"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=categoriesdb;Integrated Security=True;MultipleActiveResultSets=true"; + optionsBuilder.UseSqlServer(connStr); + base.OnConfiguring(optionsBuilder); + } + } + +} diff --git a/hszf-z2-gyakorlas/hszf-z2-gyakorlas.csproj b/hszf-z2-gyakorlas/hszf-z2-gyakorlas.csproj new file mode 100644 index 0000000..078eef4 --- /dev/null +++ b/hszf-z2-gyakorlas/hszf-z2-gyakorlas.csproj @@ -0,0 +1,28 @@ + + + + Exe + net8.0 + hszf_z2_gyakorlas + enable + enable + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + PreserveNewest + + + + diff --git a/hszf-z2-gyakorlas/products.xml b/hszf-z2-gyakorlas/products.xml new file mode 100644 index 0000000..af44491 --- /dev/null +++ b/hszf-z2-gyakorlas/products.xml @@ -0,0 +1,164 @@ + + + + + Coffee + + COF-1001Premium Arabica12990 + COF-1002Extra Dark Roast9990 + + + + + Tea + + TEA-2001Green Tea14990 + TEA-2002Black Tea8990 + + + + + Chocolate + + CHO-3001Dark Chocolate 85%11990 + CHO-3002Milk Chocolate4990 + + + + + Biscuits + + BIS-4001Butter Cookies10990 + BIS-4002Chocolate Chip9990 + + + + + Juice + + JUI-5001Orange Juice6990 + JUI-5002Mango Juice10990 + + + + + Snacks + + SNA-6001Potato Chips4990 + SNA-6002Nachos Cheese11990 + + + + + Water + + WAT-7001Mineral Water 0.5L1990 + WAT-7002Mineral Water 1.5L2490 + + + + + Energy Drinks + + ENE-8001Energy Boost11990 + ENE-8002Zero Sugar Energy10990 + + + + + Beer + + BEE-9001Lager Classic5990 + BEE-9002Craft IPA12990 + + + + + Wine + + WIN-10001Red Merlot18990 + WIN-10002White Chardonnay17990 + + + + + Cheese + + CHE-11001Gouda8990 + CHE-11002Parmesan13990 + + + + + Meat + + MEA-12001Beef Steak25990 + MEA-12002Pork Sausage9990 + + + + + Fish + + FIS-13001Salmon Fillet18990 + FIS-13002Tuna Can6990 + + + + + Vegetables + + VEG-14001Broccoli4990 + VEG-14002Carrots2990 + + + + + Fruits + + FRU-15001Bananas3990 + FRU-15002Apples4490 + + + + + Dairy + + DAI-16001Whole Milk2590 + DAI-16002Yogurt3290 + + + + + Bread + + BRE-17001Wholegrain Bread6990 + BRE-17002White Toast5990 + + + + + Electronics + + ELE-18001Wireless Headphones29990 + ELE-18002Bluetooth Speaker24990 + + + + + Books + + BOO-19001Programming in C#15990 + BOO-19002History of AI18990 + + + + + Games + + GAM-20001Chess Set8990 + GAM-2002Video Game Controller21990 + + + +