public ActionResult Add(Song song, HttpPostedFileBase FilePath, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                using (var db = new MusicStationDbContext())
                {
                    var userId = User.Identity.GetUserId();

                    song.UserId = userId;

                    var random = new Random();
                    var number = random.Next();

                    if (image != null)
                    {
                        var allowedImageTypes = new[] { "image/jpg", "image/jpeg", "image/png" };


                        if (allowedImageTypes.Contains(image.ContentType))
                        {
                            var imagesPath        = "/Content/Images/";
                            var imageName         = number.ToString() + image.FileName;
                            var imageUploadPath   = imagesPath + imageName;
                            var imagePhysicalPath = Server.MapPath(imageUploadPath);

                            image.SaveAs(imagePhysicalPath);

                            song.ImagePath = imageUploadPath;
                        }
                    }

                    var allowedAudioTypes = new[] { "audio/mp3" };

                    if (allowedAudioTypes.Contains(FilePath.ContentType))
                    {
                        var filePath     = "/Content/Songs/";
                        var fileName     = number.ToString() + FilePath.FileName;
                        var uploadPath   = filePath + fileName;
                        var physicalPath = Server.MapPath(uploadPath);
                        FilePath.SaveAs(physicalPath);

                        song.FilePath = uploadPath;

                        db.Songs.Add(song);
                        db.SaveChanges();

                        return(RedirectToAction("List"));
                    }
                    else
                    {
                        return(RedirectToAction("SongError"));
                    }
                }
            }

            return(View(song));
        }
        public ActionResult Edit(SongEditModel model, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                using (var db = new MusicStationDbContext())
                {
                    var song = db.Songs
                               .Where(s => s.Id == model.Id)
                               .FirstOrDefault();

                    var allowedImageTypes = new[] { "image/jpg", "image/jpeg", "image/png" };

                    if (song == null)
                    {
                        return(HttpNotFound());
                    }

                    var originalImageUploadPath = Server.MapPath(song.ImagePath);

                    if (image == null || !allowedImageTypes.Contains(image.ContentType))
                    {
                        model.ImagePath = song.ImagePath;
                    }
                    else
                    {
                        var imagesPath           = "/Content/Images/";
                        var newImageName         = song.Id.ToString() + image.FileName;
                        var newImageUploadPath   = imagesPath + newImageName;
                        var newImagePhysicalPath = Server.MapPath(newImageUploadPath);
                        if (song.ImagePath != null)
                        {
                            System.IO.File.Delete(originalImageUploadPath);
                        }

                        image.SaveAs(newImagePhysicalPath);
                        model.ImagePath = newImageUploadPath;
                    }

                    song.Id        = model.Id;
                    song.Artist    = model.Artist;
                    song.Title     = model.Title;
                    song.Details   = model.Details;
                    song.Genre     = model.Genre;
                    song.ImagePath = model.ImagePath;

                    db.Entry(song).State = EntityState.Modified;
                    db.SaveChanges();

                    return(RedirectToAction("Details", new { id = song.Id }));
                }
            }
            return(View(model));
        }
        public ActionResult ConfirmDelete(int?id)
        {
            if (id == null)
            {
                return(HttpNotFound());
            }

            using (var db = new MusicStationDbContext())
            {
                var song = db.Songs
                           .Where(s => s.Id == id)
                           .Include(s => s.User)
                           .FirstOrDefault();

                if (song == null)
                {
                    return(HttpNotFound());
                }

                var filePath  = Request.MapPath(song.FilePath);
                var imagePath = Request.MapPath(song.ImagePath);

                if (System.IO.File.Exists(imagePath))
                {
                    System.IO.File.Delete(imagePath);
                }

                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }

                db.Songs.Remove(song);
                db.SaveChanges();

                return(RedirectToAction("List"));
            }
        }