miroslavbstoyanov

Untitled

Jul 27th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. using AutoMapper;
  2. using ProductShop.Data;
  3. using ProductShop.Models;
  4. using ProductShopDatabase.Dtos;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel.DataAnnotations;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Xml.Serialization;
  11.  
  12. namespace ProductShopDatabase
  13. {
  14. public class StarUp
  15. {
  16. static void Main(string[] args)
  17. {
  18. var context = new ProductShopContext();
  19. context.Database.EnsureDeleted();
  20. context.Database.EnsureCreated();
  21.  
  22. Mapper.Initialize(cfg =>
  23. {
  24. cfg.AddProfile<ProductShopProfile>();
  25. });
  26.  
  27. UserDto[] usersDto = DeserializeUsers();
  28. var users = usersDto.Where(x => IsValid(x)).Select(x => Mapper.Map<User>(x)).ToArray();
  29.  
  30. context.Users.AddRange(users);
  31. context.SaveChanges();
  32.  
  33. ProductDto[] productsDto = DeserializeProducts();
  34. var products = productsDto.Where(x => IsValid(x)).Select(x => Mapper.Map<Product>(x)).ToArray();
  35. Random random = new Random();
  36.  
  37. foreach (var product in products)
  38. {
  39. product.SellerId = random.Next(1, 57);
  40.  
  41. bool productWithoutBuyer = random.Next(1, 5) == 1;
  42.  
  43. if (productWithoutBuyer)
  44. {
  45. continue;
  46. }
  47.  
  48. product.BuyerId = random.Next(1, 57);
  49. }
  50.  
  51. context.Products.AddRange(products);
  52. context.SaveChanges();
  53.  
  54. CategoryDto[] categoriesDto = DeserializeCategories();
  55. var categories = categoriesDto.Where(x => IsValid(x)).Select(x => Mapper.Map<Category>(x)).ToArray();
  56.  
  57. context.Categories.AddRange(categories);
  58. context.SaveChanges();
  59.  
  60. List<CategoryProduct> categoryProducts = new List<CategoryProduct>();
  61.  
  62. for (int i = 1; i <= 200; i++)
  63. {
  64. var categoryProduct = new CategoryProduct
  65. {
  66. CategoryId = random.Next(1, 12),
  67. ProductId = i
  68. };
  69.  
  70. categoryProducts.Add(categoryProduct);
  71. }
  72.  
  73. context.CategoryProducts.AddRange(categoryProducts);
  74. context.SaveChanges();
  75. }
  76.  
  77. private static CategoryDto[] DeserializeCategories()
  78. {
  79. XmlSerializer serializer = new XmlSerializer(typeof(CategoryDto[]), new XmlRootAttribute("categories"));
  80. string xmlCategories = File.ReadAllText(@"..\..\..\..\categories.xml");
  81.  
  82. var categories = (CategoryDto[])serializer.Deserialize(new StringReader(xmlCategories));
  83.  
  84. return categories;
  85. }
  86.  
  87. private static ProductDto[] DeserializeProducts()
  88. {
  89. XmlSerializer serializer = new XmlSerializer(typeof(ProductDto[]), new XmlRootAttribute("products"));
  90. string xmlProducts = File.ReadAllText(@"..\..\..\..\products.xml");
  91.  
  92. var productsDto = (ProductDto[])serializer.Deserialize(new StringReader(xmlProducts));
  93.  
  94. return productsDto;
  95. }
  96.  
  97. private static UserDto[] DeserializeUsers()
  98. {
  99. XmlSerializer serializer = new XmlSerializer(typeof(UserDto[]), new XmlRootAttribute("users"));
  100. string xmlUsers = File.ReadAllText(@"..\..\..\..\users.xml");
  101.  
  102. var users = (UserDto[])serializer.Deserialize(new StringReader(xmlUsers));
  103.  
  104. return users;
  105. }
  106.  
  107. private static bool IsValid(object obj)
  108. {
  109. var validationContext = new System.ComponentModel.DataAnnotations.ValidationContext(obj);
  110. var validationResults = new List<ValidationResult>();
  111.  
  112. return Validator.TryValidateObject(obj, validationContext, validationResults, true);
  113. }
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment