public async Task <IActionResult> Create(MusicCreateViewModel createModel) { List <string> songsFromDb = await _mediaWebDbContext.Songs.Select(s => s.Title).ToListAsync(); List <string> artistFromDb = await _mediaWebDbContext.Artists.Select(a => a.Name).ToListAsync(); //check for duplicates if (songsFromDb.Contains(StringEdits.FirstLettterToUpper(createModel.Title))) { return(RedirectToAction("Index")); } Song newSong = new Song() { Title = StringEdits.FirstLettterToUpper(createModel.Title), ReleaseDate = createModel.ReleaseDate, SongFile = UploadUtility.UploadFile(createModel.Song, "songs", _hostingEnvironment) }; //add artists to song var songArtists = new List <SongArtist>(); //create only if new artists if (createModel.createdArtists != null) { var createdArtists = StringEdits.FirstLettterToUpper(createModel.createdArtists); var createdArtistsArray = createdArtists.Split(", "); var newArtists = new List <Artist>(); var artistsFromDb = await _mediaWebDbContext.Artists.ToListAsync(); foreach (var createdArtist in createdArtistsArray) { if (!artistsFromDb.Select(g => g.Name).Contains(createdArtist)) { newArtists.Add(new Artist() { Name = createdArtist }); } else { songArtists.Add(new SongArtist() { Artist = artistsFromDb.Find(g => g.Name == createdArtist) }); } } foreach (var newArtist in newArtists) { songArtists.Add(new SongArtist() { ArtistId = newArtist.Id, Artist = newArtist }); } } newSong.SongArtists = songArtists; _mediaWebDbContext.Update(newSong); await _mediaWebDbContext.SaveChangesAsync(); return(RedirectToAction("Index")); }
public async Task <IActionResult> Create(PodcastCreateViewModel createModel) { List <string> podcastTitlesFromDb = await _mediaWebDbContext.PodCasts.Select(p => p.Title).ToListAsync(); if (podcastTitlesFromDb.Contains(StringEdits.FirstLettterToUpper(createModel.Title))) { return(RedirectToAction("Index")); } PodCast newPodcast = new PodCast() { Title = StringEdits.FirstLettterToUpper(createModel.Title), ReleaseDate = createModel.ReleaseDate, Description = createModel.Description, File = createModel.PodcastLink }; //add guests to podcast var podcastGuests = new List <PodcastGuest>(); //create only if new guests if (createModel.createdGuests != null) { var createdGuests = StringEdits.FirstLettterToUpper(createModel.createdGuests); var createdGuestsArray = createdGuests.Split(", "); var newGuests = new List <Guest>(); var guestsFromDb = await _mediaWebDbContext.Guests.ToListAsync(); foreach (var createdGuest in createdGuestsArray) { if (!guestsFromDb.Select(g => g.Name).Contains(createdGuest)) { newGuests.Add(new Guest() { Name = createdGuest }); } else { podcastGuests.Add(new PodcastGuest() { Guest = guestsFromDb.Find(g => g.Name == createdGuest) }); } } foreach (var newGuest in newGuests) { podcastGuests.Add(new PodcastGuest() { Guest = newGuest }); } } newPodcast.PodCastGuests = podcastGuests; //add hosts to podcast var podcastHosts = new List <PodcastHost>(); //create only if new hosts if (createModel.createdHosts != null) { var createdHosts = StringEdits.FirstLettterToUpper(createModel.createdHosts); var createdHostsArray = createdHosts.Split(", "); var newHosts = new List <Host>(); var hostsFromDb = await _mediaWebDbContext.Hosts.ToListAsync(); foreach (var createdHost in createdHostsArray) { if (!hostsFromDb.Select(g => g.Name).Contains(createdHost)) { newHosts.Add(new Host() { Name = createdHost }); } else { podcastHosts.Add(new PodcastHost() { Host = hostsFromDb.Find(g => g.Name == createdHost) }); } } foreach (var newHost in newHosts) { podcastHosts.Add(new PodcastHost() { Host = newHost }); } } newPodcast.PodCastHosts = podcastHosts; _mediaWebDbContext.Update(newPodcast); await _mediaWebDbContext.SaveChangesAsync(); return(RedirectToAction("Index")); }
public async Task <IActionResult> Create(MovieCreateViewModel createModel) { //check for duplicate List <string> movieTitlesFromDb = await _mediaWebDbContext.Movies.Select(m => m.Title).ToListAsync(); if (movieTitlesFromDb.Contains(StringEdits.FirstLettterToUpper(createModel.Title))) { return(RedirectToAction("Index")); } //create if no duplicate Movie newMovie = new Movie() { Title = StringEdits.FirstLettterToUpper(createModel.Title), ReleaseDate = createModel.ReleaseDate, Summary = createModel.Summary, Photo = UploadUtility.UploadFile(createModel.Photo, "pics", _hostingEnvironment), IsHidden = false }; //Add genres to movie var movieGenres = new List <MovieGenre>(); //Select from existing genres if (createModel.SelectedGenres != null) { foreach (var selectedGenre in createModel.SelectedGenres) { movieGenres.Add(new MovieGenre() { GenreId = selectedGenre }); } } //create only if new genres if (createModel.createdGenres != null) { var createdGenres = StringEdits.FirstLettterToUpper(createModel.createdGenres); var createdGenresArray = createdGenres.Split(", "); var newGenres = new List <Genre>(); var genresFromDb = await _mediaWebDbContext.Genres.ToListAsync(); foreach (var createdGenre in createdGenresArray) { if (!genresFromDb.Select(g => g.Name).Contains(createdGenre)) { newGenres.Add(new Genre() { Name = createdGenre }); } else { movieGenres.Add(new MovieGenre() { Genre = genresFromDb.Find(g => g.Name == createdGenre) }); } } foreach (var newGenre in newGenres) { movieGenres.Add(new MovieGenre() { Genre = newGenre }); } } newMovie.MovieGenres = movieGenres; //add actors to movie var movieActors = new List <MovieActor>(); //create only if new actors if (createModel.createdActors != null) { var createdActors = StringEdits.FirstLettterToUpper(createModel.createdActors); var createdActorsArray = createdActors.Split(", "); var newActors = new List <Actor>(); var actorsFromDb = await _mediaWebDbContext.Actors.ToListAsync(); foreach (var createdActor in createdActorsArray) { //check for duplicates if (!actorsFromDb.Select(g => g.Name).Contains(createdActor)) { newActors.Add(new Actor() { Name = createdActor }); } else { movieActors.Add(new MovieActor() { Actor = actorsFromDb.Find(g => g.Name == createdActor) }); } } foreach (var newActor in newActors) { movieActors.Add(new MovieActor() { Actor = newActor }); } } newMovie.MovieActors = movieActors; //add directors to movie var movieDirectors = new List <MovieDirector>(); //create only if new directors if (createModel.createdDirectors != null) { var createdDirectors = StringEdits.FirstLettterToUpper(createModel.createdDirectors); var createdDirectorsArray = createdDirectors.Split(", "); var newDirectors = new List <Director>(); var directorsFromDb = await _mediaWebDbContext.Directors.ToListAsync(); foreach (var createdDirector in createdDirectorsArray) { if (!directorsFromDb.Select(g => g.Name).Contains(createdDirector)) { newDirectors.Add(new Director() { Name = createdDirector }); } else { movieDirectors.Add(new MovieDirector() { Director = directorsFromDb.Find(g => g.Name == createdDirector) }); } } foreach (var newDirector in newDirectors) { movieDirectors.Add(new MovieDirector() { Director = newDirector }); } } newMovie.MovieDirectors = movieDirectors; _mediaWebDbContext.Update(newMovie); await _mediaWebDbContext.SaveChangesAsync(); return(RedirectToAction("Index")); }
public async Task <IActionResult> Create(TVShowCreateViewModel createModel) { List <string> tvshowTitlesFromDb = await _mediaWebDbContext.TVShows.Select(tvs => tvs.Name).ToListAsync(); if (tvshowTitlesFromDb.Contains(StringEdits.FirstLettterToUpper(createModel.Name))) { return(RedirectToAction("Index")); } TVshow newTvshow = new TVshow() { Name = StringEdits.FirstLettterToUpper(createModel.Name), ReleaseDate = createModel.ReleaseDate, Summary = createModel.Summary, Picture = UploadUtility.UploadFile(createModel.Picture, "tvshows", _hostingEnvironment) }; //Add genres to tvshow var tvshowGenres = new List <TVShowGenre>(); //Select from existing genres if (createModel.SelectedGenres != null) { foreach (var selectedGenre in createModel.SelectedGenres) { tvshowGenres.Add(new TVShowGenre() { GenreId = selectedGenre }); } } //create only if new genres if (createModel.createdGenres != null) { var createdGenres = StringEdits.FirstLettterToUpper(createModel.createdGenres); var createdGenresArray = createdGenres.Split(", "); var newGenres = new List <Genre>(); var genresFromDb = await _mediaWebDbContext.Genres.ToListAsync(); foreach (var createdGenre in createdGenresArray) { if (!genresFromDb.Select(g => g.Name).Contains(createdGenre)) { newGenres.Add(new Genre() { Name = createdGenre }); } else { tvshowGenres.Add(new TVShowGenre() { Genre = genresFromDb.Find(g => g.Name == createdGenre) }); } } foreach (var newGenre in newGenres) { tvshowGenres.Add(new TVShowGenre() { Genre = newGenre }); } } newTvshow.TVShowGenres = tvshowGenres; //add actors to tvshow var tvshowActors = new List <TVShowActor>(); //create only if new actors if (createModel.createdActors != null) { var createdActors = StringEdits.FirstLettterToUpper(createModel.createdActors); var createdActorsArray = createdActors.Split(", "); var newActors = new List <Actor>(); var actorsFromDb = await _mediaWebDbContext.Actors.ToListAsync(); foreach (var createdActor in createdActorsArray) { if (!actorsFromDb.Select(g => g.Name).Contains(createdActor)) { newActors.Add(new Actor() { Name = createdActor }); } else { tvshowActors.Add(new TVShowActor() { Actor = actorsFromDb.Find(g => g.Name == createdActor) }); } } foreach (var newActor in newActors) { tvshowActors.Add(new TVShowActor() { Actor = newActor }); } } newTvshow.TVShowActors = tvshowActors; //add directors to tvshow var tvshowDirectors = new List <TVShowDirector>(); //create only if new directors if (createModel.createdDirectors != null) { var createdDirectors = StringEdits.FirstLettterToUpper(createModel.createdDirectors); var createdDirectorsArray = createdDirectors.Split(", "); var newDirectors = new List <Director>(); var directorsFromDb = await _mediaWebDbContext.Directors.ToListAsync(); foreach (var createdDirector in createdDirectorsArray) { if (!directorsFromDb.Select(g => g.Name).Contains(createdDirector)) { newDirectors.Add(new Director() { Name = createdDirector }); } else { tvshowDirectors.Add(new TVShowDirector() { Director = directorsFromDb.Find(g => g.Name == createdDirector) }); } } foreach (var newDirector in newDirectors) { tvshowDirectors.Add(new TVShowDirector() { Director = newDirector }); } } newTvshow.TVShowDirectors = tvshowDirectors; _mediaWebDbContext.Update(newTvshow); await _mediaWebDbContext.SaveChangesAsync(); return(RedirectToAction("Index")); }