public static string ImportSongs(MusicHubDbContext context, string xmlString) { var sb = new StringBuilder(); var serializer = new XmlSerializer(typeof(List <ImportSongDto>), new XmlRootAttribute("Songs")); var namespaces = new XmlSerializerNamespaces(); namespaces.Add(string.Empty, string.Empty); var reader = new StringReader(xmlString); var songsToAdd = new List <Song>(); using (reader) { var songDtos = (List <ImportSongDto>)serializer.Deserialize(reader); foreach (var songDto in songDtos) { if (!IsValid(songDto)) { sb.AppendLine(ErrorMessage); continue; } var album = context.Albums.FirstOrDefault(a => a.Id == songDto.AlbumId); var writer = context.Writers.FirstOrDefault(w => w.Id == songDto.WriterId); var isGenreValid = Enum.IsDefined(typeof(Genre), songDto.Genre); DateTime createdOn; var isCreatedOnValid = DateTime.TryParseExact(songDto.CreatedOn, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out createdOn); if (album == null || writer == null || isGenreValid == false || isCreatedOnValid == false) { sb.AppendLine(ErrorMessage); continue; } var song = new Song() { Name = songDto.Name, Album = album, Writer = writer, AlbumId = songDto.AlbumId, Genre = Enum.Parse <Genre>(songDto.Genre), Price = songDto.Price, WriterId = songDto.WriterId, CreatedOn = createdOn, Duration = TimeSpan.ParseExact(songDto.Duration, @"hh\:mm\:ss", CultureInfo.InvariantCulture) }; songsToAdd.Add(song); sb.AppendLine(string.Format(SuccessfullyImportedSong, song.Name, song.Genre, song.Duration)); } } context.Songs.AddRange(songsToAdd); context.SaveChanges(); return(sb.ToString().Trim()); }
public static string ImportSongPerformers(MusicHubDbContext context, string xmlString) { var xmlSerializer = new XmlSerializer(typeof(ImportPerformerDto[]), new XmlRootAttribute("Performers")); var sb = new StringBuilder(); using (var reader = new StringReader(xmlString)) { var performersDto = (ImportPerformerDto[])xmlSerializer.Deserialize(reader); var performers = new List <Performer>(); foreach (var performerDto in performersDto) { if (!IsValid(performerDto)) { sb.AppendLine(ErrorMessage); continue; } var currentPerformer = new Performer() { FirstName = performerDto.FirstName, LastName = performerDto.LastName, Age = performerDto.Age, NetWorth = performerDto.NetWorth }; bool isSongIdCorrect = true; foreach (var song in performerDto.PerformersSongs) { if (!IsValid(song)) { sb.AppendLine(ErrorMessage); isSongIdCorrect = false; break; } var findSongId = context .Songs .FirstOrDefault(s => s.Id == song.Id); if (findSongId == null) { sb.AppendLine(ErrorMessage); isSongIdCorrect = false; break; } SongPerformer songPerformer = new SongPerformer() { Song = findSongId, Performer = currentPerformer }; currentPerformer.PerformerSongs.Add(songPerformer); } if (isSongIdCorrect) { performers.Add(currentPerformer); sb.AppendLine(String.Format(SuccessfullyImportedPerformer, currentPerformer.FirstName, currentPerformer.PerformerSongs.Count)); } } context.Performers.AddRange(performers); context.SaveChanges(); return(sb.ToString().TrimEnd()); } }
public static string ImportProducersAlbums(MusicHubDbContext context, string jsonString) { var producersDto = JsonConvert.DeserializeObject <ImportProducersDto[]>(jsonString); var sb = new StringBuilder(); var producers = new List <Producer>(); foreach (var producerDto in producersDto) { if (!IsValid(producerDto)) { sb.AppendLine(ErrorMessage); continue; } var producer = new Producer() { Name = producerDto.Name, Pseudonym = producerDto.Pseudonym, PhoneNumber = producerDto.PhoneNumber }; bool isAlbumCorrect = true; foreach (var album in producerDto.Albums) { if (!IsValid(album)) { sb.AppendLine(ErrorMessage); isAlbumCorrect = false; break; } DateTime ReleaseDate; bool isCorrectReleaseDate = DateTime .TryParseExact(album.ReleaseDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out ReleaseDate); if (!isCorrectReleaseDate) { sb.AppendLine(ErrorMessage); isAlbumCorrect = false; break; } var currentAlbum = new Album() { Name = album.Name, ReleaseDate = ReleaseDate }; producer.Albums.Add(currentAlbum); } if (isAlbumCorrect) { producers.Add(producer); if (!producer.PhoneNumber.IsNullOrEmpty()) { sb.AppendLine(string.Format(SuccessfullyImportedProducerWithPhone, producer.Name, producer.PhoneNumber, producer.Albums.Count)); } else { sb.AppendLine(string.Format(SuccessfullyImportedProducerWithNoPhone, producer.Name, producer.Albums.Count)); } } } context.Producers.AddRange(producers); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportProducersAlbums(MusicHubDbContext context, string jsonString) { var prodicersList = JsonConvert.DeserializeObject <List <ProducerImportDto> >(jsonString); StringBuilder sb = new StringBuilder(); List <Producer> validProducers = new List <Producer>(); foreach (var producerDto in prodicersList) { if (!IsValid(producerDto)) { sb.AppendLine(ErrorMessage); continue; } List <Album> validAlbums = new List <Album>(); bool areValidAlbums = true; foreach (var albumDto in producerDto.Albums) { if (!IsValid(albumDto)) { sb.AppendLine(ErrorMessage); areValidAlbums = false; break; } } if (!areValidAlbums) { continue; } else { foreach (var albumDto in producerDto.Albums) { var album = new Album() { Name = albumDto.Name, ReleaseDate = DateTime.ParseExact(albumDto.ReleaseDate, "dd/MM/yyyy", CultureInfo.InvariantCulture) }; validAlbums.Add(album); } var producer = new Producer() { Name = producerDto.Name, Pseudonym = producerDto.Pseudonym, PhoneNumber = producerDto.PhoneNumber, Albums = validAlbums }; validProducers.Add(producer); if (producer.PhoneNumber == null) { sb.AppendLine(string.Format(SuccessfullyImportedProducerWithNoPhone, producer.Name, producer.Albums.Count)); } else { sb.AppendLine(string.Format(SuccessfullyImportedProducerWithPhone, producer.Name, producer.PhoneNumber, producer.Albums.Count)); } context.Albums.AddRange(validAlbums); context.SaveChanges(); } } context.Producers.AddRange(validProducers); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportSongs(MusicHubDbContext context, string xmlString) { var xmlSerializer = new XmlSerializer(typeof(ImportSongsDto[]), new XmlRootAttribute("Songs")); var sb = new StringBuilder(); using (var reader = new StringReader(xmlString)) { var songsDto = (ImportSongsDto[])xmlSerializer.Deserialize(reader); var songs = new List <Song>(); foreach (var songDto in songsDto) { if (!IsValid(songDto)) { sb.AppendLine(ErrorMessage); continue; } DateTime songCreatedOn; bool isSongCreatedOn = DateTime .TryParseExact(songDto.CreatedOn, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out songCreatedOn); if (!isSongCreatedOn) { sb.AppendLine(ErrorMessage); continue; } TimeSpan songDuration; bool isSongDuration = TimeSpan .TryParseExact(songDto.Duration, "c", CultureInfo.InvariantCulture, out songDuration); if (!isSongDuration) { sb.AppendLine(ErrorMessage); continue; } Genre songGenre; var isSongGenre = Enum .TryParse(songDto.Genre, out songGenre); if (!isSongGenre) { sb.AppendLine(ErrorMessage); continue; } Album album = null; if (songDto.AlbumId.HasValue) { album = context .Albums .FirstOrDefault(a => a.Id == songDto.AlbumId); } Writer writer = context .Writers .FirstOrDefault(w => w.Id == songDto.WriterId); if (writer == null || album == null) { sb.AppendLine(ErrorMessage); continue; } var currentSong = new Song() { Name = songDto.Name, Duration = songDuration, CreatedOn = songCreatedOn, Genre = songGenre, AlbumId = songDto.AlbumId, WriterId = songDto.WriterId, Price = songDto.Price }; songs.Add(currentSong); sb.AppendLine(String.Format(SuccessfullyImportedSong, currentSong.Name, currentSong.Genre, currentSong.Duration)); } context.Songs.AddRange(songs); context.SaveChanges(); return(sb.ToString().TrimEnd()); } }
public static string ImportSongPerformers(MusicHubDbContext context, string xmlString) { XmlSerializer ser = new XmlSerializer(typeof(PerformerImportDto[]), new XmlRootAttribute("Performers")); var performersDto = (PerformerImportDto[])ser.Deserialize(new StringReader(xmlString)); List <Performer> performers = new List <Performer>(); StringBuilder sb = new StringBuilder(); foreach (var performerDto in performersDto) { if (!IsValid(performerDto)) { sb.AppendLine(ErrorMessage); continue; } List <SongPerformer> validSongsPerformers = new List <SongPerformer>(); bool areValidSongs = true; foreach (var songDto in performerDto.PerformersSongs) { if (!context.Songs.Any(s => s.Id == songDto.Id)) { sb.AppendLine(ErrorMessage); areValidSongs = false; break; } } if (!areValidSongs) { continue; } else { var performer = new Performer() { FirstName = performerDto.FirstName, LastName = performerDto.LastName, Age = performerDto.Age, NetWorth = performerDto.NetWorth }; foreach (var songDto in performerDto.PerformersSongs.Distinct()) { var song = context.Songs.FirstOrDefault(s => s.Id == songDto.Id); var songPerformer = new SongPerformer() { Song = song, Performer = performer }; validSongsPerformers.Add(songPerformer); } performer.PerformerSongs = validSongsPerformers; context.SongsPerformers.AddRange(validSongsPerformers); context.Performers.Add(performer); sb.AppendLine(string.Format(SuccessfullyImportedPerformer, performer.FirstName, performer.PerformerSongs.Count)); context.SaveChanges(); } } return(sb.ToString().TrimEnd()); }
public static string ImportSongs(MusicHubDbContext context, string xmlString) { var sb = new StringBuilder(); var serializer = new XmlSerializer(typeof(SongDto[]), new XmlRootAttribute("Songs")); var songDtos = (SongDto[])serializer.Deserialize(new StringReader(xmlString)); var validSongs = new List <Song>(); foreach (var songDto in songDtos) { if (!IsValid(songDto)) { sb.AppendLine(ErrorMessage); continue; } Genre genre; if (!Enum.TryParse(songDto.Genre, out genre)) { sb.AppendLine(ErrorMessage); continue; } var album = context.Albums.SingleOrDefault(a => a.Id == songDto.AlbumId); if (album == null) { sb.AppendLine(ErrorMessage); continue; } var writer = context.Writers.SingleOrDefault(w => w.Id == songDto.WriterId); if (writer == null) { sb.AppendLine(ErrorMessage); continue; } var song = new Song { Name = songDto.Name, CreatedOn = DateTime.ParseExact(songDto.CreatedOn, "dd/MM/yyyy", CultureInfo.InvariantCulture), Album = album, Duration = TimeSpan.ParseExact(songDto.Duration, "c", CultureInfo.InvariantCulture), Price = songDto.Price, Genre = genre, Writer = writer }; validSongs.Add(song); sb.AppendLine(string.Format(SuccessfullyImportedSong, song.Name, songDto.Genre, song.Duration)); } context.Songs.AddRange(validSongs); context.SaveChanges(); var result = sb.ToString().TrimEnd(); return(result); }
public static string ImportProducersAlbums(MusicHubDbContext context, string jsonString) { var producerAlbums = JsonConvert.DeserializeObject <ImportProducerAndAlbumDto[]>(jsonString); var validProducerAlbums = new List <Producer>(); var sb = new StringBuilder(); foreach (var producerAlbum in producerAlbums) { var albums = new List <Album>(); if (IsValid(producerAlbum) && producerAlbum.Albums.All(IsValid) && producerAlbum.PhoneNumber == null) { foreach (var album in producerAlbum.Albums) { albums.Add(new Album() { Name = album.Name, ReleaseDate = DateTime.ParseExact(album.ReleaseDate, "dd/MM/yyyy", CultureInfo.InvariantCulture) }); } sb.AppendLine(string.Format(SuccessfullyImportedProducerWithNoPhone, producerAlbum.Name, producerAlbum.Albums.Count())); validProducerAlbums.Add(new Producer() { Name = producerAlbum.Name, Pseudonym = producerAlbum.Pseudonym, PhoneNumber = producerAlbum.PhoneNumber, Albums = albums }); } else if (IsValid(producerAlbum) && producerAlbum.Albums.All(IsValid) && producerAlbum.PhoneNumber != null) { foreach (var album in producerAlbum.Albums) { albums.Add(new Album() { Name = album.Name, ReleaseDate = DateTime.ParseExact(album.ReleaseDate, "dd/MM/yyyy", CultureInfo.InvariantCulture) }); } sb.AppendLine(string.Format(SuccessfullyImportedProducerWithPhone, producerAlbum.Name, producerAlbum.PhoneNumber, producerAlbum.Albums.Count())); validProducerAlbums.Add(new Producer() { Name = producerAlbum.Name, Pseudonym = producerAlbum.Pseudonym, PhoneNumber = producerAlbum.PhoneNumber, Albums = albums }); } else { sb.AppendLine(ErrorMessage); } } context.Producers.AddRange(validProducerAlbums); context.SaveChanges(); return(sb.ToString().TrimEnd()); }
public static string ImportSongPerformers(MusicHubDbContext context, string xmlString) { var ser = new XmlSerializer(typeof(List <XmlImportPerformer>), new XmlRootAttribute("Performers")); var res = new StringBuilder(); var performers = new List <Performer>(); var validPerfsDto = new List <XmlImportPerformer>(); var performersDto = (List <XmlImportPerformer>)ser.Deserialize(new StringReader(xmlString)); foreach (var perfDto in performersDto) { var invalidId = false; foreach (var songId in perfDto.SongIds) { if (!context.Songs.Any(s => s.Id == songId.Id)) { invalidId = true; } } if (invalidId || !IsValid(perfDto)) { res.AppendLine(ErrorMessage); continue; } var performer = Mapper.Map <Performer>(perfDto); performers.Add(performer); validPerfsDto.Add(perfDto); res.AppendLine(string.Format(SuccessfullyImportedPerformer, performer.FirstName, perfDto.SongIds.Count)); } context.Performers.AddRange(performers); var songsPerfs = new List <SongPerformer>(); for (int i = 0; i < performers.Count; i++) { var perfId = performers[i].Id; var perfDto = validPerfsDto[i]; foreach (var songId in perfDto.SongIds) { var song = new SongPerformer { SongId = songId.Id, PerformerId = perfId }; songsPerfs.Add(song); } } context.SongsPerformers.AddRange(songsPerfs); context.SaveChanges(); return(res.ToString().Trim()); }
public static string ImportProducersAlbums(MusicHubDbContext context, string jsonString) { var ProducersDto = JsonConvert.DeserializeObject <ImportProducersDto[]>(jsonString); var producers = new List <Producer>(); var albums = new List <Album>(); var sb = new StringBuilder(); foreach (var producerDto in ProducersDto) { if (!IsValid(producerDto)) { sb.AppendLine(ErrorMessage); continue; } bool albumsAreValid = true; foreach (var albumDto in producerDto.Albums) { if (!IsValid(albumDto)) { sb.AppendLine(ErrorMessage); albumsAreValid = false; break; } } if (albumsAreValid) { var producer = (new Producer { Name = producerDto.Name, Pseudonym = producerDto.Pseudonym, PhoneNumber = producerDto.PhoneNumber, }); foreach (var albumDto in producerDto.Albums) { albums.Add(new Album { Name = albumDto.Name, ReleaseDate = DateTime.ParseExact(albumDto.ReleaseDate, "dd/MM/yyyy", CultureInfo.InvariantCulture), Producer = producer, }); } producers.Add(producer); if (producer.PhoneNumber == null) { sb.AppendLine($"Imported {producerDto.Name} with no phone number produces {producerDto.Albums.Count} albums"); } else { sb.AppendLine($"Imported {producerDto.Name} with phone: {producerDto.PhoneNumber} produces {producerDto.Albums.Count} albums"); } } } context.Producers.AddRange(producers); context.Albums.AddRange(albums); context.SaveChanges(); return(sb.ToString().TrimEnd()); }