Advertisement
Somo4k

Untitled

Aug 15th, 2022
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. using System.Text;
  2. using Castle.Core.Internal;
  3. using Trucks.Data.Models;
  4. using Trucks.Data.Models.Enums;
  5. using Trucks.DataProcessor.ImportDto;
  6.  
  7. namespace Trucks.DataProcessor
  8. {
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel.DataAnnotations;
  12. using System.Linq;
  13.  
  14. using Data;
  15.  
  16. public class Deserializer
  17. {
  18. private const string ErrorMessage = "Invalid data!";
  19.  
  20. private const string SuccessfullyImportedDespatcher
  21. = "Successfully imported despatcher - {0} with {1} trucks.";
  22.  
  23. private const string SuccessfullyImportedClient
  24. = "Successfully imported client - {0} with {1} trucks.";
  25.  
  26. public static string ImportDespatcher(TrucksContext context, string xmlString)
  27. {
  28. var sb = new StringBuilder();
  29.  
  30. var despatcherDto = XmlConverter.Deserializer<ImportDespatchersXML>(xmlString, "Despatchers");
  31.  
  32. var valiDespatchers = new List<Despatcher>();
  33.  
  34.  
  35. foreach (var dDto in despatcherDto)
  36. {
  37.  
  38. if (!IsValid(dDto))
  39. {
  40. sb.AppendLine(ErrorMessage);
  41. continue;
  42. }
  43.  
  44. if (dDto.Position.IsNullOrEmpty())
  45. {
  46. sb.AppendLine(ErrorMessage);
  47. continue;
  48. }
  49.  
  50. var trucks = new List<Truck>();
  51.  
  52.  
  53. foreach (var tDto in dDto.Trucks)
  54. {
  55.  
  56. if (!IsValid(tDto))
  57. {
  58. sb.AppendLine(ErrorMessage);
  59. continue;
  60. }
  61.  
  62.  
  63. if (tDto.VinNumber.IsNullOrEmpty())
  64. {
  65. sb.AppendLine(ErrorMessage);
  66. continue;
  67. }
  68.  
  69.  
  70. trucks.Add(new Truck()
  71. {
  72. RegistrationNumber = tDto.RegistrationNumber,
  73. VinNumber = tDto.VinNumber,
  74. TankCapacity = tDto.TankCapacity,
  75. CargoCapacity = tDto.CargoCapacity,
  76. CategoryType = (CategoryType)tDto.CategoryType,
  77. MakeType = (MakeType)tDto.MakeType
  78. });
  79.  
  80.  
  81. }
  82.  
  83. Despatcher despatcher = new Despatcher()
  84. {
  85. Name = dDto.Name,
  86. Position = dDto.Position,
  87. Trucks = trucks
  88. };
  89.  
  90. valiDespatchers.Add(despatcher);
  91.  
  92. sb.AppendLine(string.Format(SuccessfullyImportedDespatcher, despatcher.Name, trucks.Count()));
  93. }
  94.  
  95. context.Despatchers.AddRange(valiDespatchers);
  96. context.SaveChanges();
  97.  
  98. return sb.ToString().TrimEnd();
  99.  
  100.  
  101.  
  102. }
  103. public static string ImportClient(TrucksContext context, string jsonString)
  104. {
  105. throw new NotImplementedException();
  106. }
  107.  
  108. private static bool IsValid(object dto)
  109. {
  110. var validationContext = new ValidationContext(dto);
  111. var validationResult = new List<ValidationResult>();
  112.  
  113. return Validator.TryValidateObject(dto, validationContext, validationResult, true);
  114. }
  115. }
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement