Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.96 KB | None | 0 0
  1. namespace Cinema.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 Cinema.Data.Models;
  12. using Cinema.Data.Models.Enums;
  13. using Cinema.DataProcessor.ImportDto;
  14. using Data;
  15. using Newtonsoft.Json;
  16.  
  17. public class Deserializer
  18. {
  19. private const string ErrorMessage = "Invalid data!";
  20. private const string SuccessfulImportMovie
  21. = "Successfully imported {0} with genre {1} and rating {2}!";
  22. private const string SuccessfulImportHallSeat
  23. = "Successfully imported {0}({1}) with {2} seats!";
  24. private const string SuccessfulImportProjection
  25. = "Successfully imported projection {0} on {1}!";
  26. private const string SuccessfulImportCustomerTicket
  27. = "Successfully imported customer {0} {1} with bought tickets: {2}!";
  28.  
  29. public static string ImportMovies(CinemaContext context, string jsonString)
  30. {
  31. var moviesDtos = JsonConvert.DeserializeObject<Movie[]>(jsonString);
  32.  
  33. var sb = new StringBuilder();
  34.  
  35. foreach (var movieDto in moviesDtos)
  36. {
  37.  
  38. var isValidDto = IsValid(movieDto);
  39.  
  40.  
  41. if (!isValidDto)
  42. {
  43. sb.AppendLine(ErrorMessage);
  44. continue;
  45. }
  46.  
  47. context.Movies.Add(movieDto);
  48.  
  49. sb.AppendLine(string.Format(SuccessfulImportMovie, movieDto.Title, movieDto.Genre, movieDto.Rating.ToString("F2")));
  50. }
  51.  
  52.  
  53. context.SaveChanges();
  54.  
  55. var result = sb.ToString();
  56.  
  57. return result;
  58. }
  59.  
  60.  
  61. public static string ImportHallSeats(CinemaContext context, string jsonString)
  62. {
  63. var hallSeatDtos = JsonConvert.DeserializeObject<ImportHallSeatDto[]>(jsonString);
  64. var halls = new List<Hall>();
  65.  
  66. var sb = new StringBuilder();
  67.  
  68. foreach (var hallSeatDto in hallSeatDtos)
  69. {
  70. if (!IsValid(hallSeatDto))
  71. {
  72. sb.AppendLine(ErrorMessage);
  73. continue;
  74. }
  75.  
  76. var hall = new Hall
  77. {
  78. Name = hallSeatDto.Name,
  79. Is3D = hallSeatDto.Is3D,
  80. Is4Dx = hallSeatDto.Is4Dx,
  81. };
  82.  
  83. for (int i = 0; i < hallSeatDto.Seats; i++)
  84. {
  85. hall.Seats.Add(new Seat());
  86. }
  87.  
  88. halls.Add(hall);
  89.  
  90. string status = "";
  91.  
  92. if (hall.Is4Dx)
  93. {
  94. status = hall.Is3D ? "4Dx/3D" : "4Dx";
  95. }
  96. else if (hall.Is3D)
  97. {
  98. status = "3D";
  99. }
  100. else
  101. {
  102. status = "Normal";
  103. }
  104.  
  105. sb.AppendLine(string.Format(SuccessfulImportHallSeat, hall.Name, status, hall.Seats.Count));
  106. }
  107.  
  108. context.Halls.AddRange(halls);
  109. context.SaveChanges();
  110.  
  111. return sb.ToString();
  112. }
  113.  
  114. public static string ImportProjections(CinemaContext context, string xmlString)
  115. {
  116. var xmlSerializer = new XmlSerializer(typeof(ImportProjectionDto[]),
  117. new XmlRootAttribute("Projections"));
  118.  
  119. var projectionsDto = (ImportProjectionDto[])xmlSerializer
  120. .Deserialize(new StringReader(xmlString));
  121.  
  122. var projections = new List<Projection>();
  123. var sb = new StringBuilder();
  124.  
  125. foreach (var projectionDto in projectionsDto)
  126. {
  127. var movie = context.Movies.Find(projectionDto.MovieId);
  128. var hall = context.Halls.Find(projectionDto.HallId);
  129.  
  130. if (movie == null || hall == null)
  131. {
  132. sb.AppendLine(ErrorMessage);
  133. continue;
  134. }
  135.  
  136. var projection = new Projection
  137. {
  138. MovieId = projectionDto.MovieId,
  139. HallId = projectionDto.HallId,
  140. DateTime = DateTime.ParseExact(projectionDto.DateTime, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)
  141. //in the dto property DateTime has to be string
  142. };
  143.  
  144. projections.Add(projection);
  145.  
  146. sb.AppendLine(string.Format(SuccessfulImportProjection, movie.Title, projection.DateTime.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)));
  147. }
  148.  
  149. context.Projections.AddRange(projections);
  150. context.SaveChanges();
  151. return sb.ToString();
  152. }
  153.  
  154. public static string ImportCustomerTickets(CinemaContext context, string xmlString)
  155. {
  156. var xmlSerializer = new XmlSerializer(typeof(ImportTicketDto[]), new XmlRootAttribute("Customers"));
  157.  
  158. var customersDto = (ImportTicketDto[])xmlSerializer.Deserialize(new StringReader(xmlString));
  159.  
  160. var customers = new List<Customer>();
  161. var sb = new StringBuilder();
  162.  
  163. foreach (var customerDto in customersDto)
  164. {
  165. if (IsValid(customerDto))
  166. {
  167. var customer = new Customer
  168. {
  169. FirstName = customerDto.FirstName,
  170. LastName = customerDto.LastName,
  171. Age = customerDto.Age,
  172. Balance = customerDto.Balance
  173. };
  174.  
  175. foreach (var ticket in customerDto.Tickets)
  176. {
  177. customer.Tickets.Add(new Ticket
  178. {
  179. ProjectionId = ticket.ProjectionId,
  180. Price = ticket.Price
  181. });
  182. }
  183.  
  184. sb.AppendLine(string.Format(SuccessfulImportCustomerTicket, customer.FirstName, customer.LastName, customer.Tickets.Count));
  185.  
  186. customers.Add(customer);
  187. }
  188. else
  189. {
  190. sb.AppendLine(ErrorMessage);
  191. }
  192. }
  193. context.Customers.AddRange(customers);
  194. context.SaveChanges();
  195. return sb.ToString();
  196. }
  197.  
  198. private static bool IsValid(object dto)
  199. {
  200. var validationContext = new ValidationContext(dto);
  201. var validationResult = new List<ValidationResult>();
  202.  
  203. return Validator.TryValidateObject(dto, validationContext, validationResult, true);
  204. }
  205. }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement