mirror of
https://github.com/Lendaia/oe-alga-feladatok.git
synced 2026-04-23 12:26:07 +01:00
111 lines
3.4 KiB
C#
111 lines
3.4 KiB
C#
using System.Reflection;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Xml.Serialization;
|
|
using Castle.Components.DictionaryAdapter.Xml;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.VisualBasic;
|
|
|
|
namespace zh
|
|
{
|
|
public class ProductPackage
|
|
{
|
|
public List<Category> Categories { get; set; } = new List<Category>();
|
|
}
|
|
public class Category
|
|
{
|
|
string Name { get; set; }
|
|
public List<Product> Products = new List<Product>();
|
|
}
|
|
public class Product
|
|
{
|
|
[Requirements(Requirement.RequiredNonEmpty)]
|
|
public string Sku { get; set; }
|
|
|
|
public string Name { get; set; }
|
|
|
|
[Requirements(Requirement.RequiredNonNegative)]
|
|
public int Price { get; set; }
|
|
}
|
|
public class Program
|
|
{
|
|
public static void Main()
|
|
{
|
|
ProductPackage productackage = new ProductPackage();
|
|
XmlSerializer serializer = new XmlSerializer(typeof(ProductPackage));
|
|
using (StreamReader reader = new StreamReader("zh/products.xml"))
|
|
{
|
|
productackage.Categories = (List<Category>)serializer.Deserialize(reader);
|
|
}
|
|
Shop shop = new Shop();
|
|
shop.Category.AddRange(productackage.Categories);
|
|
shop.SaveChanges();
|
|
List<Product> expensive = new List<Product>();
|
|
foreach (Category category in productackage.Categories)
|
|
{
|
|
expensive.Concat(from x in category.Products
|
|
where x.Price > 10000
|
|
select x);
|
|
}
|
|
}
|
|
}
|
|
public enum Requirement
|
|
{
|
|
RequiredNonEmpty,
|
|
RequiredNonNegative
|
|
}
|
|
public class RequirementsAttribute : Attribute
|
|
{
|
|
public Requirement Rule { get; set; }
|
|
public RequirementsAttribute(Requirement rule)
|
|
{
|
|
Rule = rule;
|
|
}
|
|
}
|
|
public class Validator
|
|
{
|
|
public static void Validate(Product product)
|
|
{
|
|
var t = product.GetType();
|
|
var p = t.GetProperty("Sku");
|
|
var a = p.GetCustomAttribute<RequirementsAttribute>();
|
|
var v = p.GetValue(product);
|
|
if (a.Rule == Requirement.RequiredNonEmpty)
|
|
{
|
|
if (v == null || (string)v == "")
|
|
{
|
|
throw new WrongProductException();
|
|
}
|
|
}
|
|
else if(a.Rule == Requirement.RequiredNonNegative)
|
|
{
|
|
if ((int)v < 0)
|
|
{
|
|
throw new WrongProductException();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public class WrongProductException : Exception
|
|
{
|
|
|
|
}
|
|
public class Shop : DbContext
|
|
{
|
|
//public DbSet<ProductPackage> ProductPackage { get; set; }
|
|
public DbSet<Category> Category { get; set; }
|
|
public DbSet<Product> Product { get; set; }
|
|
public DbSet<Product> Expensive { get; set; }
|
|
public Shop()
|
|
{
|
|
Database.EnsureDeleted();
|
|
Database.EnsureCreated();
|
|
}
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
string connStr = @"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=Shopdb;Integrated Security=True;MultipleActiveResultSets=true";
|
|
optionsBuilder.UseSqlServer(connStr);
|
|
base.OnConfiguring(optionsBuilder);
|
|
}
|
|
}
|
|
}
|