Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Cinema.DataProcessor
- {
- using System;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Xml;
- using System.Xml.Serialization;
- using Cinema.DataProcessor.ExportDto;
- using Data;
- using Newtonsoft.Json;
- using Formatting = Newtonsoft.Json.Formatting;
- public class Serializer
- {
- public static string ExportTopMovies(CinemaContext context, int rating)
- {
- var movies = context.Movies
- .Where(x => x.Rating >= rating && x.Projections.Count != 0)
- .OrderByDescending(r => r.Rating)
- .ThenByDescending(ti => ti.Projections.Sum(y => y.Tickets.Sum(p => p.Price)))
- .Take(10)
- .Select(x => new
- {
- MovieName = x.Title,
- Rating = x.Rating.ToString("F2"),
- TotalIncomes = x.Projections.Sum(y => y.Tickets.Sum(p => p.Price)).ToString("F2"),
- Customers = x.Projections.SelectMany(y => y.Tickets).Select(c => new
- {
- FirstName = c.Customer.FirstName,
- LastName = c.Customer.LastName,
- Balance = c.Customer.Balance.ToString("F2")
- })
- .OrderByDescending(b => b.Balance)
- .ThenBy(fn => fn.FirstName)
- .ThenBy(ln => ln.LastName)
- .ToArray()
- }).ToArray();
- var jsonResult = JsonConvert.SerializeObject(movies, Formatting.Indented);
- return jsonResult;
- }
- public static string ExportTopCustomers(CinemaContext context, int age)
- {
- var sb = new StringBuilder();
- var customers = context.Customers.Where(x => x.Age >= age)
- .OrderByDescending(x => x.Tickets.Sum(sm => sm.Price))
- .Take(10)
- .Select(x => new CustomerExportDto
- {
- FirstName = x.FirstName,
- LastName = x.LastName,
- SpentMoney = (x.Tickets.Sum(sm => sm.Price)).ToString("F2"),
- SpentTime = TimeSpan.FromSeconds(x.Tickets.Sum(y=>y.Projection.Movie.Duration.TotalSeconds))
- .ToString("hh\\:mm\\:ss")
- })
- .ToArray();
- var xmlSerializer = new XmlSerializer(typeof(CustomerExportDto[]), new XmlRootAttribute("Customers"));
- var namespaces = new XmlSerializerNamespaces(new[]
- {
- XmlQualifiedName.Empty
- });
- xmlSerializer.Serialize(new StringWriter(sb), customers, namespaces);
- return sb.ToString().Trim();
- }
- }
- }
- [XmlType("Customer")]
- public class CustomerExportDto
- {
- [XmlAttribute("FirstName")]
- public string FirstName { get; set; }
- [XmlAttribute("LastName")]
- public string LastName { get; set; }
- [XmlElement("SpentMoney")]
- public string SpentMoney { get; set; }
- [XmlElement("SpentTime")]
- public string SpentTime { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement