예제 #1
0
        public ActionResult UpdateAudioItem(AudioFormViewModel model)
        {
            // Placeholder for errors
            var errors = new List <string>();

            if (ModelState.IsValid)
            {
                // Remove special characters from the file input
                string audioFile = RemoveSpecialCharacters(model.AudioLocation);

                // Placeholders for the audio upload paths
                string uploadPath = uploadAudioDirectory + audioFile;
                string audioPath  = audioDirectory + audioFile;

                // Check whether the audioID exists
                // Placeholder for the audioIDInt

                int audioIdInt = 0;
                if (String.IsNullOrWhiteSpace(model.AudioId.ToString()))
                {
                    errorDict.Add("AudioItem", "There was an error updating this audio item. Please try again.");
                }
                else if (!Int32.TryParse(model.AudioId.ToString(), out audioIdInt))
                {
                    errorDict.Add("AudioItem", "There was an error updating this audio item. Please try again");
                }

                // Move the file from /Content/Audio/Upload to Content/Audio
                if (System.IO.File.Exists(uploadPath))
                {
                    // If the file is in uploadPath. Check whether it exists in the /Content/Audio directory
                    if (!System.IO.File.Exists(audioPath))
                    {
                        System.IO.File.Move(uploadPath, audioPath);
                    }
                }

                // Remove all the files in the Upload directory
                System.IO.DirectoryInfo di = new DirectoryInfo(uploadAudioDirectory);
                foreach (FileInfo file in di.GetFiles())
                {
                    file.Delete();
                }

                // Get the audio type id
                AudioType audioTypeRef = _db.AudioType.Where(r => r.AudioTypeId.Equals(model.AudioType)).First();

                // Convert the inputted time stamps to seconds and return an error if they can't be converted.
                int audioIn       = CalculateSeconds(model.AudioIn);
                int audioOut      = CalculateSeconds(model.AudioOut);
                int audioDuration = CalculateSeconds(model.AudioDuration);

                if (audioIn == -1 || audioOut == -1 || audioDuration == -1)
                {
                    errors.Add("There was an error with the file upload. Please try again.");
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(Json(new { status = "error", errors = errors }));
                }

                var audioItems = _db.Audio.Where(r => r.AudioId.Equals(audioIdInt));
                foreach (Audio audioItem in audioItems)
                {
                    audioItem.ArtistName            = model.ArtistName;
                    audioItem.AudioTitle            = model.AudioTitle;
                    audioItem.AudioLocation         = audioFile;
                    audioItem.AudioDuration         = audioDuration;
                    audioItem.AudioIn               = audioIn;
                    audioItem.AudioOut              = audioOut;
                    audioItem.AudioReleaseYear      = model.AudioReleaseYear;
                    audioItem.AudioType.AudioTypeId = audioTypeRef.AudioTypeId;
                }
                _db.SaveChanges();

                Response.StatusCode = (int)HttpStatusCode.OK;
                return(Json("Success"));
            }

            // If it gets this far an error has occured
            foreach (var modelStateVal in ViewData.ModelState.Values)
            {
                errors.AddRange(modelStateVal.Errors.Select(error => error.ErrorMessage));
            }
            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { status = "error", errors = errors }));
        }
예제 #2
0
        public async Task <ActionResult> AddNewAudioItem(AudioFormViewModel model)
        {
            // Placeholder for errors
            List <string> errors = new List <string>();

            if (ModelState.IsValid)
            {
                // Remove special characters from audioFile
                string audioFile = RemoveSpecialCharacters(model.AudioLocation);

                // Placeholders for the audio upload paths
                string uploadPath = uploadAudioDirectory + audioFile;
                string audioPath  = audioDirectory + audioFile;

                // Move the file from Content/Audio/Upload to Content/Audio
                if (System.IO.File.Exists(audioPath) && System.IO.File.Exists(uploadPath))
                {
                    // Delete the file from the audio directory
                    System.IO.File.Delete(audioPath);
                    // Move the new file into the audio directory
                    System.IO.File.Move(uploadPath, audioPath);
                }
                else if (!System.IO.File.Exists(audioPath) && System.IO.File.Exists(uploadPath))
                {
                    // Move the file from the upload directory to the audio directory
                    System.IO.File.Move(uploadPath, audioPath);
                }
                else
                {
                    // When the user is adding a new audio file this part of the code should never run.
                    // This would only run if the audio is present in /Content/Audio but not in the /Content/Audio/Upload directory
                    errors.Add("There was an error with the file upload. Please try again.");
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(Json(new { status = "error", errors = errors }));
                }

                // Remove all the files in the Upload directory
                System.IO.DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/Content/Audio/Upload/"));
                foreach (FileInfo file in di.GetFiles())
                {
                    file.Delete();
                }

                // Convert the inputted time stamps to seconds and return an error if they can't be converted.
                int audioIn       = CalculateSeconds(model.AudioIn);
                int audioOut      = CalculateSeconds(model.AudioOut);
                int audioDuration = CalculateSeconds(model.AudioDuration);

                if (audioIn == -1 || audioOut == -1 || audioDuration == -1)
                {
                    errors.Add("There was an error with the file upload. Please try again.");
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(Json(new { status = "error", errors = errors }));
                }

                // Get the audio type id
                int       audioTypeInt = model.AudioType;
                AudioType audioTypeRef = _db.AudioType.Where(r => r.AudioTypeId.Equals(audioTypeInt)).First();

                Audio newAudio = new Audio
                {
                    ArtistName       = model.ArtistName,
                    AudioTitle       = model.AudioTitle,
                    AudioLocation    = audioFile,
                    AudioDuration    = audioDuration,
                    AudioIn          = audioIn,
                    AudioOut         = audioOut,
                    AudioReleaseYear = model.AudioReleaseYear,
                    AudioType        = audioTypeRef
                };

                _db.Audio.Add(newAudio);
                _db.SaveChanges();

                Response.StatusCode = (int)HttpStatusCode.OK;
                return(Json("Success"));
            }

            // If it gets this far an error has occured
            foreach (var modelStateVal in ViewData.ModelState.Values)
            {
                errors.AddRange(modelStateVal.Errors.Select(error => error.ErrorMessage));
            }
            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { status = "error", errors = errors }));
        }