Example #1
0
        public async Task <IActionResult> Edit(int id, [Bind("ArtistName,CountryOfOrigin,Subgenre")] ArtistModel artistModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var artistToUpdate = await _context.Artists
                                         .Include(a => a.Albums)
                                         .ThenInclude(t => t.Tracks)
                                         .Include(s => s.Subgenre)
                                         .AsNoTracking()
                                         .FirstOrDefaultAsync(c => c.ArtistID == id);

                    if (String.IsNullOrWhiteSpace(artistModel.ArtistName))
                    {
                        TempData["messageArtist"] = "You must enter an artist. Try again.";
                        return(View());
                    }
                    if (String.IsNullOrWhiteSpace(artistModel.CountryOfOrigin))
                    {
                        artistModel.CountryOfOrigin = "Unknown";
                    }

                    if (String.IsNullOrWhiteSpace(artistModel.Subgenre.SubgenreName))
                    {
                        artistModel.Subgenre.SubgenreName = "Unknown";
                    }

                    TextInfo caseSwitch = new CultureInfo("en-US", false).TextInfo;

                    artistModel.ArtistName = artistModel.ArtistName.ToLower();
                    artistModel.ArtistName = caseSwitch.ToTitleCase(artistModel.ArtistName);

                    artistModel.Subgenre.SubgenreName = artistModel.Subgenre.SubgenreName.ToLower();
                    artistModel.Subgenre.SubgenreName = caseSwitch.ToTitleCase(artistModel.Subgenre.SubgenreName);

                    artistModel.CountryOfOrigin = artistModel.CountryOfOrigin.ToLower();
                    artistModel.CountryOfOrigin = caseSwitch.ToTitleCase(artistModel.CountryOfOrigin);

                    string[] ukArray  = new string[] { "uk", "united kingdom", "britain", "great britain", "england", "scotland", "wales", "northern ireland" };
                    string[] usaArray = new string[] { "usa", "us", "america", "united states", "united states of america",
                                                       "u.s", "u.s.", "u.s.a", "u.s.a.", "us of a", "'murica", "murica", "merica", "'merica" };

                    foreach (var u in ukArray)
                    {
                        if (artistModel.CountryOfOrigin.ToLower() == u)
                        {
                            artistModel.CountryOfOrigin = "UK";
                        }
                    }

                    foreach (var u in usaArray)
                    {
                        if (artistModel.CountryOfOrigin.ToLower() == u)
                        {
                            artistModel.CountryOfOrigin = "USA";
                        }
                    }

                    if (artistModel.ArtistName.ToLower() != artistToUpdate.ArtistName.ToLower())
                    {
                        artistModel.ArtistID = id;
                    }

                    if (artistModel.CountryOfOrigin.ToLower() != artistToUpdate.CountryOfOrigin.ToLower())
                    {
                        artistToUpdate.CountryOfOrigin = artistModel.CountryOfOrigin;
                        artistModel.ArtistID           = id;
                    }

                    if (artistModel.ArtistName.ToLower() == artistToUpdate.ArtistName.ToLower() &&
                        artistModel.CountryOfOrigin.ToLower() == artistToUpdate.CountryOfOrigin.ToLower() &&
                        artistModel.Subgenre.SubgenreName.ToLower() == artistToUpdate.Subgenre.SubgenreName.ToLower())
                    {
                        artistModel.ArtistID = id;
                    }

                    var duplicateCheck = _context.Artists
                                         .Include(s => s.Subgenre)
                                         .Include(a => a.Albums)
                                         .ThenInclude(t => t.Tracks)
                                         .AsNoTracking();

                    foreach (var d in duplicateCheck)
                    {
                        if (artistModel.ArtistName.ToLower() == d.ArtistName.ToLower())
                        {
                            if (artistModel.ArtistName.ToLower() != artistToUpdate.ArtistName.ToLower())
                            {
                                TempData["messageExists"] = "That artist already exists.";
                                return(View());
                            }
                        }
                    }

                    var subgenreCheck = _context.Subgenres
                                        .AsNoTracking();

                    foreach (var s in subgenreCheck)
                    {
                        if (artistModel.Subgenre.SubgenreName == s.SubgenreName)
                        {
                            artistModel.ArtistID            = id;
                            artistModel.Subgenre.SubgenreID = s.SubgenreID;
                        }
                    }

                    if (!subgenreCheck.Any(s => s.SubgenreName == artistModel.Subgenre.SubgenreName))
                    {
                        artistModel.ArtistID = id;
                        SubgenreModel newSubgenre = new SubgenreModel();
                    }

                    _context.Update(artistModel);
                    await _context.SaveChangesAsync();
                }

                catch (DbUpdateException)
                {
                    if (!ArtistModelExists(artistModel.ArtistID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }

            return(View(artistModel));
        }
        public async Task <IActionResult> Edit(int id, [Bind("AlbumName,Artist")] AlbumModel albumModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (String.IsNullOrWhiteSpace(albumModel.AlbumName))
                    {
                        TempData["messageAlbum"] = "You must enter an album. Try again.";
                        return(View());
                    }

                    if (albumModel.AlbumName.ToLower() == "single" || albumModel.AlbumName.ToLower() == "singles" ||
                        albumModel.AlbumName.ToLower() == "no album" || albumModel.AlbumName.ToLower() == "n/a")
                    {
                        TempData["message"] = "If you are trying to add an individual track, please go to 'Tracks' and add it there.";
                        return(View());
                    }

                    if (String.IsNullOrWhiteSpace(albumModel.Artist.ArtistName))
                    {
                        TempData["messageArtist"] = "You must enter an artist. Try again.";
                        return(View());
                    }



                    TextInfo caseSwitch = new CultureInfo("en-US", false).TextInfo;

                    albumModel.AlbumName = albumModel.AlbumName.ToLower();
                    albumModel.AlbumName = caseSwitch.ToTitleCase(albumModel.AlbumName);

                    albumModel.Artist.ArtistName = albumModel.Artist.ArtistName.ToLower();
                    albumModel.Artist.ArtistName = caseSwitch.ToTitleCase(albumModel.Artist.ArtistName);

                    var albumToUpdate = await _context.Albums
                                        .Include(a => a.Artist)
                                        .ThenInclude(s => s.Subgenre)
                                        .AsNoTracking()
                                        .FirstOrDefaultAsync(c => c.AlbumID == id);

                    if (albumModel.AlbumName.ToLower() != albumToUpdate.AlbumName.ToLower())
                    {
                        albumModel.AlbumID = id;
                    }

                    var duplicateCheck = _context.Albums
                                         .Include(a => a.Artist)
                                         .AsNoTracking();

                    foreach (var d in duplicateCheck)
                    {
                        if (albumModel.AlbumName.ToLower() == d.AlbumName.ToLower())
                        {
                            if (albumModel.Artist.ArtistName.ToLower() == d.Artist.ArtistName.ToLower())
                            {
                                TempData["message"] = "That album already exists.";
                                return(View());
                            }
                        }
                    }

                    var artistCheck = _context.Artists
                                      .Include(s => s.Subgenre)
                                      .AsNoTracking();

                    foreach (var a in artistCheck)
                    {
                        if (albumModel.Artist.ArtistName.ToLower() == a.ArtistName.ToLower())
                        {
                            albumModel.Artist.ArtistID        = a.ArtistID;
                            albumModel.Artist.CountryOfOrigin = a.CountryOfOrigin;
                            albumModel.Artist.SubgenreID      = a.SubgenreID;
                            albumModel.AlbumID = id;
                        }
                    }

                    if (!artistCheck.Any(a => a.ArtistName == albumModel.Artist.ArtistName))
                    {
                        TempData["message"] = "Oops! That artist has not been entered in DNBase yet. " +
                                              "Please add them below.";

                        return(RedirectToAction("Create", "ArtistModels"));
                    }

                    _context.Update(albumModel);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AlbumModelExists(albumModel.AlbumID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }

            return(View(albumModel));
        }
        public async Task <IActionResult> Edit(int id, [Bind("TrackName,Album")] TrackModel trackModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (String.IsNullOrWhiteSpace(trackModel.TrackName))
                    {
                        TempData["messageTrackName"] = "You must enter a track name. Try again.";
                        return(View());
                    }

                    if (String.IsNullOrWhiteSpace(trackModel.Album.AlbumName))
                    {
                        TempData["messageAlbumName"] = "You must enter an album name. If its a single, please write 'Single'. " +
                                                       "If it is unknown, please write 'Unknown'";
                        return(View());
                    }

                    if (String.IsNullOrWhiteSpace(trackModel.Album.Artist.ArtistName))
                    {
                        TempData["messageArtistName"] = "You must enter the artists name. " +
                                                        "Let's give them some credit here.";
                        return(View());
                    }

                    TextInfo caseSwitch = new CultureInfo("en-US", false).TextInfo;

                    trackModel.TrackName = trackModel.TrackName.ToLower();
                    trackModel.TrackName = caseSwitch.ToTitleCase(trackModel.TrackName);

                    trackModel.Album.AlbumName = trackModel.Album.AlbumName.ToLower();
                    trackModel.Album.AlbumName = caseSwitch.ToTitleCase(trackModel.Album.AlbumName);

                    trackModel.Album.Artist.ArtistName = trackModel.Album.Artist.ArtistName.ToLower();
                    trackModel.Album.Artist.ArtistName = caseSwitch.ToTitleCase(trackModel.Album.Artist.ArtistName);

                    if (trackModel.Album.AlbumName.ToLower() == "single" || trackModel.Album.AlbumName.ToLower() == "singles")
                    {
                        trackModel.Album.AlbumName = "Singles";
                    }

                    var trackToUpdate = await _context.Tracks
                                        .AsNoTracking()
                                        .FirstOrDefaultAsync(c => c.TrackID == id);

                    if (trackModel.TrackName.ToLower() != trackToUpdate.TrackName.ToLower())
                    {
                        trackModel.TrackID = id;
                    }

                    var duplicateCheck = _context.Tracks
                                         .Include(a => a.Album)
                                         .ThenInclude(b => b.Artist)
                                         .AsNoTracking();

                    foreach (var d in duplicateCheck)
                    {
                        if (trackModel.TrackName.ToLower() == d.TrackName.ToLower())
                        {
                            if (trackModel.Album.AlbumName.ToLower() == d.Album.AlbumName.ToLower())
                            {
                                if (trackModel.Album.Artist.ArtistName.ToLower() == d.Album.Artist.ArtistName.ToLower())
                                {
                                    TempData["message"] = "That track already exists.";
                                    return(View());
                                }
                            }
                        }
                    }

                    var albumOrArtistExistsCheck = _context.Artists
                                                   .Include(a => a.Albums)
                                                   .Include(s => s.Subgenre)
                                                   .AsNoTracking();

                    foreach (var a in albumOrArtistExistsCheck)
                    {
                        if (trackModel.Album.Artist.ArtistName.ToLower() == a.ArtistName.ToLower())
                        {
                            trackModel.Album.Artist.ArtistID        = a.ArtistID;
                            trackModel.Album.Artist.CountryOfOrigin = a.CountryOfOrigin;
                            trackModel.Album.Artist.SubgenreID      = a.SubgenreID;
                            trackModel.TrackID = id;
                        }
                    }

                    if (!albumOrArtistExistsCheck.Any(a => a.ArtistName == trackModel.Album.Artist.ArtistName))
                    {
                        TempData["message"] = "Oops! That artist has not been entered in DNBase yet. " +
                                              "Please add them below.";

                        return(RedirectToAction("Create", "ArtistModels"));
                    }

                    ArtistModel existingArtist = _context.Artists.SingleOrDefault(a => a.ArtistName == trackModel.Album.Artist.ArtistName);

                    trackModel.Album.Artist = existingArtist;

                    var checkAlbums = _context.Albums
                                      .Include(a => a.Artist)
                                      .AsNoTracking();

                    foreach (var check in checkAlbums)
                    {
                        if (trackModel.Album.AlbumName.ToLower() == check.AlbumName.ToLower())
                        {
                            AlbumModel existingAlbum = _context.Albums.SingleOrDefault(a => a.AlbumID == check.AlbumID);

                            if (existingAlbum.ArtistID == existingArtist.ArtistID)
                            {
                                trackModel.Album = existingAlbum;
                            }
                        }
                    }

                    if (!checkAlbums.Any(c => c.AlbumName == trackModel.Album.AlbumName))
                    {
                        AlbumModel newAlbum = new AlbumModel()
                        {
                            AlbumName = trackModel.Album.AlbumName
                        };
                    }

                    _context.Update(trackModel);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TrackModelExists(trackModel.TrackID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(trackModel));
        }