Advertisement
dimitrix85

Serializer Exam 07.04.2019

Apr 7th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.24 KB | None | 0 0
  1. namespace Cinema.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;
  9.     using System.Xml.Serialization;
  10.     using Cinema.DataProcessor.ExportDto;
  11.     using Data;
  12.     using Newtonsoft.Json;
  13.     using Formatting = Newtonsoft.Json.Formatting;
  14.  
  15.     public class Serializer
  16.     {
  17.         public static string ExportTopMovies(CinemaContext context, int rating)
  18.         {
  19.            
  20.             var movies = context.Movies
  21.                 .Where(x => x.Rating >= rating && x.Projections.Count != 0)
  22.                 .OrderByDescending(r => r.Rating)
  23.                 .ThenByDescending(ti => ti.Projections.Sum(y => y.Tickets.Sum(p => p.Price)))
  24.                 .Take(10)
  25.                 .Select(x => new
  26.                 {
  27.                     MovieName = x.Title,
  28.                     Rating = x.Rating.ToString("F2"),
  29.                     TotalIncomes = x.Projections.Sum(y => y.Tickets.Sum(p => p.Price)).ToString("F2"),
  30.                     Customers = x.Projections.SelectMany(y => y.Tickets).Select(c => new
  31.                     {
  32.                         FirstName = c.Customer.FirstName,
  33.                         LastName = c.Customer.LastName,
  34.                         Balance = c.Customer.Balance.ToString("F2")
  35.                     })
  36.                     .OrderByDescending(b => b.Balance)
  37.                     .ThenBy(fn => fn.FirstName)
  38.                     .ThenBy(ln => ln.LastName)
  39.                     .ToArray()
  40.                 }).ToArray();
  41.  
  42.  
  43.             var jsonResult = JsonConvert.SerializeObject(movies, Formatting.Indented);
  44.  
  45.             return jsonResult;
  46.         }
  47.  
  48.         public static string ExportTopCustomers(CinemaContext context, int age)
  49.         {
  50.             var sb = new StringBuilder();
  51.  
  52.             var customers = context.Customers.Where(x => x.Age >= age)
  53.                 .OrderByDescending(x => x.Tickets.Sum(sm => sm.Price))
  54.                 .Take(10)
  55.                 .Select(x => new CustomerExportDto
  56.                 {
  57.                     FirstName = x.FirstName,
  58.                     LastName = x.LastName,
  59.                     SpentMoney = (x.Tickets.Sum(sm => sm.Price)).ToString("F2"),
  60.                     SpentTime = TimeSpan.FromSeconds(x.Tickets.Sum(y=>y.Projection.Movie.Duration.TotalSeconds))
  61.                     .ToString("hh\\:mm\\:ss")
  62.                 })
  63.                
  64.                 .ToArray();
  65.  
  66.             var xmlSerializer = new XmlSerializer(typeof(CustomerExportDto[]), new XmlRootAttribute("Customers"));
  67.  
  68.             var namespaces = new XmlSerializerNamespaces(new[]
  69.             {
  70.                 XmlQualifiedName.Empty
  71.             });
  72.  
  73.             xmlSerializer.Serialize(new StringWriter(sb), customers, namespaces);
  74.  
  75.             return sb.ToString().Trim();
  76.         }
  77.     }
  78. }
  79.  
  80. [XmlType("Customer")]
  81.     public class CustomerExportDto
  82.     {
  83.         [XmlAttribute("FirstName")]
  84.         public string FirstName { get; set; }
  85.  
  86.         [XmlAttribute("LastName")]
  87.         public string LastName { get; set; }
  88.  
  89.         [XmlElement("SpentMoney")]
  90.         public string SpentMoney { get; set; }
  91.  
  92.         [XmlElement("SpentTime")]
  93.         public string SpentTime { get; set; }
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement