public void Track_Create()
        {
            MockSixteenBarsDb mockDb = new MockSixteenBarsDb();
            TrackController ctrl = new TrackController(mockDb);

            TrackViewModel newTrack = new TrackViewModel()
            {
                Title = "Shabba",
                AlbumTitle = "Trap Lord",
                ArtistName = "A$AP Ferg",
                ReleaseDate = new DateTime(2013, 8, 20)
            };

            ctrl.Create(newTrack);

            Track foundTrack = mockDb.Tracks.SingleOrDefault(t => t.Title == newTrack.Title);
            Assert.AreEqual(newTrack.Title, foundTrack.Title, "Title not Shabba");
            Assert.AreEqual(newTrack.AlbumTitle, foundTrack.Album.Title, "Name not Trap Lord");
            Assert.AreEqual(newTrack.ArtistName, foundTrack.Album.Artist.Name, "Name not A$AP Ferg");

            newTrack = new TrackViewModel()
            {
                Title = "She Don't Want A Man",
                ArtistName = "Curren$y",
                AlbumTitle = "Weekend At Burnie's",
                ReleaseDate = new DateTime(2011,6,28)
            };

            ctrl.Create(newTrack);

            foundTrack = mockDb.Tracks.SingleOrDefault(t => t.Title == newTrack.Title);
            Int32 cntArtists = mockDb.Artists.Where(a => a.Name == newTrack.ArtistName.Trim()).Count();
            Assert.AreEqual(newTrack.Title, foundTrack.Title, "Title not She Don't Want A Man");
            Assert.AreEqual(newTrack.AlbumTitle, foundTrack.Album.Title, "Name not Weekend At Burnie's");
            Assert.AreEqual(newTrack.ArtistName, foundTrack.Album.Artist.Name, "Name not Curren$y");
            Assert.AreEqual(1, cntArtists, "Artist Currenc$y was duplicated.");

            newTrack = new TrackViewModel()
            {
                Title = "I.Crawl",
                ArtistName = "Childish Gambino",
                AlbumTitle = "Because The Internet",
                ReleaseDate = new DateTime(2014, 3, 1)
            };
            ctrl.Create(newTrack);

            foundTrack = mockDb.Tracks.SingleOrDefault(t => t.Title == newTrack.Title);
            cntArtists = mockDb.Artists.Where(a => a.Name == newTrack.ArtistName.Trim()).Count();
            Int32 cntAlbums = mockDb.Albums.Where(a => a.Title == newTrack.AlbumTitle.Trim()).Count();
            Assert.AreEqual(newTrack.Title, foundTrack.Title, "Title not I.Crawl");
            Assert.AreEqual(newTrack.AlbumTitle, foundTrack.Album.Title, "Name not Because The Internet");
            Assert.AreEqual(newTrack.ArtistName, foundTrack.Album.Artist.Name, "Name not Childish Gambino");
            Assert.AreEqual(1, cntArtists, "Artist Childish Gambino was duplicated.");
            Assert.AreEqual(1, cntAlbums, "Album Because The Internet was duplicated.");
        }
        public ActionResult Create(TrackViewModel trackVM)
        {
            if (ModelState.IsValid)
            {
                Artist artist = _db.Artists.FirstOrDefault(a => a.Name.ToLower() == trackVM.ArtistName.Trim().ToLower());
                if (artist == null)
                {
                    _db.Artists.Add(new Artist() { Name = trackVM.ArtistName.Trim(), DateModified = DateTime.Now });
                    _db.SaveChanges();
                    artist = _db.Artists.FirstOrDefault(a => a.Name.ToLower() == trackVM.ArtistName.Trim().ToLower());
                }

                Album album = _db.Albums.FirstOrDefault(a => a.Title.ToLower() == trackVM.AlbumTitle.Trim().ToLower());
                if (album == null)
                {
                    _db.Albums.Add(new Album() {
                        Title = trackVM.AlbumTitle.Trim(),
                        Artist = artist,
                        ReleaseDate = trackVM.ReleaseDate,
                        DateModified = DateTime.Now
                    });
                    _db.SaveChanges();
                    album = _db.Albums.FirstOrDefault(a => a.Title.ToLower() == trackVM.AlbumTitle.Trim().ToLower());
                }

                Track track = new Track();
                track.Title = trackVM.Title;
                track.ReleaseDate = trackVM.ReleaseDate;
                track.Album = album;

                TrackAPIController api = new TrackAPIController(_db);

                if (!api.TrackExists(track.Title, track.Album.Title, track.Album.Artist.Name, (DateTime)track.ReleaseDate))
                {
                    _db.Tracks.Add(track);
                    _db.SaveChanges();
                }
                else {

                    ViewBag.ErrorMessage = "The track titled '" + track.Title + "' on the album titled '" + track.Album.Title + "' already exists.";
                    return View(trackVM);
                }

                return RedirectToAction("Index");
            }

            return View(trackVM);
        }