public ActionResult AddSongsInAlbum(AddSongsInAlbumViewModel model) { var Context = new ProjectDBEntities(); var userID = (Context.SiteUsers.Where(ax => ax.UserName == User.Identity.Name).FirstOrDefault <SiteUser>()).UserID; ViewBag.Albums = Context.Albums .Where(x => x.UserID == userID && x.IsActive == true && x.UserID == userID) .ToList(); ViewBag.SharingTypes = Context.SharingTypes.ToList(); if (!ModelState.IsValid) { return(View(model)); } var selectedGeneres = model.Generes.Where(x => x.IsChecked).ToList(); var album = Context.Albums .Where(x => x.AlbumID == model.AlbumID && x.AlbumTitle != "Default") .FirstOrDefault <Album>(); if (album == null) { ModelState.AddModelError("Album", "Album ID is not Correct.."); } if (!VerifyMP3Extension(model.Track.FileName)) { ModelState.AddModelError("Track", "Only \".mp3\" file is allowed for Track.."); } if (!VerifyPictureExtension(model.TrackCover.FileName)) { ModelState.AddModelError("TrackCover", "Only \".jpg\" & \".png\" files are allowed for cover.."); } if (!ModelState.IsValid) { return(View(model)); } var trackName = Guid.NewGuid() + Path.GetExtension(model.Track.FileName); var trackUploadPath = Server.MapPath("~/users/tracks"); var trackPathForDb = "/users/tracks/" + trackName; model.Track.SaveAs(Path.Combine(trackUploadPath, trackName)); var trackCoverName = Guid.NewGuid() + Path.GetExtension(model.TrackCover.FileName); var trackCoverUploadPath = Server.MapPath("~/users/tracks/images"); model.TrackCover.SaveAs(Path.Combine(trackCoverUploadPath, trackCoverName)); var trackCoverPathForDb = "/users/tracks/images/" + trackCoverName; Track track = new Track() { TrackTitle = model.Title, AlbumID = model.AlbumID, TrackPath = trackPathForDb, TrackCoverPath = trackCoverPathForDb, DateAdded = DateTime.UtcNow, IsActive = true, UserID = userID }; Context.Tracks.Add(track); Context.SaveChanges(); foreach (var item in selectedGeneres) { Context.TrackGeneres.Add(new TrackGenere { TrackId = track.TrackID, GenereID = (int)item.ID }); } Context.SaveChanges(); Context.UserTrackShares.Add(new UserTrackShare { TrackID = track.TrackID, SharingTypeID = model.SharingTypeID, SharingDate = DateTime.UtcNow }); Context.SaveChanges(); ViewBag.Message = "Track added.."; return(View(new AddSongsInAlbumViewModel { Generes = Context.Generes .Where(x => x.IsActive == true) .Select(genere => new GenereCheckBox { ID = genere.GenereID, Name = genere.GenereDetail, IsChecked = false }).ToList() })); }
public ActionResult AddSongsInAlbum(AddSongsInAlbumViewModel model) { var Context = new ProjectDBEntities(); var userID = (Context.SiteUsers.Where(ax => ax.UserName == User.Identity.Name).FirstOrDefault<SiteUser>()).UserID; ViewBag.Albums = Context.Albums .Where(x => x.UserID == userID && x.IsActive == true && x.UserID == userID) .ToList(); ViewBag.SharingTypes = Context.SharingTypes.ToList(); if (!ModelState.IsValid) return View(model); var selectedGeneres = model.Generes.Where(x => x.IsChecked).ToList(); var album = Context.Albums .Where(x => x.AlbumID == model.AlbumID && x.AlbumTitle != "Default") .FirstOrDefault<Album>(); if (album == null) ModelState.AddModelError("Album", "Album ID is not Correct.."); if (!VerifyMP3Extension(model.Track.FileName)) ModelState.AddModelError("Track", "Only \".mp3\" file is allowed for Track.."); if (!VerifyPictureExtension(model.TrackCover.FileName)) ModelState.AddModelError("TrackCover", "Only \".jpg\" & \".png\" files are allowed for cover.."); if (!ModelState.IsValid) return View(model); var trackName = Guid.NewGuid() + Path.GetExtension(model.Track.FileName); var trackUploadPath = Server.MapPath("~/users/tracks"); var trackPathForDb = "/users/tracks/" + trackName; model.Track.SaveAs(Path.Combine(trackUploadPath, trackName)); var trackCoverName = Guid.NewGuid() + Path.GetExtension(model.TrackCover.FileName); var trackCoverUploadPath = Server.MapPath("~/users/tracks/images"); model.TrackCover.SaveAs(Path.Combine(trackCoverUploadPath, trackCoverName)); var trackCoverPathForDb = "/users/tracks/images/" + trackCoverName; Track track = new Track() { TrackTitle = model.Title, AlbumID = model.AlbumID, TrackPath = trackPathForDb, TrackCoverPath = trackCoverPathForDb, DateAdded = DateTime.UtcNow, IsActive = true, UserID = userID }; Context.Tracks.Add(track); Context.SaveChanges(); foreach (var item in selectedGeneres) { Context.TrackGeneres.Add(new TrackGenere { TrackId = track.TrackID, GenereID = (int)item.ID }); } Context.SaveChanges(); Context.UserTrackShares.Add(new UserTrackShare { TrackID = track.TrackID, SharingTypeID = model.SharingTypeID, SharingDate = DateTime.UtcNow }); Context.SaveChanges(); ViewBag.Message = "Track added.."; return View(new AddSongsInAlbumViewModel { Generes = Context.Generes .Where(x => x.IsActive == true) .Select(genere => new GenereCheckBox { ID = genere.GenereID, Name = genere.GenereDetail, IsChecked = false }).ToList() }); }