public static string ExportPlays(TheatreContext context, double rating) { var plays = context .Plays .Where(x => x.Rating <= rating) .ToList() .Select(x => new PlayExportModel { Title = x.Title, Duration = x.Duration.ToString("c", CultureInfo.InvariantCulture), Rating = x.Rating == 0 ? "Premier" : x.Rating.ToString(), Genre = x.Genre.ToString(), Actors = x.Casts .Where(x => x.IsMainCharacter) .Select(a => new ActorExportModel() { FullName = a.FullName, MainCharacter = $"Plays main character in '{x.Title}'." }) .OrderByDescending(a => a.FullName) .ToList() }) .OrderBy(p => p.Title) .ThenByDescending(p => p.Genre) .ToList(); var xml = XMLConverter.Serialize(plays, "Plays"); return(xml); }
public static string ImportPlays(TheatreContext context, string xmlString) { var sb = new StringBuilder(); var plays = new List <Play>(); var importPlays = XMLConverter.Deserializer <PlayInputModel>(xmlString, "Plays"); foreach (var importPlay in importPlays) { bool isValidGenre = Enum.TryParse(typeof(Genre), importPlay.Genre, out var verifiedGenre); var isValidDuration = TimeSpan.TryParseExact(importPlay.Duration, "c", CultureInfo.InvariantCulture, out TimeSpan verifiedTimeSpan); if (!IsValid(importPlay) || isValidDuration == false || isValidGenre == false || verifiedTimeSpan.Hours < 1) { sb.AppendLine(ErrorMessage); continue; } var currentPlay = new Play { Title = importPlay.Title, Duration = verifiedTimeSpan, Rating = importPlay.Rating, Genre = (Genre)verifiedGenre, Description = importPlay.Descripton, Screenwriter = importPlay.Screenwriter }; plays.Add(currentPlay); sb.AppendLine(string.Format(SuccessfulImportPlay, currentPlay.Title, currentPlay.Genre, currentPlay.Rating)); } context.Plays.AddRange(plays); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportCasts(TheatreContext context, string xmlString) { var sb = new StringBuilder(); var casts = new List <Cast>(); var importCasts = XMLConverter.Deserializer <CastInputModel>(xmlString, "Casts"); foreach (var importCast in importCasts) { // no one asked for checking this !!! //var playIds = context // .Plays // .Select(p => p.Id) // .ToList(); if (!IsValid(importCast)) // || playIds.Contains(importCast.PlayId)) NO!!! { sb.AppendLine(ErrorMessage); continue; } var currentCast = new Cast { FullName = importCast.FullName, IsMainCharacter = importCast.IsMainCharacter, PhoneNumber = importCast.PhoneNumber, PlayId = importCast.PlayId }; casts.Add(currentCast); sb.AppendLine(string.Format(SuccessfulImportActor, currentCast.FullName, currentCast.IsMainCharacter ? "main" : "lesser")); } context.Casts.AddRange(casts); context.SaveChanges(); return(sb.ToString().TrimEnd()); }