public ActionResult AddSong(int?id) { if (id != null) { Entities db = new Entities(); AlbumSongGenresModel ASG = new AlbumSongGenresModel(); ASG.Genres = db.Genres; ASG.Album = (from a in db.Albums where a.AlbumID == id select a).Single(); return(View(ASG)); } return(View(id)); }
public ActionResult AddSong(Song song, HttpPostedFileBase file) { Entities db = new Entities(); AlbumSongGenresModel ASG = new AlbumSongGenresModel(); ASG.Genres = db.Genres; ASG.Album = (from a in db.Albums where a.AlbumID == song.AlbumID select a).Single(); ASG.song = song; if (file == null) { ViewBag.Error = "Nie wybrano pliku"; return(View("AddSong", ASG)); } if ( ASG.Album.Songs.Any(a => a.Name.ToUpper() == song.Name.ToUpper() || ASG.Album.Songs.Any(s => s.AlbumPosition == song.AlbumPosition)) ) { ViewBag.Error = ("Piosenka o podanym tytule lub pozycji istnieje w bazie"); return(View("AddSong", ASG)); } if (song.Name == null) { ViewBag.Error = ("Nie podano imienia"); return(View("AddSong", ASG)); } if ((song.AlbumPosition <= 0 || song.AlbumPosition > 30)) { ViewBag.Error = ("Zła pozycja piosenki"); return(View("AddSong", ASG)); } if (ModelState.IsValid) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/Files/Songs/"), fileName); file.SaveAs(path); Mp3FileReader reader = new Mp3FileReader(path); var duration = reader.TotalTime; song.Link = Url.Content(("~/Files/Songs/") + fileName); song.Duration = duration; //pobrać duration z wstawianego utworu db.Songs.Add(song); db.SaveChanges(); ViewBag.Success = "Piosenka " + song.Name + " została pomyślnie dodana do bazy"; } return(View("AddSong", ASG)); }