191 lines
6.1 KiB
C#
191 lines
6.1 KiB
C#
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<product> Products { get; set; }
|
|
|
|
public category(string name, List<product> products)
|
|
{
|
|
this.Name = name;
|
|
this.Products = products;
|
|
}
|
|
|
|
public category()
|
|
{
|
|
Products = new HashSet<product>();
|
|
}
|
|
|
|
}
|
|
|
|
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<category> categories = new List<category>();
|
|
foreach (var item in doc.Descendants("Categories").Descendants("Category"))
|
|
{
|
|
string nev = item.Element("Name").Value;
|
|
List<product> array2 = new List<product>();
|
|
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<category> categories1 = JsonConvert.DeserializeObject<List<category>>(asd);
|
|
Console.WriteLine((categories1[1].Products).ToList()[0].Name);
|
|
}
|
|
}
|
|
|
|
public class Validate
|
|
{
|
|
public static bool Valid(List<category> 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<RequiredNonEmpty>() is not null && (propertyInfo.GetValue(item) == null || propertyInfo.GetValue(item) == ""))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static bool ValidPrice(List<category> 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<CorrectPrice>();
|
|
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<category> 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);
|
|
}
|
|
}
|
|
|
|
}
|