Example #1
0
        public static string ImportSongs(MusicHubDbContext context, string xmlString)
        {
            //XmlConverter is helper class
            var root = "Songs";
            var data = XmlConverter.Deserializer <ImportSongDTO>(xmlString, root);

            StringBuilder sb = new StringBuilder();

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

                if (item.AlbumId == null || !context.Albums.Any(a => a.Id == item.AlbumId))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                if (!context.Writers.Any(w => w.Id == item.WriterId))
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                //invalid genre
                Genre genre;
                var   validGenre = Enum.TryParse <Genre>(item.Genre.ToString(), out genre);

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

                var song = new Song
                {
                    Name      = item.Name,
                    CreatedOn = DateTime.ParseExact(item.CreatedOn, "dd/MM/yyyy", CultureInfo.InvariantCulture),
                    Duration  = TimeSpan.Parse(item.Duration),
                    Price     = item.Price,
                    Genre     = genre,
                    AlbumId   = item.AlbumId,
                    WriterId  = item.WriterId
                };

                context.Songs.Add(song);
                sb.AppendLine(string.Format(SuccessfullyImportedSong, song.Name, song.Genre, song.Duration));
            }

            context.SaveChanges();
            return(sb.ToString());
        }
Example #2
0
        public static string ImportSongPerformers(MusicHubDbContext context, string xmlString)
        {
            var root = "Performers";
            var data = XmlConverter.Deserializer <ImportSongPerformerDTO>(xmlString, root);

            StringBuilder sb = new StringBuilder();

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

                var performer = new Performer
                {
                    FirstName = item.FirstName,
                    LastName  = item.LastName,
                    Age       = item.Age,
                    NetWorth  = item.NetWorth
                };

                bool isValidPerformer = true;
                foreach (var performerSong in item.PerformersSongs)
                {
                    if (!IsValid(performerSong) || !context.Songs.Any(s => s.Id == performerSong.Id))
                    {
                        isValidPerformer = false;
                        sb.AppendLine(ErrorMessage);
                        break;
                    }

                    var ps = new SongPerformer
                    {
                        PerformerId = performer.Id,
                        SongId      = performerSong.Id
                    };

                    performer.PerformerSongs.Add(ps);
                }

                if (!isValidPerformer)
                {
                    continue;
                }

                context.Performers.Add(performer);
                sb.AppendLine(string.Format(SuccessfullyImportedPerformer, performer.FirstName, performer.PerformerSongs.Count));
            }

            context.SaveChanges();
            return(sb.ToString());
        }
        public static string ImportSongs(MusicHubDbContext context, string xmlString)
        {
            StringBuilder sb = new StringBuilder();

            var songs = XmlConverter.Deserializer <SongXmlInputModel>(xmlString, "Songs");

            foreach (var currentSong in songs)
            {
                bool isValidCreatedOn = DateTime.TryParseExact(currentSong.CreatedOn,
                                                               "dd/MM/yyyy",
                                                               CultureInfo.InvariantCulture,
                                                               DateTimeStyles.None,
                                                               out DateTime currentCreatedOn);

                bool isValidDuration = TimeSpan.TryParseExact(currentSong.Duration,
                                                              "c", CultureInfo.InvariantCulture,
                                                              TimeSpanStyles.None,
                                                              out TimeSpan currentDuration);

                bool isValidGenre = Enum.TryParse <Genre>(currentSong.Genre, out Genre currentGenre);

                //проверка дали currentSong.AlbumId не е null
                var currentAlbum = context.Albums.FirstOrDefault(a => a.Id == currentSong.AlbumId);

                var currentWriter = context.Writers.FirstOrDefault(w => w.Id == currentSong.WriterId);

                if (!IsValid(currentSong) ||
                    !isValidCreatedOn ||
                    !isValidDuration ||
                    !isValidGenre ||
                    currentAlbum == null ||
                    currentWriter == null)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var songToAdd = new Song
                {
                    Name      = currentSong.Name,
                    Duration  = currentDuration,
                    CreatedOn = currentCreatedOn,
                    Genre     = currentGenre,
                    Album     = currentAlbum,
                    Writer    = currentWriter,
                    Price     = currentSong.Price
                };

                context.Songs.Add(songToAdd);
                context.SaveChanges();
                sb.AppendLine(string.Format(SuccessfullyImportedSong, songToAdd.Name, songToAdd.Genre, songToAdd.Duration));
            }
            return(sb.ToString().Trim());
        }
