Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using AutoMapper;
- using ProductShop.Data;
- using ProductShop.Models;
- using ProductShopDatabase.Dtos;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.IO;
- using System.Linq;
- using System.Xml.Serialization;
- namespace ProductShopDatabase
- {
- public class StarUp
- {
- static void Main(string[] args)
- {
- var context = new ProductShopContext();
- context.Database.EnsureDeleted();
- context.Database.EnsureCreated();
- Mapper.Initialize(cfg =>
- {
- cfg.AddProfile<ProductShopProfile>();
- });
- UserDto[] usersDto = DeserializeUsers();
- var users = usersDto.Where(x => IsValid(x)).Select(x => Mapper.Map<User>(x)).ToArray();
- context.Users.AddRange(users);
- context.SaveChanges();
- ProductDto[] productsDto = DeserializeProducts();
- var products = productsDto.Where(x => IsValid(x)).Select(x => Mapper.Map<Product>(x)).ToArray();
- Random random = new Random();
- foreach (var product in products)
- {
- product.SellerId = random.Next(1, 57);
- bool productWithoutBuyer = random.Next(1, 5) == 1;
- if (productWithoutBuyer)
- {
- continue;
- }
- product.BuyerId = random.Next(1, 57);
- }
- context.Products.AddRange(products);
- context.SaveChanges();
- CategoryDto[] categoriesDto = DeserializeCategories();
- var categories = categoriesDto.Where(x => IsValid(x)).Select(x => Mapper.Map<Category>(x)).ToArray();
- context.Categories.AddRange(categories);
- context.SaveChanges();
- List<CategoryProduct> categoryProducts = new List<CategoryProduct>();
- for (int i = 1; i <= 200; i++)
- {
- var categoryProduct = new CategoryProduct
- {
- CategoryId = random.Next(1, 12),
- ProductId = i
- };
- categoryProducts.Add(categoryProduct);
- }
- context.CategoryProducts.AddRange(categoryProducts);
- context.SaveChanges();
- }
- private static CategoryDto[] DeserializeCategories()
- {
- XmlSerializer serializer = new XmlSerializer(typeof(CategoryDto[]), new XmlRootAttribute("categories"));
- string xmlCategories = File.ReadAllText(@"..\..\..\..\categories.xml");
- var categories = (CategoryDto[])serializer.Deserialize(new StringReader(xmlCategories));
- return categories;
- }
- private static ProductDto[] DeserializeProducts()
- {
- XmlSerializer serializer = new XmlSerializer(typeof(ProductDto[]), new XmlRootAttribute("products"));
- string xmlProducts = File.ReadAllText(@"..\..\..\..\products.xml");
- var productsDto = (ProductDto[])serializer.Deserialize(new StringReader(xmlProducts));
- return productsDto;
- }
- private static UserDto[] DeserializeUsers()
- {
- XmlSerializer serializer = new XmlSerializer(typeof(UserDto[]), new XmlRootAttribute("users"));
- string xmlUsers = File.ReadAllText(@"..\..\..\..\users.xml");
- var users = (UserDto[])serializer.Deserialize(new StringReader(xmlUsers));
- return users;
- }
- private static bool IsValid(object obj)
- {
- var validationContext = new System.ComponentModel.DataAnnotations.ValidationContext(obj);
- var validationResults = new List<ValidationResult>();
- return Validator.TryValidateObject(obj, validationContext, validationResults, true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment