Add project files.
This commit is contained in:
25
hszf-z2-gyakorlas.sln
Normal file
25
hszf-z2-gyakorlas.sln
Normal file
@@ -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
|
||||||
190
hszf-z2-gyakorlas/Program.cs
Normal file
190
hszf-z2-gyakorlas/Program.cs
Normal file
@@ -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<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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
28
hszf-z2-gyakorlas/hszf-z2-gyakorlas.csproj
Normal file
28
hszf-z2-gyakorlas/hszf-z2-gyakorlas.csproj
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<RootNamespace>hszf_z2_gyakorlas</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.10" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="9.0.10" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.10" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.10">
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="products.xml">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
164
hszf-z2-gyakorlas/products.xml
Normal file
164
hszf-z2-gyakorlas/products.xml
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<ProductPackage>
|
||||||
|
<Categories>
|
||||||
|
<Category>
|
||||||
|
<Name>Coffee</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>COF-1001</Sku><Name>Premium Arabica</Name><Price>12990</Price></Product>
|
||||||
|
<Product><Sku>COF-1002</Sku><Name>Extra Dark Roast</Name><Price>9990</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Tea</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>TEA-2001</Sku><Name>Green Tea</Name><Price>14990</Price></Product>
|
||||||
|
<Product><Sku>TEA-2002</Sku><Name>Black Tea</Name><Price>8990</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Chocolate</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>CHO-3001</Sku><Name>Dark Chocolate 85%</Name><Price>11990</Price></Product>
|
||||||
|
<Product><Sku>CHO-3002</Sku><Name>Milk Chocolate</Name><Price>4990</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Biscuits</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>BIS-4001</Sku><Name>Butter Cookies</Name><Price>10990</Price></Product>
|
||||||
|
<Product><Sku>BIS-4002</Sku><Name>Chocolate Chip</Name><Price>9990</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Juice</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>JUI-5001</Sku><Name>Orange Juice</Name><Price>6990</Price></Product>
|
||||||
|
<Product><Sku>JUI-5002</Sku><Name>Mango Juice</Name><Price>10990</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Snacks</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>SNA-6001</Sku><Name>Potato Chips</Name><Price>4990</Price></Product>
|
||||||
|
<Product><Sku>SNA-6002</Sku><Name>Nachos Cheese</Name><Price>11990</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Water</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>WAT-7001</Sku><Name>Mineral Water 0.5L</Name><Price>1990</Price></Product>
|
||||||
|
<Product><Sku>WAT-7002</Sku><Name>Mineral Water 1.5L</Name><Price>2490</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Energy Drinks</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>ENE-8001</Sku><Name>Energy Boost</Name><Price>11990</Price></Product>
|
||||||
|
<Product><Sku>ENE-8002</Sku><Name>Zero Sugar Energy</Name><Price>10990</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Beer</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>BEE-9001</Sku><Name>Lager Classic</Name><Price>5990</Price></Product>
|
||||||
|
<Product><Sku>BEE-9002</Sku><Name>Craft IPA</Name><Price>12990</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Wine</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>WIN-10001</Sku><Name>Red Merlot</Name><Price>18990</Price></Product>
|
||||||
|
<Product><Sku>WIN-10002</Sku><Name>White Chardonnay</Name><Price>17990</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Cheese</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>CHE-11001</Sku><Name>Gouda</Name><Price>8990</Price></Product>
|
||||||
|
<Product><Sku>CHE-11002</Sku><Name>Parmesan</Name><Price>13990</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Meat</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>MEA-12001</Sku><Name>Beef Steak</Name><Price>25990</Price></Product>
|
||||||
|
<Product><Sku>MEA-12002</Sku><Name>Pork Sausage</Name><Price>9990</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Fish</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>FIS-13001</Sku><Name>Salmon Fillet</Name><Price>18990</Price></Product>
|
||||||
|
<Product><Sku>FIS-13002</Sku><Name>Tuna Can</Name><Price>6990</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Vegetables</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>VEG-14001</Sku><Name>Broccoli</Name><Price>4990</Price></Product>
|
||||||
|
<Product><Sku>VEG-14002</Sku><Name>Carrots</Name><Price>2990</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Fruits</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>FRU-15001</Sku><Name>Bananas</Name><Price>3990</Price></Product>
|
||||||
|
<Product><Sku>FRU-15002</Sku><Name>Apples</Name><Price>4490</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Dairy</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>DAI-16001</Sku><Name>Whole Milk</Name><Price>2590</Price></Product>
|
||||||
|
<Product><Sku>DAI-16002</Sku><Name>Yogurt</Name><Price>3290</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Bread</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>BRE-17001</Sku><Name>Wholegrain Bread</Name><Price>6990</Price></Product>
|
||||||
|
<Product><Sku>BRE-17002</Sku><Name>White Toast</Name><Price>5990</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Electronics</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>ELE-18001</Sku><Name>Wireless Headphones</Name><Price>29990</Price></Product>
|
||||||
|
<Product><Sku>ELE-18002</Sku><Name>Bluetooth Speaker</Name><Price>24990</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Books</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>BOO-19001</Sku><Name>Programming in C#</Name><Price>15990</Price></Product>
|
||||||
|
<Product><Sku>BOO-19002</Sku><Name>History of AI</Name><Price>18990</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
|
||||||
|
<Category>
|
||||||
|
<Name>Games</Name>
|
||||||
|
<Products>
|
||||||
|
<Product><Sku>GAM-20001</Sku><Name>Chess Set</Name><Price>8990</Price></Product>
|
||||||
|
<Product><Sku>GAM-2002</Sku><Name>Video Game Controller</Name><Price>21990</Price></Product>
|
||||||
|
</Products>
|
||||||
|
</Category>
|
||||||
|
</Categories>
|
||||||
|
</ProductPackage>
|
||||||
Reference in New Issue
Block a user