Example #4
0
        public static string ImportSongPerformers(MusicHubDbContext context, string xmlString)
        {
            var sb = new StringBuilder();
            var songPerformersRootName = "Performers";
            var songPerformersDtos     = XmlConverter.Deserializer <ImportSongsPerformersDTO>(xmlString, songPerformersRootName);

            var realPerformers = new List <Performer>();

            foreach (var songPerformerDto in songPerformersDtos)
            {
                var areAllSongsValid = true;
                foreach (var songId in songPerformerDto.Songs)
                {
                    if (context.Songs.All(x => x.Id != songId.Id))
                    {
                        areAllSongsValid = false;
                        break;
                    }
                }
                if (!IsValid(songPerformerDto) || !areAllSongsValid)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }
                var newPerformer = new Performer
                {
                    FirstName = songPerformerDto.FirstName,
                    LastName  = songPerformerDto.LastName,
                    Age       = songPerformerDto.Age,
                    NetWorth  = songPerformerDto.NetWorth
                };

                foreach (var songId in songPerformerDto.Songs)
                {
                    var existedSong = context.Songs.FirstOrDefault(x => x.Id == songId.Id);

                    var songPerformer = new SongPerformer
                    {
                        Performer = newPerformer,
                        Song      = existedSong
                    };

                    newPerformer.PerformerSongs.Add(songPerformer);
                }

                realPerformers.Add(newPerformer);

                sb.AppendLine(string.Format(SuccessfullyImportedPerformer, newPerformer.FirstName, newPerformer.PerformerSongs.Count));
            }
            context.Performers.AddRange(realPerformers);
            context.SaveChanges();
            return(sb.ToString().TrimEnd());
        }
        public static string ImportSongPerformers(MusicHubDbContext context, string xmlString)
        {
            StringBuilder sb         = new StringBuilder();
            var           performers = XmlConverter.Deserializer <PerformerXmlInputModel>(xmlString, "Performers");

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

                var performerToAdd = new Performer
                {
                    FirstName = currentPerformer.FirstName,
                    LastName  = currentPerformer.LastName,
                    Age       = currentPerformer.Age,
                    NetWorth  = currentPerformer.NetWorth
                };

                bool SongsValid = true;
                foreach (var currentSong in currentPerformer.PerformersSongs)
                {
                    var songToAdd = context.Songs.FirstOrDefault(s => s.Id == currentSong.Id);
                    if (songToAdd == null)
                    {
                        SongsValid = false;
                        sb.AppendLine(ErrorMessage);
                        break;
                    }
                    performerToAdd.PerformerSongs.Add(new SongPerformer
                    {
                        Song = songToAdd
                    });
                }
                if (SongsValid)
                {
                    context.Performers.Add(performerToAdd);
                    context.SaveChanges();
                    sb.AppendLine(string.Format(SuccessfullyImportedPerformer, performerToAdd.FirstName, performerToAdd.PerformerSongs.Count));
                }
            }
            return(sb.ToString().Trim());
        }
Example #6
0
        public static string ImportSongs(MusicHubDbContext context, string xmlString)
        {
            var outputString        = new StringBuilder();
            var importSongsRootName = "Songs";
            var resultXmlDtos       = XmlConverter.Deserializer <ImportSongDto>(xmlString, importSongsRootName);

            var songsToAdd = new List <Song>();

            foreach (var songDto in resultXmlDtos)
            {
                Genre givenGenre;
                var   isGenreValid = Enum.TryParse(songDto.Genre, true, out givenGenre);

                var isAlbumIdValid  = context.Albums.Any(x => x.Id == songDto.AlbumId) || songDto.AlbumId == null;
                var isWriterIdValid = context.Writers.Any(x => x.Id == songDto.WriterId);

                if (!IsValid(songDto) || !isGenreValid || !isAlbumIdValid || !isWriterIdValid)
                {
                    outputString.AppendLine(ErrorMessage);
                    continue;
                }
                var newSong = new Song
                {
                    Name      = songDto.Name,
                    Duration  = TimeSpan.ParseExact(songDto.Duration, "c", CultureInfo.InvariantCulture),
                    CreatedOn = DateTime.ParseExact(songDto.CreatedOn, "dd/MM/yyyy", CultureInfo.InvariantCulture),
                    Genre     = givenGenre,
                    AlbumId   = songDto.AlbumId,
                    WriterId  = songDto.WriterId,
                    Price     = songDto.Price
                };
                songsToAdd.Add(newSong);
                outputString.AppendLine(string.Format(SuccessfullyImportedSong, newSong.Name, newSong.Genre.ToString(), newSong.Duration.ToString()));
            }
            context.Songs.AddRange(songsToAdd);
            context.SaveChanges();

            return(outputString.ToString().TrimEnd());
        }
Example #7
0
        public static string ImportSongs(MusicHubDbContext context, string xmlString)
        {
            var sb = new StringBuilder();

            var songsDtos = XmlConverter.Deserializer <ImportSongDto>(xmlString, "Songs");

            var songs = new List <Song>();

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

                DateTime createdOn;
                bool     isCreatedOnValid = DateTime.TryParseExact(song.CreatedOn, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out createdOn);

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

                TimeSpan duration;
                bool     isDurationValid = TimeSpan.TryParseExact(song.Duration, "c", CultureInfo.InvariantCulture, TimeSpanStyles.None, out duration);

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

                var album     = context.Albums.Find(song.AlbumId);
                var writer    = context.Writers.Find(song.WriterId);
                var songTitle = songs.Any(s => s.Name == song.Name);

                if (album == null || writer == null || songTitle)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                Genre genre;
                bool  isValidGenre = Enum.TryParse(song.Genre, out genre);
                if (!isValidGenre)
                {
                    sb.AppendLine(ErrorMessage);
                    continue;
                }

                var newSong = new Song
                {
                    Name      = song.Name,
                    Duration  = duration,
                    CreatedOn = createdOn,
                    Genre     = genre,
                    AlbumId   = song.AlbumId,
                    WriterId  = song.WriterId,
                    Price     = song.Price
                };

                songs.Add(newSong);
                sb.AppendLine(string.Format(SuccessfullyImportedSong, newSong.Name, newSong.Genre, newSong.Duration));
            }

            context.Songs.AddRange(songs);
            context.SaveChanges();

            return(sb.ToString().TrimEnd());
        }