Beispiel #1
0
        public async Task <IActionResult> Create([Bind("SubgenreName")] SubgenreModel subgenreModel)
        {
            if (ModelState.IsValid)
            {
                if (String.IsNullOrWhiteSpace(subgenreModel.SubgenreName))
                {
                    TempData["message"] = "You must enter a subgenre. Try again.";
                    return(View());
                }

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

                subgenreModel.SubgenreName = subgenreModel.SubgenreName.ToLower();
                subgenreModel.SubgenreName = toTitleCase.ToTitleCase(subgenreModel.SubgenreName);

                var subgenreNames = _context.Subgenres;

                foreach (var s in subgenreNames)
                {
                    if (subgenreModel.SubgenreName.ToLower() == s.SubgenreName.ToLower())
                    {
                        TempData["message"] = "That subgenre already exists.";
                        return(View());
                    }
                }

                _context.Add(subgenreModel);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(subgenreModel));
        }
Beispiel #2
0
        public async Task <IActionResult> Create([Bind("ArtistName,CountryOfOrigin,Subgenre")] ArtistModel artistModel)
        {
            if (ModelState.IsValid)
            {
                if (String.IsNullOrWhiteSpace(artistModel.ArtistName))
                {
                    TempData["messageArtist"] = "You must enter an artist name. 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";
                    }
                }

                SubgenreModel subgenre = await _context.Subgenres.SingleOrDefaultAsync(s => s.SubgenreName == artistModel.Subgenre.SubgenreName);

                if (subgenre == null)
                {
                    subgenre = new SubgenreModel()
                    {
                        SubgenreName = artistModel.Subgenre.SubgenreName
                    };
                }

                ArtistModel newArtist = new ArtistModel()
                {
                    ArtistName      = artistModel.ArtistName,
                    CountryOfOrigin = artistModel.CountryOfOrigin,
                    Subgenre        = subgenre
                };

                var subgenreNames = _context.Subgenres;

                foreach (var s in subgenreNames)
                {
                    if (artistModel.Subgenre.SubgenreName.ToLower() == s.SubgenreName.ToLower())
                    {
                        artistModel.SubgenreID = s.SubgenreID;
                    }
                }

                var artistNames = _context.Artists;
                foreach (var a in artistNames)
                {
                    if (artistModel.ArtistName.ToLower() == a.ArtistName.ToLower())
                    {
                        TempData["message"] = "That artist already exists in the database.";
                        return(View());
                    }
                }

                _context.Add(newArtist);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(artistModel));
        }
        public async Task <IActionResult> Create([Bind("AlbumName,Artist")] AlbumModel albumModel)
        {
            if (ModelState.IsValid)
            {
                if (String.IsNullOrWhiteSpace(albumModel.AlbumName))
                {
                    TempData["messageAlbumName"] = "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["messageAlbumName"] = "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["messageArtistName"] = "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 albumDuplicate = _context.Albums
                                     .Include(a => a.Artist)
                                     .AsNoTracking();

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

                var artistNames = _context.Artists
                                  .AsNoTracking();

                foreach (var a in artistNames)
                {
                    if (albumModel.Artist.ArtistName.ToLower() == a.ArtistName.ToLower())
                    {
                        albumModel.ArtistID = a.ArtistID;
                    }
                }

                ArtistModel artist = await _context.Artists.SingleOrDefaultAsync(a => a.ArtistName == albumModel.Artist.ArtistName);

                if (artist == null)
                {
                    TempData["message"] = "Oops! That artist has not been entered in DNBase yet. " +
                                          "Please add them below.";
                    return(RedirectToAction("Create", "ArtistModels"));
                }
                else
                {
                    AlbumModel newAlbum = new AlbumModel()
                    {
                        AlbumName = albumModel.AlbumName,
                        Artist    = artist
                    };

                    _context.Add(newAlbum);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            return(View(albumModel));
        }
        public async Task <IActionResult> Create([Bind("TrackName,Album,Artist")] TrackModel trackModel)
        {
            if (ModelState.IsValid)
            {
                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 trackDuplicate = _context.Tracks
                                     .Include(a => a.Album)
                                     .ThenInclude(t => t.Artist)
                                     .AsNoTracking();

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

                var checkArtists = _context.Artists
                                   .Include(a => a.Albums)
                                   .AsNoTracking();

                if (!checkArtists.Any(c => c.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.Add(trackModel);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(trackModel));
        }