Advertisement
Iv555

Untitled

Aug 6th, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. namespace Footballers.DataProcessor
  2. {
  3. using System;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Xml.Serialization;
  9. using Data;
  10. using Footballers.DataProcessor.ExportDto;
  11. using Newtonsoft.Json;
  12. using Formatting = Newtonsoft.Json.Formatting;
  13.  
  14. public class Serializer
  15. {
  16. public static string ExportCoachesWithTheirFootballers(FootballersContext context)
  17. {
  18. CoachOutputModel[] dtoCoaches = context.Coaches.ToArray().Where(x => x.Footballers.Any()).Select( x=> new CoachOutputModel
  19. {
  20. FootballersCount = x.Footballers.Count,
  21. CoachName = x.Name,
  22. Footballers = x.Footballers.Select(f=> new FootballerOfCoachOutputModel
  23. {
  24. FootballerName = f.Name,
  25. PositionType = f.PositionType.ToString()
  26. })
  27. .OrderBy(x=>x.FootballerName)
  28. .ToArray()
  29. })
  30. .OrderByDescending(x=>x.FootballersCount)
  31. .ThenBy(x=>x.CoachName)
  32. .ToArray();
  33.  
  34. XmlRootAttribute xmlRootAttribute = new XmlRootAttribute("Coaches");
  35.  
  36. XmlSerializer xmlSerializer = new XmlSerializer(typeof(CoachOutputModel[]), xmlRootAttribute);
  37.  
  38. XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
  39. namespaces.Add("", "");
  40.  
  41. StringBuilder sb = new StringBuilder();
  42. using StringWriter writer = new StringWriter(sb);
  43.  
  44. xmlSerializer.Serialize(writer, dtoCoaches, namespaces);
  45.  
  46.  
  47. return sb.ToString().TrimEnd();
  48.  
  49.  
  50. }
  51.  
  52. public static string ExportTeamsWithMostFootballers(FootballersContext context, DateTime date)
  53. {
  54.  
  55. var teams = context.Teams.ToArray().Where(x => x.TeamsFootballers.Any(f => f.Footballer.ContractStartDate >= date)).Select(x => new
  56. {
  57. Name = x.Name,
  58. Footballers = x.TeamsFootballers.OrderByDescending(tf=>tf.Footballer.ContractEndDate).ThenBy(tf=>tf.Footballer.Name).Where(tf => tf.Footballer.ContractStartDate >= date).Select(f => new
  59. {
  60. FootballerName = f.Footballer.Name,
  61. ContractStartDate = f.Footballer.ContractStartDate.ToString("d", CultureInfo.InvariantCulture),
  62. ContractEndDate = f.Footballer.ContractEndDate.ToString("d", CultureInfo.InvariantCulture),
  63. BestSkillType = f.Footballer.BestSkillType.ToString(),
  64. PositionType = f.Footballer.PositionType.ToString()
  65.  
  66. })
  67. .ToArray()
  68.  
  69. })
  70. .OrderByDescending(x=>x.Footballers.Count())
  71. .ThenBy(x=>x.Name)
  72. .Take(5)
  73. .ToArray();
  74. return JsonConvert.SerializeObject(teams, Formatting.Indented);
  75. }
  76. }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement