public ActionResult EditSong(SongViewModel collection, HttpPostedFileBase file) { Song s2 = collection.Song; if (collection.Song != null) { s2 = db.Songs.Find(collection.Song.Id); s2.Title = collection.Song.Title; } if (ModelState.IsValid) { Song s = db.Songs.Find(collection.Song.Id); Album newAlbum = db.Albums.Find(collection.SelectedAlbumId); Artist newArtist = db.Artists.Find(collection.SelectedArtistId); if (s.AlbumName != newAlbum.Name || s.ArtistName != newArtist.Name) { Album oldAlbum = (from a in db.Albums where a.Name == s.AlbumName select a).FirstOrDefault(); oldAlbum.Songs.Remove(s); s.Genre = collection.Song.Genre; s.Title = collection.Song.Title; s.AlbumName = newAlbum.Name; s.ArtistName = newArtist.Name; if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); string path1 = Server.MapPath("/Music/"); if (!Directory.Exists(path1 + s.ArtistName)) Directory.CreateDirectory(path1 + s.ArtistName); if (!Directory.Exists(path1 + s.ArtistName + "/" + s.AlbumName)) Directory.CreateDirectory(path1 + s.ArtistName + "/" + s.AlbumName); var path = Server.MapPath("/Music/" + s.ArtistName + "/" + s.AlbumName + "/" + fileName); file.SaveAs(path); s.FilePath = s.ArtistName + "\\" + s.AlbumName + "\\" + fileName; } newAlbum.Songs.Add(s); db.Entry(oldAlbum).State = EntityState.Modified; db.Entry(newAlbum).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Album", new { id = newAlbum.Id }); } s.Genre = collection.Song.Genre; s.Title = collection.Song.Title; s.AlbumName = newAlbum.Name; s.ArtistName = newArtist.Name; if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); string path1 = Server.MapPath("/Music/"); if (!Directory.Exists(path1 + s.ArtistName)) Directory.CreateDirectory(path1 + s.ArtistName); if (!Directory.Exists(path1 + s.ArtistName + "/" + s.AlbumName)) Directory.CreateDirectory(path1 + s.ArtistName + "/" + s.AlbumName); var path = Server.MapPath("/Music/" + s.ArtistName + "/" + s.AlbumName + "/" + fileName); file.SaveAs(path); s.FilePath = s.ArtistName + "\\" + s.AlbumName + "\\" + fileName; } db.Entry(s).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Album", new { id = newAlbum.Id }); } collection.Song = s2; collection.Artists = db.Artists.ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }); Artist art = (from s in db.Artists where s.Id == collection.SelectedArtistId select s).FirstOrDefault(); if (art != null) { collection.Albums = art.Albums.ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }); } return View(collection); }
public ActionResult CreateSong(SongViewModel collection, HttpPostedFileBase file) { if (ModelState.IsValid) { Song s = new Song(); s.AddDate = DateTime.Now; Album album = db.Albums.Find(collection.SelectedAlbumId); s.AlbumName = album.Name; Artist artist = db.Artists.Find(collection.SelectedArtistId); s.ArtistName = artist.Name; s.Genre = collection.Song.Genre; s.NumberOfPlays = 0; s.Position = -1; s.Rating = 0; s.Title = collection.Song.Title; s.Voters = new List<Voting>(); if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); string path1 = Server.MapPath("/Music/"); if (!Directory.Exists(path1 + artist.Name)) Directory.CreateDirectory(path1 + artist.Name); if (!Directory.Exists(path1 + artist.Name + "/" + album.Name)) Directory.CreateDirectory(path1 + artist.Name + "/" + album.Name); var path = Server.MapPath("/Music/" + artist.Name + "/" + album.Name + "/" + fileName); file.SaveAs(path); s.FilePath = artist.Name + "\\" + album.Name + "\\" + fileName; } album.Songs.Add(s); db.Entry(album).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Album", new { id = album.Id }); } collection.Artists = db.Artists.ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }); Artist art = (from s in db.Artists where s.Id == collection.SelectedArtistId select s).FirstOrDefault(); if (art != null) { collection.Albums = art.Albums.ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }); } return View(collection); }
public ActionResult EditSong(int id, string artist, string album) { Artist art = (from s in db.Artists where s.Name == artist select s).FirstOrDefault(); var model = new SongViewModel { Artists = db.Artists.ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }) }; if (art != null) { model.SelectedArtistId = art.Id; model.Albums = art.Albums.ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }); Album alb = (from s in db.Albums where s.Name == album select s).FirstOrDefault(); if (alb != null) { model.SelectedAlbumId = alb.Id; } } model.Song = db.Songs.Find(id); return View(model); }
public ActionResult CreateSong(string artist, string album) { var model = new SongViewModel { Artists = db.Artists.ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }) }; Artist art = (from s in db.Artists where s.Name == artist select s).FirstOrDefault(); if (art == null) art = (from s in db.Artists select s).FirstOrDefault(); if (art != null) { model.SelectedArtistId = art.Id; model.Albums = art.Albums.ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }); Album alb = (from s in db.Albums where s.Name == album select s).FirstOrDefault(); if (alb != null) { model.SelectedAlbumId = alb.Id; if (alb.Songs.Count > 0) { model.Song = new Song(); model.Song.Genre = alb.Songs[0].Genre; } } } return View(model); }