public static string ImportPlays(TheatreContext context, string xmlString) { StringBuilder sb = new StringBuilder(); List <Play> plays = new List <Play>(); var playDtos = XmlConverter.Deserializer <ImportPlayDto>(xmlString, "Plays"); foreach (var playDto in playDtos) { TimeSpan duration = TimeSpan.ParseExact(playDto.Duration, "c", CultureInfo.InvariantCulture); if (!IsValid(playDto) || duration.Hours == 0) { sb.AppendLine(ErrorMessage); continue; } Play play = new Play { Title = playDto.Title, Duration = duration, Rating = playDto.Rating, Genre = Enum.Parse <Genre>(playDto.Genre), Description = playDto.Description, Screenwriter = playDto.Screenwriter }; plays.Add(play); sb.AppendLine(string.Format(SuccessfulImportPlay, play.Title, play.Genre.ToString(), play.Rating)); } context.Plays.AddRange(plays); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ExportPlays(TheatreContext context, double rating) { var plays = context.Plays .ToArray() .Where(x => x.Rating <= rating) .Select(x => new ExportPlayDto { Title = x.Title, Duration = x.Duration.ToString("c"), //Rating = x.Rating == 0 ? "Premier" : $"{x.Rating}", Rating = x.Rating.ToString(), Genre = x.Genre.ToString(), Actors = x.Casts .Where(s => s.IsMainCharacter == true) .Select(x => new ActorsDto { FullName = x.FullName, MainCharacter = $"Plays main character in '{x.Play.Title}'.", }) .OrderBy(x => x.FullName) .ToArray(), }) .OrderBy(x => x.Title) .ThenByDescending(x => x.Genre) .ToArray(); foreach (var item in plays) { if (item.Rating.Equals("0")) { item.Rating = "Premier"; } } var serialized = XmlConverter.Serialize(plays, "Plays"); return(serialized); }
public static string ImportCasts(TheatreContext context, string xmlString) { StringBuilder sb = new StringBuilder(); const string root = "Casts"; var castsDto = XmlConverter.Deserializer <ImportCastsDto>(xmlString, root); List <Cast> casts = new List <Cast>(); foreach (var castXml in castsDto) { if (!IsValid(castXml)) { sb.AppendLine(ErrorMessage); continue; } var cast = new Cast { FullName = castXml.FullName, IsMainCharacter = castXml.IsMainCharacter, PhoneNumber = castXml.PhoneNumber, PlayId = castXml.PlayId, }; casts.Add(cast); sb.AppendLine(string.Format(SuccessfulImportActor, cast.FullName, cast.IsMainCharacter == true ? "main" : "lesser")); } context.Casts.AddRange(casts); context.SaveChanges(); return(sb.ToString().TrimEnd()); }