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); } } }