public ActionResult Create(Album album) { if (ModelState.IsValid) { Artist tempArtist = null; Album tempAlbum = null; tempAlbum = _db.Albums.SingleOrDefault(a => a.Title.ToLower() == album.Title.Trim().ToLower() && a.Artist.Name.ToLower() == album.Artist.Name.ToLower()); if (tempAlbum == null) { tempArtist = _db.Artists.SingleOrDefault(a => a.Name.ToLower() == album.Artist.Name.Trim().ToLower()); if (tempArtist != null) { album.Artist = tempArtist; } else { _db.Artists.Add(new Artist() { Name = album.Artist.Name }); _db.SaveChanges(); album.Artist = _db.Artists.SingleOrDefault(a => a.Name.ToLower() == album.Artist.Name.Trim().ToLower()); } _db.Albums.Add(album); _db.SaveChanges(); return RedirectToAction("Index"); } else { ViewBag.ErrorMessage = "The album '" + album.Title + "' by '" + album.Artist.Name + "' already exists."; return View(album); } } return View(album); }
public ActionResult Edit(Album album) { if (ModelState.IsValid) { if (!AlbumExists(album.Title, album.Artist.Name)) { Album edittedAlbum = _db.Albums.Find(album.Id); edittedAlbum.Title = album.Title; edittedAlbum.ReleaseDate = album.ReleaseDate; Artist tempArtist = _db.Artists.FirstOrDefault(artist => artist.Name.ToLower() == album.Artist.Name.Trim().ToLower()); if (tempArtist == null) { edittedAlbum.Artist = new Artist() { Name = album.Artist.Name, DateModified = DateTime.Now }; } else { edittedAlbum.Artist = tempArtist; } _db.SetModified(edittedAlbum); _db.SaveChanges(); return RedirectToAction("Index"); } return View(album); } else { ViewBag.ErrorMessage = "The album '" + album.Title + "' by '" + album.Artist.Name + "' already exists."; return View(album); } }
public void Album_Create() { ISixteenBarsDb mockDb = new MockSixteenBarsDb(); AlbumController ctrl = new AlbumController(mockDb); Album newAlbum = new Album() { Id = 5, Title = "Doggystyle", ReleaseDate = new DateTime(1993,11, 23), Artist = new Artist() { Id=11, Name = "Snoop Doggy Dogg" } }; ctrl.Create(newAlbum); Assert.AreEqual(newAlbum.Title, mockDb.Albums.Find(5).Title, "Doggystyle not added."); Assert.AreEqual(newAlbum.ReleaseDate, mockDb.Albums.Find(5).ReleaseDate, "Doggystyle release date not 11/23/93."); Assert.AreEqual(newAlbum.Artist.Name, mockDb.Albums.Find(5).Artist.Name, "Snoop Doggy Dogg not added as artist for Doggystyle."); newAlbum = new Album() { Id = 6, Title = ".~`!@#$%^&*()_+-={}|:\"<>?[]\\;',./", ReleaseDate = new DateTime(1993,11, 23), Artist = new Artist() { Id=12, Name = ".~`!@#$%^&*()_+-={}|:\"<>?[]\\;',./" } }; ctrl.Create(newAlbum); Assert.AreEqual(newAlbum.Title, mockDb.Albums.Find(6).Title, ".~`!@#$%^&*()_+-={}|:\"<>?[]\\;',./ not added."); Assert.AreEqual(newAlbum.ReleaseDate, mockDb.Albums.Find(6).ReleaseDate, ".~`!@#$%^&*()_+-={}|:\"<>?[]\\;',./ release date not 11/23/93."); Assert.AreEqual(newAlbum.Artist.Name, mockDb.Albums.Find(6).Artist.Name, ".~`!@#$%^&*()_+-={}|:\"<>?[]\\;',./ not added as artist for .~`!@#$%^&*()_+-={}|:\"<>?[]\\;',./."); newAlbum = new Album() { Id = 7, Title = "The Blueprint", ReleaseDate = new DateTime(2001, 9, 11), Artist = new Artist() { Id = 1 } }; ctrl.Create(newAlbum); Assert.AreEqual(newAlbum.Title, mockDb.Albums.Find(7).Title, "The Blueprint not added."); Assert.AreEqual(newAlbum.ReleaseDate, mockDb.Albums.Find(7).ReleaseDate, "The Blueprint release date not 9/11/01."); Assert.AreEqual("Jay-Z", mockDb.Albums.Find(7).Artist.Name, "Jay-Z not added as artist for The Blueprint."); newAlbum = new Album() { Id = 8, Title = "Because the Internet", ReleaseDate = new DateTime(2013, 12, 3), Artist = new Artist() { Id = 3 } }; ctrl.Create(newAlbum); Assert.IsNull(mockDb.Albums.Find(8), "Because the Internet was created twice"); newAlbum = new Album() { Id = 9, Title = "Because the Internet", ReleaseDate = new DateTime(2013, 12, 3), Artist = new Artist() { Id = 12, Name = "Childish Gambino" } }; ctrl.Create(newAlbum); Assert.IsNull(mockDb.Albums.Find(9), "Because the Internet was created twice"); Assert.IsNull(mockDb.Artists.Find(12), "Childish Gambino was created twice"); newAlbum = new Album() { Id = 9, Title = "The Blueprint", ReleaseDate = new DateTime(2015, 5, 4), Artist = new Artist() { Id = 12, Name = "Jay-Z Imposter" } }; ctrl.Create(newAlbum); Assert.AreEqual(newAlbum.Title, mockDb.Albums.Find(9).Title, "The Blueprint from different artist not added."); Assert.AreEqual(newAlbum.ReleaseDate, mockDb.Albums.Find(9).ReleaseDate, "The Blueprint from different artist release date not 5/4/15."); Assert.AreEqual("Jay-Z Imposter", mockDb.Albums.Find(9).Artist.Name, "The Jay-Z Imposter not added as artist for The other Blueprint album."); }