Advertisement
Iv555

Untitled

Aug 6th, 2022
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. namespace Footballers.DataProcessor
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel.DataAnnotations;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Xml.Serialization;
  11. using Data;
  12. using Footballers.Data.Models;
  13. using Footballers.Data.Models.Enums;
  14. using Footballers.DataProcessor.ImportDto;
  15. using Newtonsoft.Json;
  16.  
  17. public class Deserializer
  18. {
  19. private const string ErrorMessage = "Invalid data!";
  20.  
  21. private const string SuccessfullyImportedCoach
  22. = "Successfully imported coach - {0} with {1} footballers.";
  23.  
  24. private const string SuccessfullyImportedTeam
  25. = "Successfully imported team - {0} with {1} footballers.";
  26.  
  27. public static string ImportCoaches(FootballersContext context, string xmlString)
  28. {
  29. XmlRootAttribute xmlRootAttribute = new XmlRootAttribute("Coaches");
  30. XmlSerializer xmlSerializer = new XmlSerializer(typeof(CoachWithFootballersInputModel[]), xmlRootAttribute);
  31.  
  32. using StringReader reader = new StringReader(xmlString);
  33.  
  34. CoachWithFootballersInputModel[] dtoCoaches = (CoachWithFootballersInputModel[])xmlSerializer.Deserialize(reader);
  35.  
  36. StringBuilder sb = new StringBuilder();
  37. List<Coach> validCoaches = new List<Coach>();
  38.  
  39. foreach (CoachWithFootballersInputModel dtoCoach in dtoCoaches)
  40. {
  41. if(!IsValid(dtoCoach) || String.IsNullOrEmpty(dtoCoach.Nationality) || DateTime.TryParse(dtoCoach.Nationality, out DateTime dt) == true)
  42. {
  43. sb.AppendLine(ErrorMessage);
  44. continue;
  45. }
  46. Coach newCoach = new Coach()
  47. {
  48. Name = dtoCoach.Name,
  49. Nationality = dtoCoach.Nationality,
  50.  
  51. };
  52. List<Footballer> validFootballers = new List<Footballer>();
  53. foreach (FootballerOfCoachInputModel dtoFootballer in dtoCoach.Footballers)
  54. {
  55. if (!IsValid(dtoFootballer))
  56. {
  57. sb.AppendLine(ErrorMessage);
  58. continue;
  59. }
  60. bool IsValidContractStartDate = DateTime.TryParseExact(dtoFootballer.ContractStartDate, "dd/mm/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime contractStarDate);
  61.  
  62. bool IsValidContractEndDate = DateTime.TryParseExact(dtoFootballer.ContractEndDate, "dd/mm/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime contractEndDate);
  63.  
  64. if(!IsValidContractStartDate || !IsValidContractEndDate)
  65. {
  66. sb.AppendLine(ErrorMessage);
  67. continue;
  68. }
  69. if(contractStarDate > contractEndDate)
  70. {
  71. sb.AppendLine(ErrorMessage);
  72. continue;
  73. }
  74. Footballer newFootballer = new Footballer()
  75. {
  76. Name = dtoFootballer.Name,
  77. ContractStartDate = DateTime.ParseExact(dtoFootballer.ContractStartDate, "dd/mm/yyyy", CultureInfo.InvariantCulture),
  78. ContractEndDate = DateTime.ParseExact(dtoFootballer.ContractEndDate, "dd/mm/yyyy", CultureInfo.InvariantCulture),
  79. BestSkillType = Enum.Parse<BestSkillType>(dtoFootballer.BestSkillType),
  80. PositionType = Enum.Parse<PositionType>(dtoFootballer.PositionType)
  81. };
  82. validFootballers.Add(newFootballer);
  83.  
  84. }
  85. newCoach.Footballers = validFootballers;
  86. validCoaches.Add(newCoach);
  87. sb.AppendLine(String.Format(SuccessfullyImportedCoach, newCoach.Name, newCoach.Footballers.Count));
  88. }
  89. context.Coaches.AddRange(validCoaches);
  90. context.SaveChanges();
  91.  
  92. return sb.ToString().TrimEnd();
  93. }
  94. public static string ImportTeams(FootballersContext context, string jsonString)
  95. {
  96. TeamInputModel[] dtoTeams = JsonConvert.DeserializeObject<TeamInputModel[]>(jsonString);
  97.  
  98. StringBuilder sb = new StringBuilder();
  99.  
  100. List<Team> validTeams = new List<Team>();
  101.  
  102. foreach (TeamInputModel dtoTeam in dtoTeams)
  103. {
  104. if(!IsValid(dtoTeam) || dtoTeam.Trophies <= 0)
  105. {
  106. sb.AppendLine(ErrorMessage);
  107. continue;
  108. }
  109.  
  110. Team newTeam = new Team
  111. {
  112. Name = dtoTeam.Name,
  113. Nationality = dtoTeam.Nationality,
  114. Trophies = dtoTeam.Trophies
  115.  
  116. };
  117. List<int> validFootballersIds = context.Footballers.Select(f => f.Id).ToList();
  118. List<int> validFootballersForImport = new List<int>();
  119.  
  120. foreach (int dtoFootballerId in dtoTeam.Footballers.Distinct())
  121. {
  122. if (!validFootballersIds.Contains(dtoFootballerId))
  123. {
  124. sb.AppendLine(ErrorMessage);
  125. continue;
  126. }
  127. newTeam.TeamsFootballers.Add(new TeamFootballer
  128. {
  129. FootballerId = dtoFootballerId,
  130. Team = newTeam
  131. });
  132. }
  133.  
  134. validTeams.Add(newTeam);
  135. sb.AppendLine(String.Format(SuccessfullyImportedTeam, newTeam.Name, newTeam.TeamsFootballers.Count));
  136.  
  137. }
  138. context.AddRange(validTeams);
  139. context.SaveChanges();
  140.  
  141. return sb.ToString().TrimEnd();
  142. }
  143.  
  144. private static bool IsValid(object dto)
  145. {
  146. var validationContext = new ValidationContext(dto);
  147. var validationResult = new List<ValidationResult>();
  148.  
  149. return Validator.TryValidateObject(dto, validationContext, validationResult, true);
  150. }
  151. }
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement