public async void CreateAlbum(MusicAlbumCreateViewModel model, Artist artist) { Album album = new Album() { Naam = model.Naam, Artist = artist }; using (var memoryStream = new MemoryStream()) { await model.Cover.CopyToAsync(memoryStream); album.Cover = memoryStream.ToArray(); } _appContext.Albums.Add(album); _appContext.SaveChanges(); ArtistAlbumSong artistAlbumSong = new ArtistAlbumSong() { AlbumId = album.Id, ArtistId = album.Artist.Id }; _appContext.ArtistAlbumSongs.Add(artistAlbumSong); _appContext.SaveChanges(); }
public IActionResult SongCreate(MusicSongCreateViewModel model) { IEnumerable <Artist> artists = _appContext.Artists.Where(a => a.Naam == model.Artist.Naam); IEnumerable <Songs> songs = _appContext.Songs.Where(s => s.Title == model.Title); IEnumerable <Album> albums = _appContext.Albums.Where(a => a.Naam == model.Album.Naam); if (!TryValidateModel(model)) { return(View(model)); } if (artists.Count() == 0) { if (model.Album.Naam == null) { Artist artist = CreateArtist(model.Artist.Naam); if (songs.Count() == 0) { Songs song = new Songs() { Title = model.Title, Duration = model.Duration, Artist = artist }; _appContext.Songs.Add(song); _appContext.SaveChanges(); ArtistAlbumSong Aas = new ArtistAlbumSong() { ArtistId = artist.Id, SongId = song.Id }; _appContext.ArtistAlbumSongs.Add(Aas); _appContext.SaveChanges(); } } else { var newArtist = CreateArtist(model.Artist.Naam); if (albums.Count() == 0) { Album album = new Album() { Naam = model.Album.Naam, Artist = model.Artist }; _appContext.Albums.Add(album); _appContext.SaveChanges(); Songs song = new Songs() { Title = model.Title, Duration = model.Duration, Artist = newArtist }; _appContext.Songs.Add(song); _appContext.SaveChanges(); ArtistAlbumSong Aas = new ArtistAlbumSong() { ArtistId = newArtist.Id, SongId = song.Id, AlbumId = album.Id }; _appContext.ArtistAlbumSongs.Add(Aas); _appContext.SaveChanges(); } else { Songs song = new Songs() { Title = model.Title, Duration = model.Duration, Artist = newArtist }; _appContext.Songs.Add(song); _appContext.SaveChanges(); foreach (var item in albums) { if (item.Naam == model.Album.Naam && item.Artist.Naam == model.Artist.Naam) { ArtistAlbumSong Aas = new ArtistAlbumSong() { ArtistId = newArtist.Id, SongId = song.Id, AlbumId = item.Id }; _appContext.ArtistAlbumSongs.Add(Aas); _appContext.SaveChanges(); } } } } } else { if (songs.Count() == 0) { Songs song = new Songs() { Title = model.Title, Duration = model.Duration, Artist = artists.FirstOrDefault(x => x.Naam == model.Artist.Naam) }; _appContext.Songs.Add(song); _appContext.SaveChanges(); ArtistAlbumSong Aas = new ArtistAlbumSong() { ArtistId = song.Artist.Id, SongId = song.Id }; _appContext.ArtistAlbumSongs.Add(Aas); _appContext.SaveChanges(); } } return(RedirectToAction("Music")); }