Пример #1
0
        public static string ImportCasts(TheatreContext context, string xmlString)
        {
            StringBuilder sb = new StringBuilder();

            List <Cast> casts = new List <Cast>();

            var castDtos = XmlConverter.Deserializer <ImportCastDto>(xmlString, "Casts");

            foreach (var castDto in castDtos)
            {
                if (!IsValid(castDto))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                Cast cast = new Cast
                {
                    FullName        = castDto.FullName,
                    IsMainCharacter = castDto.IsMainCharacter,
                    PhoneNumber     = castDto.PhoneNumber,
                    PlayId          = castDto.PlayId
                };

                casts.Add(cast);
                sb.AppendLine(string.Format(SuccessfulImportActor, cast.FullName,
                                            cast.IsMainCharacter ? "main" : "lesser"));
            }

            context.Casts.AddRange(casts);
            context.SaveChanges();

            return(sb.ToString());
        }
Пример #2
0
        public static string ImportPlays(TheatreContext context, string xmlString)
        {
            StringBuilder sb = new StringBuilder();

            const string root     = "Plays";
            var          playsDto = XmlConverter.Deserializer <ImportPlaysDto>(xmlString, root);

            List <Play> plays = new List <Play>();

            foreach (var playXml in playsDto)
            {
                if (!IsValid(playXml))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var duration = TimeSpan.ParseExact(playXml.Duration, "c", CultureInfo.InvariantCulture);
                if (duration.Hours < 01)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }
                Genre genre;
                var   isValidGenre = Enum.TryParse(playXml.Genre, out genre);

                if (!isValidGenre)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                Genre playGenre = (Genre)genre;


                var play = new Play
                {
                    Title        = playXml.Title,
                    Duration     = duration,
                    Rating       = playXml.Rating,
                    Genre        = genre,
                    Description  = playXml.Description,
                    Screenwriter = playXml.Screenwriter,
                };

                plays.Add(play);

                sb.AppendLine(string.Format(SuccessfulImportPlay, play.Title, play.Genre, play.Rating));
            }

            context.Plays.AddRange(plays);

            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
Пример #3
0
        public static string ImportPlays(TheatreContext context, string xmlString)
        {
            var plays = new List <Play>();
            var sb    = new StringBuilder();

            var playsDtos = XmlConverter.Deserializer <ImportPlaysDto>(xmlString, "Plays");

            foreach (var playDto in playsDtos)
            {
                bool isValidGenre = Enum.TryParse(typeof(Genre), playDto.Genre, out var playGenre);



                bool isValidDuration = TimeSpan.TryParseExact(playDto.Duration, "c",
                                                              CultureInfo.InvariantCulture, TimeSpanStyles.None, out var validDuration);

                if (!IsValid(playDto) ||
                    !isValidGenre ||
                    !isValidDuration)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }


                if (TimeSpan.Parse(playDto.Duration).Hours < 1)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var play = new Play()
                {
                    Title        = playDto.Title,
                    Duration     = validDuration,
                    Rating       = (float)playDto.Rating,
                    Genre        = (Genre)playGenre,
                    Description  = playDto.Description,
                    Screenwriter = playDto.Screenwriter
                };

                plays.Add(play);
                sb.AppendLine(string.Format(SuccessfulImportPlay, play.Title, play.Genre, play.Rating));
            }

            context.Plays.AddRange(plays);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }
Пример #4
0
        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());
        }