deleriumbg

Export XML

Apr 7th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | None | 0 0
  1. using System.IO;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Xml;
  5. using System.Xml.Serialization;
  6. using Cinema.DataProcessor.ExportDto;
  7. using Newtonsoft.Json;
  8. using Formatting = Newtonsoft.Json.Formatting;
  9.  
  10. namespace Cinema.DataProcessor
  11. {
  12.     using System;
  13.  
  14.     using Data;
  15.  
  16.     public class Serializer
  17.     {
  18.         public static string ExportTopMovies(CinemaContext context, int rating)
  19.         {
  20.             var movies = context.Movies
  21.                 .Where(x => x.Rating >= rating && x.Projections.Any(p => p.Tickets.Count > 0))
  22.                 .Select(x => new
  23.                 {
  24.                     MovieName = x.Title,
  25.                     Rating = x.Rating.ToString("F2"),
  26.                     TotalIncomes = x.Projections.Sum(t => t.Tickets.Sum(p => p.Price)).ToString("F2"),
  27.                     Customers = x.Projections.SelectMany(p => p.Tickets).Select(c => new
  28.                     {
  29.                         FirstName = c.Customer.FirstName,
  30.                         LastName = c.Customer.LastName,
  31.                         Balance = c.Customer.Balance.ToString("F2")
  32.                     })
  33.                         .OrderByDescending(c => c.Balance)
  34.                         .ThenBy(c => c.FirstName)
  35.                         .ThenBy(c => c.LastName)
  36.                         .ToArray()
  37.                 })
  38.                 .OrderByDescending(x => double.Parse(x.Rating))
  39.                 .ThenByDescending(x => decimal.Parse(x.TotalIncomes))
  40.                 .Take(10)
  41.                 .ToArray();
  42.  
  43.             string serializedMovies = JsonConvert.SerializeObject(movies, Formatting.Indented);
  44.             return serializedMovies;
  45.         }
  46.  
  47.         public static string ExportTopCustomers(CinemaContext context, int age)
  48.         {
  49.             var customers = context.Customers
  50.                 .Where(x => x.Age >= age)
  51.                 .Select(x => new ExportTopCustomersDto
  52.                 {
  53.                     FirstName = x.FirstName,
  54.                     LastName = x.LastName,
  55.                     SpentMoney = x.Tickets.Sum(t => t.Price).ToString("F2"),
  56.                     SpentTime = new TimeSpan(x.Tickets.Sum(p => p.Projection.Movie.Duration.Ticks)).ToString("hh\\:mm\\:ss")
  57.                 })
  58.                 .OrderByDescending(z => decimal.Parse(z.SpentMoney))
  59.                 .Take(10)
  60.                 .ToArray();
  61.  
  62.             StringBuilder sb = new StringBuilder();
  63.             var namespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
  64.             var serializer = new XmlSerializer(typeof(ExportTopCustomersDto[]), new XmlRootAttribute("Customers"));
  65.             serializer.Serialize(new StringWriter(sb), customers, namespaces);
  66.  
  67.             var result = sb.ToString().TrimEnd();
  68.             return result;
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment