Ejemplo n.º 1
0
        public ActionResult SongForm(ShareSongPostForm model)
        {
            model.IsNew = model.ID == null;

            using (var Context = new ProjectDBEntities())
            {

                ViewBag.SharingTypes = Context.SharingTypes.ToList();


                if (!ModelState.IsValid)
                    return View(model);

                var selectedGeneres = model.Generes.Where(x => x.IsChecked).ToList();

                var userGuid = (Context.SiteUsers.Where(ax => ax.UserName == User.Identity.Name).FirstOrDefault<SiteUser>()).UserID;
                var albumID = (Context.Albums.Where(ax => ax.AlbumTitle == "Default").FirstOrDefault<Album>()).AlbumID;

                if (!Context.SharingTypes.Any(x => x.SharingTypeID == model.SharingTypeID))
                    ModelState.AddModelError("SharingType", "No Such Sharing Exists..");

                if (model.IsNew)
                {
                    if (model.Track == null)
                        ModelState.AddModelError("Track", "Track File is required..");

                    if (model.TrackCover == null)
                        ModelState.AddModelError("TrackCover", "Track Cover is required..");
                    
                    if (!ModelState.IsValid)
                        return View(model);

                    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 = albumID,
                        TrackPath = trackPathForDb,
                        TrackCoverPath = trackCoverPathForDb,
                        DateAdded = DateTime.UtcNow,
                        IsActive = true,
                        UserID = userGuid
                    };
                    
                    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();
                }
                else
                {
                    var track = Context.Tracks.Where(tx => tx.TrackID == model.ID).FirstOrDefault<Track>();

                    if (track == null)
                        return HttpNotFound();

                    var trackShare = Context.UserTrackShares
                        .Where(x => x.UserTrackSharedID == model.TrackShareID)
                        .FirstOrDefault<UserTrackShare>();

                    if (trackShare == null)
                        return HttpNotFound();

                    string trackPathForDb = null;
                    string oldTrack = null;

                    if (model.Track != null)
                    {
                        if (!VerifyMP3Extension(model.Track.FileName))
                            ModelState.AddModelError("Track", "Only \".mp3\" file is allowed for Track..");
                        
                        if (!ModelState.IsValid)
                            return View(model);

                        oldTrack = track.TrackPath;

                        var trackName = Guid.NewGuid() + Path.GetExtension(model.Track.FileName);
                        var trackUploadPath = Server.MapPath("~/users/tracks");
                        trackPathForDb = "/users/tracks/" + trackName;
                        model.Track.SaveAs(Path.Combine(trackUploadPath, trackName));
                        
                    }
                    else
                        trackPathForDb = track.TrackPath;

                    string trackCoverPathForDb = null;
                    string oldCover = null;
                    if (model.TrackCover != null)
                    {
                        if (!VerifyPictureExtension(model.TrackCover.FileName))
                            ModelState.AddModelError("TrackCover", "Only \".jpg\" & \".png\" files are allowed for cover..");

                        if (!ModelState.IsValid)
                            return View(model);

                        oldCover = track.TrackCoverPath;

                        var trackCoverName = Guid.NewGuid() + Path.GetExtension(model.TrackCover.FileName);
                        var trackCoverUploadPath = Server.MapPath("~/users/tracks/images");
                        model.TrackCover.SaveAs(Path.Combine(trackCoverUploadPath, trackCoverName));
                        trackCoverPathForDb = "/users/tracks/images/" + trackCoverName;
                        RemoveOld(track.TrackCoverPath);
                    }
                    else
                        trackCoverPathForDb = track.TrackCoverPath;

                    if (!ModelState.IsValid)
                        return View(model);
                    
                    track.TrackTitle = model.Title;
                    track.AlbumID = (Guid)model.AlbumID;
                    track.TrackPath = trackPathForDb;
                    track.TrackCoverPath = trackCoverPathForDb;
                    track.DateAdded = DateTime.UtcNow;
                    track.IsActive = model.IsActive;
                    track.UserID = userGuid;

                    trackShare.SharingTypeID = model.SharingTypeID;

                    foreach (var item in Context.TrackGeneres.Where(x => x.TrackId == track.TrackID).ToList())
                    {
                        Context.TrackGeneres.Remove(item);
                    }

                    foreach (var item in selectedGeneres)
                    {
                        Context.TrackGeneres.Add(new TrackGenere
                            {
                                TrackId = track.TrackID,
                                GenereID = (int)item.ID
                            });    
                    }

                    Context.SaveChanges();

                    if (oldTrack != null)
                        RemoveOld(oldTrack);

                    if (oldCover != null)
                        RemoveOld(oldCover);

                }
            }
            return RedirectToAction("ListSongs");
        }
Ejemplo n.º 2
0
        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()
            });
        }