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)); }
public ActionResult Create(Artist artist) { if (ModelState.IsValid) { Artist tempArtist = _db.Artists.SingleOrDefault(a => a.Name == artist.Name); if (tempArtist == null) { _db.Artists.Add(artist); _db.SaveChanges(); } else { ViewBag.ErrorMessage = "The artist titled '" + artist.Name + "' already exists."; return(View(artist)); } return(RedirectToAction("Index")); } return(View(artist)); }
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)); }